-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSitePreparationViewModel.kt
More file actions
363 lines (323 loc) · 14 KB
/
Copy pathSitePreparationViewModel.kt
File metadata and controls
363 lines (323 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.example.gutenbergkit
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import org.wordpress.gutenberg.model.EditorCachePolicy
import org.wordpress.gutenberg.model.EditorConfiguration
import org.wordpress.gutenberg.model.EditorDependencies
import org.wordpress.gutenberg.model.PostTypeDetails
import org.wordpress.gutenberg.services.EditorService
import rs.wordpress.api.kotlin.WpRequestResult
import uniffi.wp_api.PostType as WpPostType
data class SitePreparationUiState(
val enableNativeInserter: Boolean = false,
val enableInserterMediaStrip: Boolean = false,
val enableNetworkLogging: Boolean = false,
/** All viewable post types fetched from the site, or empty while loading. */
val postTypes: List<PostTypeDetails> = emptyList(),
/** The post type currently selected in the picker. Null until [postTypes] loads. */
val selectedPostType: PostTypeDetails? = null,
val cacheBundleCount: Int? = null,
val isLoading: Boolean = false,
val error: String? = null,
val editorConfiguration: EditorConfiguration? = null,
val editorDependencies: EditorDependencies? = null,
val loadingProgress: Float? = null
)
class SitePreparationViewModel(
application: Application,
private val configurationItem: ConfigurationItem
) : AndroidViewModel(application) {
private val _uiState = MutableStateFlow(SitePreparationUiState())
val uiState: StateFlow<SitePreparationUiState> = _uiState.asStateFlow()
private val siteCapabilitiesDiscovery = SiteCapabilitiesDiscovery()
private val networkAvailabilityProvider =
(application as GutenbergKitApplication).networkAvailabilityProvider
fun startLoading() {
viewModelScope.launch {
try {
val configuration = when (configurationItem) {
is ConfigurationItem.BundledEditor -> createBundledConfiguration()
is ConfigurationItem.LocalWordPress -> {
val credentials = LocalWordPressCredentials.load()
?: throw IllegalStateException(
"Local WordPress not configured.\n\nRun 'make wp-env-start' from the project root, then rebuild the app."
)
try {
loadConfiguration(
ConfigurationItem.ConfiguredEditor(
accountId = 0u,
name = "Local WordPress",
siteUrl = credentials.siteUrl,
siteApiRoot = credentials.siteApiRoot,
authHeader = credentials.authHeader
)
)
} catch (e: java.net.ConnectException) {
throw IllegalStateException(
"Could not connect to Local WordPress at ${credentials.siteUrl}.\n\nThe wp-env server may not be running. Start it with 'make wp-env-start'."
)
}
}
is ConfigurationItem.ConfiguredEditor -> loadConfiguration(configurationItem)
}
_uiState.update { it.copy(editorConfiguration = configuration) }
countAssetBundles()
} catch (e: Exception) {
_uiState.update { it.copy(error = e.message ?: "Unknown error") }
}
}
}
fun setEnableNativeInserter(enabled: Boolean) {
_uiState.update { it.copy(enableNativeInserter = enabled) }
}
fun setEnableInserterMediaStrip(enabled: Boolean) {
_uiState.update { it.copy(enableInserterMediaStrip = enabled) }
}
fun setEnableNetworkLogging(enabled: Boolean) {
_uiState.update { it.copy(enableNetworkLogging = enabled) }
}
fun setPostType(postType: PostTypeDetails) {
_uiState.update { it.copy(selectedPostType = postType) }
}
fun prepareEditor() {
val configuration = _uiState.value.editorConfiguration ?: return
val cacheIntervalSeconds = 86_400L // Cache for one day
val editorService = EditorService.create(
context = getApplication(),
configuration = configuration,
cachePolicy = EditorCachePolicy.MaxAge(cacheIntervalSeconds),
coroutineScope = viewModelScope
)
prepareEditor(editorService)
}
fun prepareEditorFromScratch() {
val configuration = _uiState.value.editorConfiguration ?: return
val editorService = EditorService.create(
context = getApplication(),
configuration = configuration,
cachePolicy = EditorCachePolicy.Ignore,
coroutineScope = viewModelScope
)
prepareEditor(editorService)
}
private fun prepareEditor(editorService: EditorService) {
viewModelScope.launch {
_uiState.update { it.copy(isLoading = true, error = null) }
try {
val dependencies = editorService.prepare { progress ->
_uiState.update { it.copy(loadingProgress = progress.fractionCompleted.toFloat()) }
}
_uiState.update {
it.copy(
editorDependencies = dependencies,
isLoading = false,
loadingProgress = null
)
}
countAssetBundles()
} catch (e: Exception) {
_uiState.update {
it.copy(
error = e.message ?: "Unknown error",
isLoading = false,
loadingProgress = null
)
}
}
}
}
fun resetEditorCaches() {
val configuration = _uiState.value.editorConfiguration ?: return
viewModelScope.launch {
_uiState.update { it.copy(isLoading = true, error = null) }
try {
_uiState.update { it.copy(editorDependencies = null) }
val editorService = EditorService.create(
context = getApplication(),
configuration = configuration,
coroutineScope = viewModelScope
)
editorService.purge()
countAssetBundles()
_uiState.update { it.copy(isLoading = false) }
} catch (e: Exception) {
_uiState.update {
it.copy(
error = e.message ?: "Unknown error",
isLoading = false
)
}
}
}
}
private fun countAssetBundles() {
viewModelScope.launch {
try {
val configuration = _uiState.value.editorConfiguration ?: run {
_uiState.update { it.copy(cacheBundleCount = 0) }
return@launch
}
val editorService = EditorService.create(
context = getApplication(),
configuration = configuration,
coroutineScope = viewModelScope
)
val count = editorService.fetchAssetBundleCount()
_uiState.update { it.copy(cacheBundleCount = count) }
} catch (e: Exception) {
_uiState.update { it.copy(error = e.message ?: "Unknown error") }
}
}
}
private fun createBundledConfiguration(): EditorConfiguration {
// Bundled offline editor: only the standard `post` type is meaningful.
_uiState.update {
it.copy(
postTypes = listOf(PostTypeDetails.post),
selectedPostType = PostTypeDetails.post
)
}
return EditorConfiguration.builder(
siteURL = "https://example.com",
siteApiRoot = "https://example.com",
postType = PostTypeDetails.post
)
.setPlugins(false)
.setSiteApiNamespace(arrayOf())
.setNamespaceExcludedPaths(arrayOf())
.setAuthHeader("")
.setCookies(emptyMap())
.setEnableOfflineMode(true)
.build()
}
private suspend fun loadConfiguration(config: ConfigurationItem.ConfiguredEditor): EditorConfiguration {
val capabilities = siteCapabilitiesDiscovery.discoverCapabilities(
siteUrl = config.siteUrl,
networkAvailabilityProvider = networkAvailabilityProvider
)
// For WP.com sites, the stored siteApiRoot is namespace-specific
// (e.g. https://public-api.wordpress.com/wp/v2/sites/1562023).
// The editor needs the base REST API root and a siteApiNamespace
// so the JS middleware can insert the site ID into request paths.
val wpComSiteId = extractWpComSiteId(config.siteApiRoot)
val siteApiRoot = if (wpComSiteId != null) {
"https://public-api.wordpress.com/"
} else {
config.siteApiRoot
}
val siteApiNamespace = if (wpComSiteId != null) {
arrayOf("sites/$wpComSiteId/")
} else {
arrayOf()
}
// Fetch the site's post types. Default the picker to `post` when it's
// available (the typical case); otherwise pick the first type in the
// list. Falls back to `PostTypeDetails.post` if the call fails so the
// editor can still launch with a sensible default.
val postTypes = loadPostTypes(config) ?: listOf(PostTypeDetails.post)
val defaultPostType = postTypes.firstOrNull { it.postType == "post" } ?: postTypes.first()
_uiState.update {
it.copy(
postTypes = postTypes,
selectedPostType = defaultPostType
)
}
return EditorConfiguration.builder(
siteURL = config.siteUrl,
siteApiRoot = siteApiRoot,
postType = defaultPostType
)
.setPlugins(capabilities.supportsPlugins)
.setThemeStyles(capabilities.supportsThemeStyles)
.setSiteApiNamespace(siteApiNamespace)
.setNamespaceExcludedPaths(arrayOf())
.setAuthHeader(config.authHeader)
.setTitle("")
.setContent("")
.setHideTitle(false)
.setCookies(emptyMap())
.setEnableNetworkLogging(true)
.setEnableAssetCaching(capabilities.supportsPlugins)
.build()
}
/**
* Fetches the site's post types from the WordPress REST API and returns the
* subset relevant to the editor picker.
*
* Mirrors the iOS [SitePreparationView.loadPostTypes] filter:
* - always include the standard `post` and `page` types
* - include custom types only when they are `viewable` and have UI visibility
* - exclude all internal types (`Attachment`, `WpBlock`, etc.)
*
* Returns `null` if the post types could not be fetched (e.g. account not
* found, network error). Callers fall back to a sensible default.
*/
private suspend fun loadPostTypes(
config: ConfigurationItem.ConfiguredEditor
): List<PostTypeDetails>? {
val app = getApplication<GutenbergKitApplication>()
val account = app.accountRepository.all().firstOrNull { it.id() == config.accountId }
?: return null
val client = app.createApiClient(account)
val result = client.request { builder ->
builder.postTypes().listWithEditContext()
}
if (result !is WpRequestResult.Success) return null
return result.response.data.postTypes
.filter { (type, details) ->
when (type) {
is WpPostType.Post, is WpPostType.Page -> true
is WpPostType.Custom -> details.viewable && details.visibility.showUi
else -> false
}
}
.map { (_, details) ->
PostTypeDetails(
postType = details.slug,
restBase = details.restBase,
restNamespace = details.restNamespace
)
}
.sortedBy { it.postType }
}
/**
* Extracts the WP.com site ID from a namespace-specific API root URL.
* Returns null if the URL is not a WP.com API root.
*
* Example: "https://public-api.wordpress.com/wp/v2/sites/1562023" -> "1562023"
*/
private fun extractWpComSiteId(siteApiRoot: String): String? {
val regex = Regex("""public-api\.wordpress\.com/.+/sites/(\d+)""")
return regex.find(siteApiRoot)?.groupValues?.get(1)
}
fun buildConfiguration(): EditorConfiguration? {
val baseConfig = _uiState.value.editorConfiguration ?: return null
val selectedPostType = _uiState.value.selectedPostType ?: return null
return baseConfig.toBuilder()
.setEnableNetworkLogging(_uiState.value.enableNetworkLogging)
.setEnableNativeBlockInserter(_uiState.value.enableNativeInserter)
.setEnableInserterMediaStrip(_uiState.value.enableInserterMediaStrip)
.setPostType(selectedPostType)
.build()
}
}
class SitePreparationViewModelFactory(
private val application: Application,
private val configurationItem: ConfigurationItem
) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(SitePreparationViewModel::class.java)) {
return SitePreparationViewModel(application, configurationItem) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}