Skip to content

Commit

Permalink
feat: toggle auto advance in new reviewer
Browse files Browse the repository at this point in the history
it also changes the behavior to match the desktop code, so auto advance only starts after the user toggles it
  • Loading branch information
RobozinhoD committed Jan 5, 2025
1 parent 5ff3c18 commit c4e6849
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class CardMediaPlayer : Closeable {
}

private var playSoundsJob: Job? = null
val isPlaying get() = playSoundsJob != null

private var onSoundGroupCompleted: (() -> Unit)? = null

Expand Down Expand Up @@ -224,7 +225,7 @@ class CardMediaPlayer : Closeable {
}

suspend fun stopSounds() {
if (playSoundsJob != null) Timber.i("stopping sounds")
if (isPlaying) Timber.i("stopping sounds")
cancelPlaySoundsJob(playSoundsJob)
ReadText.stopTts() // TODO: Reconsider design
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ enum class ViewerAction(
DECK_OPTIONS(R.id.action_deck_options, R.drawable.ic_tune_white, R.string.menu__deck_options, DISABLED),
CARD_INFO(R.id.action_card_info, R.drawable.ic_dialog_info, R.string.card_info_title, DISABLED),
ADD_NOTE(R.id.action_add_note, R.drawable.ic_add, R.string.menu_add_note, DISABLED),
TOGGLE_AUTO_ADVANCE(R.id.action_toggle_auto_advance, R.drawable.ic_fast_forward, R.string.toggle_auto_advance, DISABLED),
USER_ACTION_1(R.id.user_action_1, R.drawable.user_action_1, R.string.user_action_1, DISABLED),
USER_ACTION_2(R.id.user_action_2, R.drawable.user_action_2, R.string.user_action_2, DISABLED),
USER_ACTION_3(R.id.user_action_3, R.drawable.user_action_3, R.string.user_action_3, DISABLED),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import com.ichi2.anki.preferences.reviewer.ViewerAction.REDO
import com.ichi2.anki.preferences.reviewer.ViewerAction.SUSPEND_CARD
import com.ichi2.anki.preferences.reviewer.ViewerAction.SUSPEND_MENU
import com.ichi2.anki.preferences.reviewer.ViewerAction.SUSPEND_NOTE
import com.ichi2.anki.preferences.reviewer.ViewerAction.TOGGLE_AUTO_ADVANCE
import com.ichi2.anki.preferences.reviewer.ViewerAction.UNDO
import com.ichi2.anki.preferences.reviewer.ViewerAction.UNSET_FLAG
import com.ichi2.anki.preferences.reviewer.ViewerAction.USER_ACTION_1
Expand Down Expand Up @@ -180,6 +181,7 @@ class ReviewerFragment :
MARK -> viewModel.toggleMark()
REDO -> viewModel.redo()
UNDO -> viewModel.undo()
TOGGLE_AUTO_ADVANCE -> viewModel.toggleAutoAdvance()
BURY_NOTE -> viewModel.buryNote()
BURY_CARD -> viewModel.buryCard()
SUSPEND_NOTE -> viewModel.suspendNote()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,28 @@ class ReviewerViewModel(
autoAdvance.cancelQuestionAndAnswerActionJobs()
}

fun toggleAutoAdvance() {
launchCatchingIO {
autoAdvance.isEnabled = !autoAdvance.isEnabled

val message =
if (autoAdvance.isEnabled) {
CollectionManager.TR.actionsAutoAdvanceActivated()
} else {
CollectionManager.TR.actionsAutoAdvanceDeactivated()
}
actionFeedbackFlow.emit(message)

if (autoAdvance.shouldWaitForAudio() && cardMediaPlayer.isPlaying) return@launchCatchingIO

if (showingAnswer.value) {
autoAdvance.onShowAnswer()
} else {
autoAdvance.onShowQuestion()
}
}
}

/* *********************************************************************************************
*************************************** Internal methods ***************************************
********************************************************************************************* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ import kotlinx.coroutines.delay
class AutoAdvance(
val viewModel: ReviewerViewModel,
) {
var isEnabled = false
set(value) {
field = value
if (!value) {
cancelQuestionAndAnswerActionJobs()
}
}
private var questionActionJob: Job? = null
private var answerActionJob: Job? = null

Expand Down Expand Up @@ -70,7 +77,7 @@ class AutoAdvance(

suspend fun onShowQuestion() {
answerActionJob?.cancel()
if (!durationToShowQuestionFor().isPositive()) return
if (!durationToShowQuestionFor().isPositive() || !isEnabled) return

questionActionJob =
viewModel.launchCatchingIO {
Expand All @@ -84,7 +91,7 @@ class AutoAdvance(

suspend fun onShowAnswer() {
questionActionJob?.cancel()
if (!durationToShowAnswerFor().isPositive()) return
if (!durationToShowAnswerFor().isPositive() || !isEnabled) return

answerActionJob =
viewModel.launchCatchingIO {
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<item type="id" name="action_bury_note"/>
<item type="id" name="action_suspend_card"/>
<item type="id" name="action_suspend_note"/>
<item type="id" name="action_toggle_auto_advance"/>

<item type="id" name="user_action_1"/>
<item type="id" name="user_action_2"/>
Expand Down

0 comments on commit c4e6849

Please sign in to comment.