Change MessageWebView to use Koin to retrieve dependencies

This commit is contained in:
cketti 2023-04-17 18:49:47 +02:00
parent 30efd5051f
commit 423bb3e181
5 changed files with 32 additions and 14 deletions

View file

@ -50,6 +50,9 @@ class DependencyInjectionTest : AutoCloseKoinTest() {
withParameter<FolderIconProvider> {
ContextThemeWrapper(RuntimeEnvironment.getApplication(), R.style.Theme_K9_DayNight).theme
}
withParameters(clazz = Class.forName("com.fsck.k9.view.K9WebViewClient").kotlin) {
parametersOf(null, null)
}
}
}
}

View file

@ -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)
}
}
}

View file

@ -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() }
}

View file

@ -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)
}

View file

@ -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) }
}
}