Skip to content

Commit

Permalink
feat(deck-picker): make 'empty filtered' undoable
Browse files Browse the repository at this point in the history
  • Loading branch information
david-allison committed Jan 16, 2025
1 parent 89e0006 commit 3817bc6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2662,7 +2662,7 @@ open class DeckPicker :
changes: OpChanges,
handler: Any?,
) {
if (changes.studyQueues && handler !== this) {
if (changes.studyQueues && handler !== this && handler !== viewModel) {
invalidateOptionsMenu()
if (!activityPaused) {
// No need to update while the activity is paused, because `onResume` calls `refreshState` that calls `updateDeckList`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,11 @@ class DeckPickerViewModel : ViewModel() {
}

// TODO: move withProgress to the ViewModel, so we don't return 'Job'
// TODO: undoableOp { } on emptyDyn
fun emptyFilteredDeck(deckId: DeckId): Job =
viewModelScope.launch {
Timber.i("empty filtered deck %s", deckId)
withCol { decks.select(deckId) }
withCol { sched.emptyDyn(decks.selected()) }
undoableOp { sched.emptyDyn(decks.selected()) }
flowOfDeckCountsChanged.emit(Unit)
}
}
Expand Down
4 changes: 1 addition & 3 deletions AnkiDroid/src/main/java/com/ichi2/libanki/sched/Scheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,7 @@ open class Scheduler(
/** Remove all cards from a dynamic deck
* @param did The deck to empty. 0 means current deck.
*/
open fun emptyDyn(did: DeckId) {
col.backend.emptyFilteredDeck(did)
}
open fun emptyDyn(did: DeckId) = col.backend.emptyFilteredDeck(did)

fun deckDueTree(): DeckNode = deckTree(true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ class DeckPickerViewModelTest : RobolectricTest() {
}
}

@Test
fun `empty filtered - undoable`() {
runTest {
val filteredDeckId = moveAllCardsToFilteredDeck()

// ChangeManager assert
ensureOpsExecuted(1) {
viewModel.emptyFilteredDeck(filteredDeckId).join()
}

// backend assert
assertThat("col undo status", col.undoStatus().undo, equalTo("Empty"))
}
}

@CheckResult
private suspend fun createEmptyCards(): List<CardId> {
addNoteUsingNoteTypeName("Cloze", "{{c1::Hello}} {{c2::World}}", "").apply {
Expand Down

0 comments on commit 3817bc6

Please sign in to comment.