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 authored and mikehardy committed Apr 20, 2024
1 parent 5f6ce47 commit e55cae2
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 @@ -65,7 +65,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 @@ -232,9 +231,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 @@ -994,7 +992,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 @@ -1012,7 +1009,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 @@ -2235,7 +2232,6 @@ abstract class AbstractFlashcardViewer :
}

inner class CardViewerWebClient internal constructor(
private val loader: ViewerResourceHandler?,
private val onPageFinishedCallback: OnPageFinishedCallback? = null
) : WebViewClient(), JavascriptEvaluator {
private var pageFinishedFired = true
Expand All @@ -2256,14 +2252,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 e55cae2

Please sign in to comment.