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
19 changes: 14 additions & 5 deletions app/src/main/java/com/farmerbb/notepad/data/NotepadRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.farmerbb.notepad.model.SortOrder.TitleAscending
import com.farmerbb.notepad.model.SortOrder.TitleDescending
import com.squareup.sqldelight.runtime.coroutines.asFlow
import com.squareup.sqldelight.runtime.coroutines.mapToList
import java.text.Collator
import java.util.Date
import kotlinx.coroutines.flow.map

Expand All @@ -43,12 +44,20 @@ class NotepadRepository(

fun noteMetadataFlow(order: SortOrder) = with(database.noteMetadataQueries) {
when(order) {
DateDescending -> getSortedByDateDescending()
DateAscending -> getSortedByDateAscending()
TitleDescending -> getSortedByTitleDescending()
TitleAscending -> getSortedByTitleAscending()
DateDescending -> getSortedByDateDescending().asFlow().mapToList()
DateAscending -> getSortedByDateAscending().asFlow().mapToList()
TitleDescending, TitleAscending -> {
// Use collator for locale-aware, case-insensitive sorting
val collator = Collator.getInstance()
getUnsorted().asFlow().mapToList().map { notes ->
val sortedNotes = notes.sortedWith { a, b ->
collator.compare(a.title, b.title)
}
if (order == TitleDescending) sortedNotes.reversed() else sortedNotes
}
}
}
}.asFlow().mapToList()
}

fun getNote(id: Long): Note = with(database) {
transactionWithResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ DELETE FROM NoteMetadata WHERE metadataId = :id;
deleteMultiple:
DELETE FROM NoteMetadata WHERE metadataId IN ?;

getSortedByTitleDescending:
SELECT * FROM NoteMetadata ORDER BY title DESC;

getSortedByTitleAscending:
SELECT * FROM NoteMetadata ORDER BY title ASC;
getUnsorted:
SELECT * FROM NoteMetadata;

getSortedByDateDescending:
SELECT * FROM NoteMetadata ORDER BY date DESC;
Expand Down