Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ interface BaseNoteDao {
)
fun getAllRemindersAsync(): LiveData<List<NoteReminder>>

@Query("SELECT * FROM BaseNote WHERE reminders IS NOT NULL AND reminders != '[]'")
@Query(
"SELECT * FROM BaseNote WHERE reminders IS NOT NULL AND reminders != '[]' AND folder = 'NOTES'"
)
fun getAllBaseNotesWithReminders(): LiveData<List<BaseNote>>

@Query("SELECT id FROM BaseNote WHERE folder = 'DELETED'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.lifecycle.LiveData
import androidx.navigation.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SortedListAdapterCallback
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.philkes.notallyx.R
Expand All @@ -41,6 +42,7 @@ import com.philkes.notallyx.presentation.movedToResId
import com.philkes.notallyx.presentation.showKeyboard
import com.philkes.notallyx.presentation.view.main.BaseNoteAdapter
import com.philkes.notallyx.presentation.view.main.BaseNoteVHPreferences
import com.philkes.notallyx.presentation.view.main.createCallback
import com.philkes.notallyx.presentation.view.misc.ItemListener
import com.philkes.notallyx.presentation.viewmodel.BaseNoteModel
import com.philkes.notallyx.presentation.viewmodel.preference.NotesView
Expand Down Expand Up @@ -261,14 +263,15 @@ abstract class NotallyFragment : Fragment(), ItemListener {
BaseNoteAdapter(
model.actionMode.selectedIds,
dateFormat.value,
notesSorting.value,
notesAdapterSortCallback(),
BaseNoteVHPreferences(
textSizeOverview.value,
maxItems.value,
maxLines.value,
maxTitle.value,
labelTagsHiddenInOverview.value,
imagesHiddenInOverview.value,
notesSorting.value.sortedBy,
),
model.imageRoot,
this@NotallyFragment,
Expand Down Expand Up @@ -296,6 +299,11 @@ abstract class NotallyFragment : Fragment(), ItemListener {
}
}

protected open fun notesAdapterSortCallback():
(BaseNoteAdapter) -> SortedListAdapterCallback<Item> = { adapter ->
model.preferences.notesSorting.value.createCallback(adapter)
}

private fun setupObserver() {
getObservable().observe(viewLifecycleOwner) { list ->
notesAdapter?.submitList(list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import android.os.Bundle
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.SortedListAdapterCallback
import com.philkes.notallyx.R
import com.philkes.notallyx.data.model.BaseNote
import com.philkes.notallyx.data.model.Item
import com.philkes.notallyx.data.model.hasAnyUpcomingNotifications
import com.philkes.notallyx.presentation.view.main.BaseNoteAdapter
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteLastNotificationSort
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteMostRecentNotificationSort
import com.philkes.notallyx.presentation.view.main.sorting.BaseNoteNextNotificationSort
import com.philkes.notallyx.presentation.viewmodel.preference.SortDirection

class RemindersFragment : NotallyFragment() {
private val currentReminderNotes = MutableLiveData<List<Item>>()
private val allReminderNotes: LiveData<List<Item>> by lazy { model.reminderNotes!! }
private var filterMode = FilterOptions.ALL
private var filterMode = FilterOptions.UPCOMING

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand All @@ -21,7 +27,7 @@ class RemindersFragment : NotallyFragment() {
allReminderNotes.observe(viewLifecycleOwner) { _ -> applyFilter(filterMode) }
binding?.ReminderFilter?.setOnCheckedStateChangeListener { _, checkedIds ->
if (checkedIds.isEmpty()) {
binding?.ReminderFilter?.check(R.id.all)
binding?.ReminderFilter?.check(R.id.upcoming)
return@setOnCheckedStateChangeListener
}
filterMode =
Expand All @@ -38,6 +44,15 @@ class RemindersFragment : NotallyFragment() {

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

override fun notesAdapterSortCallback(): (BaseNoteAdapter) -> SortedListAdapterCallback<Item> =
{ adapter ->
when (filterMode) {
FilterOptions.UPCOMING -> BaseNoteNextNotificationSort(adapter, SortDirection.ASC)
FilterOptions.ELAPSED -> BaseNoteLastNotificationSort(adapter, SortDirection.DESC)
FilterOptions.ALL -> BaseNoteMostRecentNotificationSort(adapter, SortDirection.DESC)
}
}

fun applyFilter(filterOptions: FilterOptions) {
val items: List<Item> = allReminderNotes.value ?: return
val filteredList: List<Item> =
Expand All @@ -53,11 +68,12 @@ class RemindersFragment : NotallyFragment() {
}
}
currentReminderNotes.value = filteredList
notesAdapter?.setNotesSortCallback(notesAdapterSortCallback())
}
}

enum class FilterOptions {
ALL,
UPCOMING,
ELAPSED,
ALL,
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.philkes.notallyx.databinding.ActivityPickNoteBinding
import com.philkes.notallyx.presentation.activity.LockedActivity
import com.philkes.notallyx.presentation.view.main.BaseNoteAdapter
import com.philkes.notallyx.presentation.view.main.BaseNoteVHPreferences
import com.philkes.notallyx.presentation.view.main.createCallback
import com.philkes.notallyx.presentation.view.misc.ItemListener
import com.philkes.notallyx.presentation.viewmodel.BaseNoteModel
import com.philkes.notallyx.presentation.viewmodel.preference.NotallyXPreferences
Expand Down Expand Up @@ -50,14 +51,15 @@ open class PickNoteActivity : LockedActivity<ActivityPickNoteBinding>(), ItemLis
BaseNoteAdapter(
Collections.emptySet(),
dateFormat.value,
notesSorting.value,
{ adapter -> notesSorting.value.createCallback(adapter) },
BaseNoteVHPreferences(
textSizeOverview.value,
maxItems.value,
maxLines.value,
maxTitle.value,
labelTagsHiddenInOverview.value,
imagesHiddenInOverview.value,
notesSorting.value.sortedBy,
),
application.getCurrentImagesDirectory(),
this@PickNoteActivity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import java.io.File
class BaseNoteAdapter(
private val selectedIds: Set<Long>,
private val dateFormat: DateFormat,
private var notesSort: NotesSort,
private var notesSortCallback: (adapter: BaseNoteAdapter) -> SortedListAdapterCallback<Item>,
private val preferences: BaseNoteVHPreferences,
private val imageRoot: File?,
private val listener: ItemListener,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private var searchKeyword: String = ""

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

override fun getItemViewType(position: Int): Int {
return when (list[position]) {
Expand All @@ -50,7 +50,7 @@ class BaseNoteAdapter(
is BaseNote -> {
(holder as BaseNoteVH).apply {
setSearchKeyword(searchKeyword)
bind(item, imageRoot, selectedIds.contains(item.id), notesSort.sortedBy)
bind(item, imageRoot, selectedIds.contains(item.id), preferences.sortedBy)
}
}
}
Expand Down Expand Up @@ -119,8 +119,14 @@ class BaseNoteAdapter(
}

fun setNotesSort(notesSort: NotesSort) {
this.notesSort = notesSort
replaceSortCallback(notesSort.createCallback())
setNotesSortCallback { adapter -> notesSort.createCallback(adapter) }
}

fun setNotesSortCallback(
notesSortCallback: (adapter: BaseNoteAdapter) -> SortedListAdapterCallback<Item>
) {
this.notesSortCallback = notesSortCallback
replaceSortCallback(this.notesSortCallback(this))
}

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

private fun NotesSort.createCallback() =
when (sortedBy) {
NotesSortBy.TITLE -> BaseNoteTitleSort(this@BaseNoteAdapter, sortDirection)
NotesSortBy.MODIFIED_DATE ->
BaseNoteModifiedDateSort(this@BaseNoteAdapter, sortDirection)
NotesSortBy.CREATION_DATE ->
BaseNoteCreationDateSort(this@BaseNoteAdapter, sortDirection)
NotesSortBy.COLOR -> BaseNoteColorSort(this@BaseNoteAdapter, sortDirection)
}

private fun replaceSortCallback(sortCallback: SortedListAdapterCallback<Item>) {
val mutableList = mutableListOf<Item>()
for (i in 0 until list.size()) {
Expand All @@ -167,3 +163,11 @@ class BaseNoteAdapter(
return mutableList.toList()
}
}

fun NotesSort.createCallback(adapter: RecyclerView.Adapter<*>?) =
when (sortedBy) {
NotesSortBy.TITLE -> BaseNoteTitleSort(adapter, sortDirection)
NotesSortBy.MODIFIED_DATE -> BaseNoteModifiedDateSort(adapter, sortDirection)
NotesSortBy.CREATION_DATE -> BaseNoteCreationDateSort(adapter, sortDirection)
NotesSortBy.COLOR -> BaseNoteColorSort(adapter, sortDirection)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ data class BaseNoteVHPreferences(
val maxTitleLines: Int,
val hideLabels: Boolean,
val hideImages: Boolean,
val sortedBy: NotesSortBy,
)

class BaseNoteVH(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.philkes.notallyx.presentation.view.main.sorting

import androidx.recyclerview.widget.RecyclerView
import com.philkes.notallyx.data.model.BaseNote
import com.philkes.notallyx.data.model.findLastNotificationDate
import com.philkes.notallyx.presentation.viewmodel.preference.SortDirection

class BaseNoteLastNotificationSort(
adapter: RecyclerView.Adapter<*>?,
sortDirection: SortDirection,
) : ItemSort(adapter, sortDirection) {

override fun compare(note1: BaseNote, note2: BaseNote, sortDirection: SortDirection): Int {
val sort = note1.compareLastNotification(note2)
return if (sortDirection == SortDirection.ASC) sort else -1 * sort
}
}

fun BaseNote.compareLastNotification(other: BaseNote): Int {
if (other.reminders.isEmpty() && reminders.isNotEmpty()) {
return 1
}
if (other.reminders.isNotEmpty() && reminders.isEmpty()) {
return -1
}
if (other.reminders.isEmpty() && reminders.isEmpty()) {
return 0
}
val lastNotification = reminders.findLastNotificationDate()
val otherLastNotification = other.reminders.findLastNotificationDate()
if (lastNotification == null && otherLastNotification != null) {
return -1
}
if (lastNotification != null && otherLastNotification == null) {
return 1
}
if (lastNotification == null && otherLastNotification == null) {
return 0
}
return lastNotification!!.compareTo(otherLastNotification!!)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.philkes.notallyx.presentation.view.main.sorting

import androidx.recyclerview.widget.RecyclerView
import com.philkes.notallyx.data.model.BaseNote
import com.philkes.notallyx.presentation.viewmodel.preference.SortDirection

class BaseNoteMostRecentNotificationSort(
adapter: RecyclerView.Adapter<*>?,
sortDirection: SortDirection,
) : ItemSort(adapter, sortDirection) {

override fun compare(note1: BaseNote, note2: BaseNote, sortDirection: SortDirection): Int {
val sort =
note1.compareNextNotification(note2).takeIf { it != 0 }
?: note1.compareLastNotification(note2)
return if (sortDirection == SortDirection.ASC) sort else -1 * sort
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.philkes.notallyx.presentation.view.main.sorting

import androidx.recyclerview.widget.RecyclerView
import com.philkes.notallyx.data.model.BaseNote
import com.philkes.notallyx.data.model.findNextNotificationDate
import com.philkes.notallyx.presentation.viewmodel.preference.SortDirection

class BaseNoteNextNotificationSort(
adapter: RecyclerView.Adapter<*>?,
sortDirection: SortDirection,
) : ItemSort(adapter, sortDirection) {

override fun compare(note1: BaseNote, note2: BaseNote, sortDirection: SortDirection): Int {
val sort = note1.compareNextNotification(note2)
return if (sortDirection == SortDirection.ASC) sort else -1 * sort
}
}

fun BaseNote.compareNextNotification(other: BaseNote): Int {
if (other.reminders.isEmpty() && reminders.isNotEmpty()) {
return 1
}
if (other.reminders.isNotEmpty() && reminders.isEmpty()) {
return -1
}
if (other.reminders.isEmpty() && reminders.isEmpty()) {
return 0
}
val nextNotification = reminders.findNextNotificationDate()
val otherNextNotification = other.reminders.findNextNotificationDate()
if (nextNotification == null && otherNextNotification != null) {
return -1
}
if (nextNotification != null && otherNextNotification == null) {
return 1
}
if (nextNotification == null && otherNextNotification == null) {
return 0
}
return nextNotification!!.compareTo(otherNextNotification!!)
}
8 changes: 4 additions & 4 deletions app/src/main/res/layout/fragment_notes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,24 @@
>

<com.google.android.material.chip.Chip
android:id="@+id/all"
android:id="@+id/upcoming"
style="@style/FilterChip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/all" />
android:text="@string/upcoming" />
<com.google.android.material.chip.Chip
android:id="@+id/elapsed"
style="@style/FilterChip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/elapsed" />
<com.google.android.material.chip.Chip
android:id="@+id/upcoming"
android:id="@+id/all"
style="@style/FilterChip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/upcoming" />
android:text="@string/all" />

</com.google.android.material.chip.ChipGroup>

Expand Down
Loading