Simple-Contacts/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ChangeSortingDialog.kt

98 lines
3.9 KiB
Kotlin
Raw Normal View History

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
import com.simplemobiletools.commons.extensions.beGoneIf
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
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
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
private val binding = DialogChangeSortingBinding.inflate(activity.layoutInflater)
2017-12-10 16:19:50 +01:00
init {
activity.getAlertDialogBuilder()
.setPositiveButton(com.simplemobiletools.commons.R.string.ok) { dialog, which -> dialogConfirmed() }
.setNegativeButton(com.simplemobiletools.commons.R.string.cancel, null)
.apply {
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
} else {
config.sorting
}
2017-12-10 16:19:50 +01:00
setupSortRadio()
setupOrderRadio()
}
private fun setupSortRadio() {
val sortingRadio = binding.sortingDialogRadioSorting
sortingRadio.setOnCheckedChangeListener { group, checkedId ->
val isCustomSorting = checkedId == binding.sortingDialogRadioCustom.id
binding.sortingDialogRadioOrder.beGoneIf(isCustomSorting)
binding.divider.beGoneIf(isCustomSorting)
}
2017-12-10 16:19:50 +01:00
val sortBtn = when {
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
if (showCustomSorting) {
binding.sortingDialogRadioCustom.isChecked = config.isCustomOrderSelected
}
binding.sortingDialogRadioCustom.beGoneIf(!showCustomSorting)
2017-12-10 16:19:50 +01:00
}
private fun setupOrderRadio() {
var orderBtn = binding.sortingDialogRadioAscending
2017-12-10 16:19:50 +01:00
if (currSorting and SORT_DESCENDING != 0) {
orderBtn = binding.sortingDialogRadioDescending
2017-12-10 16:19:50 +01:00
}
orderBtn.isChecked = true
}
private fun dialogConfirmed() {
val sortingRadio = binding.sortingDialogRadioSorting
2017-12-10 16:19:50 +01:00
var sorting = when (sortingRadio.checkedRadioButtonId) {
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
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
}
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
}
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()
}
}