Skip to content

Commit bd76ac5

Browse files
committed
refactor: Android Demo app uses Jetpack Compose
1 parent d4ed94a commit bd76ac5

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

android/app/build.gradle.kts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
alias(libs.plugins.android.application)
33
alias(libs.plugins.jetbrains.kotlin.android)
4+
alias(libs.plugins.jetbrains.kotlin.compose)
45
}
56

67
android {
@@ -33,6 +34,9 @@ android {
3334
kotlinOptions {
3435
jvmTarget = "1.8"
3536
}
37+
buildFeatures {
38+
compose = true
39+
}
3640
}
3741

3842
dependencies {
@@ -46,6 +50,14 @@ dependencies {
4650
implementation(libs.androidx.recyclerview)
4751
implementation(libs.wordpress.rs.android)
4852
implementation(project(":Gutenberg"))
53+
54+
// Compose
55+
implementation(platform(libs.androidx.compose.bom))
56+
implementation(libs.androidx.compose.ui)
57+
implementation(libs.androidx.compose.material3)
58+
implementation(libs.androidx.compose.ui.tooling.preview)
59+
implementation(libs.androidx.activity.compose)
60+
4961
testImplementation(libs.junit)
5062
androidTestImplementation(libs.androidx.junit)
5163
androidTestImplementation(libs.androidx.espresso.core)

android/app/src/main/java/com/example/gutenbergkit/EditorActivity.kt

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,24 @@ package com.example.gutenbergkit
33
import android.os.Bundle
44
import android.webkit.WebView
55
import android.content.pm.ApplicationInfo
6+
import androidx.activity.ComponentActivity
7+
import androidx.activity.compose.setContent
68
import androidx.activity.enableEdgeToEdge
7-
import androidx.appcompat.app.AppCompatActivity
8-
import androidx.core.view.ViewCompat
9-
import androidx.core.view.WindowInsetsCompat
9+
import androidx.compose.foundation.layout.fillMaxSize
10+
import androidx.compose.foundation.layout.padding
11+
import androidx.compose.material3.Scaffold
12+
import androidx.compose.runtime.Composable
13+
import androidx.compose.ui.Modifier
14+
import androidx.compose.ui.viewinterop.AndroidView
1015
import org.wordpress.gutenberg.EditorConfiguration
1116
import org.wordpress.gutenberg.GutenbergView
1217

13-
class EditorActivity : AppCompatActivity() {
18+
class EditorActivity : ComponentActivity() {
1419
override fun onCreate(savedInstanceState: Bundle?) {
1520
super.onCreate(savedInstanceState)
1621
enableEdgeToEdge()
17-
setContentView(R.layout.activity_editor)
1822

19-
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.editor)) { v, insets ->
20-
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
21-
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
22-
insets
23-
}
24-
25-
if (0 != (applicationInfo.flags and android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE)) {
23+
if (0 != (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE)) {
2624
WebView.setWebContentsDebuggingEnabled(true)
2725
}
2826

@@ -38,7 +36,32 @@ class EditorActivity : AppCompatActivity() {
3836
intent.getParcelableExtra<EditorConfiguration>(MainActivity.EXTRA_CONFIGURATION)
3937
} ?: EditorConfiguration.builder().build()
4038

41-
val gbView = findViewById<GutenbergView>(R.id.gutenbergView)
42-
gbView.start(configuration)
39+
setContent {
40+
EditorScreen(
41+
configuration = configuration,
42+
onClose = { finish() }
43+
)
44+
}
45+
}
46+
}
47+
48+
@Composable
49+
fun EditorScreen(
50+
configuration: EditorConfiguration,
51+
onClose: () -> Unit
52+
) {
53+
Scaffold(
54+
modifier = Modifier.fillMaxSize()
55+
) { innerPadding ->
56+
AndroidView(
57+
factory = { context ->
58+
GutenbergView(context).apply {
59+
start(configuration)
60+
}
61+
},
62+
modifier = Modifier
63+
.fillMaxSize()
64+
.padding(innerPadding)
65+
)
4366
}
4467
}

android/gradle/libs.versions.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ robolectric = "4.14.1"
1616
kotlinx-coroutines = '1.10.2'
1717
androidx-recyclerview = '1.3.2'
1818
wordpress-rs = 'trunk-503f1da9e067677d1517d09f926b1d038dfa58a1'
19+
composeBom = "2024.12.01"
20+
activityCompose = "1.9.3"
1921

2022
[libraries]
2123
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -34,8 +36,14 @@ robolectric = { group = "org.robolectric", name = "robolectric", version.ref = "
3436
kotlinx-coroutines-android = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" }
3537
androidx-recyclerview = { group = "androidx.recyclerview", name = "recyclerview", version.ref = "androidx-recyclerview" }
3638
wordpress-rs-android = { group = "rs.wordpress.api", name = "android", version.ref = "wordpress-rs" }
39+
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
40+
androidx-compose-ui = { group = "androidx.compose.ui", name = "ui" }
41+
androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3" }
42+
androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
43+
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
3744

3845
[plugins]
3946
android-application = { id = "com.android.application", version.ref = "agp" }
4047
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
48+
jetbrains-kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
4149
android-library = { id = "com.android.library", version.ref = "agp" }

0 commit comments

Comments
 (0)