Skip to content

Commit

Permalink
fix(card-browser): incorrect 'notes' sort order
Browse files Browse the repository at this point in the history
Issue: when moving to 'cards or notes'
We used 'sortType' instead of 'noteSortType'

Then when we loaded it from the collection, it caused problems

Caused:

```
com.ichi2.libanki.exception.InvalidSearchException: net.ankiweb.rsdroid.exceptions.BackendInvalidInputException: Can't sort Notes by Custom.
  	at com.ichi2.libanki.Collection.findNotes(Collection.kt:398)
```

Fixes 16514

(cherry picked from commit 0acdba3)
  • Loading branch information
david-allison authored and mikehardy committed Jun 2, 2024
1 parent 8f1238e commit b2ab424
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class CardBrowserViewModel(
flowOfCardsOrNotes.update { cardsOrNotes }

withCol {
sortTypeFlow.update { SortType.fromCol(config, sharedPrefs()) }
sortTypeFlow.update { SortType.fromCol(config, cardsOrNotes, sharedPrefs()) }
reverseDirectionFlow.update { ReverseDirection.fromConfig(config) }
}
Timber.i("initCompleted")
Expand Down
7 changes: 5 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/model/SortType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ enum class SortType(val ankiSortType: String?, val cardBrowserLabelIndex: Int) {
config.set("sortType", this.ankiSortType ?: SORT_FIELD.ankiSortType)
config.set("noteSortType", this.ankiSortType ?: SORT_FIELD.ankiSortType)
preferences.edit {
// TODO: This should be changed to use the collection
// and have a different value for cards & notes
putBoolean("cardBrowserNoSorting", this@SortType == NO_SORTING)
}
}
Expand All @@ -64,8 +66,9 @@ enum class SortType(val ankiSortType: String?, val cardBrowserLabelIndex: Int) {
if (this == NO_SORTING) SortOrder.NoOrdering() else SortOrder.UseCollectionOrdering()

companion object {
fun fromCol(config: Config, preferences: SharedPreferences): SortType {
val colOrder = config.get<String>("sortType")
fun fromCol(config: Config, cardsOrNotes: CardsOrNotes, preferences: SharedPreferences): SortType {
val configKey = if (cardsOrNotes == CardsOrNotes.CARDS) "sortType" else "noteSortType"
val colOrder = config.get<String>(configKey)
val type = entries.firstOrNull { it.ankiSortType == colOrder } ?: NO_SORTING
if (type == SORT_FIELD && preferences.getBoolean("cardBrowserNoSorting", false)) {
return NO_SORTING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ class CardBrowserViewModelTest : JvmTest() {
assertThat("unbury: queue -> NEW", getQueue(), equalTo(QUEUE_TYPE_NEW))
}

@Test
fun `sort order from notes is selected - 16514`() {
col.config.set("sortType", "noteCrt")
col.config.set("noteSortType", "_field_Frequency")
with(col) { CardsOrNotes.NOTES.saveToCollection() }

runViewModelTest(notes = 1) {
assertThat("1 row returned", rowCount, equalTo(1))
}
}

private fun runViewModelTest(notes: Int = 0, manualInit: Boolean = true, testBody: suspend CardBrowserViewModel.() -> Unit) = runTest {
for (i in 0 until notes) {
addNoteUsingBasicModel()
Expand Down

0 comments on commit b2ab424

Please sign in to comment.