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

@ -426,17 +426,23 @@ class ContactsAdapter(
}
}
if (enableDrag) {
findViewById<ImageView>(R.id.drag_handle_icon).apply {
val dragIcon = findViewById<ImageView>(R.id.drag_handle_icon)
if (enableDrag && textToHighlight.isEmpty()) {
dragIcon.apply {
beVisibleIf(selectedKeys.isNotEmpty())
applyColorFilter(textColor)
setOnTouchListener { v, event ->
setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
startReorderDragListener?.requestDrag(holder)
}
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> {
val favoritesOrder = activity!!.config.favoritesContactsOrder
if (favoritesOrder.isEmpty()) {
return ArrayList(starred)
}
val orderList = Converters().jsonToStringList(favoritesOrder)
val map = orderList.withIndex().associate { it.value to it.index }
val sorted = starred.sortedBy { map[it.id.toString()] }
return ArrayList(sorted)
}
@ -252,9 +258,10 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
onDragEndListener = {
val adapter = fragment_list?.adapter
if (adapter is ContactsAdapter) {
saveCustomOrderToPrefs(adapter.contactItems)
val items = adapter.contactItems
saveCustomOrderToPrefs(items)
setupLetterFastscroller(items)
}
refreshContacts(contacts)
}
}
}