Skip to content

Commit b7f0b0c

Browse files
committed
Integrate Dependency Loading
1 parent bcc69a8 commit b7f0b0c

File tree

6 files changed

+848
-43
lines changed

6 files changed

+848
-43
lines changed

android/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<activity
3737
android:name=".EditorActivity"
3838
android:exported="false" />
39+
<activity
40+
android:name=".SitePreparationActivity"
41+
android:exported="false" />
3942
</application>
4043
<uses-permission android:name="android.permission.INTERNET"/>
4144
</manifest>

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import com.example.gutenbergkit.ui.theme.AppTheme
4141
import kotlinx.coroutines.launch
4242
import org.wordpress.gutenberg.model.EditorConfiguration
4343
import org.wordpress.gutenberg.GutenbergView
44+
import org.wordpress.gutenberg.model.EditorDependencies
4445

4546
class EditorActivity : ComponentActivity() {
4647
private var gutenbergView: GutenbergView? = null
@@ -75,12 +76,16 @@ class EditorActivity : ComponentActivity() {
7576
} else {
7677
@Suppress("DEPRECATION")
7778
intent.getParcelableExtra<EditorConfiguration>(MainActivity.EXTRA_CONFIGURATION)
78-
} ?: EditorConfiguration.builder().build()
79+
} ?: EditorConfiguration.bundled()
80+
81+
// Get dependencies from the holder (set by SitePreparationActivity)
82+
val dependencies = EditorDependenciesHolder.getAndClear()
7983

