allow reordering favorites by drag and drop

This commit is contained in:
Pavel Poley 2022-05-05 18:03:58 +03:00
parent 307ce40e19
commit f807961768
6 changed files with 60 additions and 6 deletions

View File

@ -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<ImageView>(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
}
}

View File

@ -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
}

View File

@ -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<Contact>
this is FavoritesFragment -> {
val favorites = contacts.filter { it.starred == 1 } as ArrayList<Contact>
if (activity!!.config.sorting == SORT_BY_CUSTOM) {
sortByCustomOrder(favorites)
} else {
favorites
}
}
else -> {
val contactSources = activity!!.getVisibleContactSources()
contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
@ -152,6 +160,14 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
}
}
private fun sortByCustomOrder(starred: List<Contact>): ArrayList<Contact> {
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<Contact>) {
if (this is GroupsFragment) {
setupGroupsAdapter(contacts) {

View File

@ -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()
}

View File

@ -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"

View File

@ -59,6 +59,14 @@
android:paddingBottom="@dimen/medium_margin"
android:text="@string/date_created" />
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/sorting_dialog_radio_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin"
android:text="@string/custom" />
</RadioGroup>
<include layout="@layout/divider" />