Skip to content

Commit 0b1af74

Browse files
authored
Merge pull request #456 from KryptKode/feat/checklist-sort
Feat/checklist sort
2 parents 46adfc6 + a26ae22 commit 0b1af74

File tree

9 files changed

+184
-11
lines changed

9 files changed

+184
-11
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ import com.simplemobiletools.notes.pro.helpers.NoteType
4040
import com.simplemobiletools.notes.pro.helpers.NotesHelper
4141
import com.simplemobiletools.notes.pro.helpers.OPEN_NOTE_ID
4242
import com.simplemobiletools.notes.pro.models.Note
43-
import kotlinx.android.synthetic.main.activity_main.*
4443
import java.io.File
4544
import java.nio.charset.Charset
45+
import kotlinx.android.synthetic.main.activity_main.*
4646

4747
class MainActivity : SimpleActivity() {
4848
private val EXPORT_FILE_SYNC = 1
@@ -159,13 +159,16 @@ class MainActivity : SimpleActivity() {
159159

160160
override fun onPrepareOptionsMenu(menu: Menu): Boolean {
161161
val multipleNotesExist = mNotes.size > 1
162+
val isCurrentItemChecklist = isCurrentItemChecklist()
163+
162164
menu.apply {
163165
findItem(R.id.rename_note).isVisible = multipleNotesExist
164166
findItem(R.id.open_note).isVisible = multipleNotesExist
165167
findItem(R.id.delete_note).isVisible = multipleNotesExist
166168
findItem(R.id.export_all_notes).isVisible = multipleNotesExist && hasPermission(PERMISSION_WRITE_STORAGE)
167-
findItem(R.id.open_search).isVisible = !isCurrentItemChecklist()
168-
findItem(R.id.remove_done_items).isVisible = isCurrentItemChecklist()
169+
findItem(R.id.open_search).isVisible = !isCurrentItemChecklist
170+
findItem(R.id.remove_done_items).isVisible = isCurrentItemChecklist
171+
findItem(R.id.sort_checklist).isVisible = isCurrentItemChecklist
169172
findItem(R.id.import_folder).isVisible = hasPermission(PERMISSION_READ_STORAGE)
170173
findItem(R.id.lock_note).isVisible = mNotes.isNotEmpty() && !mCurrentNote.isLocked()
171174
findItem(R.id.unlock_note).isVisible = mNotes.isNotEmpty() && mCurrentNote.isLocked()
@@ -204,6 +207,7 @@ class MainActivity : SimpleActivity() {
204207
R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java))
205208
R.id.about -> launchAbout()
206209
R.id.remove_done_items -> fragment?.handleUnlocking { removeDoneItems() }
210+
R.id.sort_checklist -> fragment?.handleUnlocking { displaySortChecklistDialog() }
207211
else -> return super.onOptionsItemSelected(item)
208212
}
209213
return true
@@ -1172,4 +1176,10 @@ class MainActivity : SimpleActivity() {
11721176
private fun removeDoneItems() {
11731177
getPagerAdapter().removeDoneCheckListItems(view_pager.currentItem)
11741178
}
1179+
1180+
private fun displaySortChecklistDialog() {
1181+
SortChecklistDialog(this) {
1182+
getPagerAdapter().refreshChecklist(view_pager.currentItem)
1183+
}
1184+
}
11751185
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/WidgetConfigureActivity.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ class WidgetConfigureActivity : SimpleActivity() {
158158
val items = Gson().fromJson<ArrayList<ChecklistItem>>(note.value, checklistItemType) ?: ArrayList(1)
159159
items.apply {
160160
if (isEmpty()) {
161-
add(ChecklistItem(0, "Milk", true))
162-
add(ChecklistItem(1, "Butter", true))
163-
add(ChecklistItem(2, "Salt", false))
164-
add(ChecklistItem(3, "Water", false))
165-
add(ChecklistItem(4, "Meat", true))
161+
add(ChecklistItem(0, System.currentTimeMillis(), "Milk", true))
162+
add(ChecklistItem(1, System.currentTimeMillis(), "Butter", true))
163+
add(ChecklistItem(2, System.currentTimeMillis(), "Salt", false))
164+
add(ChecklistItem(3, System.currentTimeMillis(), "Water", false))
165+
add(ChecklistItem(4, System.currentTimeMillis(), "Meat", true))
166166
}
167167
}
168168

app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,8 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
9595
fun removeDoneCheckListItems(position: Int) {
9696
(fragments[position] as? ChecklistFragment)?.removeDoneItems()
9797
}
98+
99+
fun refreshChecklist(position: Int) {
100+
(fragments[position] as? ChecklistFragment)?.refreshItems()
101+
}
98102
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.simplemobiletools.notes.pro.dialogs
2+
3+
import androidx.appcompat.app.AlertDialog
4+
import com.simplemobiletools.commons.extensions.setupDialogStuff
5+
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_CREATED
6+
import com.simplemobiletools.commons.helpers.SORT_BY_TITLE
7+
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
8+
import com.simplemobiletools.notes.pro.R
9+
import com.simplemobiletools.notes.pro.activities.SimpleActivity
10+
import com.simplemobiletools.notes.pro.extensions.config
11+
import kotlinx.android.synthetic.main.dialog_sort_checklist.view.*
12+
13+
class SortChecklistDialog(private val activity: SimpleActivity, private val callback: () -> Unit) {
14+
private val view = activity.layoutInflater.inflate(R.layout.dialog_sort_checklist, null)
15+
private val config = activity.config
16+
private var currSorting = config.sorting
17+
18+
init {
19+
setupSortRadio()
20+
setupOrderRadio()
21+
AlertDialog.Builder(activity)
22+
.setPositiveButton(R.string.ok) { _, _ -> dialogConfirmed() }
23+
.setNegativeButton(R.string.cancel, null)
24+
.create().apply {
25+
activity.setupDialogStuff(view, this, R.string.sort_by)
26+
}
27+
}
28+
29+
private fun setupSortRadio() {
30+
val fieldRadio = view.sorting_dialog_radio_sorting
31+
var fieldBtn = fieldRadio.sorting_dialog_radio_title
32+
33+
if (currSorting and SORT_BY_DATE_CREATED != 0) {
34+
fieldBtn = fieldRadio.sorting_dialog_radio_date_created
35+
}
36+
37+
fieldBtn.isChecked = true
38+
}
39+
40+
private fun setupOrderRadio() {
41+
val orderRadio = view.sorting_dialog_radio_order
42+
var orderBtn = orderRadio.sorting_dialog_radio_ascending
43+
44+
if (currSorting and SORT_DESCENDING != 0) {
45+
orderBtn = orderRadio.sorting_dialog_radio_descending
46+
}
47+
48+
orderBtn.isChecked = true
49+
}
50+
51+
private fun dialogConfirmed() {
52+
val sortingRadio = view.sorting_dialog_radio_sorting
53+
var sorting = when (sortingRadio.checkedRadioButtonId) {
54+
R.id.sorting_dialog_radio_date_created -> SORT_BY_DATE_CREATED
55+
else -> SORT_BY_TITLE
56+
}
57+
58+
if (view.sorting_dialog_radio_order.checkedRadioButtonId == R.id.sorting_dialog_radio_descending) {
59+
sorting = sorting or SORT_DESCENDING
60+
}
61+
62+
if (currSorting != sorting) {
63+
config.sorting = sorting
64+
callback()
65+
}
66+
}
67+
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
140140

141141
titles.forEach { title ->
142142
title.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEach { row ->
143-
newItems.add(ChecklistItem(currentMaxId + 1, row, false))
143+
newItems.add(ChecklistItem(currentMaxId + 1, System.currentTimeMillis(), row, false))
144144
currentMaxId++
145145
}
146146
}
@@ -158,7 +158,8 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
158158

159159
private fun setupAdapter() {
160160
updateUIVisibility()
161-
161+
ChecklistItem.sorting = requireContext().config.sorting
162+
items.sort()
162163
ChecklistAdapter(
163164
activity = activity as SimpleActivity,
164165
items = items,
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
package com.simplemobiletools.notes.pro.models
22

3-
data class ChecklistItem(val id: Int, var title: String, var isDone: Boolean)
3+
import com.simplemobiletools.commons.helpers.AlphanumericComparator
4+
import com.simplemobiletools.commons.helpers.SORT_BY_TITLE
5+
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
6+
7+
data class ChecklistItem(val id: Int, val dateCreated: Long = 0L, var title: String, var isDone: Boolean) : Comparable<ChecklistItem> {
8+
companion object {
9+
var sorting = 0
10+
}
11+
12+
override fun compareTo(other: ChecklistItem): Int {
13+
var result = when {
14+
sorting and SORT_BY_TITLE != 0 -> AlphanumericComparator().compare(title.lowercase(), other.title.lowercase())
15+
else -> dateCreated.compareTo(other.dateCreated)
16+
}
17+
18+
if (sorting and SORT_DESCENDING != 0) {
19+
result *= -1
20+
}
21+
22+
return result
23+
}
24+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/dialog_holder"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
android:paddingStart="@dimen/activity_margin"
8+
android:paddingTop="@dimen/small_margin"
9+
android:paddingEnd="@dimen/activity_margin">
10+
11+
<RadioGroup
12+
android:id="@+id/sorting_dialog_radio_sorting"
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:layout_marginBottom="@dimen/medium_margin">
16+
17+
<com.simplemobiletools.commons.views.MyCompatRadioButton
18+
android:id="@+id/sorting_dialog_radio_title"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:checked="true"
22+
android:paddingTop="@dimen/normal_margin"
23+
android:paddingBottom="@dimen/normal_margin"
24+
android:text="@string/title" />
25+
26+
<com.simplemobiletools.commons.views.MyCompatRadioButton
27+
android:id="@+id/sorting_dialog_radio_date_created"
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content"
30+
android:paddingTop="@dimen/normal_margin"
31+
android:paddingBottom="@dimen/normal_margin"
32+
android:text="@string/date_created" />
33+
34+
</RadioGroup>
35+
36+
<include layout="@layout/divider" />
37+
38+
<RadioGroup
39+
android:id="@+id/sorting_dialog_radio_order"
40+
android:layout_width="match_parent"
41+
android:layout_height="wrap_content"
42+
android:layout_marginTop="@dimen/medium_margin">
43+
44+
<com.simplemobiletools.commons.views.MyCompatRadioButton
45+
android:id="@+id/sorting_dialog_radio_ascending"
46+
android:layout_width="match_parent"
47+
android:layout_height="wrap_content"
48+
android:checked="true"
49+
android:paddingTop="@dimen/normal_margin"
50+
android:paddingBottom="@dimen/normal_margin"
51+
android:text="@string/ascending" />
52+
53+
<com.simplemobiletools.commons.views.MyCompatRadioButton
54+
android:id="@+id/sorting_dialog_radio_descending"
55+
android:layout_width="match_parent"
56+
android:layout_height="wrap_content"
57+
android:paddingTop="@dimen/normal_margin"
58+
android:paddingBottom="@dimen/normal_margin"
59+
android:text="@string/descending" />
60+
61+
</RadioGroup>
62+
</LinearLayout>

app/src/main/res/menu/menu.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
android:icon="@drawable/ic_delete_vector"
4242
android:title="@string/remove_done_items"
4343
app:showAsAction="ifRoom" />
44+
<item
45+
android:id="@+id/sort_checklist"
46+
android:icon="@drawable/ic_sort_vector"
47+
android:title="@string/sort_by"
48+
app:showAsAction="ifRoom" />
4449
<item
4550
android:id="@+id/share"
4651
android:icon="@drawable/ic_share_vector"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<integer name="default_sorting">2048</integer>
3+
</resources>

0 commit comments

Comments
 (0)