Skip to content

Commit

Permalink
perf/refactor: use ViewerAssetLoader in the new previewer
Browse files Browse the repository at this point in the history
which is faster and has the bonus of eliminating the need of a server (for now at least)
  • Loading branch information
BrayanDSO committed Dec 23, 2023
1 parent d103ca4 commit 830819b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 98 deletions.
4 changes: 0 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/anki/ViewerAssetLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ fun Context.getViewerAssetLoader(domain: String): WebViewAssetLoader {
return WebViewAssetLoader.Builder()
.setHttpAllowed(true)
.setDomain(domain)
.addPathHandler("/assets/") { path ->
val inputStream = assets.open(path)
WebResourceResponse(guessMimeType(path), null, inputStream)
}
.addPathHandler("/web/") { path: String ->
val inputStream = this.javaClass.classLoader!!.getResourceAsStream("web/$path")
WebResourceResponse(guessMimeType(path), null, inputStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.webkit.CookieManager
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.result.contract.ActivityResultContracts
Expand All @@ -38,18 +40,18 @@ import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.button.MaterialButton
import com.google.android.material.slider.Slider
import com.google.android.material.textview.MaterialTextView
import com.ichi2.anki.CollectionHelper
import com.ichi2.anki.Flag
import com.ichi2.anki.NoteEditor
import com.ichi2.anki.R
import com.ichi2.anki.SingleFragmentActivity
import com.ichi2.anki.getViewerAssetLoader
import com.ichi2.anki.pages.AnkiServer.Companion.LOCALHOST
import com.ichi2.anki.previewer.PreviewerViewModel.Companion.stdHtml
import com.ichi2.themes.Themes
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import java.io.File

class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener {
private lateinit var viewModel: PreviewerViewModel
Expand Down Expand Up @@ -77,17 +79,24 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val selectedCardIds = requireArguments().getLongArray(CARD_IDS_EXTRA)!!
val currentIndex = requireArguments().getInt(CURRENT_INDEX_EXTRA, 0)
val mediaDir = File(CollectionHelper.getCurrentAnkiDroidDirectory(requireContext()), "collection.media").path

viewModel = ViewModelProvider(
requireActivity(),
PreviewerViewModel.factory(mediaDir, selectedCardIds, currentIndex)
PreviewerViewModel.factory(selectedCardIds, currentIndex)
)[PreviewerViewModel::class.java]

val assetLoader = requireContext().getViewerAssetLoader(LOCALHOST)
val webView = view.findViewById<WebView>(R.id.webview)
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
with(webView) {
webViewClient = object : WebViewClient() {
override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest
): WebResourceResponse? {
return assetLoader.shouldInterceptRequest(request.url)
}

override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
viewModel.loadCurrentCard()
Expand All @@ -103,7 +112,7 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener {
domStorageEnabled = true
}
loadDataWithBaseURL(
viewModel.serverBaseUrl(),
"http://$LOCALHOST/",
stdHtml(requireContext(), Themes.currentTheme.isNightMode),
"text/html",
null,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import kotlinx.serialization.json.Json
import org.intellij.lang.annotations.Language
import timber.log.Timber

class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArray, firstIndex: Int) : ViewModel() {
class PreviewerViewModel(private val selectedCardIds: LongArray, firstIndex: Int) : ViewModel() {
val eval = MutableSharedFlow<String>()
val onError = MutableSharedFlow<String>()
val currentIndex = MutableStateFlow(firstIndex)
Expand All @@ -51,8 +51,6 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra

private var showingAnswer = false

// TODO maybe move the server to a Service and move it out of here?
private val server = PreviewerServer(mediaDir).also { it.start() }
private lateinit var currentCard: Card

fun toggleBacksideOnly() {
Expand Down Expand Up @@ -82,8 +80,6 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
}
}

fun serverBaseUrl() = server.baseUrl()

fun cardId() = currentCard.id

/**
Expand Down Expand Up @@ -175,10 +171,10 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
}

companion object {
fun factory(mediaDir: String, selectedCardIds: LongArray, currentIndex: Int): ViewModelProvider.Factory {
fun factory(selectedCardIds: LongArray, currentIndex: Int): ViewModelProvider.Factory {
return viewModelFactory {
initializer {
PreviewerViewModel(mediaDir, selectedCardIds, currentIndex)
PreviewerViewModel(selectedCardIds, currentIndex)
}
}
}
Expand Down Expand Up @@ -221,8 +217,8 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
<html class="$docClass" dir="$languageDirectionality" data-bs-theme="$baseTheme">
<head>
<title>AnkiDroid</title>
<link rel="stylesheet" type="text/css" href="/assets/web/root-vars.css">
<link rel="stylesheet" type="text/css" href="/assets/web/reviewer.css">
<link rel="stylesheet" type="text/css" href="/web/root-vars.css">
<link rel="stylesheet" type="text/css" href="/web/reviewer.css">
<style type="text/css">
.night-mode button { --canvas: #606060; --fg: #eee; }
$colors
Expand All @@ -232,9 +228,9 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
<div id="_mark" hidden>&#x2605;</div>
<div id="_flag" hidden>&#x2691;</div>
<div id="qa"></div>
<script src="/assets/jquery.min.js"></script>
<script src="/assets/mathjax/tex-chtml.js"></script>
<script src="/assets/web/reviewer.js"></script>
<script src="file:///android_asset/jquery.min.js"></script>
<script src="file:///android_asset/mathjax/tex-chtml.js"></script>
<script src="/web/reviewer.js"></script>
<script>bridgeCommand = function(){};</script>
</body>
</html>
Expand Down

0 comments on commit 830819b

Please sign in to comment.