1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-09 12:32:30 +01:00

Convert all dispatchers to use cases

This commit is contained in:
Allan Wang 2023-06-21 00:10:12 -07:00
parent 7da9524758
commit 626f1302ca
No known key found for this signature in database
GPG Key ID: C93E3F9C679D7A56
6 changed files with 34 additions and 40 deletions

View File

@ -33,12 +33,9 @@ import com.google.common.flogger.FluentLogger
import com.pitchedapps.frost.ext.WebTargetId import com.pitchedapps.frost.ext.WebTargetId
import com.pitchedapps.frost.view.FrostWebView import com.pitchedapps.frost.view.FrostWebView
import com.pitchedapps.frost.web.state.FrostWebStore import com.pitchedapps.frost.web.state.FrostWebStore
import com.pitchedapps.frost.web.state.TabAction
import com.pitchedapps.frost.web.state.TabAction.ResponseAction.LoadUrlResponseAction
import com.pitchedapps.frost.web.state.TabAction.ResponseAction.WebStepResponseAction
import com.pitchedapps.frost.web.state.get import com.pitchedapps.frost.web.state.get
import com.pitchedapps.frost.web.state.state.ContentState import com.pitchedapps.frost.web.state.state.ContentState
import com.pitchedapps.frost.web.usecases.UseCases import com.pitchedapps.frost.web.usecases.TabUseCases
import com.pitchedapps.frost.webview.FrostChromeClient import com.pitchedapps.frost.webview.FrostChromeClient
import com.pitchedapps.frost.webview.FrostWeb import com.pitchedapps.frost.webview.FrostWeb
import com.pitchedapps.frost.webview.FrostWebScoped import com.pitchedapps.frost.webview.FrostWebScoped
@ -60,13 +57,9 @@ internal constructor(
private val store: FrostWebStore, private val store: FrostWebStore,
private val client: FrostWebViewClient, private val client: FrostWebViewClient,
private val chromeClient: FrostChromeClient, private val chromeClient: FrostChromeClient,
private val useCases: UseCases, private val tabUseCases: TabUseCases,
) { ) {
private fun FrostWebStore.dispatch(action: TabAction.Action) {
dispatch(TabAction(tabId = tabId, action = action))
}
/** /**
* Webview implementation in compose * Webview implementation in compose
* *
@ -98,7 +91,7 @@ internal constructor(
val canGoBack by val canGoBack by
store.observeAsState(initialValue = false) { it[tabId]?.content?.canGoBack == true } store.observeAsState(initialValue = false) { it[tabId]?.content?.canGoBack == true }
BackHandler(captureBackPresses && canGoBack) { useCases.tabs.requests.goBack(tabId) } BackHandler(captureBackPresses && canGoBack) { tabUseCases.requests.goBack(tabId) }
LaunchedEffect(wv, store) { LaunchedEffect(wv, store) {
fun storeFlow(action: suspend Flow<ContentState>.() -> Unit) = launch { fun storeFlow(action: suspend Flow<ContentState>.() -> Unit) = launch {
@ -109,7 +102,7 @@ internal constructor(
mapNotNull { it.transientState.targetUrl } mapNotNull { it.transientState.targetUrl }
.distinctUntilChanged() .distinctUntilChanged()
.collect { url -> .collect { url ->
store.dispatch(LoadUrlResponseAction(url)) tabUseCases.responses.respondUrl(tabId, url)
wv.loadUrl(url) wv.loadUrl(url)
} }
} }
@ -118,7 +111,7 @@ internal constructor(
.distinctUntilChanged() .distinctUntilChanged()
.filter { it != 0 } .filter { it != 0 }
.collect { steps -> .collect { steps ->
store.dispatch(WebStepResponseAction(steps)) tabUseCases.responses.respondSteps(tabId, steps)
if (wv.canGoBackOrForward(steps)) { if (wv.canGoBackOrForward(steps)) {
wv.goBackOrForward(steps) wv.goBackOrForward(steps)
} else { } else {

View File

@ -26,6 +26,7 @@ import com.pitchedapps.frost.ext.GeckoContextId
import com.pitchedapps.frost.ext.idData import com.pitchedapps.frost.ext.idData
import com.pitchedapps.frost.ext.toContextId import com.pitchedapps.frost.ext.toContextId
import com.pitchedapps.frost.web.state.FrostWebStore import com.pitchedapps.frost.web.state.FrostWebStore
import com.pitchedapps.frost.web.usecases.UseCases
import com.pitchedapps.frost.webview.FrostWebComposer import com.pitchedapps.frost.webview.FrostWebComposer
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
@ -40,6 +41,7 @@ internal constructor(
@ApplicationContext context: Context, @ApplicationContext context: Context,
val components: FrostComponents, val components: FrostComponents,
val store: FrostWebStore, val store: FrostWebStore,
val useCases: UseCases,
val frostWebComposer: FrostWebComposer, val frostWebComposer: FrostWebComposer,
) : ViewModel() { ) : ViewModel() {

View File

@ -46,7 +46,6 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.pitchedapps.frost.compose.webview.FrostWebCompose import com.pitchedapps.frost.compose.webview.FrostWebCompose
import com.pitchedapps.frost.ext.WebTargetId import com.pitchedapps.frost.ext.WebTargetId
import com.pitchedapps.frost.web.state.FrostWebStore import com.pitchedapps.frost.web.state.FrostWebStore
import com.pitchedapps.frost.web.state.TabListAction.SelectHomeTab
import com.pitchedapps.frost.webview.FrostWebComposer import com.pitchedapps.frost.webview.FrostWebComposer
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -66,7 +65,7 @@ fun MainScreenWebView(modifier: Modifier = Modifier, homeTabs: List<MainTabItem>
MainBottomBar( MainBottomBar(
selectedTab = selectedHomeTab, selectedTab = selectedHomeTab,
items = homeTabs, items = homeTabs,
onSelect = { vm.store.dispatch(SelectHomeTab(it)) }, onSelect = { vm.useCases.homeTabs.selectHomeTab(it) },
) )
}, },
) { paddingValues -> ) { paddingValues ->

View File

@ -41,6 +41,15 @@ internal constructor(
) )
} }
fun updateProgress(tabId: WebTargetId, progress: Int) {
store.dispatch(
TabAction(
tabId = tabId,
action = TabAction.ContentAction.UpdateProgressAction(progress),
),
)
}
fun updateNavigation(tabId: WebTargetId, canGoBack: Boolean, canGoForward: Boolean) { fun updateNavigation(tabId: WebTargetId, canGoBack: Boolean, canGoForward: Boolean) {
store.dispatch( store.dispatch(
TabAction( TabAction(

View File

@ -23,22 +23,19 @@ import android.webkit.WebView
import com.google.common.flogger.FluentLogger import com.google.common.flogger.FluentLogger
import com.pitchedapps.frost.ext.WebTargetId import com.pitchedapps.frost.ext.WebTargetId
import com.pitchedapps.frost.web.state.FrostWebStore import com.pitchedapps.frost.web.state.FrostWebStore
import com.pitchedapps.frost.web.state.TabAction
import com.pitchedapps.frost.web.state.TabAction.ContentAction.UpdateProgressAction
import com.pitchedapps.frost.web.state.TabAction.ContentAction.UpdateTitleAction
import com.pitchedapps.frost.web.state.get import com.pitchedapps.frost.web.state.get
import com.pitchedapps.frost.web.usecases.TabUseCases
import javax.inject.Inject import javax.inject.Inject
/** The default chrome client */ /** The default chrome client */
@FrostWebScoped @FrostWebScoped
class FrostChromeClient class FrostChromeClient
@Inject @Inject
internal constructor(@FrostWeb private val tabId: WebTargetId, private val store: FrostWebStore) : internal constructor(
WebChromeClient() { @FrostWeb private val tabId: WebTargetId,
private val store: FrostWebStore,
private fun FrostWebStore.dispatch(action: TabAction.Action) { private val tabUseCases: TabUseCases,
dispatch(TabAction(tabId = tabId, action = action)) ) : WebChromeClient() {
}
override fun getDefaultVideoPoster(): Bitmap? = override fun getDefaultVideoPoster(): Bitmap? =
super.getDefaultVideoPoster() ?: Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888) super.getDefaultVideoPoster() ?: Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888)
@ -53,14 +50,15 @@ internal constructor(@FrostWeb private val tabId: WebTargetId, private val store
override fun onReceivedTitle(view: WebView, title: String) { override fun onReceivedTitle(view: WebView, title: String) {
super.onReceivedTitle(view, title) super.onReceivedTitle(view, title)
if (title.startsWith("http")) return if (title.startsWith("http")) return
store.dispatch(UpdateTitleAction(title)) tabUseCases.updateTitle(tabId, title)
} }
override fun onProgressChanged(view: WebView, newProgress: Int) { override fun onProgressChanged(view: WebView, newProgress: Int) {
super.onProgressChanged(view, newProgress) super.onProgressChanged(view, newProgress)
// TODO remove? // TODO remove?
if (store.state[tabId]?.content?.progress == 100) return if (store.state[tabId]?.content?.progress == 100) return
store.dispatch(UpdateProgressAction(newProgress)) // view progress is for current page
tabUseCases.updateProgress(tabId, view.progress)
} }
// override fun onShowFileChooser( // override fun onShowFileChooser(

View File

@ -30,10 +30,7 @@ import com.pitchedapps.frost.facebook.isExplicitIntent
import com.pitchedapps.frost.facebook.isFacebookUrl import com.pitchedapps.frost.facebook.isFacebookUrl
import com.pitchedapps.frost.web.FrostWebHelper import com.pitchedapps.frost.web.FrostWebHelper
import com.pitchedapps.frost.web.state.FrostWebStore import com.pitchedapps.frost.web.state.FrostWebStore
import com.pitchedapps.frost.web.state.TabAction import com.pitchedapps.frost.web.usecases.TabUseCases
import com.pitchedapps.frost.web.state.TabAction.ContentAction.UpdateNavigationAction
import com.pitchedapps.frost.web.state.TabAction.ContentAction.UpdateProgressAction
import com.pitchedapps.frost.web.state.TabAction.ContentAction.UpdateTitleAction
import com.pitchedapps.frost.webview.injection.FrostJsInjectors import com.pitchedapps.frost.webview.injection.FrostJsInjectors
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import javax.inject.Inject import javax.inject.Inject
@ -72,23 +69,19 @@ internal constructor(
private val store: FrostWebStore, private val store: FrostWebStore,
override val webHelper: FrostWebHelper, override val webHelper: FrostWebHelper,
private val frostJsInjectors: FrostJsInjectors, private val frostJsInjectors: FrostJsInjectors,
private val tabUseCases: TabUseCases,
) : BaseWebViewClient() { ) : BaseWebViewClient() {
private fun FrostWebStore.dispatch(action: TabAction.Action) {
dispatch(TabAction(tabId = tabId, action = action))
}
/** True if current url supports refresh. See [doUpdateVisitedHistory] for updates */ /** True if current url supports refresh. See [doUpdateVisitedHistory] for updates */
internal var urlSupportsRefresh: Boolean = true internal var urlSupportsRefresh: Boolean = true
override fun doUpdateVisitedHistory(view: WebView, url: String, isReload: Boolean) { override fun doUpdateVisitedHistory(view: WebView, url: String, isReload: Boolean) {
super.doUpdateVisitedHistory(view, url, isReload) super.doUpdateVisitedHistory(view, url, isReload)
urlSupportsRefresh = webHelper.allowUrlSwipeToRefresh(url) urlSupportsRefresh = webHelper.allowUrlSwipeToRefresh(url)
store.dispatch( tabUseCases.updateNavigation(
UpdateNavigationAction( tabId,
canGoBack = view.canGoBack(), canGoBack = view.canGoBack(),
canGoForward = view.canGoForward(), canGoForward = view.canGoForward(),
),
) )
// web.parent.swipeAllowedByPage = urlSupportsRefresh // web.parent.swipeAllowedByPage = urlSupportsRefresh
// view.jsInject(JsAssets.AUTO_RESIZE_TEXTAREA.maybe(prefs.autoExpandTextBox), prefs = prefs) // view.jsInject(JsAssets.AUTO_RESIZE_TEXTAREA.maybe(prefs.autoExpandTextBox), prefs = prefs)
@ -132,8 +125,8 @@ internal constructor(
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) { override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon) super.onPageStarted(view, url, favicon)
store.dispatch(UpdateProgressAction(0)) tabUseCases.updateProgress(tabId, 0)
store.dispatch(UpdateTitleAction(null)) tabUseCases.updateTitle(tabId, null)
// v { "loading $url ${web.settings.userAgentString}" } // v { "loading $url ${web.settings.userAgentString}" }
// refresh.offer(true) // refresh.offer(true)
} }