2018-11-05 12:36:29 +01:00
|
|
|
package com.simplemobiletools.contacts.pro.dialogs
|
2017-12-10 16:19:50 +01:00
|
|
|
|
|
|
|
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
2022-05-06 14:57:20 +02:00
|
|
|
import com.simplemobiletools.commons.extensions.beGoneIf
|
2022-07-21 12:02:18 +02:00
|
|
|
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
|
2017-12-10 16:19:50 +01:00
|
|
|
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
2020-06-03 11:18:47 +02:00
|
|
|
import com.simplemobiletools.commons.helpers.*
|
2018-11-05 12:36:29 +01:00
|
|
|
import com.simplemobiletools.contacts.pro.R
|
2023-08-07 16:13:37 +02:00
|
|
|
import com.simplemobiletools.contacts.pro.databinding.DialogChangeSortingBinding
|
2018-11-05 12:36:29 +01:00
|
|
|
import com.simplemobiletools.contacts.pro.extensions.config
|
2017-12-10 16:19:50 +01:00
|
|
|
|
2022-05-06 21:19:17 +02:00
|
|
|
class ChangeSortingDialog(val activity: BaseSimpleActivity, private val showCustomSorting: Boolean = false, private val callback: () -> Unit) {
|
2017-12-10 16:19:50 +01:00
|
|
|
private var currSorting = 0
|
|
|
|
private var config = activity.config
|
2023-08-07 16:13:37 +02:00
|
|
|
private val binding = DialogChangeSortingBinding.inflate(activity.layoutInflater)
|
2017-12-10 16:19:50 +01:00
|
|
|
|
|
|
|
init {
|
2022-07-21 12:02:18 +02:00
|
|
|
activity.getAlertDialogBuilder()
|
2023-08-07 16:13:37 +02:00
|
|
|
.setPositiveButton(com.simplemobiletools.commons.R.string.ok) { dialog, which -> dialogConfirmed() }
|
|
|
|
.setNegativeButton(com.simplemobiletools.commons.R.string.cancel, null)
|
2022-07-21 12:02:18 +02:00
|
|
|
.apply {
|
2023-08-07 16:13:37 +02:00
|
|
|
activity.setupDialogStuff(binding.root, this, com.simplemobiletools.commons.R.string.sort_by)
|
2020-06-03 11:18:47 +02:00
|
|
|
}
|
2017-12-10 16:19:50 +01:00
|
|
|
|
2022-05-11 11:04:26 +02:00
|
|
|
currSorting = if (showCustomSorting && config.isCustomOrderSelected) {
|
|
|
|
SORT_BY_CUSTOM
|
2022-05-06 21:19:17 +02:00
|
|
|
} else {
|
|
|
|
config.sorting
|
|
|
|
}
|
|
|
|
|
2017-12-10 16:19:50 +01:00
|
|
|
setupSortRadio()
|
|
|
|
setupOrderRadio()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setupSortRadio() {
|
2023-08-07 16:13:37 +02:00
|
|
|
val sortingRadio = binding.sortingDialogRadioSorting
|
2022-05-06 14:57:20 +02:00
|
|
|
|
|
|
|
sortingRadio.setOnCheckedChangeListener { group, checkedId ->
|
2023-08-07 16:13:37 +02:00
|
|
|
val isCustomSorting = checkedId == binding.sortingDialogRadioCustom.id
|
|
|
|
binding.sortingDialogRadioOrder.beGoneIf(isCustomSorting)
|
|
|
|
binding.divider.beGoneIf(isCustomSorting)
|
2022-05-06 14:57:20 +02:00
|
|
|
}
|
|
|
|
|
2017-12-10 16:19:50 +01:00
|
|
|
val sortBtn = when {
|
2023-08-07 16:13:37 +02:00
|
|
|
currSorting and SORT_BY_FIRST_NAME != 0 -> binding.sortingDialogRadioFirstName
|
|
|
|
currSorting and SORT_BY_MIDDLE_NAME != 0 -> binding.sortingDialogRadioMiddleName
|
|
|
|
currSorting and SORT_BY_SURNAME != 0 -> binding.sortingDialogRadioSurname
|
|
|
|
currSorting and SORT_BY_FULL_NAME != 0 -> binding.sortingDialogRadioFullName
|
|
|
|
currSorting and SORT_BY_CUSTOM != 0 -> binding.sortingDialogRadioCustom
|
|
|
|
else -> binding.sortingDialogRadioDateCreated
|
2017-12-10 16:19:50 +01:00
|
|
|
}
|
|
|
|
sortBtn.isChecked = true
|
2022-05-06 21:19:17 +02:00
|
|
|
|
|
|
|
if (showCustomSorting) {
|
2023-08-07 16:13:37 +02:00
|
|
|
binding.sortingDialogRadioCustom.isChecked = config.isCustomOrderSelected
|
2022-05-06 21:19:17 +02:00
|
|
|
}
|
2023-08-07 16:13:37 +02:00
|
|
|
binding.sortingDialogRadioCustom.beGoneIf(!showCustomSorting)
|
2017-12-10 16:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun setupOrderRadio() {
|
2023-08-07 16:13:37 +02:00
|
|
|
var orderBtn = binding.sortingDialogRadioAscending
|
2017-12-10 16:19:50 +01:00
|
|
|
|
|
|
|
if (currSorting and SORT_DESCENDING != 0) {
|
2023-08-07 16:13:37 +02:00
|
|
|
orderBtn = binding.sortingDialogRadioDescending
|
2017-12-10 16:19:50 +01:00
|
|
|
}
|
|
|
|
orderBtn.isChecked = true
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun dialogConfirmed() {
|
2023-08-07 16:13:37 +02:00
|
|
|
val sortingRadio = binding.sortingDialogRadioSorting
|
2017-12-10 16:19:50 +01:00
|
|
|
var sorting = when (sortingRadio.checkedRadioButtonId) {
|
2017-12-13 17:16:16 +01:00
|
|
|
R.id.sorting_dialog_radio_first_name -> SORT_BY_FIRST_NAME
|
|
|
|
R.id.sorting_dialog_radio_middle_name -> SORT_BY_MIDDLE_NAME
|
2020-06-03 11:18:47 +02:00
|
|
|
R.id.sorting_dialog_radio_surname -> SORT_BY_SURNAME
|
2021-08-17 13:51:58 +02:00
|
|
|
R.id.sorting_dialog_radio_full_name -> SORT_BY_FULL_NAME
|
2022-05-05 17:03:58 +02:00
|
|
|
R.id.sorting_dialog_radio_custom -> SORT_BY_CUSTOM
|
2021-08-17 13:51:58 +02:00
|
|
|
else -> SORT_BY_DATE_CREATED
|
2017-12-10 16:19:50 +01:00
|
|
|
}
|
|
|
|
|
2023-08-07 16:13:37 +02:00
|
|
|
if (sorting != SORT_BY_CUSTOM && binding.sortingDialogRadioOrder.checkedRadioButtonId == R.id.sorting_dialog_radio_descending) {
|
2017-12-10 16:19:50 +01:00
|
|
|
sorting = sorting or SORT_DESCENDING
|
|
|
|
}
|
|
|
|
|
2022-05-06 21:19:17 +02:00
|
|
|
if (showCustomSorting) {
|
|
|
|
if (sorting == SORT_BY_CUSTOM) {
|
|
|
|
config.isCustomOrderSelected = true
|
|
|
|
} else {
|
|
|
|
config.isCustomOrderSelected = false
|
|
|
|
config.sorting = sorting
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config.sorting = sorting
|
|
|
|
}
|
|
|
|
|
2017-12-10 16:19:50 +01:00
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|