allow reordering favorites by drag and drop

This commit is contained in:
Pavel Poley 2022-05-06 22:19:17 +03:00
parent 145d781ecb
commit 5b75c5f26b
6 changed files with 41 additions and 8 deletions

View file

@ -35,6 +35,7 @@ import com.simplemobiletools.contacts.pro.dialogs.ImportContactsDialog
import com.simplemobiletools.contacts.pro.extensions.config
import com.simplemobiletools.contacts.pro.extensions.getTempFile
import com.simplemobiletools.contacts.pro.extensions.handleGenericContactClick
import com.simplemobiletools.contacts.pro.fragments.FavoritesFragment
import com.simplemobiletools.contacts.pro.fragments.MyViewPagerFragment
import com.simplemobiletools.contacts.pro.helpers.ALL_TABS_MASK
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
@ -185,8 +186,9 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.sort -> showSortingDialog()
R.id.sort -> showSortingDialog(showCustomSorting = getCurrentFragment() is FavoritesFragment)
R.id.filter -> showFilterDialog()
R.id.dialpad -> launchDialpad()
R.id.import_contacts -> tryImportContacts()
@ -410,8 +412,8 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
}
}
private fun showSortingDialog() {
ChangeSortingDialog(this) {
private fun showSortingDialog(showCustomSorting: Boolean) {
ChangeSortingDialog(this, showCustomSorting) {
refreshContacts(TAB_CONTACTS or TAB_FAVORITES)
}
}

View file

@ -450,7 +450,8 @@ class ContactsAdapter(
override fun onChange(position: Int) = contactItems.getOrNull(position)?.getBubbleText() ?: ""
override fun onRowMoved(fromPosition: Int, toPosition: Int) {
activity.config.sorting = SORT_BY_CUSTOM
activity.config.isCustomOrderSelected = true
if (fromPosition < toPosition) {
for (i in fromPosition until toPosition) {
Collections.swap(contactItems, i, i + 1)
@ -460,6 +461,7 @@ class ContactsAdapter(
Collections.swap(contactItems, i, i - 1)
}
}
notifyItemMoved(fromPosition, toPosition)
}

View file

@ -9,7 +9,7 @@ import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.extensions.config
import kotlinx.android.synthetic.main.dialog_change_sorting.view.*
class ChangeSortingDialog(val activity: BaseSimpleActivity, private val callback: () -> Unit) {
class ChangeSortingDialog(val activity: BaseSimpleActivity, private val showCustomSorting: Boolean = false, private val callback: () -> Unit) {
private var currSorting = 0
private var config = activity.config
private var view = activity.layoutInflater.inflate(R.layout.dialog_change_sorting, null)
@ -22,7 +22,16 @@ class ChangeSortingDialog(val activity: BaseSimpleActivity, private val callback
activity.setupDialogStuff(view, this, R.string.sort_by)
}
currSorting = config.sorting
currSorting = if (showCustomSorting) {
if (config.isCustomOrderSelected) {
SORT_BY_CUSTOM
} else {
config.sorting
}
} else {
config.sorting
}
setupSortRadio()
setupOrderRadio()
}
@ -44,6 +53,11 @@ class ChangeSortingDialog(val activity: BaseSimpleActivity, private val callback
else -> sortingRadio.sorting_dialog_radio_date_created
}
sortBtn.isChecked = true
if (showCustomSorting) {
sortingRadio.sorting_dialog_radio_custom.isChecked = config.isCustomOrderSelected
}
view.sorting_dialog_radio_custom.beGoneIf(!showCustomSorting)
}
private fun setupOrderRadio() {
@ -71,7 +85,17 @@ class ChangeSortingDialog(val activity: BaseSimpleActivity, private val callback
sorting = sorting or SORT_DESCENDING
}
config.sorting = sorting
if (showCustomSorting) {
if (sorting == SORT_BY_CUSTOM) {
config.isCustomOrderSelected = true
} else {
config.isCustomOrderSelected = false
config.sorting = sorting
}
} else {
config.sorting = sorting
}
callback()
}
}

View file

@ -128,7 +128,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
this is FavoritesFragment -> {
val favorites = contacts.filter { it.starred == 1 } as ArrayList<Contact>
if (activity!!.config.sorting == SORT_BY_CUSTOM) {
if (activity!!.config.isCustomOrderSelected) {
sortByCustomOrder(favorites)
} else {
favorites

View file

@ -70,4 +70,8 @@ class Config(context: Context) : BaseConfig(context) {
var favoritesContactsOrder: String
get() = prefs.getString(FAVORITES_CONTACTS_ORDER, "")!!
set(order) = prefs.edit().putString(FAVORITES_CONTACTS_ORDER, order).apply()
var isCustomOrderSelected: Boolean
get() = prefs.getBoolean(FAVORITES_CUSTOM_ORDER_SELECTED, false)
set(selected) = prefs.edit().putBoolean(FAVORITES_CUSTOM_ORDER_SELECTED, selected).apply()
}

View file

@ -23,6 +23,7 @@ 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 FAVORITES_CUSTOM_ORDER_SELECTED = "favorites_custom_order_selected"
const val SMT_PRIVATE = "smt_private" // used at the contact source of local contacts hidden from other apps
const val GROUP = "group"