mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-03-24 20:40:09 +01:00
Implement checklist sort
This commit is contained in:
parent
8d8ab9554d
commit
a90bfb9a0a
3
app/proguard-rules.pro
vendored
3
app/proguard-rules.pro
vendored
@ -1,3 +1,6 @@
|
||||
-keep class com.simplemobiletools.notes.pro.models.ChecklistItem {
|
||||
<fields>;
|
||||
}
|
||||
-keep class com.simplemobiletools.notes.pro.models.ChecklistSort {
|
||||
<fields>;
|
||||
}
|
||||
|
@ -163,6 +163,7 @@ class MainActivity : SimpleActivity() {
|
||||
findItem(R.id.export_all_notes).isVisible = multipleNotesExist && hasPermission(PERMISSION_WRITE_STORAGE)
|
||||
findItem(R.id.open_search).isVisible = !isCurrentItemChecklist()
|
||||
findItem(R.id.remove_done_items).isVisible = isCurrentItemChecklist()
|
||||
findItem(R.id.sort_checklist).isVisible = isCurrentItemChecklist()
|
||||
findItem(R.id.import_folder).isVisible = hasPermission(PERMISSION_READ_STORAGE)
|
||||
findItem(R.id.lock_note).isVisible = mNotes.isNotEmpty() && !mCurrentNote.isLocked()
|
||||
findItem(R.id.unlock_note).isVisible = mNotes.isNotEmpty() && mCurrentNote.isLocked()
|
||||
@ -201,6 +202,7 @@ class MainActivity : SimpleActivity() {
|
||||
R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java))
|
||||
R.id.about -> launchAbout()
|
||||
R.id.remove_done_items -> fragment?.handleUnlocking { removeDoneItems() }
|
||||
R.id.sort_checklist -> fragment?.handleUnlocking { displaySortChecklistDialog() }
|
||||
else -> return super.onOptionsItemSelected(item)
|
||||
}
|
||||
return true
|
||||
@ -1169,4 +1171,10 @@ class MainActivity : SimpleActivity() {
|
||||
private fun removeDoneItems() {
|
||||
getPagerAdapter().removeDoneCheckListItems(view_pager.currentItem)
|
||||
}
|
||||
|
||||
private fun displaySortChecklistDialog() {
|
||||
SortChecklistDialog(this) {
|
||||
getPagerAdapter().sortChecklistItems(view_pager.currentItem, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -149,11 +149,11 @@ class WidgetConfigureActivity : SimpleActivity() {
|
||||
val items = Gson().fromJson<ArrayList<ChecklistItem>>(note.value, checklistItemType) ?: ArrayList(1)
|
||||
items.apply {
|
||||
if (isEmpty()) {
|
||||
add(ChecklistItem(0, "Milk", true))
|
||||
add(ChecklistItem(1, "Butter", true))
|
||||
add(ChecklistItem(2, "Salt", false))
|
||||
add(ChecklistItem(3, "Water", false))
|
||||
add(ChecklistItem(4, "Meat", true))
|
||||
add(ChecklistItem(0, System.currentTimeMillis(),"Milk", true))
|
||||
add(ChecklistItem(1, System.currentTimeMillis(),"Butter", true))
|
||||
add(ChecklistItem(2, System.currentTimeMillis(),"Salt", false))
|
||||
add(ChecklistItem(3, System.currentTimeMillis(),"Water", false))
|
||||
add(ChecklistItem(4, System.currentTimeMillis(),"Meat", true))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@ 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<Note>, val activity: Activity) : FragmentStatePagerAdapter(fm) {
|
||||
@ -95,4 +96,8 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
|
||||
fun removeDoneCheckListItems(position: Int) {
|
||||
(fragments[position] as? ChecklistFragment)?.removeDoneItems()
|
||||
}
|
||||
|
||||
fun sortChecklistItems(position: Int, sort:ChecklistSort){
|
||||
(fragments[position] as? ChecklistFragment)?.sortChecklist(sort)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,53 @@
|
||||
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.notes.pro.R
|
||||
import com.simplemobiletools.notes.pro.activities.SimpleActivity
|
||||
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.separate_items_checkbox
|
||||
import kotlinx.android.synthetic.main.dialog_sort_checklist.view.sort_direction_type
|
||||
import kotlinx.android.synthetic.main.dialog_sort_checklist.view.sort_field_type
|
||||
|
||||
class SortChecklistDialog(private val activity: SimpleActivity, val callback: (ChecklistSort) -> Unit) {
|
||||
init {
|
||||
val view = (activity.layoutInflater.inflate(R.layout.dialog_sort_checklist, null) as ViewGroup)
|
||||
|
||||
AlertDialog.Builder(activity)
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.create().apply {
|
||||
activity.setupDialogStuff(view, this, R.string.sort_checklist) {
|
||||
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
|
||||
callback.invoke(
|
||||
ChecklistSort(
|
||||
field = getSortField(view),
|
||||
direction = getSortDirection(view),
|
||||
separateCheckedFromUnchecked = view.separate_items_checkbox.isChecked
|
||||
)
|
||||
)
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 getSortDirection(view: View): ChecklistSortDirection {
|
||||
return when (view.sort_direction_type.checkedRadioButtonId) {
|
||||
R.id.sort_direction_asc -> ChecklistSortDirection.ASCENDING
|
||||
else -> ChecklistSortDirection.DESCENDING
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.simplemobiletools.notes.pro.fragments
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@ -18,8 +19,7 @@ import com.simplemobiletools.notes.pro.extensions.updateWidgets
|
||||
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.Note
|
||||
import com.simplemobiletools.notes.pro.models.*
|
||||
import kotlinx.android.synthetic.main.fragment_checklist.view.*
|
||||
|
||||
class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
|
||||
@ -140,7 +140,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
|
||||
|
||||
titles.forEach { title ->
|
||||
title.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEach { row ->
|
||||
newItems.add(ChecklistItem(currentMaxId + 1, row, false))
|
||||
newItems.add(ChecklistItem(currentMaxId + 1, System.currentTimeMillis(), row, false))
|
||||
currentMaxId++
|
||||
}
|
||||
}
|
||||
@ -215,4 +215,11 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
|
||||
override fun refreshItems() {
|
||||
setupAdapter()
|
||||
}
|
||||
|
||||
private val TAG = "ChecklistFragment"
|
||||
fun sortChecklist(sort: ChecklistSort) {
|
||||
Log.d(TAG, "sortChecklist: $sort")
|
||||
items = items.sortedWith(sort.getSortComparator()).toMutableList() as ArrayList<ChecklistItem>
|
||||
setupAdapter()
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
package com.simplemobiletools.notes.pro.models
|
||||
|
||||
data class ChecklistItem(val id: Int, var title: String, var isDone: Boolean)
|
||||
data class ChecklistItem(val id: Int, val dateCreated: Long = 0L, var title: String, var isDone: Boolean)
|
||||
|
@ -0,0 +1,26 @@
|
||||
package com.simplemobiletools.notes.pro.models
|
||||
|
||||
data class ChecklistSort(
|
||||
val field: ChecklistSortField,
|
||||
val direction: ChecklistSortDirection,
|
||||
val separateCheckedFromUnchecked: Boolean,
|
||||
) {
|
||||
|
||||
companion object {
|
||||
val DEFAULT = ChecklistSort(ChecklistSortField.TITLE, ChecklistSortDirection.ASCENDING, false)
|
||||
}
|
||||
|
||||
fun getSortComparator(): Comparator<ChecklistItem> {
|
||||
return when (field) {
|
||||
ChecklistSortField.TITLE -> compareWithSortDirection { it.title }
|
||||
ChecklistSortField.DATE_CREATED -> compareWithSortDirection { it.dateCreated }
|
||||
}
|
||||
}
|
||||
|
||||
private fun compareWithSortDirection(compareFunc: (ChecklistItem) -> Comparable<*>): Comparator<ChecklistItem> {
|
||||
return when (direction) {
|
||||
ChecklistSortDirection.ASCENDING -> if(separateCheckedFromUnchecked) compareBy<ChecklistItem> { it.isDone }.thenBy(compareFunc) else compareBy(compareFunc)
|
||||
ChecklistSortDirection.DESCENDING -> if(separateCheckedFromUnchecked) compareByDescending<ChecklistItem> { it.isDone }.thenByDescending(compareFunc) else compareByDescending(compareFunc)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.simplemobiletools.notes.pro.models
|
||||
|
||||
enum class ChecklistSortDirection {
|
||||
ASCENDING,
|
||||
DESCENDING,
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.simplemobiletools.notes.pro.models
|
||||
|
||||
enum class ChecklistSortField {
|
||||
TITLE,
|
||||
DATE_CREATED,
|
||||
}
|
81
app/src/main/res/layout/dialog_sort_checklist.xml
Normal file
81
app/src/main/res/layout/dialog_sort_checklist.xml
Normal file
@ -0,0 +1,81 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/dialog_holder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:paddingStart="@dimen/activity_margin"
|
||||
android:paddingTop="@dimen/small_margin"
|
||||
android:paddingEnd="@dimen/activity_margin">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/sort_field_label"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:text="@string/sort_by" />
|
||||
|
||||
<RadioGroup
|
||||
android:id="@+id/sort_field_type"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||
android:id="@+id/sort_field_title"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:checked="true"
|
||||
android:paddingTop="@dimen/normal_margin"
|
||||
android:paddingBottom="@dimen/normal_margin"
|
||||
android:text="@string/title" />
|
||||
|
||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||
android:id="@+id/sort_field_date_created"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="@dimen/normal_margin"
|
||||
android:paddingBottom="@dimen/normal_margin"
|
||||
android:text="@string/date_created" />
|
||||
|
||||
</RadioGroup>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/sort_direction_label"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="@dimen/medium_margin"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:text="@string/sort_direction" />
|
||||
|
||||
<RadioGroup
|
||||
android:id="@+id/sort_direction_type"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||
android:id="@+id/sort_direction_asc"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:checked="true"
|
||||
android:paddingTop="@dimen/normal_margin"
|
||||
android:paddingBottom="@dimen/normal_margin"
|
||||
android:text="@string/ascending" />
|
||||
|
||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||
android:id="@+id/sort_direction_desc"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="@dimen/normal_margin"
|
||||
android:paddingBottom="@dimen/normal_margin"
|
||||
android:text="@string/descending" />
|
||||
|
||||
</RadioGroup>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
|
||||
android:id="@+id/separate_items_checkbox"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="@dimen/medium_margin"
|
||||
android:text="@string/sort_separate_checked_from_unchecked" />
|
||||
|
||||
</LinearLayout>
|
@ -41,6 +41,11 @@
|
||||
android:icon="@drawable/ic_delete_vector"
|
||||
android:title="@string/remove_done_items"
|
||||
app:showAsAction="ifRoom" />
|
||||
<item
|
||||
android:id="@+id/sort_checklist"
|
||||
android:icon="@drawable/ic_sort_vector"
|
||||
android:title="@string/sort_checklist"
|
||||
app:showAsAction="ifRoom" />
|
||||
<item
|
||||
android:id="@+id/share"
|
||||
android:icon="@drawable/ic_share_vector"
|
||||
|
@ -109,6 +109,9 @@
|
||||
<b>Reddit:</b>
|
||||
https://www.reddit.com/r/SimpleMobileTools
|
||||
</string>
|
||||
<string name="sort_direction">Sort direction</string>
|
||||
<string name="sort_separate_checked_from_unchecked">Separately sort checked from unchecked items</string>
|
||||
<string name="sort_checklist">Sort checklist</string>
|
||||
|
||||
<!--
|
||||
Haven't found some strings? There's more at
|
||||
|
Loading…
x
Reference in New Issue
Block a user