mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-04-11 06:01:04 +02:00
Edit sorting logic
This commit is contained in:
parent
f5ccdc45b9
commit
0937d0da24
app/src/main
kotlin/com/simplemobiletools/notes/pro
activities
adapters
dialogs
fragments
helpers
models
res
@ -40,9 +40,9 @@ import com.simplemobiletools.notes.pro.helpers.NoteType
|
|||||||
import com.simplemobiletools.notes.pro.helpers.NotesHelper
|
import com.simplemobiletools.notes.pro.helpers.NotesHelper
|
||||||
import com.simplemobiletools.notes.pro.helpers.OPEN_NOTE_ID
|
import com.simplemobiletools.notes.pro.helpers.OPEN_NOTE_ID
|
||||||
import com.simplemobiletools.notes.pro.models.Note
|
import com.simplemobiletools.notes.pro.models.Note
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
|
|
||||||
class MainActivity : SimpleActivity() {
|
class MainActivity : SimpleActivity() {
|
||||||
private val EXPORT_FILE_SYNC = 1
|
private val EXPORT_FILE_SYNC = 1
|
||||||
@ -1176,7 +1176,7 @@ class MainActivity : SimpleActivity() {
|
|||||||
|
|
||||||
private fun displaySortChecklistDialog() {
|
private fun displaySortChecklistDialog() {
|
||||||
SortChecklistDialog(this) {
|
SortChecklistDialog(this) {
|
||||||
getPagerAdapter().sortChecklistItems(view_pager.currentItem, it)
|
getPagerAdapter().refreshChecklist(view_pager.currentItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ import com.simplemobiletools.notes.pro.fragments.NoteFragment
|
|||||||
import com.simplemobiletools.notes.pro.fragments.TextFragment
|
import com.simplemobiletools.notes.pro.fragments.TextFragment
|
||||||
import com.simplemobiletools.notes.pro.helpers.NOTE_ID
|
import com.simplemobiletools.notes.pro.helpers.NOTE_ID
|
||||||
import com.simplemobiletools.notes.pro.helpers.NoteType
|
import com.simplemobiletools.notes.pro.helpers.NoteType
|
||||||
import com.simplemobiletools.notes.pro.models.ChecklistSort
|
|
||||||
import com.simplemobiletools.notes.pro.models.Note
|
import com.simplemobiletools.notes.pro.models.Note
|
||||||
|
|
||||||
class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity: Activity) : FragmentStatePagerAdapter(fm) {
|
class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity: Activity) : FragmentStatePagerAdapter(fm) {
|
||||||
@ -97,7 +96,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
|
|||||||
(fragments[position] as? ChecklistFragment)?.removeDoneItems()
|
(fragments[position] as? ChecklistFragment)?.removeDoneItems()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sortChecklistItems(position: Int, sort:ChecklistSort){
|
fun refreshChecklist(position: Int){
|
||||||
(fragments[position] as? ChecklistFragment)?.sortChecklist(sort)
|
(fragments[position] as? ChecklistFragment)?.refreshItems()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,65 +1,67 @@
|
|||||||
package com.simplemobiletools.notes.pro.dialogs
|
package com.simplemobiletools.notes.pro.dialogs
|
||||||
|
|
||||||
import android.content.DialogInterface
|
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
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.R
|
||||||
import com.simplemobiletools.notes.pro.activities.SimpleActivity
|
import com.simplemobiletools.notes.pro.activities.SimpleActivity
|
||||||
import com.simplemobiletools.notes.pro.extensions.config
|
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.*
|
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 {
|
init {
|
||||||
val config = activity.config
|
setupSortRadio()
|
||||||
val view = (activity.layoutInflater.inflate(R.layout.dialog_sort_checklist, null) as ViewGroup).apply {
|
setupOrderRadio()
|
||||||
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
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
AlertDialog.Builder(activity)
|
AlertDialog.Builder(activity)
|
||||||
.setPositiveButton(R.string.ok, null)
|
.setPositiveButton(R.string.ok) { _, _ -> dialogConfirmed() }
|
||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
.create().apply {
|
.create().apply {
|
||||||
activity.setupDialogStuff(view, this, R.string.sort_by) {
|
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSortField(view: View): ChecklistSortField {
|
private fun setupSortRadio() {
|
||||||
return when (view.sort_field_type.checkedRadioButtonId) {
|
val fieldRadio = view.sorting_dialog_radio_sorting
|
||||||
R.id.sort_field_title -> ChecklistSortField.TITLE
|
var fieldBtn = fieldRadio.sorting_dialog_radio_title
|
||||||
else -> ChecklistSortField.DATE_CREATED
|
|
||||||
|
if (currSorting and SORT_BY_DATE_CREATED != 0) {
|
||||||
|
fieldBtn = fieldRadio.sorting_dialog_radio_date_created
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fieldBtn.isChecked = true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSortDirection(view: View): ChecklistSortDirection {
|
private fun setupOrderRadio() {
|
||||||
return when (view.sort_direction_type.checkedRadioButtonId) {
|
val orderRadio = view.sorting_dialog_radio_order
|
||||||
R.id.sort_direction_asc -> ChecklistSortDirection.ASCENDING
|
var orderBtn = orderRadio.sorting_dialog_radio_ascending
|
||||||
else -> ChecklistSortDirection.DESCENDING
|
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ import com.simplemobiletools.notes.pro.helpers.NOTE_ID
|
|||||||
import com.simplemobiletools.notes.pro.helpers.NotesHelper
|
import com.simplemobiletools.notes.pro.helpers.NotesHelper
|
||||||
import com.simplemobiletools.notes.pro.interfaces.ChecklistItemsListener
|
import com.simplemobiletools.notes.pro.interfaces.ChecklistItemsListener
|
||||||
import com.simplemobiletools.notes.pro.models.ChecklistItem
|
import com.simplemobiletools.notes.pro.models.ChecklistItem
|
||||||
import com.simplemobiletools.notes.pro.models.ChecklistSort
|
|
||||||
import com.simplemobiletools.notes.pro.models.Note
|
import com.simplemobiletools.notes.pro.models.Note
|
||||||
import kotlinx.android.synthetic.main.fragment_checklist.view.*
|
import kotlinx.android.synthetic.main.fragment_checklist.view.*
|
||||||
|
|
||||||
@ -159,7 +158,8 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
|
|||||||
|
|
||||||
private fun setupAdapter() {
|
private fun setupAdapter() {
|
||||||
updateUIVisibility()
|
updateUIVisibility()
|
||||||
|
ChecklistItem.sorting = requireContext().config.sorting
|
||||||
|
items.sort()
|
||||||
ChecklistAdapter(
|
ChecklistAdapter(
|
||||||
activity = activity as SimpleActivity,
|
activity = activity as SimpleActivity,
|
||||||
items = items,
|
items = items,
|
||||||
@ -216,9 +216,4 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
|
|||||||
override fun refreshItems() {
|
override fun refreshItems() {
|
||||||
setupAdapter()
|
setupAdapter()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sortChecklist(sort: ChecklistSort) {
|
|
||||||
items = items.sortedWith(sort.getSortComparator()).toMutableList() as ArrayList<ChecklistItem>
|
|
||||||
setupAdapter()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,6 @@ import android.content.Context
|
|||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.view.Gravity
|
import android.view.Gravity
|
||||||
import com.simplemobiletools.commons.helpers.BaseConfig
|
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) {
|
class Config(context: Context) : BaseConfig(context) {
|
||||||
companion object {
|
companion object {
|
||||||
@ -95,12 +93,4 @@ class Config(context: Context) : BaseConfig(context) {
|
|||||||
var addNewChecklistItemsTop: Boolean
|
var addNewChecklistItemsTop: Boolean
|
||||||
get() = prefs.getBoolean(ADD_NEW_CHECKLIST_ITEMS_TOP, false)
|
get() = prefs.getBoolean(ADD_NEW_CHECKLIST_ITEMS_TOP, false)
|
||||||
set(addNewCheckListItemsTop) = prefs.edit().putBoolean(ADD_NEW_CHECKLIST_ITEMS_TOP, addNewCheckListItemsTop).apply()
|
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()
|
|
||||||
}
|
}
|
||||||
|
@ -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 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 FONT_SIZE_PERCENTAGE = "font_size_percentage"
|
||||||
const val ADD_NEW_CHECKLIST_ITEMS_TOP = "add_new_checklist_items_top"
|
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
|
// gravity
|
||||||
const val GRAVITY_LEFT = 0
|
const val GRAVITY_LEFT = 0
|
||||||
|
@ -1,3 +1,24 @@
|
|||||||
package com.simplemobiletools.notes.pro.models
|
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<ChecklistItem> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -9,13 +9,13 @@
|
|||||||
android:paddingEnd="@dimen/activity_margin">
|
android:paddingEnd="@dimen/activity_margin">
|
||||||
|
|
||||||
<RadioGroup
|
<RadioGroup
|
||||||
android:id="@+id/sort_field_type"
|
android:id="@+id/sorting_dialog_radio_sorting"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginBottom="@dimen/medium_margin">
|
android:layout_marginBottom="@dimen/medium_margin">
|
||||||
|
|
||||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||||
android:id="@+id/sort_field_title"
|
android:id="@+id/sorting_dialog_radio_title"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:checked="true"
|
android:checked="true"
|
||||||
@ -24,7 +24,7 @@
|
|||||||
android:text="@string/title" />
|
android:text="@string/title" />
|
||||||
|
|
||||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||||
android:id="@+id/sort_field_date_created"
|
android:id="@+id/sorting_dialog_radio_date_created"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:paddingTop="@dimen/normal_margin"
|
android:paddingTop="@dimen/normal_margin"
|
||||||
@ -36,13 +36,13 @@
|
|||||||
<include layout="@layout/divider" />
|
<include layout="@layout/divider" />
|
||||||
|
|
||||||
<RadioGroup
|
<RadioGroup
|
||||||
android:id="@+id/sort_direction_type"
|
android:id="@+id/sorting_dialog_radio_order"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="@dimen/medium_margin">
|
android:layout_marginTop="@dimen/medium_margin">
|
||||||
|
|
||||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||||
android:id="@+id/sort_direction_asc"
|
android:id="@+id/sorting_dialog_radio_ascending"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:checked="true"
|
android:checked="true"
|
||||||
@ -51,7 +51,7 @@
|
|||||||
android:text="@string/ascending" />
|
android:text="@string/ascending" />
|
||||||
|
|
||||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||||
android:id="@+id/sort_direction_desc"
|
android:id="@+id/sorting_dialog_radio_descending"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:paddingTop="@dimen/normal_margin"
|
android:paddingTop="@dimen/normal_margin"
|
||||||
|
3
app/src/main/res/values/integers.xml
Normal file
3
app/src/main/res/values/integers.xml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<resources>
|
||||||
|
<integer name="default_sorting">2048</integer>
|
||||||
|
</resources>
|
Loading…
x
Reference in New Issue
Block a user