From 14384abedbb856da88030469fff9c344972b5100 Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:19:39 -0300 Subject: [PATCH] ok --- .../ichi2/anki/previewer/PreviewerActivity.kt | 14 ++++++---- .../anki/previewer/PreviewerViewModel.kt | 26 +++---------------- AnkiDroid/src/main/res/layout/previewer.xml | 1 + 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerActivity.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerActivity.kt index 3c437148217c..0e7228b82826 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerActivity.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerActivity.kt @@ -102,7 +102,8 @@ class PreviewerActivity : AnkiActivity() { val progressIndicator = findViewById(R.id.progress_indicator) lifecycleScope.launch { - viewModel.onError.flowWithLifecycle(lifecycle) + viewModel.onError + .flowWithLifecycle(lifecycle) .collect { errorMessage -> AlertDialog.Builder(this@PreviewerActivity) .setTitle(R.string.vague_error) @@ -111,13 +112,15 @@ class PreviewerActivity : AnkiActivity() { } } lifecycleScope.launch { - viewModel.eval.flowWithLifecycle(lifecycle) + viewModel.eval + .flowWithLifecycle(lifecycle) .collect { eval -> webView.evaluateJavascript(eval, null) } } - lifecycleScope.launch() { - viewModel.backsideOnly.flowWithLifecycle(lifecycle) + lifecycleScope.launch { + viewModel.backsideOnly + .flowWithLifecycle(lifecycle) .collectLatest { isBacksideOnly -> setBacksideOnlyButtonIcon(isBacksideOnly) } @@ -125,7 +128,8 @@ class PreviewerActivity : AnkiActivity() { val cardsCount = selectedCardIds.count() lifecycleScope.launch { - viewModel.currentIndex.flowWithLifecycle(lifecycle) + viewModel.currentIndex + .flowWithLifecycle(lifecycle) .collectLatest { currentIndex -> val displayIndex = currentIndex + 1 slider.value = displayIndex.toFloat() diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt index e9bc42b01167..8c05363fb639 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt @@ -16,7 +16,6 @@ package com.ichi2.anki.previewer import android.content.Context -import android.net.Uri import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope @@ -25,7 +24,6 @@ import com.ichi2.anki.AnkiDroidApp import com.ichi2.anki.CollectionManager.withCol import com.ichi2.anki.LanguageUtils import com.ichi2.libanki.Card -import com.ichi2.libanki.Sound import com.ichi2.libanki.addPlayButtons import com.ichi2.themes.Themes import com.ichi2.utils.toRGBHex @@ -39,7 +37,6 @@ import kotlinx.serialization.json.Json import net.ankiweb.rsdroid.BackendException import org.intellij.lang.annotations.Language import timber.log.Timber -import java.io.File class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArray, firstIndex: Int) : ViewModel() { val eval = MutableSharedFlow() @@ -47,22 +44,12 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra val currentIndex = MutableStateFlow(firstIndex) val backsideOnly = MutableStateFlow(false) - private val soundManager: Sound private var showingAnswer = false - // TODO(perf): make Reviewer/Previewer/AnkiServer a service or something similar in order - // to avoid having multiple server instances running while only one is necessary - private val server: PreviewerServer + // TODO maybe move the server to a Service? + private val server = PreviewerServer(mediaDir).also { it.start() } private lateinit var currentCard: Card - init { - Timber.v("PreviewerViewModel::init") - soundManager = Sound(Uri.fromFile(File(mediaDir)).toString()) - - server = PreviewerServer(mediaDir) - server.start() - } - fun toggleBacksideOnly() { Timber.v("toggleBacksideOnly() %b", !backsideOnly.value) launchCatching { @@ -83,7 +70,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra * @param reload useful if the note has been edited */ fun loadCurrentCard(reload: Boolean = false) { - Timber.v("loadCard()") + Timber.v("loadCurrentCard()") launchCatching { if (!this::currentCard.isInitialized || reload) { currentCard = withCol { getCard(selectedCardIds[currentIndex.value]) } @@ -107,13 +94,13 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra private suspend fun showQuestion() { Timber.v("showQuestion()") showingAnswer = false + val question = prepareCardTextForDisplay(currentCard.question()) val answer = withCol { media.escapeMediaFilenames(currentCard.answer()) } val bodyClass = bodyClassForCardOrd(currentCard.ord) eval.emit("_showQuestion(${Json.encodeToString(question)}, ${Json.encodeToString(answer)}, '$bodyClass');") -// soundManager.addSounds(currentCard.questionAvTags(), isQuestion = true) updateFlagIcon() updateMarkIcon() } @@ -173,11 +160,6 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra } } - override fun onCleared() { - super.onCleared() - server.stop() - } - companion object { /** * Not exactly equal to anki's stdHtml. diff --git a/AnkiDroid/src/main/res/layout/previewer.xml b/AnkiDroid/src/main/res/layout/previewer.xml index e22e516826af..e135d866674f 100644 --- a/AnkiDroid/src/main/res/layout/previewer.xml +++ b/AnkiDroid/src/main/res/layout/previewer.xml @@ -23,6 +23,7 @@ app:layout_constraintTop_toTopOf="parent" app:navigationContentDescription="@string/abc_action_bar_up_description" app:navigationIcon="?attr/homeAsUpIndicator" + tools:menu="@menu/previewer" />