Skip to content

Commit

Permalink
fix: lateinit property of questions and answers not initialised
Browse files Browse the repository at this point in the history
`isAfterRecreation = true` -> sounds were not loaded

This was triggered if "Don't keep activities" was set

Fixes 16302
  • Loading branch information
david-allison authored and mikehardy committed May 2, 2024
1 parent 4bd2cd1 commit 731b85c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ open class SingleFragmentActivity : AnkiActivity() {
setTransparentStatusBar()

// avoid recreating the fragment on configuration changes
// the fragment should handle state restoration
if (savedInstanceState != null) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ class CardMediaPlayer : Closeable {
}
}

/**
* Ensures that [questions] and [answers] are loaded
*
* Does not affect playback if they are
*/
suspend fun ensureCardSoundsLoaded(card: Card) {
if (this::questions.isInitialized) return

Timber.i("loading sounds for card %d", card.id)
val renderOutput = withCol { card.renderOutput(this) }
this.questions = renderOutput.questionAvTags
this.answers = renderOutput.answerAvTags

if (!this::config.isInitialized || !config.appliesTo(card)) {
config = withCol { CardSoundConfig.create(card) }
}
}

suspend fun playAllSoundsForSide(cardSide: CardSide): Job? {
if (!isEnabled) return null
playSoundsJob {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.ichi2.anki.launchCatchingIO
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.anki.servicelayer.MARKED_TAG
import com.ichi2.anki.servicelayer.NoteService
import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.Card
import com.ichi2.libanki.hasTag
import com.ichi2.libanki.note
Expand Down Expand Up @@ -72,9 +73,19 @@ class PreviewerViewModel(previewerIdsFile: PreviewerIdsFile, firstIndex: Int, ca
********************************************************************************************* */

/** Call this after the webView has finished loading the page */
@NeedsTest("16302 - a sound-only card on the back/flipped with 'don't keep activities'")
@NeedsTest("16302 - on config changes, sound continues to play")
override fun onPageFinished(isAfterRecreation: Boolean) {
if (isAfterRecreation) {
launchCatchingIO { showCard(showAnswerOnReload) }
launchCatchingIO {
showCard(showAnswerOnReload)
// isAfterRecreation can either mean:
// * after config change (ViewModel exists)
// * after recreation (ViewModel did not exist)
// if the ViewModel existed, we want to continue playing audio
// if not, we want to setup the sound player
cardMediaPlayer.ensureCardSoundsLoaded(currentCard.await())
}
return
}
launchCatchingIO {
Expand Down

0 comments on commit 731b85c

Please sign in to comment.