From 423bb3e18171bd1c22aab36c2dd529fb8d60d3b9 Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 17 Apr 2023 18:49:47 +0200 Subject: [PATCH] Change `MessageWebView` to use Koin to retrieve dependencies --- .../java/com/fsck/k9/DependencyInjectionTest.kt | 3 +++ .../java/com/fsck/k9/view/K9WebViewClient.kt | 10 +--------- .../src/main/java/com/fsck/k9/view/KoinModule.kt | 6 ++++++ .../main/java/com/fsck/k9/view/MessageWebView.kt | 11 ++++++----- .../com/fsck/k9/view/WebViewClientFactory.kt | 16 ++++++++++++++++ 5 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/view/WebViewClientFactory.kt diff --git a/app/k9mail/src/test/java/com/fsck/k9/DependencyInjectionTest.kt b/app/k9mail/src/test/java/com/fsck/k9/DependencyInjectionTest.kt index 1c73b1dd3..6c0da20e6 100644 --- a/app/k9mail/src/test/java/com/fsck/k9/DependencyInjectionTest.kt +++ b/app/k9mail/src/test/java/com/fsck/k9/DependencyInjectionTest.kt @@ -50,6 +50,9 @@ class DependencyInjectionTest : AutoCloseKoinTest() { withParameter { ContextThemeWrapper(RuntimeEnvironment.getApplication(), R.style.Theme_K9_DayNight).theme } + withParameters(clazz = Class.forName("com.fsck.k9.view.K9WebViewClient").kotlin) { + parametersOf(null, null) + } } } } diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/view/K9WebViewClient.kt b/app/ui/legacy/src/main/java/com/fsck/k9/view/K9WebViewClient.kt index 7a19f2090..782638290 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/view/K9WebViewClient.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/view/K9WebViewClient.kt @@ -22,8 +22,8 @@ import com.fsck.k9.view.MessageWebView.OnPageFinishedListener */ internal class K9WebViewClient( private val attachmentResolver: AttachmentResolver?, + private val onPageFinishedListener: OnPageFinishedListener?, ) : WebViewClient() { - private var onPageFinishedListener: OnPageFinishedListener? = null @Deprecated("Deprecated in parent class") override fun shouldOverrideUrlLoading(webView: WebView, url: String): Boolean { @@ -100,10 +100,6 @@ internal class K9WebViewClient( responseHeaders = mapOf("Cache-Control" to "no-store") } - fun setOnPageFinishedListener(onPageFinishedListener: OnPageFinishedListener?) { - this.onPageFinishedListener = onPageFinishedListener - } - override fun onPageFinished(view: WebView, url: String) { super.onPageFinished(view, url) @@ -115,9 +111,5 @@ internal class K9WebViewClient( private val RESULT_DO_NOT_INTERCEPT: WebResourceResponse? = null private val RESULT_DUMMY_RESPONSE = WebResourceResponse(null, null, null) - - fun newInstance(attachmentResolver: AttachmentResolver?): K9WebViewClient { - return K9WebViewClient(attachmentResolver) - } } } diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/view/KoinModule.kt b/app/ui/legacy/src/main/java/com/fsck/k9/view/KoinModule.kt index 269d220fd..b27ad145a 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/view/KoinModule.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/view/KoinModule.kt @@ -1,8 +1,10 @@ package com.fsck.k9.view import com.fsck.k9.helper.ReplyToParser +import com.fsck.k9.mailstore.AttachmentResolver import com.fsck.k9.message.ReplyActionStrategy import com.fsck.k9.ui.helper.RelativeDateTimeFormatter +import com.fsck.k9.view.MessageWebView.OnPageFinishedListener import org.koin.dsl.module val viewModule = module { @@ -10,4 +12,8 @@ val viewModule = module { factory { RelativeDateTimeFormatter(context = get(), clock = get()) } factory { ReplyToParser() } factory { ReplyActionStrategy(replyRoParser = get()) } + factory { (attachmentResolver: AttachmentResolver?, onPageFinishedListener: OnPageFinishedListener?) -> + K9WebViewClient(attachmentResolver, onPageFinishedListener) + } + factory { WebViewClientFactory() } } diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/view/MessageWebView.kt b/app/ui/legacy/src/main/java/com/fsck/k9/view/MessageWebView.kt index dc4b238cc..47efd8a3f 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/view/MessageWebView.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/view/MessageWebView.kt @@ -7,13 +7,17 @@ import android.webkit.WebSettings.LayoutAlgorithm import android.webkit.WebSettings.RenderPriority import android.webkit.WebView import com.fsck.k9.mailstore.AttachmentResolver +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject import timber.log.Timber -class MessageWebView : WebView { +class MessageWebView : WebView, KoinComponent { constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) + private val webViewClientFactory: WebViewClientFactory by inject() + fun blockNetworkData(shouldBlockNetworkData: Boolean) { // Images with content: URIs will not be blocked, nor will network images that are already in the WebView cache. try { @@ -81,10 +85,7 @@ class MessageWebView : WebView { attachmentResolver: AttachmentResolver?, onPageFinishedListener: OnPageFinishedListener?, ) { - val webViewClient = K9WebViewClient.newInstance(attachmentResolver) - if (onPageFinishedListener != null) { - webViewClient.setOnPageFinishedListener(onPageFinishedListener) - } + val webViewClient = webViewClientFactory.create(attachmentResolver, onPageFinishedListener) setWebViewClient(webViewClient) } diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/view/WebViewClientFactory.kt b/app/ui/legacy/src/main/java/com/fsck/k9/view/WebViewClientFactory.kt new file mode 100644 index 000000000..8f367be31 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/view/WebViewClientFactory.kt @@ -0,0 +1,16 @@ +package com.fsck.k9.view + +import android.webkit.WebViewClient +import com.fsck.k9.mailstore.AttachmentResolver +import com.fsck.k9.view.MessageWebView.OnPageFinishedListener +import org.koin.core.parameter.parametersOf +import org.koin.java.KoinJavaComponent.getKoin + +internal class WebViewClientFactory { + fun create( + attachmentResolver: AttachmentResolver?, + onPageFinishedListener: OnPageFinishedListener?, + ): WebViewClient { + return getKoin().get(K9WebViewClient::class) { parametersOf(attachmentResolver, onPageFinishedListener) } + } +}