Skip to content

Commit

Permalink
extract: repositionSelectedRows
Browse files Browse the repository at this point in the history
  • Loading branch information
david-allison authored and BrayanDSO committed Dec 8, 2023
1 parent d08d1b6 commit 1747ac9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
29 changes: 11 additions & 18 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ open class CardBrowser :
prompt = this@CardBrowser.getString(R.string.reposition_card_dialog_message),
digits = 5
)
setCallbackRunnable { pos -> repositionCardsNoValidation(selectedCardIds, pos) }
setCallbackRunnable(::repositionCardsNoValidation)
}
showDialogFragment(repositionDialog)
return true
Expand Down Expand Up @@ -1226,23 +1226,16 @@ open class CardBrowser :
}

@VisibleForTesting
fun repositionCardsNoValidation(cardIds: List<CardId>, position: Int) {
launchCatchingTask {
val changes = withProgress {
undoableOp {
sched.sortCards(cardIds, position, 1, false, true)
}
}
val count = changes.count
showSnackbar(
resources.getQuantityString(
R.plurals.reposition_card_dialog_acknowledge,
count,
count
),
Snackbar.LENGTH_SHORT
)
}
fun repositionCardsNoValidation(position: Int) = launchCatchingTask {
val count = withProgress { viewModel.repositionSelectedRows(position) }
showSnackbar(
resources.getQuantityString(
R.plurals.reposition_card_dialog_acknowledge,
count,
count
),
Snackbar.LENGTH_SHORT
)
}

protected fun onPreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ class CardBrowserViewModel(
}
}

/**
* @see [com.ichi2.libanki.sched.Scheduler.sortCards]
* @return the number of cards which were repositioned
*/
suspend fun repositionSelectedRows(position: Int) = undoableOp {
sched.sortCards(selectedCardIds, position, 1, shuffle = false, shift = true)
}.count

companion object {
const val DISPLAY_COLUMN_1_KEY = "cardBrowserColumn1"
const val DISPLAY_COLUMN_2_KEY = "cardBrowserColumn2"
Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/test/java/com/ichi2/anki/CardBrowserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ class CardBrowserTest : RobolectricTest() {
equalTo("1")
)

b.repositionCardsNoValidation(listOf(card.id), 2)
b.viewModel.repositionSelectedRows(2)

card.reload()

Expand Down Expand Up @@ -601,7 +601,7 @@ class CardBrowserTest : RobolectricTest() {
equalTo("1")
)

b.repositionCardsNoValidation(listOf(card.id), 2)
b.repositionCardsNoValidation(2)

assertThat(
"Position of checked card after reposition",
Expand Down

0 comments on commit 1747ac9

Please sign in to comment.