All Downloads are FREE. Search and download functionalities are using the official Maven repository.

iosMain.web.WKNavigationDelegate.kt Maven / Gradle / Ivy

There is a newer version: 1.9.40-alpha04
Show newest version
package web

import co.touchlab.kermit.Logger
import platform.Foundation.NSError
import platform.WebKit.WKNavigation
import platform.WebKit.WKNavigationDelegateProtocol
import platform.WebKit.WKWebView
import platform.darwin.NSObject

/**
 * Created By Kevin Zou On 2023/9/13
 */
@Suppress("CONFLICTING_OVERLOADS")
class WKNavigationDelegate(
    private val state: WebViewState,
    private val navigator: WebViewNavigator
) : NSObject(), WKNavigationDelegateProtocol {
    override fun webView(
        webView: WKWebView,
        didStartProvisionalNavigation: WKNavigation?
    ) {
        state.loadingState = LoadingState.Loading(0f)
        state.lastLoadedUrl = webView.URL.toString()
        state.errorsForCurrentRequest.clear()
        Logger.i {
            "didStartProvisionalNavigation"
        }
    }

    override fun webView(
        webView: WKWebView,
        didCommitNavigation: WKNavigation?
    ) {
        state.loadingState =
            LoadingState.Loading(webView.estimatedProgress.toFloat())
        Logger.i { "didCommitNavigation" }
    }

    override fun webView(
        webView: WKWebView,
        didFinishNavigation: WKNavigation?
    ) {
        state.pageTitle = webView.title
        state.lastLoadedUrl = webView.URL.toString()
        state.loadingState = LoadingState.Finished
        navigator.canGoBack = webView.canGoBack
        navigator.canGoForward = webView.canGoForward
        Logger.i { "didFinishNavigation" }
    }

    override fun webView(
        webView: WKWebView,
        didFailProvisionalNavigation: WKNavigation?,
        withError: NSError
    ) {
        state.errorsForCurrentRequest.add(
            WebViewError(
                withError.code.toInt(),
                withError.localizedDescription
            )
        )
        Logger.i {
            "didFailNavigation"
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy