Skip to content

Commit

Permalink
refactor: Card Browser - Preview
Browse files Browse the repository at this point in the history
  • Loading branch information
david-allison authored and lukstbit committed Dec 24, 2023
1 parent 0717daa commit 7d64f36
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
19 changes: 4 additions & 15 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1192,21 +1192,10 @@ open class CardBrowser :
)
}

protected fun onPreview() {
onPreviewCardsActivityResult.launch(previewIntent)
} // Preview all cards, starting from the one that is currently selected

// Multiple cards have been explicitly selected, so preview only those cards
@get:VisibleForTesting
val previewIntent: Intent
get() = if (isInMultiSelectMode && viewModel.selectedRowCount() > 1) {
// Multiple cards have been explicitly selected, so preview only those cards
getPreviewIntent(0, selectedCardIds.toLongArray())
} else {
// Preview all cards, starting from the one that is currently selected
val startIndex = viewModel.selectedRows.firstOrNull()?.position ?: 0
getPreviewIntent(startIndex, viewModel.allCardIds)
}
private fun onPreview() {
val intentData = viewModel.previewIntentData
onPreviewCardsActivityResult.launch(getPreviewIntent(intentData.index, intentData.cardList))
}

private fun getPreviewIntent(index: Int, selectedCardIds: LongArray): Intent {
return if (sharedPrefs().getBoolean("new_previewer", false)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.ichi2.anki.CardBrowser
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.DeckSpinnerSelection.Companion.ALL_DECKS_ID
import com.ichi2.anki.Flag
import com.ichi2.anki.PreviewDestination
import com.ichi2.anki.R
import com.ichi2.anki.dialogs.ExportDialogParams
import com.ichi2.anki.export.ExportType
Expand Down Expand Up @@ -459,6 +460,24 @@ class CardBrowserViewModel(
setFilterQuery(sb.toString())
}

/** Previewing */

val previewIntentData: PreviewDestination
get() = if (selectedRowCount() > 1) {
// Multiple cards have been explicitly selected, so preview only those cards
PreviewDestination(index = 0, cardList = selectedCardIds.toLongArray())
} else {
// Preview all cards, starting from the one that is currently selected
val startIndex = indexOfFirstCheckedCard() ?: 0
PreviewDestination(startIndex, allCardIds)
}

/** @return the index of the first checked card in [cards], or `null` if no cards are checked */
private fun indexOfFirstCheckedCard(): Int? {
val idToFind = selectedRows.firstOrNull()?.id ?: return null
return allCardIds.indexOf(idToFind)
}

companion object {
const val DISPLAY_COLUMN_1_KEY = "cardBrowserColumn1"
const val DISPLAY_COLUMN_2_KEY = "cardBrowserColumn2"
Expand Down
12 changes: 6 additions & 6 deletions AnkiDroid/src/test/java/com/ichi2/anki/CardBrowserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,11 @@ class CardBrowserTest : RobolectricTest() {
assertThat(b.getPropertiesForCardId(cid2).position, equalTo(1))

b.selectRowsWithPositions(0)
val previewIntent = b.previewIntent
assertThat("before: index", previewIntent.getIntExtra("index", -100), equalTo(0))
val previewIntent = b.viewModel.previewIntentData
assertThat("before: index", previewIntent.index, equalTo(0))
assertThat(
"before: cards",
previewIntent.getLongArrayExtra("cardList"),
previewIntent.cardList,
equalTo(longArrayOf(cid1, cid2))
)

Expand All @@ -450,11 +450,11 @@ class CardBrowserTest : RobolectricTest() {
assertThat(b.getPropertiesForCardId(cid2).position, equalTo(0))

b.replaceSelectionWith(intArrayOf(0))
val intentAfterReverse = b.previewIntent
assertThat("after: index", intentAfterReverse.getIntExtra("index", -100), equalTo(0))
val intentAfterReverse = b.viewModel.previewIntentData
assertThat("after: index", intentAfterReverse.index, equalTo(0))
assertThat(
"after: cards",
intentAfterReverse.getLongArrayExtra("cardList"),
intentAfterReverse.cardList,
equalTo(longArrayOf(cid2, cid1))
)
}
Expand Down

0 comments on commit 7d64f36

Please sign in to comment.