From f807961768aa0fa56ad27b377c80bd8f8e193bd2 Mon Sep 17 00:00:00 2001 From: Pavel Poley Date: Thu, 5 May 2022 18:03:58 +0300 Subject: [PATCH] allow reordering favorites by drag and drop --- .../contacts/pro/adapters/ContactsAdapter.kt | 33 ++++++++++++++++--- .../pro/dialogs/ChangeSortingDialog.kt | 2 ++ .../pro/fragments/MyViewPagerFragment.kt | 18 +++++++++- .../contacts/pro/helpers/Config.kt | 4 +++ .../contacts/pro/helpers/Constants.kt | 1 + .../main/res/layout/dialog_change_sorting.xml | 8 +++++ 6 files changed, 60 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 7423a08c..bdddaf79 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 @@ -8,6 +8,7 @@ import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable import android.graphics.drawable.Icon import android.graphics.drawable.LayerDrawable +import android.util.Log import android.util.TypedValue import android.view.Menu import android.view.MotionEvent @@ -22,6 +23,7 @@ import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.signature.ObjectKey +import com.google.gson.Gson import com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.dialogs.ConfirmationDialog @@ -42,6 +44,7 @@ import com.simplemobiletools.contacts.pro.helpers.* import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener import com.simplemobiletools.contacts.pro.interfaces.RemoveFromGroupListener import com.simplemobiletools.contacts.pro.models.Contact +import java.util.* class ContactsAdapter( activity: SimpleActivity, @@ -420,6 +423,10 @@ class ContactsAdapter( } } + // TODO: + if (enableDrag) { + + } findViewById(R.id.item_contact_image).setOnTouchListener { v, event -> if (event.action == MotionEvent.ACTION_DOWN) { startReorderDragListener?.requestDrag(holder) @@ -431,15 +438,31 @@ class ContactsAdapter( override fun onChange(position: Int) = contactItems.getOrNull(position)?.getBubbleText() ?: "" - override fun onRowClear(myViewHolder: ViewHolder?) { - - } - override fun onRowMoved(fromPosition: Int, toPosition: Int) { - + activity.config.sorting = SORT_BY_CUSTOM // TODO: check if i can use this constant + if (fromPosition < toPosition) { + for (i in fromPosition until toPosition) { + Collections.swap(contactItems, i, i + 1) + } + } else { + for (i in fromPosition downTo toPosition + 1) { + Collections.swap(contactItems, i, i - 1) + } + } + notifyItemMoved(fromPosition, toPosition) } override fun onRowSelected(myViewHolder: ViewHolder?) { } + + override fun onRowClear(myViewHolder: ViewHolder?) { + for (contact in contactItems) { + Log.d("test", "onRowClear: ${contact.getNameToDisplay()} ${contact.id} ${contact.contactId}") + } + val orderIds = contactItems.map { it.id } + val orderGsonString = Gson().toJson(orderIds) + Log.d("test", "onRowClear: $orderGsonString") + activity.config.favoritesContactsOrder = orderGsonString + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ChangeSortingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ChangeSortingDialog.kt index 480ed683..50192c9a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ChangeSortingDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ChangeSortingDialog.kt @@ -33,6 +33,7 @@ class ChangeSortingDialog(val activity: BaseSimpleActivity, private val callback currSorting and SORT_BY_MIDDLE_NAME != 0 -> sortingRadio.sorting_dialog_radio_middle_name currSorting and SORT_BY_SURNAME != 0 -> sortingRadio.sorting_dialog_radio_surname currSorting and SORT_BY_FULL_NAME != 0 -> sortingRadio.sorting_dialog_radio_full_name + currSorting and SORT_BY_CUSTOM != 0 -> sortingRadio.sorting_dialog_radio_custom else -> sortingRadio.sorting_dialog_radio_date_created } sortBtn.isChecked = true @@ -55,6 +56,7 @@ class ChangeSortingDialog(val activity: BaseSimpleActivity, private val callback R.id.sorting_dialog_radio_middle_name -> SORT_BY_MIDDLE_NAME R.id.sorting_dialog_radio_surname -> SORT_BY_SURNAME R.id.sorting_dialog_radio_full_name -> SORT_BY_FULL_NAME + R.id.sorting_dialog_radio_custom -> SORT_BY_CUSTOM else -> SORT_BY_DATE_CREATED } 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 5c615772..ea8690e7 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 @@ -124,7 +124,15 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) val filtered = when { this is GroupsFragment -> contacts - this is FavoritesFragment -> contacts.filter { it.starred == 1 } as ArrayList + this is FavoritesFragment -> { + val favorites = contacts.filter { it.starred == 1 } as ArrayList + + if (activity!!.config.sorting == SORT_BY_CUSTOM) { + sortByCustomOrder(favorites) + } else { + favorites + } + } else -> { val contactSources = activity!!.getVisibleContactSources() contacts.filter { contactSources.contains(it.source) } as ArrayList @@ -152,6 +160,14 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) } } + private fun sortByCustomOrder(starred: List): ArrayList { + val favoritesOrder = activity!!.config.favoritesContactsOrder + 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) + } + private fun setupContacts(contacts: ArrayList) { if (this is GroupsFragment) { setupGroupsAdapter(contacts) { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt index fe209b56..ef11b261 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt @@ -66,4 +66,8 @@ class Config(context: Context) : BaseConfig(context) { var mergeDuplicateContacts: Boolean get() = prefs.getBoolean(MERGE_DUPLICATE_CONTACTS, true) set(mergeDuplicateContacts) = prefs.edit().putBoolean(MERGE_DUPLICATE_CONTACTS, mergeDuplicateContacts).apply() + + var favoritesContactsOrder: String + get() = prefs.getString(FAVORITES_CONTACTS_ORDER, "")!! + set(order) = prefs.edit().putString(FAVORITES_CONTACTS_ORDER, order).apply() } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt index 8a347061..d345ad93 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt @@ -22,6 +22,7 @@ const val LAST_EXPORT_PATH = "last_export_path" const val WAS_LOCAL_ACCOUNT_INITIALIZED = "was_local_account_initialized" const val SHOW_PRIVATE_CONTACTS = "show_private_contacts" const val MERGE_DUPLICATE_CONTACTS = "merge_duplicate_contacts" +const val FAVORITES_CONTACTS_ORDER = "favorites_contacts_order" const val SMT_PRIVATE = "smt_private" // used at the contact source of local contacts hidden from other apps const val GROUP = "group" diff --git a/app/src/main/res/layout/dialog_change_sorting.xml b/app/src/main/res/layout/dialog_change_sorting.xml index 29946081..a60535f7 100644 --- a/app/src/main/res/layout/dialog_change_sorting.xml +++ b/app/src/main/res/layout/dialog_change_sorting.xml @@ -59,6 +59,14 @@ android:paddingBottom="@dimen/medium_margin" android:text="@string/date_created" /> + +