Skip to content
Merged
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 @@ -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")
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class OnClickedJSMessageHandler(
id = nodeClicked.id,
coordinate = NodeCoordinate(
x = nodeClicked.x,
y = nodeClicked.x
y = nodeClicked.y
)
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>()
val command: SharedFlow<String> = _command.asSharedFlow()
private val _command = Channel<String>(Channel.UNLIMITED)
val command: Flow<String> = _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 {
Expand Down
2 changes: 1 addition & 1 deletion kotlin/jujubasvg/version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=1.4.0
VERSION=1.4.1
Loading