Merge pull request #456 from KryptKode/feat/checklist-sort

Feat/checklist sort
This commit is contained in:
Tibor Kaputa 2021-10-08 18:31:22 +02:00 committed by GitHub
commit 0b1af74053
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 184 additions and 11 deletions

View File

@ -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
@ -159,13 +159,16 @@ class MainActivity : SimpleActivity() {
override fun onPrepareOptionsMenu(menu: Menu): Boolean {
val multipleNotesExist = mNotes.size > 1
val isCurrentItemChecklist = isCurrentItemChecklist()
menu.apply {
findItem(R.id.rename_note).isVisible = multipleNotesExist
findItem(R.id.open_note).isVisible = multipleNotesExist
findItem(R.id.delete_note).isVisible = multipleNotesExist
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.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()
@ -204,6 +207,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
@ -1172,4 +1176,10 @@ class MainActivity : SimpleActivity() {
private fun removeDoneItems() {
getPagerAdapter().removeDoneCheckListItems(view_pager.currentItem)
}
private fun displaySortChecklistDialog() {
SortChecklistDialog(this) {
getPagerAdapter().refreshChecklist(view_pager.currentItem)
}
}
}

View File

@ -158,11 +158,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))
}
}

View File

@ -95,4 +95,8 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
fun removeDoneCheckListItems(position: Int) {
(fragments[position] as? ChecklistFragment)?.removeDoneItems()
}
fun refreshChecklist(position: Int) {
(fragments[position] as? ChecklistFragment)?.refreshItems()
}
}

View File

@ -0,0 +1,67 @@
package com.simplemobiletools.notes.pro.dialogs
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 kotlinx.android.synthetic.main.dialog_sort_checklist.view.*
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 {
setupSortRadio()
setupOrderRadio()
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok) { _, _ -> dialogConfirmed() }
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.sort_by)
}
}
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 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()
}
}
}

View File

@ -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++
}
}
@ -158,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,

View File

@ -1,3 +1,24 @@
package com.simplemobiletools.notes.pro.models
data class ChecklistItem(val id: Int, 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
}
}

View File

@ -0,0 +1,62 @@
<?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">
<RadioGroup
android:id="@+id/sorting_dialog_radio_sorting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/medium_margin">
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/sorting_dialog_radio_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/sorting_dialog_radio_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>
<include layout="@layout/divider" />
<RadioGroup
android:id="@+id/sorting_dialog_radio_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_margin">
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/sorting_dialog_radio_ascending"
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/sorting_dialog_radio_descending"
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>
</LinearLayout>

View File

@ -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_by"
app:showAsAction="ifRoom" />
<item
android:id="@+id/share"
android:icon="@drawable/ic_share_vector"

View File

@ -0,0 +1,3 @@
<resources>
<integer name="default_sorting">2048</integer>
</resources>