rewrite Manage Favorites activity to use a recyclerview
This commit is contained in:
parent
554507f684
commit
3db20b6e93
5 changed files with 149 additions and 74 deletions
|
@ -4,14 +4,14 @@ import android.os.Bundle
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import com.simplemobiletools.commons.dialogs.FilePickerDialog
|
import com.simplemobiletools.commons.dialogs.FilePickerDialog
|
||||||
import com.simplemobiletools.commons.extensions.applyColorFilter
|
|
||||||
import com.simplemobiletools.commons.extensions.beVisibleIf
|
import com.simplemobiletools.commons.extensions.beVisibleIf
|
||||||
|
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
|
||||||
import com.simplemobiletools.filemanager.R
|
import com.simplemobiletools.filemanager.R
|
||||||
|
import com.simplemobiletools.filemanager.adapters.ManageFavoritesAdapter
|
||||||
import com.simplemobiletools.filemanager.extensions.config
|
import com.simplemobiletools.filemanager.extensions.config
|
||||||
import kotlinx.android.synthetic.main.activity_favorites.*
|
import kotlinx.android.synthetic.main.activity_favorites.*
|
||||||
import kotlinx.android.synthetic.main.item_favorite.view.*
|
|
||||||
|
|
||||||
class FavoritesActivity : SimpleActivity() {
|
class FavoritesActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_favorites)
|
setContentView(R.layout.activity_favorites)
|
||||||
|
@ -19,27 +19,14 @@ class FavoritesActivity : SimpleActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateFavorites() {
|
private fun updateFavorites() {
|
||||||
favorites_holder.removeAllViews()
|
val favorites = ArrayList<String>()
|
||||||
val favorites = config.favorites
|
config.favorites.mapTo(favorites, { it })
|
||||||
favorites_placeholder.beVisibleIf(favorites.isEmpty())
|
manage_favorites_placeholder.beVisibleIf(favorites.isEmpty())
|
||||||
favorites_placeholder.setTextColor(config.textColor)
|
manage_favorites_placeholder.setTextColor(config.textColor)
|
||||||
|
|
||||||
for (favorite in favorites) {
|
val adapter = ManageFavoritesAdapter(this, favorites, this, manage_favorites_list) {}
|
||||||
layoutInflater.inflate(R.layout.item_favorite, null, false).apply {
|
adapter.setupDragListener(true)
|
||||||
favorite_title.apply {
|
manage_favorites_list.adapter = adapter
|
||||||
text = favorite
|
|
||||||
setTextColor(config.textColor)
|
|
||||||
}
|
|
||||||
favorite_icon.apply {
|
|
||||||
applyColorFilter(config.textColor)
|
|
||||||
setOnClickListener {
|
|
||||||
config.removeFavorite(favorite)
|
|
||||||
updateFavorites()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
favorites_holder.addView(this)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||||
|
@ -55,6 +42,10 @@ class FavoritesActivity : SimpleActivity() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun refreshItems() {
|
||||||
|
updateFavorites()
|
||||||
|
}
|
||||||
|
|
||||||
private fun addFavorite() {
|
private fun addFavorite() {
|
||||||
FilePickerDialog(this, pickFile = false, showHidden = config.shouldShowHidden) {
|
FilePickerDialog(this, pickFile = false, showHidden = config.shouldShowHidden) {
|
||||||
config.addFavorite(it)
|
config.addFavorite(it)
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
package com.simplemobiletools.filemanager.adapters
|
||||||
|
|
||||||
|
import android.util.SparseArray
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
||||||
|
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
|
||||||
|
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
|
||||||
|
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
|
||||||
|
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||||
|
import com.simplemobiletools.filemanager.R
|
||||||
|
import com.simplemobiletools.filemanager.extensions.config
|
||||||
|
import kotlinx.android.synthetic.main.item_manage_favorite.view.*
|
||||||
|
|
||||||
|
class ManageFavoritesAdapter(activity: BaseSimpleActivity, var favorites: ArrayList<String>, val listener: RefreshRecyclerViewListener?,
|
||||||
|
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
|
||||||
|
|
||||||
|
private val config = activity.config
|
||||||
|
|
||||||
|
init {
|
||||||
|
selectableItemCount = favorites.size
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getActionMenuId() = R.menu.cab_delete_only
|
||||||
|
|
||||||
|
override fun prepareActionMode(menu: Menu) {}
|
||||||
|
|
||||||
|
override fun prepareItemSelection(view: View) {}
|
||||||
|
|
||||||
|
override fun markItemSelection(select: Boolean, view: View?) {
|
||||||
|
view?.manage_favorite_holder?.isSelected = select
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun actionItemPressed(id: Int) {
|
||||||
|
when (id) {
|
||||||
|
R.id.cab_delete -> askConfirmDelete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) = createViewHolder(R.layout.item_manage_favorite, parent)
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||||
|
val favorite = favorites[position]
|
||||||
|
val view = holder.bindView(favorite) {
|
||||||
|
setupView(it, favorite)
|
||||||
|
}
|
||||||
|
bindViewHolder(holder, position, view)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount() = favorites.size
|
||||||
|
|
||||||
|
private fun setupView(view: View, favorite: String) {
|
||||||
|
view.apply {
|
||||||
|
manage_favorite_title.apply {
|
||||||
|
text = favorite
|
||||||
|
setTextColor(config.textColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun askConfirmDelete() {
|
||||||
|
ConfirmationDialog(activity) {
|
||||||
|
deleteSelection()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun deleteSelection() {
|
||||||
|
val removeFavorites = ArrayList<String>(selectedPositions.size)
|
||||||
|
|
||||||
|
selectedPositions.sortedDescending().forEach {
|
||||||
|
val favorite = favorites[it]
|
||||||
|
removeFavorites.add(favorite)
|
||||||
|
notifyItemRemoved(it)
|
||||||
|
itemViews.put(it, null)
|
||||||
|
config.removeFavorite(favorite)
|
||||||
|
}
|
||||||
|
|
||||||
|
favorites.removeAll(removeFavorites)
|
||||||
|
selectedPositions.clear()
|
||||||
|
|
||||||
|
val newItems = SparseArray<View>()
|
||||||
|
(0 until itemViews.size())
|
||||||
|
.filter { itemViews[it] != null }
|
||||||
|
.forEachIndexed { curIndex, i -> newItems.put(curIndex, itemViews[i]) }
|
||||||
|
|
||||||
|
itemViews = newItems
|
||||||
|
selectableItemCount = favorites.size
|
||||||
|
finishActMode()
|
||||||
|
if (favorites.isEmpty()) {
|
||||||
|
listener?.refreshItems()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,23 +1,20 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<ScrollView
|
<RelativeLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:id="@+id/favorites_scrollview"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:id="@+id/manage_favorites_wrapper"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
<RelativeLayout
|
<com.simplemobiletools.commons.views.MyRecyclerView
|
||||||
android:id="@+id/favorites_wrapper"
|
android:id="@+id/manage_favorites_list"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="match_parent"
|
||||||
|
android:clipToPadding="false"
|
||||||
<LinearLayout
|
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
|
||||||
android:id="@+id/favorites_holder"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:orientation="vertical"/>
|
|
||||||
|
|
||||||
<com.simplemobiletools.commons.views.MyTextView
|
<com.simplemobiletools.commons.views.MyTextView
|
||||||
android:id="@+id/favorites_placeholder"
|
android:id="@+id/manage_favorites_placeholder"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="center_horizontal"
|
android:gravity="center_horizontal"
|
||||||
|
@ -26,5 +23,5 @@
|
||||||
android:paddingTop="@dimen/activity_margin"
|
android:paddingTop="@dimen/activity_margin"
|
||||||
android:text="@string/favorites_activity_placeholder"
|
android:text="@string/favorites_activity_placeholder"
|
||||||
android:visibility="gone"/>
|
android:visibility="gone"/>
|
||||||
</RelativeLayout>
|
|
||||||
</ScrollView>
|
</RelativeLayout>
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<RelativeLayout
|
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:id="@+id/favorite_holder"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:padding="@dimen/activity_margin">
|
|
||||||
|
|
||||||
<com.simplemobiletools.commons.views.MyTextView
|
|
||||||
android:id="@+id/favorite_title"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_centerVertical="true"
|
|
||||||
android:layout_marginLeft="@dimen/medium_margin"
|
|
||||||
android:layout_marginRight="@dimen/medium_margin"
|
|
||||||
android:layout_toLeftOf="@+id/favorite_icon"
|
|
||||||
android:layout_toStartOf="@+id/favorite_icon"/>
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/favorite_icon"
|
|
||||||
style="@style/MyBorderlessBackgroundStyle"
|
|
||||||
android:layout_width="@dimen/normal_icon_size"
|
|
||||||
android:layout_height="@dimen/normal_icon_size"
|
|
||||||
android:layout_alignParentEnd="true"
|
|
||||||
android:layout_alignParentRight="true"
|
|
||||||
android:padding="@dimen/medium_margin"
|
|
||||||
android:src="@drawable/ic_cross"/>
|
|
||||||
|
|
||||||
</RelativeLayout>
|
|
22
app/src/main/res/layout/item_manage_favorite.xml
Normal file
22
app/src/main/res/layout/item_manage_favorite.xml
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RelativeLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/manage_favorite_holder"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="?attr/selectableItemBackground"
|
||||||
|
android:clickable="true"
|
||||||
|
android:focusable="true"
|
||||||
|
android:foreground="@drawable/selector"
|
||||||
|
android:padding="@dimen/activity_margin">
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyTextView
|
||||||
|
android:id="@+id/manage_favorite_title"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_centerVertical="true"
|
||||||
|
android:layout_marginLeft="@dimen/medium_margin"
|
||||||
|
android:layout_marginRight="@dimen/medium_margin"
|
||||||
|
android:layout_marginTop="@dimen/medium_margin"/>
|
||||||
|
|
||||||
|
</RelativeLayout>
|
Loading…
Reference in a new issue