diff --git a/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/JujubaSVG.kt b/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/JujubaSVG.kt index ff46f49a..4b79a215 100644 --- a/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/JujubaSVG.kt +++ b/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/JujubaSVG.kt @@ -18,12 +18,10 @@ import com.github.codandotv.jujubasvg.core.ext.fileTextLines import com.github.codandotv.jujubasvg.model.NodeInfo import com.github.codandotv.jujubasvg.resources.Res import com.multiplatform.webview.jsbridge.WebViewJsBridge +import com.multiplatform.webview.web.WebView import com.multiplatform.webview.web.rememberWebViewStateWithHTMLData import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async -import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.onEach -import kotlinx.coroutines.flow.onStart import kotlin.text.StringBuilder @Suppress("LongMethod") @@ -45,14 +43,6 @@ fun JujubaSVG( ) } - var readyToReceiveFirstCommand by remember { - mutableStateOf(false) - } - - var backgroundColorApplied by remember { - mutableStateOf(false) - } - LaunchedEffect(Unit) { val jsCodeDeferred = async(Dispatchers.Default) { Res.readBytes("files/base_js.js").fileTextContent() @@ -95,35 +85,25 @@ fun JujubaSVG( } } - com.multiplatform.webview.web.WebView( + WebView( state = webViewState, modifier = modifier, webViewJsBridge = jsBridge, ) - LaunchedEffect(Unit) { - commander.command - .onStart { - readyToReceiveFirstCommand = true - } - .onEach { jsCommand -> + LaunchedEffect(webViewState.isLoading) { + if(webViewState.isLoading.not()) { + commander.execute( + Command.UpdateRootBackgroundColor(color = backgroundColor) + ) + commander.command.collect {jsCommand -> jsBridge.webView?.evaluateJavaScript(jsCommand) { Logger.d { "WebviewComponent: $jsCommand -> result: $it" } } } - .launchIn(this) - } - - LaunchedEffect(readyToReceiveFirstCommand, webViewState.isLoading) { - val isReady = readyToReceiveFirstCommand && webViewState.isLoading.not() - if (isReady && backgroundColorApplied.not()) { - commander.execute( - Command.UpdateRootBackgroundColor(color = backgroundColor) - ) - backgroundColorApplied = true } } diff --git a/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/bridge/OnClickedJSMessageHandler.kt b/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/bridge/OnClickedJSMessageHandler.kt index 277730f5..9ee8d621 100644 --- a/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/bridge/OnClickedJSMessageHandler.kt +++ b/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/bridge/OnClickedJSMessageHandler.kt @@ -28,7 +28,7 @@ internal class OnClickedJSMessageHandler( id = nodeClicked.id, coordinate = NodeCoordinate( x = nodeClicked.x, - y = nodeClicked.x + y = nodeClicked.y ) ) ) diff --git a/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/commander/JujubaCommander.kt b/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/commander/JujubaCommander.kt index 33dbdf00..c2d3592a 100644 --- a/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/commander/JujubaCommander.kt +++ b/kotlin/jujubasvg/src/commonMain/kotlin/com/github/codandotv/jujubasvg/core/commander/JujubaCommander.kt @@ -1,21 +1,21 @@ package com.github.codandotv.jujubasvg.core.commander import com.github.codandotv.jujubasvg.core.ext.toHex -import kotlinx.coroutines.flow.MutableSharedFlow -import kotlinx.coroutines.flow.SharedFlow -import kotlinx.coroutines.flow.asSharedFlow +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.receiveAsFlow class JujubaCommander { - private val _command = MutableSharedFlow() - val command: SharedFlow = _command.asSharedFlow() + private val _command = Channel(Channel.UNLIMITED) + val command: Flow = _command.receiveAsFlow() suspend fun execute(vararg command: Command) { val commandJS = command.map { convertToJSCode(it) }.reduce { acc, s -> acc.plus("\n").plus(s) } - _command.emit(commandJS) + _command.send(commandJS) } private fun convertToJSCode(command: Command): String { diff --git a/kotlin/jujubasvg/version.properties b/kotlin/jujubasvg/version.properties index c22d1734..0f8420a4 100644 --- a/kotlin/jujubasvg/version.properties +++ b/kotlin/jujubasvg/version.properties @@ -1 +1 @@ -VERSION=1.4.0 \ No newline at end of file +VERSION=1.4.1 \ No newline at end of file