SharedPlatformWebView.ios.kt
composeApp/src/iosMain/kotlin/eu/friedcutlet/yokan/ui/screens/webview/SharedPlatformWebView.ios.kt
1package eu.friedcutlet.yokan.ui.screens.webview23import com.multiplatform.webview.cookie.Cookie4import com.multiplatform.webview.web.NativeWebView5import com.multiplatform.webview.web.WebViewFactoryParam6import eu.friedcutlet.yokan.domain.ProfileId7import eu.friedcutlet.yokan.services.webview.allCookies8import eu.friedcutlet.yokan.services.webview.matchesHost9import eu.friedcutlet.yokan.services.webview.profileDataStore10import eu.friedcutlet.yokan.services.webview.toSetCookieHeader11import kotlin.coroutines.resume12import kotlinx.cinterop.ExperimentalForeignApi13import kotlinx.cinterop.readValue14import kotlinx.coroutines.Dispatchers15import kotlinx.coroutines.suspendCancellableCoroutine16import kotlinx.coroutines.withContext17import platform.CoreGraphics.CGRectZero18import platform.Foundation.NSHTTPCookie19import platform.Foundation.NSHTTPCookieDomain20import platform.Foundation.NSHTTPCookieName21import platform.Foundation.NSHTTPCookiePath22import platform.Foundation.NSHTTPCookieValue23import platform.Foundation.NSURL24import platform.WebKit.WKWebView2526@OptIn(ExperimentalForeignApi::class)27internal actual fun profileWebViewFactory(profileId: ProfileId): (WebViewFactoryParam) -> NativeWebView = { param ->28 param.config.websiteDataStore = profileDataStore(profileId)29 WKWebView(frame = CGRectZero.readValue(), configuration = param.config)30}3132internal actual suspend fun setProfileWebViewCookie(profileId: ProfileId, url: String, cookie: Cookie) {33 val nsCookie = NSHTTPCookie.cookieWithProperties(34 mapOf<Any?, Any>(35 NSHTTPCookieName to cookie.name,36 NSHTTPCookieValue to cookie.value,37 NSHTTPCookieDomain to cookie.domain.orEmpty(),38 NSHTTPCookiePath to (cookie.path ?: "/"),39 ),40 ) ?: return41 withContext(Dispatchers.Main) {42 suspendCancellableCoroutine<Unit> { cont ->43 profileDataStore(profileId).httpCookieStore.setCookie(nsCookie) { if (cont.isActive) cont.resume(Unit) }44 }45 }46}4748internal actual suspend fun profileWebViewSetCookies(profileId: ProfileId, url: String): List<String> {49 val host = NSURL.URLWithString(url)?.host?.lowercase() ?: return emptyList()50 val store = withContext(Dispatchers.Main) { profileDataStore(profileId) }51 return store.allCookies().orEmpty().filter { it.matchesHost(host) }.map { it.toSetCookieHeader() }52}53