allow reordering favorites by drag and drop

This commit is contained in:
Pavel Poley 2022-05-06 16:51:43 +03:00
parent 716c527e0f
commit 145d781ecb
2 changed files with 19 additions and 6 deletions

View File

@ -54,7 +54,7 @@ class ContactsAdapter(
highlightText: String = "", highlightText: String = "",
private val enableDrag: Boolean = false, private val enableDrag: Boolean = false,
itemClick: (Any) -> Unit itemClick: (Any) -> Unit
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), RecyclerViewFastScroller.OnPopupTextUpdate, ItemTouchHelperContract { ) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), RecyclerViewFastScroller.OnPopupTextUpdate, ItemTouchHelperContract {
private val NEW_GROUP_ID = -1 private val NEW_GROUP_ID = -1
private var config = activity.config private var config = activity.config
@ -426,17 +426,23 @@ class ContactsAdapter(
} }
} }
if (enableDrag) { val dragIcon = findViewById<ImageView>(R.id.drag_handle_icon)
findViewById<ImageView>(R.id.drag_handle_icon).apply { if (enableDrag && textToHighlight.isEmpty()) {
dragIcon.apply {
beVisibleIf(selectedKeys.isNotEmpty()) beVisibleIf(selectedKeys.isNotEmpty())
applyColorFilter(textColor) applyColorFilter(textColor)
setOnTouchListener { v, event -> setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_DOWN) { if (event.action == MotionEvent.ACTION_DOWN) {
startReorderDragListener?.requestDrag(holder) startReorderDragListener?.requestDrag(holder)
} }
false false
} }
} }
} else {
dragIcon.apply {
beGone()
setOnTouchListener(null)
}
} }
} }
} }

View File

@ -163,9 +163,15 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
private fun sortByCustomOrder(starred: List<Contact>): ArrayList<Contact> { private fun sortByCustomOrder(starred: List<Contact>): ArrayList<Contact> {
val favoritesOrder = activity!!.config.favoritesContactsOrder val favoritesOrder = activity!!.config.favoritesContactsOrder
if (favoritesOrder.isEmpty()) {
return ArrayList(starred)
}
val orderList = Converters().jsonToStringList(favoritesOrder) val orderList = Converters().jsonToStringList(favoritesOrder)
val map = orderList.withIndex().associate { it.value to it.index } val map = orderList.withIndex().associate { it.value to it.index }
val sorted = starred.sortedBy { map[it.id.toString()] } val sorted = starred.sortedBy { map[it.id.toString()] }
return ArrayList(sorted) return ArrayList(sorted)
} }
@ -252,9 +258,10 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
onDragEndListener = { onDragEndListener = {
val adapter = fragment_list?.adapter val adapter = fragment_list?.adapter
if (adapter is ContactsAdapter) { if (adapter is ContactsAdapter) {
saveCustomOrderToPrefs(adapter.contactItems) val items = adapter.contactItems
saveCustomOrderToPrefs(items)
setupLetterFastscroller(items)
} }
refreshContacts(contacts)
} }
} }
} }