From c8b54fd10a08ed53eb7d21578a4fe990fe14e3bc Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 23 Feb 2020 16:06:45 -0800 Subject: Move prefs to service locator --- .../com/pitchedapps/frost/web/DebugWebView.kt | 22 +++++++++------ .../kotlin/com/pitchedapps/frost/web/FrostJSI.kt | 3 +- .../frost/web/FrostUrlOverlayValidator.kt | 2 +- .../pitchedapps/frost/web/FrostWebViewClients.kt | 33 +++++++++++++--------- .../com/pitchedapps/frost/web/LoginWebView.kt | 8 ++++-- 5 files changed, 42 insertions(+), 26 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/web') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/DebugWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/DebugWebView.kt index 3ab954c6..6013dbf6 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/DebugWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/DebugWebView.kt @@ -34,6 +34,8 @@ import com.pitchedapps.frost.utils.isFacebookUrl import java.io.File import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2018-01-05. @@ -44,8 +46,9 @@ class DebugWebView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 -) : WebView(context, attrs, defStyleAttr) { +) : WebView(context, attrs, defStyleAttr), KoinComponent { + private val prefs: Prefs by inject() var onPageFinished: (String?) -> Unit = {} init { @@ -93,7 +96,7 @@ class DebugWebView @JvmOverloads constructor( private fun injectBackgroundColor() { setBackgroundColor( - if (url.isFacebookUrl) Prefs.bgColor.withAlpha(255) + if (url.isFacebookUrl) prefs.bgColor.withAlpha(255) else Color.WHITE ) } @@ -104,15 +107,16 @@ class DebugWebView @JvmOverloads constructor( if (url.isFacebookUrl) view.jsInject( // CssHider.CORE, - CssHider.COMPOSER.maybe(!Prefs.showComposer), - CssHider.STORIES.maybe(!Prefs.showStories), - CssHider.PEOPLE_YOU_MAY_KNOW.maybe(!Prefs.showSuggestedFriends), - CssHider.SUGGESTED_GROUPS.maybe(!Prefs.showSuggestedGroups), - Prefs.themeInjector, + CssHider.COMPOSER.maybe(!prefs.showComposer), + CssHider.STORIES.maybe(!prefs.showStories), + CssHider.PEOPLE_YOU_MAY_KNOW.maybe(!prefs.showSuggestedFriends), + CssHider.SUGGESTED_GROUPS.maybe(!prefs.showSuggestedGroups), + prefs.themeInjector, CssHider.NON_RECENT.maybe( (url?.contains("?sk=h_chr") ?: false) && - Prefs.aggressiveRecents - ) + prefs.aggressiveRecents + ), + prefs= prefs ) } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt index c8e551a3..449f0ab0 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt @@ -41,6 +41,7 @@ import kotlinx.coroutines.launch */ class FrostJSI(val web: FrostWebView) { + private val prefs: Prefs = web.prefs private val context: Context = web.context private val activity: MainActivity? = context as? MainActivity private val header: SendChannel? = activity?.headerBadgeChannel @@ -57,7 +58,7 @@ class FrostJSI(val web: FrostWebView) { @JavascriptInterface fun loadVideo(url: String?, isGif: Boolean): Boolean = - if (url != null && Prefs.enablePip) { + if (url != null && prefs.enablePip) { web.post { (context as? VideoViewHolder)?.showVideo(url, isGif) ?: L.e { "Could not load video; contract not implemented" } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt index 9e5e6c63..de364df6 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt @@ -76,7 +76,7 @@ fun FrostWebView.requestWebOverlay(url: String): Boolean { L.d { "Forbid overlay switch" } return false } - if (!Prefs.overlayEnabled) return false + if (!prefs.overlayEnabled) return false if (context is WebOverlayActivityBase) { val shouldUseDesktop = url.isFacebookUrl // already overlay; manage user agent diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt index 2cd4c5db..5e87e73a 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt @@ -42,6 +42,8 @@ import com.pitchedapps.frost.utils.launchImageActivity import com.pitchedapps.frost.utils.resolveActivityForUri import com.pitchedapps.frost.views.FrostWebView import kotlinx.coroutines.channels.SendChannel +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-05-31. @@ -67,6 +69,7 @@ open class BaseWebViewClient : WebViewClient() { */ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() { + private val prefs: Prefs get() = web.prefs private val refresh: SendChannel = web.parent.refreshChannel private val isMain = web.parent.baseEnum != null /** @@ -102,7 +105,7 @@ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() { web.setBackgroundColor( when { isMain -> Color.TRANSPARENT - web.url.isFacebookUrl -> Prefs.bgColor.withAlpha(255) + web.url.isFacebookUrl -> prefs.bgColor.withAlpha(255) else -> Color.WHITE } ) @@ -115,21 +118,22 @@ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() { view.jsInject( // CssHider.CORE, CssHider.HEADER, - CssHider.COMPOSER.maybe(!Prefs.showComposer), - CssHider.STORIES.maybe(!Prefs.showStories), - CssHider.PEOPLE_YOU_MAY_KNOW.maybe(!Prefs.showSuggestedFriends), - CssHider.SUGGESTED_GROUPS.maybe(!Prefs.showSuggestedGroups), - Prefs.themeInjector, + CssHider.COMPOSER.maybe(!prefs.showComposer), + CssHider.STORIES.maybe(!prefs.showStories), + CssHider.PEOPLE_YOU_MAY_KNOW.maybe(!prefs.showSuggestedFriends), + CssHider.SUGGESTED_GROUPS.maybe(!prefs.showSuggestedGroups), + prefs.themeInjector, CssHider.NON_RECENT.maybe( (web.url?.contains("?sk=h_chr") ?: false) && - Prefs.aggressiveRecents + prefs.aggressiveRecents ), JsAssets.DOCUMENT_WATCHER, JsAssets.HORIZONTAL_SCROLLING, JsAssets.CLICK_A, - CssHider.ADS.maybe(!Prefs.showFacebookAds), + CssHider.ADS.maybe(!prefs.showFacebookAds), JsAssets.CONTEXT_A, - JsAssets.MEDIA + JsAssets.MEDIA, + prefs = prefs ) } else { refresh.offer(false) @@ -147,7 +151,7 @@ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() { } internal open fun onPageFinishedActions(url: String) { - if (url.startsWith("${FbItem.MESSAGES.url}/read/") && Prefs.messageScrollToBottom) { + if (url.startsWith("${FbItem.MESSAGES.url}/read/") && prefs.messageScrollToBottom) { web.pageDown(true) } injectAndFinish() @@ -160,7 +164,8 @@ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() { web.jsInject( JsActions.LOGIN_CHECK, JsAssets.TEXTAREA_LISTENER, - JsAssets.HEADER_BADGES.maybe(isMain) + JsAssets.HEADER_BADGES.maybe(isMain), + prefs = prefs ) } @@ -207,7 +212,7 @@ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() { if (url.isImageUrl) { return launchImage(url.formattedFbUrl) } - if (Prefs.linksInDefaultApp && view.context.resolveActivityForUri(request.url)) { + if (prefs.linksInDefaultApp && view.context.resolveActivityForUri(request.url)) { return true } // Convert desktop urls to mobile ones @@ -229,12 +234,14 @@ private const val EMIT_FINISH = 0 */ class FrostWebViewClientMenu(web: FrostWebView) : FrostWebViewClient(web) { + private val prefs: Prefs get() = web.prefs + override fun onPageFinished(view: WebView, url: String?) { super.onPageFinished(view, url) if (url == null) { return } - jsInject(JsAssets.MENU) + jsInject(JsAssets.MENU, prefs = prefs) } override fun emit(flag: Int) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt index 33a4875f..0299ea77 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt @@ -42,6 +42,8 @@ import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.isFacebookUrl import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.coroutineScope +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-05-29. @@ -50,8 +52,9 @@ class LoginWebView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 -) : WebView(context, attrs, defStyleAttr) { +) : WebView(context, attrs, defStyleAttr), KoinComponent { + private val prefs: Prefs by inject() private val completable: CompletableDeferred = CompletableDeferred() private lateinit var progressCallback: (Int) -> Unit @@ -101,7 +104,8 @@ class LoginWebView @JvmOverloads constructor( if (url.isFacebookUrl) view.jsInject( CssHider.CORE, - Prefs.themeInjector + prefs.themeInjector, + prefs = prefs ) } -- cgit v1.2.3