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 @@ -29,6 +29,13 @@ interface ComponentManager {
*/
fun getComponent(id: ComponentId): Component?

/**
* Retrieves all components.
*
* @return A list of all components.
*/
fun getComponents(): List<Component>

/**
* Retrieves multiple components by their unique identifiers.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Component> = components.map { it.value }

override fun getComponents(ids: List<ComponentId>) =
ids.mapNotNull { getComponent(it) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ 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
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 {
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If evaluateJavascript must be called on Main thread, then switch contexts here. You can even use Main.immediate dispatcher

}
}

@SuppressLint("SetJavaScriptEnabled")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down