From 0937d0da24c94d4ed8c9de259c099aff7bc0e005 Mon Sep 17 00:00:00 2001 From: darthpaul Date: Tue, 5 Oct 2021 21:39:34 +0100 Subject: [PATCH] Edit sorting logic --- .../notes/pro/activities/MainActivity.kt | 4 +- .../notes/pro/adapters/NotesPagerAdapter.kt | 5 +- .../notes/pro/dialogs/SortChecklistDialog.kt | 88 ++++++++++--------- .../notes/pro/fragments/ChecklistFragment.kt | 9 +- .../notes/pro/helpers/Config.kt | 10 --- .../notes/pro/helpers/Constants.kt | 2 - .../notes/pro/models/ChecklistItem.kt | 23 ++++- .../main/res/layout/dialog_sort_checklist.xml | 12 +-- app/src/main/res/values/integers.xml | 3 + 9 files changed, 82 insertions(+), 74 deletions(-) create mode 100644 app/src/main/res/values/integers.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt index 214bbdd4..c98a87e4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt @@ -40,9 +40,9 @@ import com.simplemobiletools.notes.pro.helpers.NoteType import com.simplemobiletools.notes.pro.helpers.NotesHelper import com.simplemobiletools.notes.pro.helpers.OPEN_NOTE_ID import com.simplemobiletools.notes.pro.models.Note -import kotlinx.android.synthetic.main.activity_main.* import java.io.File import java.nio.charset.Charset +import kotlinx.android.synthetic.main.activity_main.* class MainActivity : SimpleActivity() { private val EXPORT_FILE_SYNC = 1 @@ -1176,7 +1176,7 @@ class MainActivity : SimpleActivity() { private fun displaySortChecklistDialog() { SortChecklistDialog(this) { - getPagerAdapter().sortChecklistItems(view_pager.currentItem, it) + getPagerAdapter().refreshChecklist(view_pager.currentItem) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt index e23a0bef..55b93fc7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt @@ -11,7 +11,6 @@ import com.simplemobiletools.notes.pro.fragments.NoteFragment import com.simplemobiletools.notes.pro.fragments.TextFragment import com.simplemobiletools.notes.pro.helpers.NOTE_ID import com.simplemobiletools.notes.pro.helpers.NoteType -import com.simplemobiletools.notes.pro.models.ChecklistSort import com.simplemobiletools.notes.pro.models.Note class NotesPagerAdapter(fm: FragmentManager, val notes: List, val activity: Activity) : FragmentStatePagerAdapter(fm) { @@ -97,7 +96,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List, val activity (fragments[position] as? ChecklistFragment)?.removeDoneItems() } - fun sortChecklistItems(position: Int, sort:ChecklistSort){ - (fragments[position] as? ChecklistFragment)?.sortChecklist(sort) + fun refreshChecklist(position: Int){ + (fragments[position] as? ChecklistFragment)?.refreshItems() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/SortChecklistDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/SortChecklistDialog.kt index a92afd90..202e61fa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/SortChecklistDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/SortChecklistDialog.kt @@ -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() } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt index a3ee16eb..8a36a75c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt @@ -19,7 +19,6 @@ import com.simplemobiletools.notes.pro.helpers.NOTE_ID import com.simplemobiletools.notes.pro.helpers.NotesHelper import com.simplemobiletools.notes.pro.interfaces.ChecklistItemsListener import com.simplemobiletools.notes.pro.models.ChecklistItem -import com.simplemobiletools.notes.pro.models.ChecklistSort import com.simplemobiletools.notes.pro.models.Note import kotlinx.android.synthetic.main.fragment_checklist.view.* @@ -159,7 +158,8 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { private fun setupAdapter() { updateUIVisibility() - + ChecklistItem.sorting = requireContext().config.sorting + items.sort() ChecklistAdapter( activity = activity as SimpleActivity, items = items, @@ -216,9 +216,4 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { override fun refreshItems() { setupAdapter() } - - fun sortChecklist(sort: ChecklistSort) { - items = items.sortedWith(sort.getSortComparator()).toMutableList() as ArrayList - setupAdapter() - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Config.kt index f04c823c..9f3da2ad 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Config.kt @@ -5,8 +5,6 @@ import android.content.Context import android.os.Environment import android.view.Gravity import com.simplemobiletools.commons.helpers.BaseConfig -import com.simplemobiletools.notes.pro.models.ChecklistSortDirection -import com.simplemobiletools.notes.pro.models.ChecklistSortField class Config(context: Context) : BaseConfig(context) { companion object { @@ -95,12 +93,4 @@ class Config(context: Context) : BaseConfig(context) { var addNewChecklistItemsTop: Boolean get() = prefs.getBoolean(ADD_NEW_CHECKLIST_ITEMS_TOP, false) set(addNewCheckListItemsTop) = prefs.edit().putBoolean(ADD_NEW_CHECKLIST_ITEMS_TOP, addNewCheckListItemsTop).apply() - - var checklistSortField: ChecklistSortField - get() = ChecklistSortField.valueOf(prefs.getString(CHECKLIST_SORT_FIELD, ChecklistSortField.TITLE.name)!!) - set(sortField) = prefs.edit().putString(CHECKLIST_SORT_FIELD, sortField.name).apply() - - var checklistSortDirection: ChecklistSortDirection - get() = ChecklistSortDirection.valueOf(prefs.getString(CHECKLIST_SORT_DIRECTION, ChecklistSortDirection.ASCENDING.name)!!) - set(sortDirection) = prefs.edit().putString(CHECKLIST_SORT_DIRECTION, sortDirection.name).apply() } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Constants.kt index 9b579fa6..2a439d41 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Constants.kt @@ -32,8 +32,6 @@ const val LAST_CREATED_NOTE_TYPE = "last_created_note_type" const val MOVE_DONE_CHECKLIST_ITEMS = "move_undone_checklist_items" // it has been replaced from moving undone items at the top to moving done to bottom const val FONT_SIZE_PERCENTAGE = "font_size_percentage" const val ADD_NEW_CHECKLIST_ITEMS_TOP = "add_new_checklist_items_top" -const val CHECKLIST_SORT_FIELD = "checklist_sort_field" -const val CHECKLIST_SORT_DIRECTION = "checklist_sort_direction" // gravity const val GRAVITY_LEFT = 0 diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/ChecklistItem.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/ChecklistItem.kt index 39b3cede..79d79be8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/ChecklistItem.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/ChecklistItem.kt @@ -1,3 +1,24 @@ package com.simplemobiletools.notes.pro.models -data class ChecklistItem(val id: Int, val dateCreated: Long = 0L, var title: String, var isDone: Boolean) +import com.simplemobiletools.commons.helpers.AlphanumericComparator +import com.simplemobiletools.commons.helpers.SORT_BY_TITLE +import com.simplemobiletools.commons.helpers.SORT_DESCENDING + +data class ChecklistItem(val id: Int, val dateCreated: Long = 0L, var title: String, var isDone: Boolean) : Comparable { + companion object { + var sorting = 0 + } + + override fun compareTo(other: ChecklistItem): Int { + var result = when { + sorting and SORT_BY_TITLE != 0 -> AlphanumericComparator().compare(title.lowercase(), other.title.lowercase()) + else -> dateCreated.compareTo(other.dateCreated) + } + + if (sorting and SORT_DESCENDING != 0) { + result *= -1 + } + + return result + } +} diff --git a/app/src/main/res/layout/dialog_sort_checklist.xml b/app/src/main/res/layout/dialog_sort_checklist.xml index 0d46d608..a8d7e200 100644 --- a/app/src/main/res/layout/dialog_sort_checklist.xml +++ b/app/src/main/res/layout/dialog_sort_checklist.xml @@ -9,13 +9,13 @@ android:paddingEnd="@dimen/activity_margin"> + 2048 +