diff --git a/core/src/commonMain/kotlin/com/pega/constellation/sdk/kmp/core/api/ComponentManager.kt b/core/src/commonMain/kotlin/com/pega/constellation/sdk/kmp/core/api/ComponentManager.kt index 8f13f1c5..14819e97 100644 --- a/core/src/commonMain/kotlin/com/pega/constellation/sdk/kmp/core/api/ComponentManager.kt +++ b/core/src/commonMain/kotlin/com/pega/constellation/sdk/kmp/core/api/ComponentManager.kt @@ -29,6 +29,13 @@ interface ComponentManager { */ fun getComponent(id: ComponentId): Component? + /** + * Retrieves all components. + * + * @return A list of all components. + */ + fun getComponents(): List + /** * Retrieves multiple components by their unique identifiers. * diff --git a/core/src/commonMain/kotlin/com/pega/constellation/sdk/kmp/core/internal/ComponentManagerImpl.kt b/core/src/commonMain/kotlin/com/pega/constellation/sdk/kmp/core/internal/ComponentManagerImpl.kt index d3d4d7c8..696f5cf6 100644 --- a/core/src/commonMain/kotlin/com/pega/constellation/sdk/kmp/core/internal/ComponentManagerImpl.kt +++ b/core/src/commonMain/kotlin/com/pega/constellation/sdk/kmp/core/internal/ComponentManagerImpl.kt @@ -25,6 +25,8 @@ internal class ComponentManagerImpl( override fun getComponent(id: ComponentId) = components[id] ?: Log.w(TAG, "Cannot find component $id").let { null } + override fun getComponents(): List = components.map { it.value } + override fun getComponents(ids: List) = ids.mapNotNull { getComponent(it) } diff --git a/engine-webview/src/androidMain/kotlin/com/pega/constellation/sdk/kmp/engine/webview/android/AndroidWebViewEngine.kt b/engine-webview/src/androidMain/kotlin/com/pega/constellation/sdk/kmp/engine/webview/android/AndroidWebViewEngine.kt index 7b857822..e28376bb 100644 --- a/engine-webview/src/androidMain/kotlin/com/pega/constellation/sdk/kmp/engine/webview/android/AndroidWebViewEngine.kt +++ b/engine-webview/src/androidMain/kotlin/com/pega/constellation/sdk/kmp/engine/webview/android/AndroidWebViewEngine.kt @@ -36,6 +36,9 @@ import com.pega.constellation.sdk.kmp.engine.webview.android.internal.SdkWebView import com.pega.constellation.sdk.kmp.engine.webview.common.EngineConfiguration import com.pega.constellation.sdk.kmp.engine.webview.common.InternalError import com.pega.constellation.sdk.kmp.engine.webview.common.JsError +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import okhttp3.OkHttpClient import org.json.JSONObject @@ -43,6 +46,7 @@ import java.util.concurrent.TimeUnit class AndroidWebViewEngine( private val context: Context, + private val scope: CoroutineScope, private val okHttpClient: OkHttpClient, private val nonDxOkHttpClient: OkHttpClient = defaultHttpClient() ) : ConstellationSdkEngine { @@ -133,7 +137,9 @@ class AndroidWebViewEngine( put("eventData", JSONObject(event.eventData)) } val script = "window.sendEventToComponent('${id.id}', '$eventJson')" - webView.evaluateJavascript(script, null) + this.scope.launch(Dispatchers.Main.immediate) { + webView.evaluateJavascript(script, null) + } } @SuppressLint("SetJavaScriptEnabled") diff --git a/samples/android-cmp-app/src/androidInstrumentedTest/kotlin/com/pega/constellation/sdk/kmp/samples/androidcmpapp/test/ComposeTest.kt b/samples/android-cmp-app/src/androidInstrumentedTest/kotlin/com/pega/constellation/sdk/kmp/samples/androidcmpapp/test/ComposeTest.kt index e0c20f69..127ab282 100644 --- a/samples/android-cmp-app/src/androidInstrumentedTest/kotlin/com/pega/constellation/sdk/kmp/samples/androidcmpapp/test/ComposeTest.kt +++ b/samples/android-cmp-app/src/androidInstrumentedTest/kotlin/com/pega/constellation/sdk/kmp/samples/androidcmpapp/test/ComposeTest.kt @@ -38,11 +38,12 @@ abstract class ComposeTest( val mode: ComposeTestMode = MockServer, ) { private val scope = CoroutineScope(Dispatchers.Default) + private val webViewScope = CoroutineScope(Dispatchers.Main) private val instrumentation = InstrumentationRegistry.getInstrumentation() private val context = instrumentation.targetContext private val authManager = AuthManager(scope, FakeAuthFlowFactory(), FakeTokenStore(mode.token)) private val httpClient = buildHttpClient(authManager) - private val engine = AndroidWebViewEngine(context, httpClient, httpClient) + private val engine = AndroidWebViewEngine(context, webViewScope, httpClient, httpClient) @BeforeTest fun setUp() { diff --git a/samples/android-cmp-app/src/androidMain/kotlin/com/pega/constellation/sdk/kmp/samples/androidcmpapp/MediaCoActivity.kt b/samples/android-cmp-app/src/androidMain/kotlin/com/pega/constellation/sdk/kmp/samples/androidcmpapp/MediaCoActivity.kt index 367e453f..13ef0bd0 100644 --- a/samples/android-cmp-app/src/androidMain/kotlin/com/pega/constellation/sdk/kmp/samples/androidcmpapp/MediaCoActivity.kt +++ b/samples/android-cmp-app/src/androidMain/kotlin/com/pega/constellation/sdk/kmp/samples/androidcmpapp/MediaCoActivity.kt @@ -28,7 +28,11 @@ class MediaCoActivity : ComponentActivity() { val authFlowFactory = AndroidCodeAuthFlowFactory().also { it.registerActivity(this) } val authManager = createAuthManager(authFlowFactory) - val engine = AndroidWebViewEngine(this, buildHttpClient(authManager)) + val engine = AndroidWebViewEngine( + context = this, + scope = this.lifecycleScope, + okHttpClient = buildHttpClient(authManager) + ) Injector.init(authManager, engine) AppContext.init(this) diff --git a/samples/android-compose-app/src/main/java/com/pega/constellation/sdk/kmp/samples/androidcomposeapp/AndroidComposeActivity.kt b/samples/android-compose-app/src/main/java/com/pega/constellation/sdk/kmp/samples/androidcomposeapp/AndroidComposeActivity.kt index 623c96b8..a8ddd9d2 100644 --- a/samples/android-compose-app/src/main/java/com/pega/constellation/sdk/kmp/samples/androidcomposeapp/AndroidComposeActivity.kt +++ b/samples/android-compose-app/src/main/java/com/pega/constellation/sdk/kmp/samples/androidcomposeapp/AndroidComposeActivity.kt @@ -19,6 +19,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp +import androidx.lifecycle.lifecycleScope import com.pega.constellation.sdk.kmp.core.ConstellationSdk import com.pega.constellation.sdk.kmp.core.ConstellationSdk.State import com.pega.constellation.sdk.kmp.core.ConstellationSdk.State.Cancelled @@ -75,7 +76,11 @@ class AndroidComposeActivity : ComponentActivity() { debuggable = true ) val caseClassName = AndroidSDKConfig.PEGA_CASE_CLASS_NAME - val engine = AndroidWebViewEngine(this, buildHttpClient()) + val engine = AndroidWebViewEngine( + context = this, + scope = this.lifecycleScope, + okHttpClient = buildHttpClient() + ) sdk = ConstellationSdk.create(config, engine) sdk.createCase(caseClassName) } diff --git a/test/src/androidDeviceTest/kotlin/com/pega/constellation/sdk/kmp/test/ConstellationSdkTest.kt b/test/src/androidDeviceTest/kotlin/com/pega/constellation/sdk/kmp/test/ConstellationSdkTest.kt index d357a3aa..8447277e 100644 --- a/test/src/androidDeviceTest/kotlin/com/pega/constellation/sdk/kmp/test/ConstellationSdkTest.kt +++ b/test/src/androidDeviceTest/kotlin/com/pega/constellation/sdk/kmp/test/ConstellationSdkTest.kt @@ -5,17 +5,21 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import com.pega.constellation.sdk.kmp.engine.webview.android.AndroidWebViewEngine import com.pega.constellation.sdk.kmp.test.mock.MockHttpClient import com.pega.constellation.sdk.kmp.test.mock.PegaVersion +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import org.junit.runner.RunWith import kotlin.test.BeforeTest @RunWith(AndroidJUnit4::class) class ConstellationSdkTest : ConstellationSdkBaseTest() { private val appContext = InstrumentationRegistry.getInstrumentation().targetContext + private val scope = CoroutineScope(Dispatchers.Main) @BeforeTest fun setup() { engine = AndroidWebViewEngine( context = appContext, + scope = scope, okHttpClient = MockHttpClient(appContext, PegaVersion.v24_1_0), nonDxOkHttpClient = MockHttpClient(appContext, PegaVersion.v24_1_0) )