diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/FavoritesActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/FavoritesActivity.kt index 5f7e500a..d99aba72 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/FavoritesActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/FavoritesActivity.kt @@ -4,14 +4,14 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import com.simplemobiletools.commons.dialogs.FilePickerDialog -import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.beVisibleIf +import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener import com.simplemobiletools.filemanager.R +import com.simplemobiletools.filemanager.adapters.ManageFavoritesAdapter import com.simplemobiletools.filemanager.extensions.config 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?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_favorites) @@ -19,27 +19,14 @@ class FavoritesActivity : SimpleActivity() { } private fun updateFavorites() { - favorites_holder.removeAllViews() - val favorites = config.favorites - favorites_placeholder.beVisibleIf(favorites.isEmpty()) - favorites_placeholder.setTextColor(config.textColor) + val favorites = ArrayList() + config.favorites.mapTo(favorites, { it }) + manage_favorites_placeholder.beVisibleIf(favorites.isEmpty()) + manage_favorites_placeholder.setTextColor(config.textColor) - for (favorite in favorites) { - layoutInflater.inflate(R.layout.item_favorite, null, false).apply { - favorite_title.apply { - text = favorite - setTextColor(config.textColor) - } - favorite_icon.apply { - applyColorFilter(config.textColor) - setOnClickListener { - config.removeFavorite(favorite) - updateFavorites() - } - } - favorites_holder.addView(this) - } - } + val adapter = ManageFavoritesAdapter(this, favorites, this, manage_favorites_list) {} + adapter.setupDragListener(true) + manage_favorites_list.adapter = adapter } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -55,6 +42,10 @@ class FavoritesActivity : SimpleActivity() { return true } + override fun refreshItems() { + updateFavorites() + } + private fun addFavorite() { FilePickerDialog(this, pickFile = false, showHidden = config.shouldShowHidden) { config.addFavorite(it) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ManageFavoritesAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ManageFavoritesAdapter.kt new file mode 100644 index 00000000..277c62fe --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ManageFavoritesAdapter.kt @@ -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, 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(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() + (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() + } + } +} diff --git a/app/src/main/res/layout/activity_favorites.xml b/app/src/main/res/layout/activity_favorites.xml index a78857a8..3e58fc9e 100644 --- a/app/src/main/res/layout/activity_favorites.xml +++ b/app/src/main/res/layout/activity_favorites.xml @@ -1,30 +1,27 @@ - - + android:layout_height="match_parent" + android:clipToPadding="false" + app:layoutManager="android.support.v7.widget.LinearLayoutManager"/> - + - - - + diff --git a/app/src/main/res/layout/item_favorite.xml b/app/src/main/res/layout/item_favorite.xml deleted file mode 100644 index 90d003f0..00000000 --- a/app/src/main/res/layout/item_favorite.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - diff --git a/app/src/main/res/layout/item_manage_favorite.xml b/app/src/main/res/layout/item_manage_favorite.xml new file mode 100644 index 00000000..ef0b058c --- /dev/null +++ b/app/src/main/res/layout/item_manage_favorite.xml @@ -0,0 +1,22 @@ + + + + + +