Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 11, 2023
1 parent a798f15 commit 14384ab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class PreviewerActivity : AnkiActivity() {
val progressIndicator = findViewById<MaterialTextView>(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)
Expand All @@ -111,21 +112,24 @@ 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)
}
}

val cardsCount = selectedCardIds.count()
lifecycleScope.launch {
viewModel.currentIndex.flowWithLifecycle(lifecycle)
viewModel.currentIndex
.flowWithLifecycle(lifecycle)
.collectLatest { currentIndex ->
val displayIndex = currentIndex + 1
slider.value = displayIndex.toFloat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -39,30 +37,19 @@ 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<String>()
val onError = MutableSharedFlow<String>()
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 {
Expand All @@ -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]) }
Expand All @@ -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()
}
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/layout/previewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>

</com.google.android.material.appbar.AppBarLayout>
Expand Down

0 comments on commit 14384ab

Please sign in to comment.