Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/246 improve navigator #247

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import android.widget.FrameLayout
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -149,7 +151,7 @@ fun AccompanistWebView(
chromeClient: AccompanistWebChromeClient = remember { AccompanistWebChromeClient() },
factory: ((Context) -> WebView)? = null,
) {
val webView = state.webView
val webView by state.webView.collectAsState()
val scope = rememberCoroutineScope()

BackHandler(captureBackPresses && navigator.canGoBack) {
Expand Down Expand Up @@ -232,7 +234,7 @@ fun AccompanistWebView(
}
}.also {
val androidWebView = AndroidWebView(it, scope, webViewJsBridge)
state.webView = androidWebView
state.webView.value = androidWebView
webViewJsBridge?.webView = androidWebView
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.multiplatform.webview.web
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import com.multiplatform.webview.jsbridge.WebViewJsBridge
Expand Down Expand Up @@ -76,7 +78,7 @@ fun WebView(
onDispose: (NativeWebView) -> Unit = {},
factory: ((WebViewFactoryParam) -> NativeWebView)? = null,
) {
val webView = state.webView
val webView by state.webView.collectAsState()

webView?.let { wv ->
LaunchedEffect(wv, navigator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.multiplatform.webview.setting.WebSettings
import com.multiplatform.webview.util.KLogger
import com.multiplatform.webview.util.getPlatform
import com.multiplatform.webview.util.isZero
import kotlinx.coroutines.flow.MutableStateFlow

/**
* Created By Kevin Zou On 2023/9/5
Expand Down Expand Up @@ -73,13 +74,13 @@ class WebViewState(webContent: WebContent) {
* We need access to this in the state saver. An internal DisposableEffect or AndroidView
* onDestroy is called after the state saver and so can't be used.
*/
internal var webView by mutableStateOf<IWebView?>(null)
val webView = MutableStateFlow<IWebView?>(null)

/**
* The native web view instance. On Android, this is an instance of [android.webkit.WebView].
* On iOS, this is an instance of [WKWebView]. On desktop, this is an instance of [KCEFBrowser].
*/
val nativeWebView get() = webView?.webView ?: error("WebView is not initialized")
val nativeWebView get() = webView.value?.webView ?: error("WebView is not initialized")

/**
* The saved view state from when the view was destroyed last. To restore state,
Expand Down Expand Up @@ -159,15 +160,15 @@ val WebStateSaver: Saver<WebViewState, Any> =

mapSaver(
save = {
val viewState = it.webView?.saveState()
val viewState = it.webView.value?.saveState()
KLogger.info {
"WebViewStateSaver Save: ${it.pageTitle}, ${it.lastLoadedUrl}, ${it.webView?.scrollOffset()}, $viewState"
"WebViewStateSaver Save: ${it.pageTitle}, ${it.lastLoadedUrl}, ${it.webView.value?.scrollOffset()}, $viewState"
}
mapOf(
pageTitleKey to it.pageTitle,
lastLoadedUrlKey to it.lastLoadedUrl,
stateBundleKey to viewState,
scrollOffsetKey to it.webView?.scrollOffset(),
scrollOffsetKey to it.webView.value?.scrollOffset(),
)
},
restore = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ internal fun CefBrowser.addDisplayHandler(state: WebViewState) {

internal fun CefBrowser.addLoadListener(
state: WebViewState,
desktopWebView: DesktopWebView?,
navigator: WebViewNavigator,
) {
this.client.addLoadHandler(
Expand All @@ -113,7 +114,7 @@ internal fun CefBrowser.addLoadListener(
} else {
state.loadingState = LoadingState.Finished
if (url != null && url != lastLoadedUrl) {
state.webView?.injectJsBridge()
desktopWebView?.injectJsBridge()
lastLoadedUrl = url
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ fun DesktopWebView(
browser?.let {
SwingPanel(
factory = {
state.webView.value = desktopWebView
onCreated(it)
state.webView = desktopWebView

webViewJsBridge?.webView = desktopWebView
browser.apply {
addDisplayHandler(state)
addLoadListener(state, navigator)
addLoadListener(state, desktopWebView, navigator)
addRequestHandler(state, navigator)
}
browser.uiComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ fun IOSWebView(
}
}.also {
val iosWebView = IOSWebView(it, scope, webViewJsBridge)
state.webView = iosWebView
state.webView.value = iosWebView
webViewJsBridge?.webView = iosWebView
}
},
modifier = modifier,
onRelease = {
state.webView = null
state.webView.value = null
it.removeProgressObservers(
observer = observer,
)
Expand Down
Loading