Skip to content

Commit e5fadbd

Browse files
committed
Sort reminders based on filter
1 parent 2dc4f45 commit e5fadbd

File tree

8 files changed

+152
-21
lines changed

8 files changed

+152
-21
lines changed

app/src/main/java/com/philkes/notallyx/presentation/activity/main/fragment/NotallyFragment.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.lifecycle.LiveData
1616
import androidx.navigation.findNavController
1717
import androidx.recyclerview.widget.LinearLayoutManager
1818
import androidx.recyclerview.widget.RecyclerView
19+
import androidx.recyclerview.widget.SortedListAdapterCallback
1920
import androidx.recyclerview.widget.StaggeredGridLayoutManager
2021
import com.google.android.material.snackbar.Snackbar
2122
import com.philkes.notallyx.R
@@ -41,6 +42,7 @@ import com.philkes.notallyx.presentation.movedToResId
4142
import com.philkes.notallyx.presentation.showKeyboard
4243
import com.philkes.notallyx.presentation.view.main.BaseNoteAdapter
4344
import com.philkes.notallyx.presentation.view.main.BaseNoteVHPreferences
45+
import com.philkes.notallyx.presentation.view.main.createCallback
4446
import com.philkes.notallyx.presentation.view.misc.ItemListener
4547
import com.philkes.notallyx.presentation.viewmodel.BaseNoteModel
4648
import com.philkes.notallyx.presentation.viewmodel.preference.NotesView
@@ -261,14 +263,15 @@ abstract class NotallyFragment : Fragment(), ItemListener {
261263
BaseNoteAdapter(
262264
model.actionMode.selectedIds,
263265
dateFormat.value,
264-
notesSorting.value,
266+
notesAdapterSortCallback(),
265267
BaseNoteVHPreferences(
266268
textSizeOverview.value,
267269
maxItems.value,
268270
maxLines.value,
269271
maxTitle.value,
270272
labelTagsHiddenInOverview.value,
271273
imagesHiddenInOverview.value,
274+
notesSorting.value.sortedBy,
272275
),
273276
model.imageRoot,
274277
this@NotallyFragment,
@@ -296,6 +299,11 @@ abstract class NotallyFragment : Fragment(), ItemListener {
296299
}
297300
}
298301

302+
protected open fun notesAdapterSortCallback():
303+
(BaseNoteAdapter) -> SortedListAdapterCallback<Item> = { adapter ->
304+
model.preferences.notesSorting.value.createCallback(adapter)
305+
}
306+
299307
private fun setupObserver() {
300308
getObservable().observe(viewLifecycleOwner) { list ->
301309
notesAdapter?.submitList(list)

app/src/main/java/com/philkes/notallyx/presentation/activity/main/fragment/RemindersFragment.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ import android.os.Bundle
44
import android.view.View
55
import androidx.lifecycle.LiveData
66
import androidx.lifecycle.MutableLiveData
7+
import androidx.recyclerview.widget.SortedListAdapterCallback
78
import com.philkes.notallyx.R
89
import com.philkes.notallyx.data.model.BaseNote
910
import com.philkes.notallyx.data.model.Item
1011
import com.philkes.notallyx.data.model.hasAnyUpcomingNotifications
12+
import com.philkes.notallyx.presentation.view.main.BaseNoteAdapter
13+
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteLastNotificationSort
14+
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteMostRecentNotificationSort
15+
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteNextNotificationSort
16+
import com.philkes.notallyx.presentation.viewmodel.preference.SortDirection
1117

1218
class RemindersFragment : NotallyFragment() {
1319
private val currentReminderNotes = MutableLiveData<List<Item>>()
1420
private val allReminderNotes: LiveData<List<Item>> by lazy { model.reminderNotes!! }
15-
private var filterMode = FilterOptions.ALL
21+
private var filterMode = FilterOptions.UPCOMING
1622

1723
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
1824
super.onViewCreated(view, savedInstanceState)
@@ -21,7 +27,7 @@ class RemindersFragment : NotallyFragment() {
2127
allReminderNotes.observe(viewLifecycleOwner) { _ -> applyFilter(filterMode) }
2228
binding?.ReminderFilter?.setOnCheckedStateChangeListener { _, checkedIds ->
2329
if (checkedIds.isEmpty()) {
24-
binding?.ReminderFilter?.check(R.id.all)
30+
binding?.ReminderFilter?.check(R.id.upcoming)
2531
return@setOnCheckedStateChangeListener
2632
}
2733
filterMode =
@@ -38,6 +44,15 @@ class RemindersFragment : NotallyFragment() {
3844

3945
override fun getObservable(): LiveData<List<Item>> = currentReminderNotes
4046

47+
override fun notesAdapterSortCallback(): (BaseNoteAdapter) -> SortedListAdapterCallback<Item> =
48+
{ adapter ->
49+
when (filterMode) {
50+
FilterOptions.UPCOMING -> BaseNoteNextNotificationSort(adapter, SortDirection.ASC)
51+
FilterOptions.ELAPSED -> BaseNoteLastNotificationSort(adapter, SortDirection.DESC)
52+
FilterOptions.ALL -> BaseNoteMostRecentNotificationSort(adapter, SortDirection.DESC)
53+
}
54+
}
55+
4156
fun applyFilter(filterOptions: FilterOptions) {
4257
val items: List<Item> = allReminderNotes.value ?: return
4358
val filteredList: List<Item> =
@@ -53,11 +68,12 @@ class RemindersFragment : NotallyFragment() {
5368
}
5469
}
5570
currentReminderNotes.value = filteredList
71+
notesAdapter?.setNotesSortCallback(notesAdapterSortCallback())
5672
}
5773
}
5874

5975
enum class FilterOptions {
60-
ALL,
6176
UPCOMING,
6277
ELAPSED,
78+
ALL,
6379
}

app/src/main/java/com/philkes/notallyx/presentation/activity/note/PickNoteActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.philkes.notallyx.databinding.ActivityPickNoteBinding
1818
import com.philkes.notallyx.presentation.activity.LockedActivity
1919
import com.philkes.notallyx.presentation.view.main.BaseNoteAdapter
2020
import com.philkes.notallyx.presentation.view.main.BaseNoteVHPreferences
21+
import com.philkes.notallyx.presentation.view.main.createCallback
2122
import com.philkes.notallyx.presentation.view.misc.ItemListener
2223
import com.philkes.notallyx.presentation.viewmodel.BaseNoteModel
2324
import com.philkes.notallyx.presentation.viewmodel.preference.NotallyXPreferences
@@ -50,14 +51,15 @@ open class PickNoteActivity : LockedActivity<ActivityPickNoteBinding>(), ItemLis
5051
BaseNoteAdapter(
5152
Collections.emptySet(),
5253
dateFormat.value,
53-
notesSorting.value,
54+
{ adapter -> notesSorting.value.createCallback(adapter) },
5455
BaseNoteVHPreferences(
5556
textSizeOverview.value,
5657
maxItems.value,
5758
maxLines.value,
5859
maxTitle.value,
5960
labelTagsHiddenInOverview.value,
6061
imagesHiddenInOverview.value,
62+
notesSorting.value.sortedBy,
6163
),
6264
application.getCurrentImagesDirectory(),
6365
this@PickNoteActivity,

app/src/main/java/com/philkes/notallyx/presentation/view/main/BaseNoteAdapter.kt

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.philkes.notallyx.databinding.RecyclerBaseNoteBinding
1212
import com.philkes.notallyx.databinding.RecyclerHeaderBinding
1313
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteColorSort
1414
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteCreationDateSort
15-
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteModifiedDateSort
15+
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteNextNotificationSort
1616
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteTitleSort
1717
import com.philkes.notallyx.presentation.view.misc.ItemListener
1818
import com.philkes.notallyx.presentation.viewmodel.preference.DateFormat
@@ -23,15 +23,15 @@ import java.io.File
2323
class BaseNoteAdapter(
2424
private val selectedIds: Set<Long>,
2525
private val dateFormat: DateFormat,
26-
private var notesSort: NotesSort,
26+
private var notesSortCallback: (adapter: BaseNoteAdapter) -> SortedListAdapterCallback<Item>,
2727
private val preferences: BaseNoteVHPreferences,
2828
private val imageRoot: File?,
2929
private val listener: ItemListener,
3030
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
3131

3232
private var searchKeyword: String = ""
3333

34-
private var list = SortedList(Item::class.java, notesSort.createCallback())
34+
private var list = SortedList(Item::class.java, notesSortCallback(this))
3535

3636
override fun getItemViewType(position: Int): Int {
3737
return when (list[position]) {
@@ -50,7 +50,7 @@ class BaseNoteAdapter(
5050
is BaseNote -> {
5151
(holder as BaseNoteVH).apply {
5252
setSearchKeyword(searchKeyword)
53-
bind(item, imageRoot, selectedIds.contains(item.id), notesSort.sortedBy)
53+
bind(item, imageRoot, selectedIds.contains(item.id), preferences.sortedBy)
5454
}
5555
}
5656
}
@@ -119,8 +119,14 @@ class BaseNoteAdapter(
119119
}
120120

121121
fun setNotesSort(notesSort: NotesSort) {
122-
this.notesSort = notesSort
123-
replaceSortCallback(notesSort.createCallback())
122+
setNotesSortCallback { adapter -> notesSort.createCallback(adapter) }
123+
}
124+
125+
fun setNotesSortCallback(
126+
notesSortCallback: (adapter: BaseNoteAdapter) -> SortedListAdapterCallback<Item>
127+
) {
128+
this.notesSortCallback = notesSortCallback
129+
replaceSortCallback(this.notesSortCallback(this))
124130
}
125131

126132
fun getItem(position: Int): Item? {
@@ -134,16 +140,6 @@ class BaseNoteAdapter(
134140
list.replaceAll(items)
135141
}
136142

137-
private fun NotesSort.createCallback() =
138-
when (sortedBy) {
139-
NotesSortBy.TITLE -> BaseNoteTitleSort(this@BaseNoteAdapter, sortDirection)
140-
NotesSortBy.MODIFIED_DATE ->
141-
BaseNoteModifiedDateSort(this@BaseNoteAdapter, sortDirection)
142-
NotesSortBy.CREATION_DATE ->
143-
BaseNoteCreationDateSort(this@BaseNoteAdapter, sortDirection)
144-
NotesSortBy.COLOR -> BaseNoteColorSort(this@BaseNoteAdapter, sortDirection)
145-
}
146-
147143
private fun replaceSortCallback(sortCallback: SortedListAdapterCallback<Item>) {
148144
val mutableList = mutableListOf<Item>()
149145
for (i in 0 until list.size()) {
@@ -167,3 +163,11 @@ class BaseNoteAdapter(
167163
return mutableList.toList()
168164
}
169165
}
166+
167+
fun NotesSort.createCallback(adapter: RecyclerView.Adapter<*>?) =
168+
when (sortedBy) {
169+
NotesSortBy.TITLE -> BaseNoteTitleSort(adapter, sortDirection)
170+
NotesSortBy.MODIFIED_DATE -> BaseNoteNextNotificationSort(adapter, sortDirection)
171+
NotesSortBy.CREATION_DATE -> BaseNoteCreationDateSort(adapter, sortDirection)
172+
NotesSortBy.COLOR -> BaseNoteColorSort(adapter, sortDirection)
173+
}

app/src/main/java/com/philkes/notallyx/presentation/view/main/BaseNoteVH.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ data class BaseNoteVHPreferences(
5151
val maxTitleLines: Int,
5252
val hideLabels: Boolean,
5353
val hideImages: Boolean,
54+
val sortedBy: NotesSortBy,
5455
)
5556

5657
class BaseNoteVH(
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.philkes.notallyx.presentation.view.main.sorting
2+
3+
import androidx.recyclerview.widget.RecyclerView
4+
import com.philkes.notallyx.data.model.BaseNote
5+
import com.philkes.notallyx.data.model.findLastNotificationDate
6+
import com.philkes.notallyx.presentation.viewmodel.preference.SortDirection
7+
8+
class BaseNoteLastNotificationSort(
9+
adapter: RecyclerView.Adapter<*>?,
10+
sortDirection: SortDirection,
11+
) : ItemSort(adapter, sortDirection) {
12+
13+
override fun compare(note1: BaseNote, note2: BaseNote, sortDirection: SortDirection): Int {
14+
val sort = note1.compareLastNotification(note2)
15+
return if (sortDirection == SortDirection.ASC) sort else -1 * sort
16+
}
17+
}
18+
19+
fun BaseNote.compareLastNotification(other: BaseNote): Int {
20+
if (other.reminders.isEmpty() && reminders.isNotEmpty()) {
21+
return 1
22+
}
23+
if (other.reminders.isNotEmpty() && reminders.isEmpty()) {
24+
return -1
25+
}
26+
if (other.reminders.isEmpty() && reminders.isEmpty()) {
27+
return 0
28+
}
29+
val lastNotification = reminders.findLastNotificationDate()
30+
val otherLastNotification = other.reminders.findLastNotificationDate()
31+
if (lastNotification == null && otherLastNotification != null) {
32+
return -1
33+
}
34+
if (lastNotification != null && otherLastNotification == null) {
35+
return 1
36+
}
37+
if (lastNotification == null && otherLastNotification == null) {
38+
return 0
39+
}
40+
return lastNotification!!.compareTo(otherLastNotification!!)
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.philkes.notallyx.presentation.view.main.sorting
2+
3+
import androidx.recyclerview.widget.RecyclerView
4+
import com.philkes.notallyx.data.model.BaseNote
5+
import com.philkes.notallyx.presentation.viewmodel.preference.SortDirection
6+
7+
class BaseNoteMostRecentNotificationSort(
8+
adapter: RecyclerView.Adapter<*>?,
9+
sortDirection: SortDirection,
10+
) : ItemSort(adapter, sortDirection) {
11+
12+
override fun compare(note1: BaseNote, note2: BaseNote, sortDirection: SortDirection): Int {
13+
val sort =
14+
note1.compareNextNotification(note2).takeIf { it != 0 }
15+
?: note1.compareLastNotification(note2)
16+
return if (sortDirection == SortDirection.ASC) sort else -1 * sort
17+
}
18+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.philkes.notallyx.presentation.view.main.sorting
2+
3+
import androidx.recyclerview.widget.RecyclerView
4+
import com.philkes.notallyx.data.model.BaseNote
5+
import com.philkes.notallyx.data.model.findNextNotificationDate
6+
import com.philkes.notallyx.presentation.viewmodel.preference.SortDirection
7+
8+
class BaseNoteNextNotificationSort(
9+
adapter: RecyclerView.Adapter<*>?,
10+
sortDirection: SortDirection,
11+
) : ItemSort(adapter, sortDirection) {
12+
13+
override fun compare(note1: BaseNote, note2: BaseNote, sortDirection: SortDirection): Int {
14+
val sort = note1.compareNextNotification(note2)
15+
return if (sortDirection == SortDirection.ASC) sort else -1 * sort
16+
}
17+
}
18+
19+
fun BaseNote.compareNextNotification(other: BaseNote): Int {
20+
if (other.reminders.isEmpty() && reminders.isNotEmpty()) {
21+
return 1
22+
}
23+
if (other.reminders.isNotEmpty() && reminders.isEmpty()) {
24+
return -1
25+
}
26+
if (other.reminders.isEmpty() && reminders.isEmpty()) {
27+
return 0
28+
}
29+
val nextNotification = reminders.findNextNotificationDate()
30+
val otherNextNotification = other.reminders.findNextNotificationDate()
31+
if (nextNotification == null && otherNextNotification != null) {
32+
return -1
33+
}
34+
if (nextNotification != null && otherNextNotification == null) {
35+
return 1
36+
}
37+
if (nextNotification == null && otherNextNotification == null) {
38+
return 0
39+
}
40+
return nextNotification!!.compareTo(otherNextNotification!!)
41+
}

0 commit comments

Comments
 (0)