From 23cdd2511b48f011deee12851a81d76caf573128 Mon Sep 17 00:00:00 2001
From: Agnieszka C <85929121+Aga-C@users.noreply.github.com>
Date: Tue, 12 Oct 2021 15:49:30 +0200
Subject: [PATCH] Added custom sorting (#464)

---
 .../notes/pro/adapters/ChecklistAdapter.kt     |  2 ++
 .../notes/pro/dialogs/SortChecklistDialog.kt   | 18 +++++++++++++++++-
 .../notes/pro/fragments/ChecklistFragment.kt   | 13 +++++++++----
 .../main/res/layout/dialog_sort_checklist.xml  | 16 ++++++++++++++--
 4 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/ChecklistAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/ChecklistAdapter.kt
index 6a083b19..6d7a1044 100644
--- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/ChecklistAdapter.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/ChecklistAdapter.kt
@@ -15,6 +15,7 @@ import com.simplemobiletools.commons.extensions.applyColorFilter
 import com.simplemobiletools.commons.extensions.beVisibleIf
 import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
 import com.simplemobiletools.commons.extensions.removeBit
+import com.simplemobiletools.commons.helpers.SORT_BY_CUSTOM
 import com.simplemobiletools.commons.interfaces.ItemMoveCallback
 import com.simplemobiletools.commons.interfaces.ItemTouchHelperContract
 import com.simplemobiletools.commons.interfaces.StartReorderDragListener
@@ -209,6 +210,7 @@ class ChecklistAdapter(activity: BaseSimpleActivity, var items: ArrayList<Checkl
     }
 
     override fun onRowMoved(fromPosition: Int, toPosition: Int) {
+        activity.config.sorting = SORT_BY_CUSTOM
         if (fromPosition < toPosition) {
             for (i in fromPosition until toPosition) {
                 Collections.swap(items, i, i + 1)
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 11a55fcf..0652cb77 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,7 +1,9 @@
 package com.simplemobiletools.notes.pro.dialogs
 
 import androidx.appcompat.app.AlertDialog
+import com.simplemobiletools.commons.extensions.beVisibleIf
 import com.simplemobiletools.commons.extensions.setupDialogStuff
+import com.simplemobiletools.commons.helpers.SORT_BY_CUSTOM
 import com.simplemobiletools.commons.helpers.SORT_BY_DATE_CREATED
 import com.simplemobiletools.commons.helpers.SORT_BY_TITLE
 import com.simplemobiletools.commons.helpers.SORT_DESCENDING
@@ -29,12 +31,24 @@ class SortChecklistDialog(private val activity: SimpleActivity, private val call
 
     private fun setupSortRadio() {
         val fieldRadio = view.sorting_dialog_radio_sorting
+        fieldRadio.setOnCheckedChangeListener { group, checkedId ->
+            val isCustomSorting = checkedId == fieldRadio.sorting_dialog_radio_custom.id
+            view.sorting_dialog_radio_order.beVisibleIf(!isCustomSorting)
+            view.sorting_dialog_order_divider.beVisibleIf(!isCustomSorting)
+            view.move_undone_checklist_items_divider.beVisibleIf((!isCustomSorting))
+            view.settings_move_undone_checklist_items_holder.beVisibleIf(!isCustomSorting)
+        }
+
         var fieldBtn = fieldRadio.sorting_dialog_radio_title
 
         if (currSorting and SORT_BY_DATE_CREATED != 0) {
             fieldBtn = fieldRadio.sorting_dialog_radio_date_created
         }
 
+        if (currSorting and SORT_BY_CUSTOM != 0) {
+            fieldBtn = fieldRadio.sorting_dialog_radio_custom
+        }
+
         fieldBtn.isChecked = true
     }
 
@@ -60,10 +74,12 @@ class SortChecklistDialog(private val activity: SimpleActivity, private val call
         val sortingRadio = view.sorting_dialog_radio_sorting
         var sorting = when (sortingRadio.checkedRadioButtonId) {
             R.id.sorting_dialog_radio_date_created -> SORT_BY_DATE_CREATED
+            R.id.sorting_dialog_radio_custom -> SORT_BY_CUSTOM
             else -> SORT_BY_TITLE
         }
 
-        if (view.sorting_dialog_radio_order.checkedRadioButtonId == R.id.sorting_dialog_radio_descending) {
+        if (sortingRadio.checkedRadioButtonId != R.id.sorting_dialog_radio_custom
+            && view.sorting_dialog_radio_order.checkedRadioButtonId == R.id.sorting_dialog_radio_descending) {
             sorting = sorting or SORT_DESCENDING
         }
 
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 6e5b2e9c..f9d0d511 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
@@ -7,6 +7,7 @@ import android.view.ViewGroup
 import com.google.gson.Gson
 import com.google.gson.reflect.TypeToken
 import com.simplemobiletools.commons.extensions.*
+import com.simplemobiletools.commons.helpers.SORT_BY_CUSTOM
 import com.simplemobiletools.commons.helpers.ensureBackgroundThread
 import com.simplemobiletools.notes.pro.R
 import com.simplemobiletools.notes.pro.activities.SimpleActivity
@@ -65,7 +66,8 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
                     migrateCheckListOnFailure(storedNote)
                 }
 
-                if (config?.moveDoneChecklistItems == true) {
+                val sorting = config?.sorting ?: 0
+                if (sorting and SORT_BY_CUSTOM == 0 && config?.moveDoneChecklistItems == true) {
                     items.sortBy { it.isDone }
                 }
 
@@ -159,9 +161,11 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
     private fun setupAdapter() {
         updateUIVisibility()
         ChecklistItem.sorting = requireContext().config.sorting
-        items.sort()
-        if (context?.config?.moveDoneChecklistItems == true) {
-            items.sortBy { it.isDone }
+        if (ChecklistItem.sorting and SORT_BY_CUSTOM == 0) {
+            items.sort()
+            if (context?.config?.moveDoneChecklistItems == true) {
+                items.sortBy { it.isDone }
+            }
         }
         ChecklistAdapter(
             activity = activity as SimpleActivity,
@@ -217,6 +221,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
     }
 
     override fun refreshItems() {
+        loadNoteById(noteId)
         setupAdapter()
     }
 }
diff --git a/app/src/main/res/layout/dialog_sort_checklist.xml b/app/src/main/res/layout/dialog_sort_checklist.xml
index cf2b42a7..042dad42 100644
--- a/app/src/main/res/layout/dialog_sort_checklist.xml
+++ b/app/src/main/res/layout/dialog_sort_checklist.xml
@@ -32,9 +32,19 @@
             android:paddingBottom="@dimen/normal_margin"
             android:text="@string/date_created" />
 
+        <com.simplemobiletools.commons.views.MyCompatRadioButton
+            android:id="@+id/sorting_dialog_radio_custom"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:paddingTop="@dimen/normal_margin"
+            android:paddingBottom="@dimen/normal_margin"
+            android:text="@string/custom" />
+
     </RadioGroup>
 
-    <include layout="@layout/divider" />
+    <include
+        android:id="@+id/sorting_dialog_order_divider"
+        layout="@layout/divider" />
 
     <RadioGroup
         android:id="@+id/sorting_dialog_radio_order"
@@ -62,7 +72,9 @@
 
     </RadioGroup>
 
-    <include layout="@layout/divider" />
+    <include
+        android:id="@+id/move_undone_checklist_items_divider"
+        layout="@layout/divider" />
 
     <RelativeLayout
         android:id="@+id/settings_move_undone_checklist_items_holder"