|
| 1 | +package org.wordpress.android.ui.posts |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import kotlinx.coroutines.CoroutineDispatcher |
| 5 | +import kotlinx.coroutines.CoroutineScope |
| 6 | +import kotlinx.coroutines.launch |
| 7 | +import org.wordpress.android.fluxc.model.SiteModel |
| 8 | +import org.wordpress.android.fluxc.network.UserAgent |
| 9 | +import org.wordpress.android.fluxc.store.AccountStore |
| 10 | +import org.wordpress.android.modules.BG_THREAD |
| 11 | +import org.wordpress.android.ui.prefs.experimentalfeatures.ExperimentalFeatures |
| 12 | +import org.wordpress.android.ui.prefs.experimentalfeatures.ExperimentalFeatures.Feature |
| 13 | +import org.wordpress.android.util.AppLog |
| 14 | +import org.wordpress.android.util.AppLog.T |
| 15 | +import org.wordpress.android.util.PerAppLocaleManager |
| 16 | +import org.wordpress.android.util.SiteUtils |
| 17 | +import org.wordpress.android.util.config.GutenbergKitFeature |
| 18 | +import org.wordpress.android.util.config.GutenbergKitPluginsFeature |
| 19 | +import org.wordpress.gutenberg.EditorConfiguration |
| 20 | +import org.wordpress.gutenberg.GutenbergView |
| 21 | +import javax.inject.Inject |
| 22 | +import javax.inject.Named |
| 23 | +import javax.inject.Singleton |
| 24 | + |
| 25 | +/** |
| 26 | + * Helper class to manage GutenbergView warmup for preloading editor assets. |
| 27 | + * This improves editor launch speed by caching WebView assets before the editor is opened. |
| 28 | + */ |
| 29 | +@Singleton |
| 30 | +class GutenbergKitWarmupHelper @Inject constructor( |
| 31 | + private val appContext: Context, |
| 32 | + private val accountStore: AccountStore, |
| 33 | + private val userAgent: UserAgent, |
| 34 | + private val perAppLocaleManager: PerAppLocaleManager, |
| 35 | + private val gutenbergKitFeature: GutenbergKitFeature, |
| 36 | + private val gutenbergKitPluginsFeature: GutenbergKitPluginsFeature, |
| 37 | + private val experimentalFeatures: ExperimentalFeatures, |
| 38 | + @Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher |
| 39 | +) { |
| 40 | + private var lastWarmedUpSiteId: Long? = null |
| 41 | + private var isWarmupInProgress = false |
| 42 | + |
| 43 | + /** |
| 44 | + * Triggers warmup for the given site if not already warmed up. |
| 45 | + * |
| 46 | + * @param site The site to warm up the editor for |
| 47 | + * @param scope The coroutine scope to launch the warmup in |
| 48 | + */ |
| 49 | + fun warmupIfNeeded(site: SiteModel?, scope: CoroutineScope) { |
| 50 | + when { |
| 51 | + site == null -> { |
| 52 | + AppLog.d(T.EDITOR, "GutenbergKitWarmupHelper: Skipping warmup - no site provided") |
| 53 | + } |
| 54 | + lastWarmedUpSiteId == site.siteId && !isWarmupInProgress -> { |
| 55 | + AppLog.d(T.EDITOR, "GutenbergKitWarmupHelper: Already warmed up for site ${site.siteId}") |
| 56 | + } |
| 57 | + isWarmupInProgress -> { |
| 58 | + AppLog.d(T.EDITOR, "GutenbergKitWarmupHelper: Warmup already in progress") |
| 59 | + } |
| 60 | + !shouldWarmupForSite(site) -> { |
| 61 | + AppLog.d(T.EDITOR, "GutenbergKitWarmupHelper: Site doesn't support block editor, skipping warmup") |
| 62 | + } |
| 63 | + else -> { |
| 64 | + scope.launch(bgDispatcher) { |
| 65 | + performWarmup(site) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Clears the warmup state when switching sites or logging out. |
| 73 | + */ |
| 74 | + fun clearWarmupState() { |
| 75 | + lastWarmedUpSiteId = null |
| 76 | + isWarmupInProgress = false |
| 77 | + AppLog.d(T.EDITOR, "GutenbergKitWarmupHelper: Warmup state cleared") |
| 78 | + } |
| 79 | + |
| 80 | + private fun shouldWarmupForSite(site: SiteModel): Boolean { |
| 81 | + val isGutenbergEnabled = experimentalFeatures.isEnabled(Feature.EXPERIMENTAL_BLOCK_EDITOR) || |
| 82 | + gutenbergKitFeature.isEnabled() |
| 83 | + val isGutenbergDisabled = experimentalFeatures.isEnabled(Feature.DISABLE_EXPERIMENTAL_BLOCK_EDITOR) |
| 84 | + val isGutenbergFeatureEnabled = isGutenbergEnabled && !isGutenbergDisabled |
| 85 | + |
| 86 | + if (!isGutenbergFeatureEnabled) { |
| 87 | + AppLog.d(T.EDITOR, "GutenbergKitWarmupHelper: Skipping warmup - GutenbergKit features disabled") |
| 88 | + return false |
| 89 | + } |
| 90 | + |
| 91 | + val shouldWarmup = SiteUtils.isBlockEditorDefaultForNewPost(site) |
| 92 | + |
| 93 | + AppLog.d(T.EDITOR, "GutenbergKitWarmupHelper: Site ${site.siteId} warmup decision: $shouldWarmup " + |
| 94 | + "(isBlockEditorDefault: ${SiteUtils.isBlockEditorDefaultForNewPost(site)}, " + |
| 95 | + "webEditor: ${site.webEditor})") |
| 96 | + |
| 97 | + return shouldWarmup |
| 98 | + } |
| 99 | + |
| 100 | + private suspend fun performWarmup(site: SiteModel) { |
| 101 | + try { |
| 102 | + isWarmupInProgress = true |
| 103 | + AppLog.d(T.EDITOR, "GutenbergKitWarmupHelper: Starting warmup for site ${site.siteId}") |
| 104 | + |
| 105 | + val configuration = buildWarmupConfiguration(site) |
| 106 | + |
| 107 | + // Perform the warmup on the main thread as it involves WebView |
| 108 | + kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Main) { |
| 109 | + GutenbergView.warmup(appContext, configuration) |
| 110 | + } |
| 111 | + |
| 112 | + lastWarmedUpSiteId = site.siteId |
| 113 | + AppLog.d(T.EDITOR, "GutenbergKitWarmupHelper: Warmup completed for site ${site.siteId}") |
| 114 | + } catch (e: IllegalStateException) { |
| 115 | + AppLog.e(T.EDITOR, "GutenbergKitWarmupHelper: Warmup failed - illegal state", e) |
| 116 | + } finally { |
| 117 | + isWarmupInProgress = false |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private fun buildWarmupConfiguration(site: SiteModel): EditorConfiguration { |
| 122 | + // Build the configuration using the same patterns as GutenbergKitSettingsBuilder |
| 123 | + val siteConfig = GutenbergKitSettingsBuilder.SiteConfig.fromSiteModel(site) |
| 124 | + |
| 125 | + // Create minimal post config for warmup (no specific post data) |
| 126 | + val postConfig = GutenbergKitSettingsBuilder.PostConfig( |
| 127 | + remotePostId = null, |
| 128 | + isPage = false, |
| 129 | + title = "", |
| 130 | + content = "" |
| 131 | + ) |
| 132 | + |
| 133 | + val appConfig = GutenbergKitSettingsBuilder.AppConfig( |
| 134 | + accessToken = accountStore.accessToken, |
| 135 | + locale = perAppLocaleManager.getCurrentLocaleLanguageCode(), |
| 136 | + cookies = null, // No cookies needed for warmup |
| 137 | + accountUserId = accountStore.account.userId, |
| 138 | + accountUserName = accountStore.account.userName, |
| 139 | + userAgent = userAgent, |
| 140 | + isJetpackSsoEnabled = false // Default to false for warmup |
| 141 | + ) |
| 142 | + |
| 143 | + val featureConfig = GutenbergKitSettingsBuilder.FeatureConfig( |
| 144 | + isPluginsFeatureEnabled = gutenbergKitPluginsFeature.isEnabled(), |
| 145 | + isThemeStylesFeatureEnabled = experimentalFeatures.isEnabled( |
| 146 | + Feature.EXPERIMENTAL_BLOCK_EDITOR_THEME_STYLES |
| 147 | + ) |
| 148 | + ) |
| 149 | + |
| 150 | + val settings = GutenbergKitSettingsBuilder.buildSettings( |
| 151 | + siteConfig = siteConfig, |
| 152 | + postConfig = postConfig, |
| 153 | + appConfig = appConfig, |
| 154 | + featureConfig = featureConfig |
| 155 | + ) |
| 156 | + |
| 157 | + return EditorConfigurationBuilder.build(settings, editorSettings = null) |
| 158 | + } |
| 159 | +} |
0 commit comments