diff --git a/webview/src/androidMain/kotlin/com/multiplatform/webview/web/AccompanistWebView.kt b/webview/src/androidMain/kotlin/com/multiplatform/webview/web/AccompanistWebView.kt index 8d6f1ec9..b7b33105 100644 --- a/webview/src/androidMain/kotlin/com/multiplatform/webview/web/AccompanistWebView.kt +++ b/webview/src/androidMain/kotlin/com/multiplatform/webview/web/AccompanistWebView.kt @@ -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 @@ -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) { @@ -232,7 +234,7 @@ fun AccompanistWebView( } }.also { val androidWebView = AndroidWebView(it, scope, webViewJsBridge) - state.webView = androidWebView + state.webView.value = androidWebView webViewJsBridge?.webView = androidWebView } }, diff --git a/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebView.kt b/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebView.kt index aa7cfade..7292b8b9 100644 --- a/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebView.kt +++ b/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebView.kt @@ -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 @@ -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) { diff --git a/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebViewState.kt b/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebViewState.kt index 32a08937..a059dd5f 100644 --- a/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebViewState.kt +++ b/webview/src/commonMain/kotlin/com/multiplatform/webview/web/WebViewState.kt @@ -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 @@ -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(null) + val webView = MutableStateFlow(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, @@ -159,15 +160,15 @@ val WebStateSaver: Saver = 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 = { diff --git a/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebEngineExt.kt b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebEngineExt.kt index caecdc37..16a22193 100644 --- a/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebEngineExt.kt +++ b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebEngineExt.kt @@ -93,6 +93,7 @@ internal fun CefBrowser.addDisplayHandler(state: WebViewState) { internal fun CefBrowser.addLoadListener( state: WebViewState, + desktopWebView: DesktopWebView?, navigator: WebViewNavigator, ) { this.client.addLoadHandler( @@ -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 } } diff --git a/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebView.desktop.kt b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebView.desktop.kt index dfc982f4..4742a570 100644 --- a/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebView.desktop.kt +++ b/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebView.desktop.kt @@ -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 diff --git a/webview/src/iosMain/kotlin/com/multiplatform/webview/web/WebView.ios.kt b/webview/src/iosMain/kotlin/com/multiplatform/webview/web/WebView.ios.kt index 82947c17..eb11dd6d 100644 --- a/webview/src/iosMain/kotlin/com/multiplatform/webview/web/WebView.ios.kt +++ b/webview/src/iosMain/kotlin/com/multiplatform/webview/web/WebView.ios.kt @@ -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, )