8084
setContent {
8185
AppTheme {
8286
EditorScreen(
8387
configuration = configuration,
88+
dependencies = dependencies,
8489
onClose = { finish() },
8590
onGutenbergViewCreated = { view ->
8691
gutenbergView = view
@@ -102,6 +107,7 @@ class EditorActivity : ComponentActivity() {
102107
@Composable
103108
fun EditorScreen(
104109
configuration: EditorConfiguration,
110+
dependencies: EditorDependencies? = null,
105111
onClose: () -> Unit,
106112
onGutenbergViewCreated: (GutenbergView) -> Unit = {}
107113
) {
@@ -255,7 +261,7 @@ fun EditorScreen(
255261
}
256262
}
257263
})
258-
start(configuration)
264+
start(configuration, dependencies)
259265
onGutenbergViewCreated(this)
260266
}
261267
},
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.example.gutenbergkit
2+
3+
import org.wordpress.gutenberg.model.EditorDependencies
4+
5+
/**
6+
* A simple holder for passing EditorDependencies between activities.
7+
*
8+
* Since EditorDependencies contains complex nested objects that aren't easily
9+
* Parcelable, we use this singleton to temporarily hold the dependencies when
10+
* navigating from SitePreparationActivity to EditorActivity.
11+
*
12+
* Usage:
13+
* 1. Set dependencies before starting EditorActivity
14+
* 2. Retrieve and clear in EditorActivity.onCreate()
15+
*/
16+
object EditorDependenciesHolder {
17+
private var dependencies: EditorDependencies? = null
18+
19+
/**
20+
* Stores dependencies to be retrieved by the next activity.
21+
*/
22+
fun set(dependencies: EditorDependencies?) {
23+
this.dependencies = dependencies
24+
}
25+
26+
/**
27+
* Retrieves and clears the stored dependencies.
28+
*
29+
* This method clears the holder after retrieval to prevent memory leaks
30+
* and ensure dependencies are only used once.
31+
*/
32+
fun getAndClear(): EditorDependencies? {
33+
val result = dependencies
34+
dependencies = null
35+
return result
36+
}
37+
}

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

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Bundle
55
import androidx.activity.ComponentActivity
66
import androidx.activity.compose.setContent
77
import androidx.activity.enableEdgeToEdge
8+
import androidx.compose.foundation.ExperimentalFoundationApi
89
import androidx.compose.foundation.combinedClickable
910
import androidx.compose.foundation.layout.Arrangement
1011
import androidx.compose.foundation.layout.Column
@@ -46,6 +47,7 @@ import androidx.lifecycle.lifecycleScope
4647
import kotlinx.coroutines.launch
4748
import org.wordpress.gutenberg.BuildConfig
4849
import org.wordpress.gutenberg.model.EditorConfiguration
50+
import uniffi.wp_api.PostType
4951

5052
class MainActivity : ComponentActivity(), AuthenticationManager.AuthenticationCallback {
5153
private val configurations = mutableStateListOf<ConfigurationItem>()
@@ -78,7 +80,7 @@ class MainActivity : ComponentActivity(), AuthenticationManager.AuthenticationCa
7880
configurations = configurations,
7981
onConfigurationClick = { config ->
8082
when (config) {
81-
is ConfigurationItem.BundledEditor -> launchEditor(createBundledConfiguration())
83+
is ConfigurationItem.BundledEditor -> launchSitePreparation(config)
8284
is ConfigurationItem.ConfiguredEditor -> loadConfiguredEditor(config)
8385
}
8486
},
@@ -105,58 +107,36 @@ class MainActivity : ComponentActivity(), AuthenticationManager.AuthenticationCa
105107
}
106108

107109
private fun createBundledConfiguration(): EditorConfiguration =
108-
createCommonConfigurationBuilder()
110+
createCommonConfigurationBuilder(
111+
siteUrl = "https://example.com",
112+
siteApiRoot = "https://example.com",
113+
postType = "post"
114+
)
109115
.setPlugins(false)
110-
.setSiteURL("")
111-
.setSiteApiRoot("")
112116
.setSiteApiNamespace(arrayOf())
113117
.setNamespaceExcludedPaths(arrayOf())
114118
.setAuthHeader("")
115119
.setCookies(emptyMap())
120+
.setEnableOfflineMode(true)
116121
.build()
117122

118123
private fun loadConfiguredEditor(config: ConfigurationItem.ConfiguredEditor) {
119-
isLoadingCapabilities.value = true
120-
121-
lifecycleScope.launch {
122-
try {
123-
val capabilities = siteCapabilitiesDiscovery.discoverCapabilities(
124-
siteApiRoot = config.siteApiRoot
125-
)
126-
127-
val editorConfiguration = createCommonConfigurationBuilder()
128-
.setPlugins(capabilities.supportsPlugins)
129-
.setThemeStyles(capabilities.supportsThemeStyles)
130-
.setSiteURL(config.siteUrl)
131-
.setSiteApiRoot(config.siteApiRoot)
132-
.setNamespaceExcludedPaths(arrayOf())
133-
.setAuthHeader(config.authHeader)
134-
.build()
135-
136-
isLoadingCapabilities.value = false
137-
launchEditor(editorConfiguration)
138-
} catch (e: Exception) {
139-
isLoadingCapabilities.value = false
140-
// If capability discovery fails, use default configuration
141-
val defaultConfiguration = createCommonConfigurationBuilder()
142-
.setPlugins(false)
143-
.setThemeStyles(false)
144-
.setSiteURL(config.siteUrl)
145-
.setSiteApiRoot(config.siteApiRoot)
146-
.setNamespaceExcludedPaths(arrayOf())
147-
.setAuthHeader(config.authHeader)
148-
.build()
124+
launchSitePreparation(config)
125+
}
149126

150-
launchEditor(defaultConfiguration)
151-
}
152-
}
127+
private fun launchSitePreparation(config: ConfigurationItem) {
128+
val intent = SitePreparationActivity.createIntent(this, config)
129+
startActivity(intent)
153130
}
154131

155-
private fun createCommonConfigurationBuilder(): EditorConfiguration.Builder =
156-
EditorConfiguration.builder()
132+
private fun createCommonConfigurationBuilder(siteUrl: String, siteApiRoot: String, postType: String = "post"): EditorConfiguration.Builder =
133+
EditorConfiguration.builder(
134+
siteURL = siteUrl,
135+
siteApiRoot = siteApiRoot,
136+
postType = postType
137+
)
157138
.setTitle("")
158139
.setContent("")
159-
.setPostType("post")
160140
.setThemeStyles(false)
161141
.setHideTitle(false)
162142
.setCookies(emptyMap())
@@ -343,7 +323,7 @@ fun MainScreen(
343323
}
344324
}
345325

346-
@OptIn(androidx.compose.foundation.ExperimentalFoundationApi::class)
326+
@OptIn(ExperimentalFoundationApi::class)
347327
@Composable
348328
fun ConfigurationCard(
349329
configuration: ConfigurationItem,

0 commit comments

Comments
 (0)