Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ abstract class AbstractFlashcardViewer :
Timber.w("media is not played as the activity is inactive")
return
}
if (!cardMediaPlayer.config.autoplay && !doMediaReplay) return
if (cardMediaPlayer.config?.autoplay != true && !doMediaReplay) return
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asking for some attention on the condition fixes of AbstractFlashcardViewer

// Use TTS if TTS preference enabled and no other media source
val useTTS = tts.enabled && !cardMediaPlayer.hasMedia(displayAnswer)
// We need to play the media from the proper side of the card
Expand All @@ -1485,7 +1485,7 @@ abstract class AbstractFlashcardViewer :
return
}

val replayQuestion = cardMediaPlayer.config.replayQuestion
val replayQuestion = cardMediaPlayer.config?.replayQuestion == true
// Text to speech is in effect here
// If the question is displayed or if the question should be replayed, read the question
if (ttsInitialized) {
Expand Down Expand Up @@ -1554,7 +1554,7 @@ abstract class AbstractFlashcardViewer :
content: String,
) {
if (card != null) {
card.settings.mediaPlaybackRequiresUserGesture = !cardMediaPlayer.config.autoplay
card.settings.mediaPlaybackRequiresUserGesture = cardMediaPlayer.config?.autoplay != true
card.loadDataWithBaseURL(
server.baseUrl(),
content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ class CardMediaPlayer : Closeable {
/** Serializes playbacks to avoid overloading the thread pool and a potential deadlock */
private val playbackMutex = Mutex()

private lateinit var questionAvTags: List<AvTag>
private lateinit var answerAvTags: List<AvTag>
private var questionAvTags: List<AvTag> = emptyList()
private var answerAvTags: List<AvTag> = emptyList()

lateinit var config: CardSoundConfig
var config: CardSoundConfig? = null
private set

var isEnabled: Boolean = true
private set
Expand Down Expand Up @@ -140,7 +141,13 @@ class CardMediaPlayer : Closeable {
this.questionAvTags = renderOutput.questionAvTags
this.answerAvTags = renderOutput.answerAvTags

if (!this::config.isInitialized || !config.appliesTo(card) || (this::config.isInitialized && autoPlay != config.autoplay)) {
val currentConfig = config
val needsConfigUpdate =
currentConfig == null ||
!currentConfig.appliesTo(card) ||
autoPlay != currentConfig.autoplay

if (needsConfigUpdate) {
config = withCol { CardSoundConfig.create(this@withCol, card) }
}
}
Expand All @@ -151,20 +158,21 @@ class CardMediaPlayer : Closeable {
* Does not affect playback if they are
*/
suspend fun ensureAvTagsLoaded(card: Card) {
if (this::questionAvTags.isInitialized) return
if (config?.appliesTo(card) == true) return

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

if (!this::config.isInitialized || !config.appliesTo(card)) {
val currentConfig = config
if (currentConfig == null || !currentConfig.appliesTo(card)) {
config = withCol { CardSoundConfig.create(this@withCol, card) }
}
}

suspend fun autoplayAllForSide(cardSide: CardSide) {
if (config.autoplay) {
if (config?.autoplay == true) {
playAllForSide(cardSide)
}
}
Expand Down Expand Up @@ -310,14 +318,14 @@ class CardMediaPlayer : Closeable {
}

/** Whether the provided side has available media */
fun hasMedia(displayAnswer: Boolean): Boolean = if (displayAnswer) answerAvTags.any() else questionAvTags.any()
fun hasMedia(displayAnswer: Boolean): Boolean = if (displayAnswer) answerAvTags.isNotEmpty() else questionAvTags.isNotEmpty()

/**
* Replays all sounds for [side], calling [onMediaGroupCompleted] when completed
*/
suspend fun replayAll(side: SingleCardSide) =
when (side) {
SingleCardSide.BACK -> if (config.replayQuestion) playAllForSide(CardSide.BOTH) else playAllForSide(CardSide.ANSWER)
SingleCardSide.BACK -> if (config?.replayQuestion == true) playAllForSide(CardSide.BOTH) else playAllForSide(CardSide.ANSWER)
SingleCardSide.FRONT -> playAllForSide(CardSide.QUESTION)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class PreviewerViewModelTest : JvmTest() {
}

@Test
@Flaky(OS.ALL, "assumed to be #19729")
fun `previous button`() =
runTest {
// Start at Index 1
Expand All @@ -122,7 +121,6 @@ class PreviewerViewModelTest : JvmTest() {
}

@Test
@Flaky(OS.ALL, "#19729")
fun `toggle back side only`() =
runTest {
assertFalse(viewModel.backSideOnly.value) // initial state should be false
Expand Down Expand Up @@ -178,7 +176,6 @@ class PreviewerViewModelTest : JvmTest() {
}

@Test
@Flaky(OS.ALL, "#19729")
fun `next, slider and previous navigation integration`() =
runTest {
// 1. Start at Index 0 (Question)
Expand Down