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 22, 2023
1 parent df45db1 commit eff889f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 92 deletions.
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,11 +40,12 @@ 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
Expand Down Expand Up @@ -76,17 +79,23 @@ 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 = CollectionHelper.getMediaDirectory(requireContext()).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 @@ -102,7 +111,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 @@ -234,7 +230,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
<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="/web/reviewer.js"></script>
<script>bridgeCommand = function(){};</script>
</body>
</html>
Expand Down

0 comments on commit eff889f

Please sign in to comment.