rewrite Manage Favorites activity to use a recyclerview

This commit is contained in:
tibbi 2017-11-18 23:38:01 +01:00
parent 554507f684
commit 3db20b6e93
5 changed files with 149 additions and 74 deletions

View file

@ -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<String>()
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)

View file

@ -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()
}
}
}

View file

@ -1,30 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
<RelativeLayout
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_height="wrap_content">
<RelativeLayout
android:id="@+id/favorites_wrapper"
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/manage_favorites_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="match_parent"
android:clipToPadding="false"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
<LinearLayout
android:id="@+id/favorites_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/manage_favorites_placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="@dimen/big_margin"
android:paddingRight="@dimen/big_margin"
android:paddingTop="@dimen/activity_margin"
android:text="@string/favorites_activity_placeholder"
android:visibility="gone"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/favorites_placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="@dimen/big_margin"
android:paddingRight="@dimen/big_margin"
android:paddingTop="@dimen/activity_margin"
android:text="@string/favorites_activity_placeholder"
android:visibility="gone"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>

View file

@ -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>

View 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>