Skip to content

Commit

Permalink
feat: use media directory as the Reviewer baseUrl
Browse files Browse the repository at this point in the history
that makes the baseUrl always the same, so `localStorage` works again

also loads media with the `file` scheme by default, which is faster
  • Loading branch information
BrayanDSO committed Apr 8, 2024
1 parent 03e081f commit ac3afec
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import com.ichi2.anki.model.CardStateFilter
import com.ichi2.anki.noteeditor.EditCardDestination
import com.ichi2.anki.noteeditor.toIntent
import com.ichi2.anki.pages.AnkiServer
import com.ichi2.anki.pages.AnkiServer.Companion.LOCALHOST
import com.ichi2.anki.pages.CongratsPage
import com.ichi2.anki.pages.PostRequestHandler
import com.ichi2.anki.preferences.sharedPrefs
Expand Down Expand Up @@ -234,9 +233,8 @@ abstract class AbstractFlashcardViewer :
@get:VisibleForTesting
var cardContent: String? = null
private set
private val baseUrl get() = server.baseUrl()
private val webviewDomain
get() = "$LOCALHOST:${server.listeningPort}"
private val baseUrl
get() = getMediaBaseUrl(CollectionHelper.getMediaDirectory(this).path)

private var viewerUrl: String? = null
private val fadeDuration = 300
Expand Down Expand Up @@ -995,7 +993,6 @@ abstract class AbstractFlashcardViewer :
}

protected open fun createWebView(): WebView {
val resourceHandler = ViewerResourceHandler(this, webviewDomain)
val webView: WebView = MyWebView(this).apply {
scrollBarStyle = View.SCROLLBARS_OUTSIDE_OVERLAY
with(settings) {
Expand All @@ -1013,7 +1010,7 @@ abstract class AbstractFlashcardViewer :
isScrollbarFadingEnabled = true
// Set transparent color to prevent flashing white when night mode enabled
setBackgroundColor(Color.argb(1, 0, 0, 0))
CardViewerWebClient(resourceHandler, this@AbstractFlashcardViewer).apply {
CardViewerWebClient(this@AbstractFlashcardViewer).apply {
webViewClient = this
this@AbstractFlashcardViewer.webViewClient = this
}
Expand Down Expand Up @@ -2238,7 +2235,6 @@ abstract class AbstractFlashcardViewer :
}

protected inner class CardViewerWebClient internal constructor(
private val loader: ViewerResourceHandler?,
private val onPageFinishedCallback: OnPageFinishedCallback? = null
) : WebViewClient() {
private var pageFinishedFired = true
Expand All @@ -2259,14 +2255,16 @@ abstract class AbstractFlashcardViewer :
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
pageRenderStopwatch.reset()
pageFinishedFired = false
val script = "globalThis.ankidroid = globalThis.ankidroid || {};" +
"ankidroid.postBaseUrl = `${server.baseUrl()}`"
view?.evaluateJavascript(script, null)
}

override fun shouldInterceptRequest(
view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
val url = request.url
loader!!.shouldInterceptRequest(request)?.let { return it }
if (url.toString().startsWith("file://")) {
url.path?.let { path -> migrationService?.migrateFileImmediately(File(path)) }
}
Expand Down
109 changes: 0 additions & 109 deletions AnkiDroid/src/main/java/com/ichi2/anki/ViewerResourceHandler.kt

This file was deleted.

0 comments on commit ac3afec

Please sign in to comment.