From 3b4b164f524575c9dc10955d329710ba0706c9ed Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 25 Sep 2021 16:54:09 -0700 Subject: Create frost web component for jsi --- .../kotlin/com/pitchedapps/frost/web/FrostJSI.kt | 41 ++++++++++++---------- 1 file changed, 22 insertions(+), 19 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt') 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 9a853dae..3ead78f4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt @@ -16,12 +16,11 @@ */ package com.pitchedapps.frost.web -import android.content.Context +import android.app.Activity import android.webkit.JavascriptInterface import ca.allanwang.kau.utils.ctxCoroutine import com.pitchedapps.frost.activities.MainActivity import com.pitchedapps.frost.activities.WebOverlayActivityBase -import com.pitchedapps.frost.contracts.MainActivityContract import com.pitchedapps.frost.contracts.VideoViewHolder import com.pitchedapps.frost.db.CookieEntity import com.pitchedapps.frost.facebook.FbCookie @@ -35,19 +34,23 @@ import com.pitchedapps.frost.utils.showWebContextMenu import com.pitchedapps.frost.views.FrostWebView import kotlinx.coroutines.channels.SendChannel import kotlinx.coroutines.launch +import javax.inject.Inject /** * Created by Allan Wang on 2017-06-01. */ -class FrostJSI(val web: FrostWebView) { - - private val fbCookie: FbCookie get() = web.fbCookie - private val prefs: Prefs get() = web.prefs - private val context: Context = web.context - private val activity: MainActivity? = context as? MainActivity - private val header: SendChannel? = activity?.headerBadgeChannel +class FrostJSI @Inject internal constructor( + val web: FrostWebView, + private val activity: Activity, + private val fbCookie: FbCookie, + private val prefs: Prefs +) { + + private val mainActivity: MainActivity? = activity as? MainActivity + private val webActivity: WebOverlayActivityBase? = activity as? WebOverlayActivityBase + private val header: SendChannel? = mainActivity?.headerBadgeChannel private val refresh: SendChannel = web.parent.refreshChannel - private val cookies: List = activity?.cookies() ?: arrayListOf() + private val cookies: List = activity.cookies() /** * Attempts to load the url in an overlay @@ -61,7 +64,7 @@ class FrostJSI(val web: FrostWebView) { fun loadVideo(url: String?, isGif: Boolean): Boolean = if (url != null && prefs.enablePip) { web.post { - (context as? VideoViewHolder)?.showVideo(url, isGif) + (activity as? VideoViewHolder)?.showVideo(url, isGif) ?: L.e { "Could not load video; contract not implemented" } } true @@ -82,7 +85,7 @@ class FrostJSI(val web: FrostWebView) { fun contextMenu(url: String?, text: String?) { // url will be formatted through webcontext web.post { - context.showWebContextMenu( + activity.showWebContextMenu( WebContext(url.takeIf { it.isIndependent }, text), fbCookie, prefs @@ -96,7 +99,7 @@ class FrostJSI(val web: FrostWebView) { */ @JavascriptInterface fun longClick(start: Boolean) { - activity?.contentBinding?.viewpager?.enableSwipe = !start + mainActivity?.contentBinding?.viewpager?.enableSwipe = !start if (web.frostWebClient.urlSupportsRefresh) { web.parent.swipeDisabledByAction = start } @@ -113,15 +116,15 @@ class FrostJSI(val web: FrostWebView) { web.parent.swipeDisabledByAction = disable if (disable) { // locked onto an input field; ensure content is visible - (context as? MainActivityContract)?.collapseAppBar() + mainActivity?.collapseAppBar() } } @JavascriptInterface fun loadLogin() { L.d { "Sign up button found; load login" } - context.ctxCoroutine.launch { - fbCookie.logout(context, deleteCookie = false) + activity.ctxCoroutine.launch { + fbCookie.logout(activity, deleteCookie = false) } } @@ -130,7 +133,7 @@ class FrostJSI(val web: FrostWebView) { */ @JavascriptInterface fun loadImage(imageUrl: String, text: String?) { - context.launchImageActivity(imageUrl, text) + activity.launchImageActivity(imageUrl, text) } @JavascriptInterface @@ -159,8 +162,8 @@ class FrostJSI(val web: FrostWebView) { @JavascriptInterface fun allowHorizontalScrolling(enable: Boolean) { - activity?.contentBinding?.viewpager?.enableSwipe = enable - (context as? WebOverlayActivityBase)?.swipeBack?.disallowIntercept = !enable + mainActivity?.contentBinding?.viewpager?.enableSwipe = enable + webActivity?.swipeBack?.disallowIntercept = !enable } private var isScrolling = false -- cgit v1.2.3