From 145d781ecb73edc8544c6e1ca8ea5a53f4705b24 Mon Sep 17 00:00:00 2001 From: Pavel Poley Date: Fri, 6 May 2022 16:51:43 +0300 Subject: [PATCH] allow reordering favorites by drag and drop --- .../contacts/pro/adapters/ContactsAdapter.kt | 14 ++++++++++---- .../contacts/pro/fragments/MyViewPagerFragment.kt | 11 +++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ContactsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ContactsAdapter.kt index 36d3b6a6..656d6eb0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ContactsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ContactsAdapter.kt @@ -54,7 +54,7 @@ class ContactsAdapter( highlightText: String = "", private val enableDrag: Boolean = false, itemClick: (Any) -> Unit - ) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), RecyclerViewFastScroller.OnPopupTextUpdate, ItemTouchHelperContract { +) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), RecyclerViewFastScroller.OnPopupTextUpdate, ItemTouchHelperContract { private val NEW_GROUP_ID = -1 private var config = activity.config @@ -426,17 +426,23 @@ class ContactsAdapter( } } - if (enableDrag) { - findViewById(R.id.drag_handle_icon).apply { + val dragIcon = findViewById(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) + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt index a5027756..5bcc0aa6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt @@ -163,9 +163,15 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) private fun sortByCustomOrder(starred: List): ArrayList { 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) } } }