Improve categories managing
This commit is contained in:
@@ -21,6 +21,7 @@ import org.koitharu.kotatsu.core.ui.list.ListSelectionController
|
||||
import org.koitharu.kotatsu.core.util.ext.observe
|
||||
import org.koitharu.kotatsu.core.util.ext.observeEvent
|
||||
import org.koitharu.kotatsu.databinding.ActivityCategoriesBinding
|
||||
import org.koitharu.kotatsu.favourites.ui.FavouritesActivity
|
||||
import org.koitharu.kotatsu.favourites.ui.categories.adapter.CategoriesAdapter
|
||||
import org.koitharu.kotatsu.favourites.ui.categories.edit.FavouritesCategoryEditActivity
|
||||
import org.koitharu.kotatsu.list.ui.adapter.ListStateHolderListener
|
||||
@@ -77,6 +78,14 @@ class FavouriteCategoriesActivity :
|
||||
}
|
||||
|
||||
override fun onItemClick(item: FavouriteCategory, view: View) {
|
||||
if (selectionController.onItemClick(item.id)) {
|
||||
return
|
||||
}
|
||||
val intent = FavouritesActivity.newIntent(view.context, item)
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
override fun onEditClick(item: FavouriteCategory, view: View) {
|
||||
if (selectionController.onItemClick(item.id)) {
|
||||
return
|
||||
}
|
||||
@@ -112,8 +121,8 @@ class FavouriteCategoriesActivity :
|
||||
)
|
||||
}
|
||||
|
||||
private fun onCategoriesChanged(categories: List<ListModel>) {
|
||||
adapter.items = categories
|
||||
private suspend fun onCategoriesChanged(categories: List<ListModel>) {
|
||||
adapter.emit(categories)
|
||||
invalidateOptionsMenu()
|
||||
}
|
||||
|
||||
@@ -128,7 +137,14 @@ class FavouriteCategoriesActivity :
|
||||
recyclerView: RecyclerView,
|
||||
viewHolder: RecyclerView.ViewHolder,
|
||||
target: RecyclerView.ViewHolder,
|
||||
): Boolean = viewHolder.itemViewType == target.itemViewType
|
||||
): Boolean {
|
||||
if (viewHolder.itemViewType != target.itemViewType) {
|
||||
return false
|
||||
}
|
||||
val fromPos = viewHolder.bindingAdapterPosition
|
||||
val toPos = target.bindingAdapterPosition
|
||||
return fromPos != toPos && fromPos != RecyclerView.NO_POSITION && toPos != RecyclerView.NO_POSITION
|
||||
}
|
||||
|
||||
override fun canDropOver(
|
||||
recyclerView: RecyclerView,
|
||||
@@ -153,7 +169,8 @@ class FavouriteCategoriesActivity :
|
||||
|
||||
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
|
||||
super.onSelectedChanged(viewHolder, actionState)
|
||||
viewBinding.recyclerView.isNestedScrollingEnabled = actionState == ItemTouchHelper.ACTION_STATE_IDLE
|
||||
viewBinding.recyclerView.isNestedScrollingEnabled =
|
||||
actionState == ItemTouchHelper.ACTION_STATE_IDLE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.koitharu.kotatsu.favourites.ui.categories
|
||||
|
||||
import android.view.View
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||
@@ -7,4 +8,6 @@ import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||
interface FavouriteCategoriesListListener : OnListItemClickListener<FavouriteCategory> {
|
||||
|
||||
fun onDragHandleTouch(holder: RecyclerView.ViewHolder): Boolean
|
||||
|
||||
fun onEditClick(item: FavouriteCategory, view: View)
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
|
||||
import org.koitharu.kotatsu.favourites.ui.categories.adapter.CategoryListModel
|
||||
import org.koitharu.kotatsu.list.ui.model.EmptyState
|
||||
import org.koitharu.kotatsu.list.ui.model.LoadingState
|
||||
import java.util.Collections
|
||||
import org.koitharu.kotatsu.parsers.util.move
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@@ -65,12 +65,11 @@ class FavouritesCategoriesViewModel @Inject constructor(
|
||||
val prevJob = reorderJob
|
||||
reorderJob = launchJob(Dispatchers.Default) {
|
||||
prevJob?.join()
|
||||
val items = categories.requireValue()
|
||||
val ids = items.mapNotNullTo(ArrayList(items.size)) {
|
||||
val snapshot = categories.requireValue().toMutableList()
|
||||
snapshot.move(oldPos, newPos)
|
||||
val ids = snapshot.mapNotNullTo(ArrayList(snapshot.size)) {
|
||||
(it as? CategoryListModel)?.category?.id
|
||||
}
|
||||
Collections.swap(ids, oldPos, newPos)
|
||||
ids.remove(0L)
|
||||
repository.reorderCategories(ids)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ class CategoriesAdapter(
|
||||
) : BaseListAdapter<ListModel>() {
|
||||
|
||||
init {
|
||||
addDelegate(ListItemType.CATEGORY_LARGE ,categoryAD(coil, lifecycleOwner, onItemClickListener))
|
||||
addDelegate(ListItemType.STATE_EMPTY ,emptyStateListAD(coil, lifecycleOwner, listListener))
|
||||
addDelegate(ListItemType.STATE_LOADING ,loadingStateAD())
|
||||
addDelegate(ListItemType.CATEGORY_LARGE, categoryAD(coil, lifecycleOwner, onItemClickListener))
|
||||
addDelegate(ListItemType.STATE_EMPTY, emptyStateListAD(coil, lifecycleOwner, listListener))
|
||||
addDelegate(ListItemType.STATE_LOADING, loadingStateAD())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,8 +34,13 @@ fun categoryAD(
|
||||
{ inflater, parent -> ItemCategoryBinding.inflate(inflater, parent, false) },
|
||||
) {
|
||||
val eventListener = object : OnClickListener, OnLongClickListener, OnTouchListener {
|
||||
override fun onClick(v: View) = clickListener.onItemClick(item.category, itemView)
|
||||
override fun onLongClick(v: View) = clickListener.onItemLongClick(item.category, itemView)
|
||||
override fun onClick(v: View) = if (v.id == R.id.imageView_edit) {
|
||||
clickListener.onEditClick(item.category, v)
|
||||
} else {
|
||||
clickListener.onItemClick(item.category, v)
|
||||
}
|
||||
|
||||
override fun onLongClick(v: View) = clickListener.onItemLongClick(item.category, v)
|
||||
override fun onTouch(v: View?, event: MotionEvent): Boolean = event.actionMasked == MotionEvent.ACTION_DOWN &&
|
||||
clickListener.onDragHandleTouch(this@adapterDelegateViewBinding)
|
||||
}
|
||||
@@ -57,6 +62,7 @@ fun categoryAD(
|
||||
val crossFadeDuration = context.getAnimationDuration(R.integer.config_defaultAnimTime).toInt()
|
||||
itemView.setOnClickListener(eventListener)
|
||||
itemView.setOnLongClickListener(eventListener)
|
||||
binding.imageViewEdit.setOnClickListener(eventListener)
|
||||
binding.imageViewHandle.setOnTouchListener(eventListener)
|
||||
|
||||
bind { payloads ->
|
||||
|
||||
@@ -46,4 +46,8 @@ class CategoryListModel(
|
||||
result = 31 * result + category.isVisibleInLibrary.hashCode()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "CategoryListModel(categoryId=${category.id})"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@
|
||||
android:singleLine="true"
|
||||
android:textAppearance="?attr/textAppearanceBodyLarge"
|
||||
app:layout_constraintBottom_toTopOf="@id/textView_subtitle"
|
||||
app:layout_constraintEnd_toStartOf="@id/imageView_handle"
|
||||
app:layout_constraintEnd_toStartOf="@id/imageView_edit"
|
||||
app:layout_constraintStart_toEndOf="@id/imageView_cover3"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_chainStyle="packed"
|
||||
@@ -120,10 +120,23 @@
|
||||
app:layout_constraintTop_toTopOf="@id/textView_subtitle"
|
||||
app:srcCompat="@drawable/ic_eye" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/imageView_edit"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?selectableItemBackgroundBorderless"
|
||||
android:contentDescription="@string/edit"
|
||||
android:padding="@dimen/margin_normal"
|
||||
android:src="@drawable/ic_edit"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@id/imageView_handle"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/imageView_handle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?selectableItemBackgroundBorderless"
|
||||
android:contentDescription="@string/reorder"
|
||||
android:padding="@dimen/margin_normal"
|
||||
android:src="@drawable/ic_reorder_handle"
|
||||
|
||||
Reference in New Issue
Block a user