Skip to content

Commit

Permalink
implement enough to run basic hello world, make js print test
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Sep 20, 2024
1 parent 846b8b6 commit 89a09e6
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ android {
java.srcDirs("src/main/kotlin")
}
getByName("androidTest") {
assets.srcDirs("src/androidTest/assets")
java.srcDirs("src/androidTest/kotlin")
}
}
Expand Down
4 changes: 4 additions & 0 deletions android/app/src/androidTest/assets/print_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Pebble.addEventListener('ready', function() {
// PebbleKit JS is ready!
console.log('PebbleKit JS ready!');
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package io.rebble.cobble.shared.js
import android.content.Context
import io.rebble.libpebblecommon.metadata.pbw.appinfo.PbwAppInfo
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.StateFlow
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

actual object JsRunnerFactory: KoinComponent {
private val context: Context by inject()
actual fun createJsRunner(
scope: CoroutineScope,
connectedAddress: StateFlow<String?>,
appInfo: PbwAppInfo,
jsPath: String
): JsRunner = WebViewJsRunner(context, scope, appInfo, jsPath)
): JsRunner = WebViewJsRunner(context, connectedAddress, scope, appInfo, jsPath)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import io.rebble.cobble.shared.Logging
import io.rebble.libpebblecommon.metadata.pbw.appinfo.PbwAppInfo
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

class WebViewJsRunner(val context: Context, private val scope: CoroutineScope, appInfo: PbwAppInfo, jsPath: String): JsRunner(appInfo, jsPath) {
class WebViewJsRunner(val context: Context, private val connectedAddress: StateFlow<String?>, private val scope: CoroutineScope, appInfo: PbwAppInfo, jsPath: String): JsRunner(appInfo, jsPath) {

companion object {
const val API_NAMESPACE = "Pebble"
Expand Down Expand Up @@ -213,12 +217,31 @@ class WebViewJsRunner(val context: Context, private val scope: CoroutineScope, a
}

suspend fun loadAppJs(params: String?) {
check(webView != null) { "WebView not initialized" }
if (params == null) {
Logging.e("No params passed to loadAppJs")
return
}
webView?.let { webView ->
if (params == null) {
Logging.e("No params passed to loadAppJs")
return
}

val paramsDecoded = Uri.decode(params)
val paramsJson = Json.decodeFromString<Map<String, String>>(paramsDecoded)
val jsUrl = paramsJson["loadUrl"]
if (jsUrl.isNullOrBlank() || !jsUrl.endsWith(".js")) {
Logging.e("loadUrl passed to loadAppJs empty or invalid")
return
}


withContext(Dispatchers.Main) {
webView.loadUrl("javascript:loadScript('$jsUrl')")
}
} ?: error("WebView not initialized")
}

suspend fun signalReady() {
val readyDeviceIds = listOf(connectedAddress.value ?: return)
val readyJson = Json.encodeToString(readyDeviceIds)
withContext(Dispatchers.Main) {
webView?.loadUrl("javascript:signalReady(${Uri.encode(readyJson)})")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,31 @@ class WebViewPrivatePKJSInterface(private val jsRunner: WebViewJsRunner, private
jsRunner.loadAppJs(params)
}
}

@JavascriptInterface
fun privateFnLocalStorageWrite(key: String, value: String) {
TODO("Not yet implemented")
}

@JavascriptInterface
fun privateFnLocalStorageRead(key: String): String {
TODO("Not yet implemented")
}

@JavascriptInterface
fun privateFnLocalStorageReadAll(): String {
return "{}"
}

@JavascriptInterface
fun privateFnLocalStorageReadAll_AtPreregistrationStage(baseUriReference: String): String {
return privateFnLocalStorageReadAll()
}

@JavascriptInterface
fun signalAppScriptLoadedByBootstrap() {
scope.launch {
jsRunner.signalReady()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package io.rebble.cobble.shared.js

import io.rebble.libpebblecommon.metadata.pbw.appinfo.PbwAppInfo
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.StateFlow
import org.koin.core.component.KoinComponent

expect object JsRunnerFactory: KoinComponent {
fun createJsRunner(scope: CoroutineScope, appInfo: PbwAppInfo, jsPath: String): JsRunner
fun createJsRunner(scope: CoroutineScope, connectedAddress: StateFlow<String?>, appInfo: PbwAppInfo, jsPath: String): JsRunner
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package io.rebble.cobble.shared.js

import io.rebble.libpebblecommon.metadata.pbw.appinfo.PbwAppInfo
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.StateFlow
import org.koin.core.component.KoinComponent

actual object JsRunnerFactory: KoinComponent {
actual fun createJsRunner(scope: CoroutineScope, appInfo: PbwAppInfo, jsPath: String): JsRunner = TODO()
actual fun createJsRunner(scope: CoroutineScope, connectedAddress: StateFlow<String?>, appInfo: PbwAppInfo, jsPath: String): JsRunner = TODO()
}

0 comments on commit 89a09e6

Please sign in to comment.