mirror of
https://github.com/AllanWang/Frost-for-Facebook.git
synced 2024-09-19 15:11:42 +02:00
Convert all dispatchers to use cases
This commit is contained in:
parent
7da9524758
commit
626f1302ca
@ -33,12 +33,9 @@ import com.google.common.flogger.FluentLogger
|
||||
import com.pitchedapps.frost.ext.WebTargetId
|
||||
import com.pitchedapps.frost.view.FrostWebView
|
||||
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.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.FrostWeb
|
||||
import com.pitchedapps.frost.webview.FrostWebScoped
|
||||
@ -60,13 +57,9 @@ internal constructor(
|
||||
private val store: FrostWebStore,
|
||||
private val client: FrostWebViewClient,
|
||||
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
|
||||
*
|
||||
@ -98,7 +91,7 @@ internal constructor(
|
||||
val canGoBack by
|
||||
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) {
|
||||
fun storeFlow(action: suspend Flow<ContentState>.() -> Unit) = launch {
|
||||
@ -109,7 +102,7 @@ internal constructor(
|
||||
mapNotNull { it.transientState.targetUrl }
|
||||
.distinctUntilChanged()
|
||||
.collect { url ->
|
||||
store.dispatch(LoadUrlResponseAction(url))
|
||||
tabUseCases.responses.respondUrl(tabId, url)
|
||||
wv.loadUrl(url)
|
||||
}
|
||||
}
|
||||
@ -118,7 +111,7 @@ internal constructor(
|
||||
.distinctUntilChanged()
|
||||
.filter { it != 0 }
|
||||
.collect { steps ->
|
||||
store.dispatch(WebStepResponseAction(steps))
|
||||
tabUseCases.responses.respondSteps(tabId, steps)
|
||||
if (wv.canGoBackOrForward(steps)) {
|
||||
wv.goBackOrForward(steps)
|
||||
} else {
|
||||
|
@ -26,6 +26,7 @@ import com.pitchedapps.frost.ext.GeckoContextId
|
||||
import com.pitchedapps.frost.ext.idData
|
||||
import com.pitchedapps.frost.ext.toContextId
|
||||
import com.pitchedapps.frost.web.state.FrostWebStore
|
||||
import com.pitchedapps.frost.web.usecases.UseCases
|
||||
import com.pitchedapps.frost.webview.FrostWebComposer
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
@ -40,6 +41,7 @@ internal constructor(
|
||||
@ApplicationContext context: Context,
|
||||
val components: FrostComponents,
|
||||
val store: FrostWebStore,
|
||||
val useCases: UseCases,
|
||||
val frostWebComposer: FrostWebComposer,
|
||||
) : ViewModel() {
|
||||
|
||||
|
@ -46,7 +46,6 @@ import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.pitchedapps.frost.compose.webview.FrostWebCompose
|
||||
import com.pitchedapps.frost.ext.WebTargetId
|
||||
import com.pitchedapps.frost.web.state.FrostWebStore
|
||||
import com.pitchedapps.frost.web.state.TabListAction.SelectHomeTab
|
||||
import com.pitchedapps.frost.webview.FrostWebComposer
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
@ -66,7 +65,7 @@ fun MainScreenWebView(modifier: Modifier = Modifier, homeTabs: List<MainTabItem>
|
||||
MainBottomBar(
|
||||
selectedTab = selectedHomeTab,
|
||||
items = homeTabs,
|
||||
onSelect = { vm.store.dispatch(SelectHomeTab(it)) },
|
||||
onSelect = { vm.useCases.homeTabs.selectHomeTab(it) },
|
||||
)
|
||||
},
|
||||
) { paddingValues ->
|
||||
|
@ -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) {
|
||||
store.dispatch(
|
||||
TabAction(
|
||||
|
@ -23,22 +23,19 @@ import android.webkit.WebView
|
||||
import com.google.common.flogger.FluentLogger
|
||||
import com.pitchedapps.frost.ext.WebTargetId
|
||||
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.usecases.TabUseCases
|
||||
import javax.inject.Inject
|
||||
|
||||
/** The default chrome client */
|
||||
@FrostWebScoped
|
||||
class FrostChromeClient
|
||||
@Inject
|
||||
internal constructor(@FrostWeb private val tabId: WebTargetId, private val store: FrostWebStore) :
|
||||
WebChromeClient() {
|
||||
|
||||
private fun FrostWebStore.dispatch(action: TabAction.Action) {
|
||||
dispatch(TabAction(tabId = tabId, action = action))
|
||||
}
|
||||
internal constructor(
|
||||
@FrostWeb private val tabId: WebTargetId,
|
||||
private val store: FrostWebStore,
|
||||
private val tabUseCases: TabUseCases,
|
||||
) : WebChromeClient() {
|
||||
|
||||
override fun getDefaultVideoPoster(): Bitmap? =
|
||||
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) {
|
||||
super.onReceivedTitle(view, title)
|
||||
if (title.startsWith("http")) return
|
||||
store.dispatch(UpdateTitleAction(title))
|
||||
tabUseCases.updateTitle(tabId, title)
|
||||
}
|
||||
|
||||
override fun onProgressChanged(view: WebView, newProgress: Int) {
|
||||
super.onProgressChanged(view, newProgress)
|
||||
// TODO remove?
|
||||
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(
|
||||
|
@ -30,10 +30,7 @@ import com.pitchedapps.frost.facebook.isExplicitIntent
|
||||
import com.pitchedapps.frost.facebook.isFacebookUrl
|
||||
import com.pitchedapps.frost.web.FrostWebHelper
|
||||
import com.pitchedapps.frost.web.state.FrostWebStore
|
||||
import com.pitchedapps.frost.web.state.TabAction
|
||||
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.web.usecases.TabUseCases
|
||||
import com.pitchedapps.frost.webview.injection.FrostJsInjectors
|
||||
import java.io.ByteArrayInputStream
|
||||
import javax.inject.Inject
|
||||
@ -72,23 +69,19 @@ internal constructor(
|
||||
private val store: FrostWebStore,
|
||||
override val webHelper: FrostWebHelper,
|
||||
private val frostJsInjectors: FrostJsInjectors,
|
||||
private val tabUseCases: TabUseCases,
|
||||
) : BaseWebViewClient() {
|
||||
|
||||
private fun FrostWebStore.dispatch(action: TabAction.Action) {
|
||||
dispatch(TabAction(tabId = tabId, action = action))
|
||||
}
|
||||
|
||||
/** True if current url supports refresh. See [doUpdateVisitedHistory] for updates */
|
||||
internal var urlSupportsRefresh: Boolean = true
|
||||
|
||||
override fun doUpdateVisitedHistory(view: WebView, url: String, isReload: Boolean) {
|
||||
super.doUpdateVisitedHistory(view, url, isReload)
|
||||
urlSupportsRefresh = webHelper.allowUrlSwipeToRefresh(url)
|
||||
store.dispatch(
|
||||
UpdateNavigationAction(
|
||||
canGoBack = view.canGoBack(),
|
||||
canGoForward = view.canGoForward(),
|
||||
),
|
||||
tabUseCases.updateNavigation(
|
||||
tabId,
|
||||
canGoBack = view.canGoBack(),
|
||||
canGoForward = view.canGoForward(),
|
||||
)
|
||||
// web.parent.swipeAllowedByPage = urlSupportsRefresh
|
||||
// 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?) {
|
||||
super.onPageStarted(view, url, favicon)
|
||||
store.dispatch(UpdateProgressAction(0))
|
||||
store.dispatch(UpdateTitleAction(null))
|
||||
tabUseCases.updateProgress(tabId, 0)
|
||||
tabUseCases.updateTitle(tabId, null)
|
||||
// v { "loading $url ${web.settings.userAgentString}" }
|
||||
// refresh.offer(true)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user