mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-06-05 17:00:23 +02:00
Edit sorting logic
This commit is contained in:
@ -1,65 +1,67 @@
|
||||
package com.simplemobiletools.notes.pro.dialogs
|
||||
|
||||
import android.content.DialogInterface
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_CREATED
|
||||
import com.simplemobiletools.commons.helpers.SORT_BY_TITLE
|
||||
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
|
||||
import com.simplemobiletools.notes.pro.R
|
||||
import com.simplemobiletools.notes.pro.activities.SimpleActivity
|
||||
import com.simplemobiletools.notes.pro.extensions.config
|
||||
import com.simplemobiletools.notes.pro.models.ChecklistSort
|
||||
import com.simplemobiletools.notes.pro.models.ChecklistSortDirection
|
||||
import com.simplemobiletools.notes.pro.models.ChecklistSortField
|
||||
import kotlinx.android.synthetic.main.dialog_sort_checklist.view.*
|
||||
|
||||
class SortChecklistDialog(private val activity: SimpleActivity, val callback: (ChecklistSort) -> Unit) {
|
||||
class SortChecklistDialog(private val activity: SimpleActivity, private val callback: () -> Unit) {
|
||||
private val view = activity.layoutInflater.inflate(R.layout.dialog_sort_checklist, null)
|
||||
private val config = activity.config
|
||||
private var currSorting = config.sorting
|
||||
|
||||
init {
|
||||
val config = activity.config
|
||||
val view = (activity.layoutInflater.inflate(R.layout.dialog_sort_checklist, null) as ViewGroup).apply {
|
||||
sort_field_type.check(
|
||||
when (config.checklistSortField) {
|
||||
ChecklistSortField.TITLE -> sort_field_title.id
|
||||
ChecklistSortField.DATE_CREATED -> sort_field_date_created.id
|
||||
}
|
||||
)
|
||||
|
||||
sort_direction_type.check(
|
||||
when (config.checklistSortDirection) {
|
||||
ChecklistSortDirection.ASCENDING -> sort_direction_asc.id
|
||||
ChecklistSortDirection.DESCENDING -> sort_direction_desc.id
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
setupSortRadio()
|
||||
setupOrderRadio()
|
||||
AlertDialog.Builder(activity)
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.setPositiveButton(R.string.ok) { _, _ -> dialogConfirmed() }
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.create().apply {
|
||||
activity.setupDialogStuff(view, this, R.string.sort_by) {
|
||||
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
|
||||
val sortField = getSortField(view)
|
||||
val sortDirection = getSortDirection(view)
|
||||
config.checklistSortField = sortField
|
||||
config.checklistSortDirection = sortDirection
|
||||
callback.invoke(ChecklistSort(sortField, sortDirection))
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
activity.setupDialogStuff(view, this, R.string.sort_by)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getSortField(view: View): ChecklistSortField {
|
||||
return when (view.sort_field_type.checkedRadioButtonId) {
|
||||
R.id.sort_field_title -> ChecklistSortField.TITLE
|
||||
else -> ChecklistSortField.DATE_CREATED
|
||||
private fun setupSortRadio() {
|
||||
val fieldRadio = view.sorting_dialog_radio_sorting
|
||||
var fieldBtn = fieldRadio.sorting_dialog_radio_title
|
||||
|
||||
if (currSorting and SORT_BY_DATE_CREATED != 0) {
|
||||
fieldBtn = fieldRadio.sorting_dialog_radio_date_created
|
||||
}
|
||||
|
||||
fieldBtn.isChecked = true
|
||||
}
|
||||
|
||||
private fun getSortDirection(view: View): ChecklistSortDirection {
|
||||
return when (view.sort_direction_type.checkedRadioButtonId) {
|
||||
R.id.sort_direction_asc -> ChecklistSortDirection.ASCENDING
|
||||
else -> ChecklistSortDirection.DESCENDING
|
||||
private fun setupOrderRadio() {
|
||||
val orderRadio = view.sorting_dialog_radio_order
|
||||
var orderBtn = orderRadio.sorting_dialog_radio_ascending
|
||||
|
||||
if (currSorting and SORT_DESCENDING != 0) {
|
||||
orderBtn = orderRadio.sorting_dialog_radio_descending
|
||||
}
|
||||
|
||||
orderBtn.isChecked = true
|
||||
}
|
||||
|
||||
private fun dialogConfirmed() {
|
||||
val sortingRadio = view.sorting_dialog_radio_sorting
|
||||
var sorting = when (sortingRadio.checkedRadioButtonId) {
|
||||
R.id.sorting_dialog_radio_date_created -> SORT_BY_DATE_CREATED
|
||||
else -> SORT_BY_TITLE
|
||||
}
|
||||
|
||||
if (view.sorting_dialog_radio_order.checkedRadioButtonId == R.id.sorting_dialog_radio_descending) {
|
||||
sorting = sorting or SORT_DESCENDING
|
||||
}
|
||||
|
||||
if (currSorting != sorting) {
|
||||
config.sorting = sorting
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user