Skip to content

Commit

Permalink
Allow fine tuning the unbury action in congrats screen
Browse files Browse the repository at this point in the history
Follows desktop code to present the user with a dialog to
select what to unbury based on the info returned for the
congrats screen.

See https://github.com/ankitects/anki/blob/acaeee91fa853e4a7a78dcddbb832d009ec3529a/qt/aqt/overview.py#L151-L174
  • Loading branch information
lukstbit committed Feb 10, 2025
1 parent 1d24177 commit c77e4ff
Showing 1 changed file with 61 additions and 8 deletions.
69 changes: 61 additions & 8 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/CongratsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import anki.collection.OpChanges
import anki.scheduler.UnburyDeckRequest
import com.google.android.material.appbar.MaterialToolbar
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.CollectionManager.withCol
Expand All @@ -49,6 +50,9 @@ import com.ichi2.anki.utils.TIME_MINUTE
import com.ichi2.libanki.ChangeManager
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.undoableOp
import com.ichi2.utils.listItemsAndMessage
import com.ichi2.utils.negativeButton
import com.ichi2.utils.show
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
Expand Down Expand Up @@ -91,9 +95,36 @@ class CongratsPage :
.show()
}.launchIn(lifecycleScope)

viewModel.openStudyOptions
.onEach { openStudyOptionsAndFinish() }
.launchIn(lifecycleScope)
viewModel.unburyState
.onEach { state ->
when (state) {
UnburyState.OpenStudy -> openStudyOptionsAndFinish()
UnburyState.SelectMode -> {
val unburyOptions =
mutableListOf(
TR.studyingManuallyBuriedCards(),
TR.studyingBuriedSiblings(),
TR.studyingAllBuriedCards(),
)
AlertDialog.Builder(requireContext()).show {
negativeButton(R.string.dialog_cancel)
listItemsAndMessage(
TR.studyingWhatWouldYouLikeToUnbury(),
unburyOptions,
) { _, position ->
val mode =
when (position) {
0 -> UnburyDeckRequest.Mode.USER_ONLY
1 -> UnburyDeckRequest.Mode.SCHED_ONLY
2 -> UnburyDeckRequest.Mode.ALL
else -> error("Unhandled unbury option: ${unburyOptions[position]}")
}
viewModel.onUnburyModeSelected(mode)
}
}
}
}
}.launchIn(lifecycleScope)

viewModel.deckOptionsDestination
.flowWithLifecycle(lifecycle)
Expand All @@ -112,7 +143,7 @@ class CongratsPage :
}
}

setFragmentResultListener(CustomStudyAction.REQUEST_KEY) { requestKey, bundle ->
setFragmentResultListener(CustomStudyAction.REQUEST_KEY) { _, bundle ->
when (CustomStudyAction.fromBundle(bundle)) {
CustomStudyAction.CUSTOM_STUDY_SESSION,
CustomStudyAction.EXTEND_STUDY_LIMITS,
Expand Down Expand Up @@ -210,18 +241,34 @@ class CongratsViewModel :
ViewModel(),
OnErrorListener {
override val onError = MutableSharedFlow<String>()
val openStudyOptions = MutableSharedFlow<Boolean>()
val unburyState = MutableSharedFlow<UnburyState>()
val deckOptionsDestination = MutableSharedFlow<DeckOptionsDestination>()

fun onUnbury() {
launchCatchingIO {
undoableOp {
sched.unburyDeck(decks.getCurrentId())
// https://github.com/ankitects/anki/blob/acaeee91fa853e4a7a78dcddbb832d009ec3529a/qt/aqt/overview.py#L154
val congratsInfo = withCol { sched.congratulationsInfo() }
if (congratsInfo.haveSchedBuried && congratsInfo.haveUserBuried) {
unburyState.emit(UnburyState.SelectMode)
return@launchCatchingIO
}
openStudyOptions.emit(true)
unburyAndStudy(UnburyDeckRequest.Mode.ALL)
}
}

fun onUnburyModeSelected(mode: UnburyDeckRequest.Mode) {
launchCatchingIO {
unburyAndStudy(mode)
}
}

private suspend fun unburyAndStudy(mode: UnburyDeckRequest.Mode) {
undoableOp {
sched.unburyDeck(decks.getCurrentId(), mode)
}
unburyState.emit(UnburyState.OpenStudy)
}

fun onDeckOptions() {
launchCatchingIO {
val deckId = withCol { decks.getCurrentId() }
Expand All @@ -242,3 +289,9 @@ class DeckOptionsDestination(
DeckOptions.getIntent(context, deckId)
}
}

sealed class UnburyState {
data object OpenStudy : UnburyState()

data object SelectMode : UnburyState()
}

0 comments on commit c77e4ff

Please sign in to comment.