summaryrefslogtreecommitdiffstats
path: root/mobile/android/android-components/components/compose/engine/src
diff options
context:
space:
mode:
Diffstat (limited to 'mobile/android/android-components/components/compose/engine/src')
-rw-r--r--mobile/android/android-components/components/compose/engine/src/main/AndroidManifest.xml4
-rw-r--r--mobile/android/android-components/components/compose/engine/src/main/java/mozilla/components/compose/engine/WebContent.kt62
-rw-r--r--mobile/android/android-components/components/compose/engine/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker2
-rw-r--r--mobile/android/android-components/components/compose/engine/src/test/resources/robolectric.properties1
4 files changed, 69 insertions, 0 deletions
diff --git a/mobile/android/android-components/components/compose/engine/src/main/AndroidManifest.xml b/mobile/android/android-components/components/compose/engine/src/main/AndroidManifest.xml
new file mode 100644
index 0000000000..e16cda1d34
--- /dev/null
+++ b/mobile/android/android-components/components/compose/engine/src/main/AndroidManifest.xml
@@ -0,0 +1,4 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+<manifest />
diff --git a/mobile/android/android-components/components/compose/engine/src/main/java/mozilla/components/compose/engine/WebContent.kt b/mobile/android/android-components/components/compose/engine/src/main/java/mozilla/components/compose/engine/WebContent.kt
new file mode 100644
index 0000000000..0d2bfb02f1
--- /dev/null
+++ b/mobile/android/android-components/components/compose/engine/src/main/java/mozilla/components/compose/engine/WebContent.kt
@@ -0,0 +1,62 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package mozilla.components.compose.engine
+
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.viewinterop.AndroidView
+import mozilla.components.browser.state.action.EngineAction
+import mozilla.components.browser.state.helper.Target
+import mozilla.components.browser.state.store.BrowserStore
+import mozilla.components.concept.engine.Engine
+import mozilla.components.concept.engine.EngineView
+
+/**
+ * Composes an [EngineView] obtained from the given [Engine] and renders the web content of the
+ * [target] from the [store] on it.
+ */
+@Composable
+fun WebContent(
+ engine: Engine,
+ store: BrowserStore,
+ target: Target,
+) {
+ val selectedTab = target.observeAsComposableStateFrom(
+ store = store,
+ observe = { tab ->
+ // Render if the tab itself changed or when the state of the linked engine session changes
+ arrayOf(
+ tab?.id,
+ tab?.engineState?.engineSession,
+ tab?.engineState?.crashed,
+ tab?.content?.firstContentfulPaint,
+ )
+ },
+ )
+
+ AndroidView(
+ modifier = Modifier.fillMaxSize(),
+ factory = { context -> engine.createView(context).asView() },
+ update = { view ->
+ val engineView = view as EngineView
+
+ val tab = selectedTab.value
+ if (tab == null) {
+ engineView.release()
+ } else {
+ val session = tab.engineState.engineSession
+ if (session == null) {
+ // This tab does not have an EngineSession that we can render yet. Let's dispatch an
+ // action to request creating one. Once one was created and linked to this session, this
+ // method will get invoked again.
+ store.dispatch(EngineAction.CreateEngineSessionAction(tab.id))
+ } else {
+ engineView.render(session)
+ }
+ }
+ },
+ )
+}
diff --git a/mobile/android/android-components/components/compose/engine/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/mobile/android/android-components/components/compose/engine/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
new file mode 100644
index 0000000000..cf1c399ea8
--- /dev/null
+++ b/mobile/android/android-components/components/compose/engine/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
@@ -0,0 +1,2 @@
+mock-maker-inline
+// This allows mocking final classes (classes are final by default in Kotlin)
diff --git a/mobile/android/android-components/components/compose/engine/src/test/resources/robolectric.properties b/mobile/android/android-components/components/compose/engine/src/test/resources/robolectric.properties
new file mode 100644
index 0000000000..932b01b9eb
--- /dev/null
+++ b/mobile/android/android-components/components/compose/engine/src/test/resources/robolectric.properties
@@ -0,0 +1 @@
+sdk=28