Skip to content

Commit

Permalink
chore: handle POST requests in CardViewerViewModel
Browse files Browse the repository at this point in the history
IO requests i18Resources now
  • Loading branch information
BrayanDSO committed Jun 9, 2024
1 parent 9c4d2a4 commit 6fe37e1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.ichi2.anki.cardviewer.SoundErrorBehavior
import com.ichi2.anki.cardviewer.SoundErrorListener
import com.ichi2.anki.launchCatchingIO
import com.ichi2.anki.pages.AnkiServer
import com.ichi2.anki.pages.PostRequestHandler
import com.ichi2.libanki.Card
import com.ichi2.libanki.Sound
import com.ichi2.libanki.TtsPlayer
Expand All @@ -46,7 +47,10 @@ import timber.log.Timber

abstract class CardViewerViewModel(
cardMediaPlayer: CardMediaPlayer
) : ViewModel(), OnErrorListener {
) : ViewModel(),
OnErrorListener,
PostRequestHandler {

override val onError = MutableSharedFlow<String>()
val onMediaError = MutableSharedFlow<String>()
val onTtsError = MutableSharedFlow<TtsPlayer.TtsError>()
Expand All @@ -62,6 +66,8 @@ abstract class CardViewerViewModel(
}
abstract var currentCard: Deferred<Card>

abstract val server: AnkiServer

@CallSuper
override fun onCleared() {
super.onCleared()
Expand All @@ -79,7 +85,7 @@ abstract class CardViewerViewModel(
*/
abstract fun onPageFinished(isAfterRecreation: Boolean)

open fun baseUrl(): String = "http://${AnkiServer.LOCALHOST}/"
fun baseUrl(): String = server.baseUrl()

fun setSoundPlayerEnabled(isEnabled: Boolean) {
cardMediaPlayer.isEnabled = isEnabled
Expand Down Expand Up @@ -177,6 +183,17 @@ abstract class CardViewerViewModel(
}
}

override suspend fun handlePostRequest(uri: String, bytes: ByteArray): ByteArray {
return if (uri.startsWith(AnkiServer.ANKI_PREFIX)) {
when (uri.substring(AnkiServer.ANKI_PREFIX.length)) {
"i18nResources" -> withCol { i18nResourcesRaw(bytes) }
else -> throw IllegalArgumentException("Unhandled Anki request: $uri")
}
} else {
throw IllegalArgumentException("Unhandled POST request: $uri")
}
}

companion object {
/* ********************************** Type-in answer ************************************ */
/** From the [desktop code](https://github.com/ankitects/anki/blob/1ff55475b93ac43748d513794bcaabd5d7df6d9d/qt/aqt/reviewer.py#L669] */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.ichi2.anki.browser.PreviewerIdsFile
import com.ichi2.anki.cardviewer.CardMediaPlayer
import com.ichi2.anki.cardviewer.SingleCardSide
import com.ichi2.anki.launchCatchingIO
import com.ichi2.anki.pages.AnkiServer
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.anki.servicelayer.MARKED_TAG
import com.ichi2.anki.servicelayer.NoteService
Expand Down Expand Up @@ -67,6 +68,7 @@ class PreviewerViewModel(previewerIdsFile: PreviewerIdsFile, firstIndex: Int, ca
override var currentCard: Deferred<Card> = asyncIO {
withCol { getCard(selectedCardIds[firstIndex]) }
}
override val server = AnkiServer(this).also { it.start() }

/* *********************************************************************************************
************************ Public methods: meant to be used by the View **************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.ichi2.anki.NotetypeFile
import com.ichi2.anki.asyncIO
import com.ichi2.anki.cardviewer.CardMediaPlayer
import com.ichi2.anki.launchCatchingIO
import com.ichi2.anki.pages.AnkiServer
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.anki.utils.ext.ifNullOrEmpty
import com.ichi2.libanki.Card
Expand Down Expand Up @@ -59,6 +60,7 @@ class TemplatePreviewerViewModel(
private val templateNames: Deferred<List<String>>
private val clozeOrds: Deferred<List<Int>>?
override var currentCard: Deferred<Card>
override val server = AnkiServer(this).also { it.start() }

init {
note = asyncIO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.ichi2.anki.asyncIO
import com.ichi2.anki.cardviewer.CardMediaPlayer
import com.ichi2.anki.launchCatchingIO
import com.ichi2.anki.pages.AnkiServer
import com.ichi2.anki.pages.PostRequestHandler
import com.ichi2.anki.previewer.CardViewerViewModel
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.libanki.sched.CurrentQueueState
Expand All @@ -36,9 +35,7 @@ import kotlinx.coroutines.Deferred
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow

class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
CardViewerViewModel(cardMediaPlayer),
PostRequestHandler {
class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : CardViewerViewModel(cardMediaPlayer) {

private var queueState: Deferred<CurrentQueueState?> = asyncIO {
// this assumes that the Reviewer won't be launched if there isn't a queueState
Expand All @@ -49,7 +46,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
}
var isQueueFinishedFlow = MutableSharedFlow<Boolean>()

private val server = AnkiServer(this).also { it.start() }
override val server = AnkiServer(this).also { it.start() }
private val stateMutationKey = TimeManager.time.intTimeMS().toString()
val statesMutationEval = MutableSharedFlow<String>()

Expand Down Expand Up @@ -85,8 +82,6 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
}
}

override fun baseUrl(): String = server.baseUrl()

fun showAnswer() {
launchCatchingIO {
while (!statesMutated) {
Expand Down Expand Up @@ -115,10 +110,10 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
when (uri.substring(AnkiServer.ANKI_PREFIX.length)) {
"getSchedulingStatesWithContext" -> getSchedulingStatesWithContext()
"setSchedulingStates" -> setSchedulingStates(bytes)
else -> byteArrayOf()
else -> super.handlePostRequest(uri, bytes)
}
} else {
byteArrayOf()
super.handlePostRequest(uri, bytes)
}
}

Expand Down

0 comments on commit 6fe37e1

Please sign in to comment.