|
| 1 | +package org.wordpress.android.repositories |
| 2 | + |
| 3 | +import kotlinx.coroutines.CoroutineScope |
| 4 | +import kotlinx.coroutines.Job |
| 5 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 6 | +import kotlinx.coroutines.flow.StateFlow |
| 7 | +import kotlinx.coroutines.launch |
| 8 | +import org.wordpress.android.fluxc.model.SiteModel |
| 9 | +import org.wordpress.android.modules.APPLICATION_SCOPE |
| 10 | +import org.wordpress.android.util.NetworkUtilsWrapper |
| 11 | +import java.util.concurrent.ConcurrentHashMap |
| 12 | +import javax.inject.Inject |
| 13 | +import javax.inject.Named |
| 14 | +import javax.inject.Singleton |
| 15 | + |
| 16 | +/** |
| 17 | + * Single owner of "what does this site's editor REST API support" as an |
| 18 | + * observable, per-site state. |
| 19 | + * |
| 20 | + * Capability detection ([EditorSettingsRepository.fetchEditorCapabilitiesForSite]) |
| 21 | + * has two async preconditions on Atomic sites — an application password and a |
| 22 | + * recovered REST root — provisioned elsewhere on the My Site screen. Routing |
| 23 | + * every consumer (connectivity banner, editor preloader) through this detector |
| 24 | + * means one probe per site, shared and deduplicated, instead of each consumer |
| 25 | + * re-deriving the same state and racing the same preconditions. |
| 26 | + * |
| 27 | + * State is keyed by [SiteModel.id] — the local DB row id, stable across the |
| 28 | + * process lifetime — mirroring `GutenbergEditorPreloader`. |
| 29 | + * |
| 30 | + * ## Entry points |
| 31 | + * - [stateFor] — the reactive entry point. Returns a shared [StateFlow]; the |
| 32 | + * first access starts detection, later accesses reuse the cached result |
| 33 | + * (capabilities rarely change). A failed probe is retried on the next access. |
| 34 | + * - [awaitProbe] — the one-shot entry point for callers that just need the |
| 35 | + * probe to have run (and its capabilities persisted) before continuing. |
| 36 | + * - [refresh] — forces a re-probe, bypassing the once-per-site gate |
| 37 | + * (pull-to-refresh, banner retry, newly established credentials). |
| 38 | + * - [clear] — cancels all work and drops all state; wire into sign-out. |
| 39 | + */ |
| 40 | +@Singleton |
| 41 | +class EditorCapabilityDetector @Inject constructor( |
| 42 | + private val editorSettingsRepository: EditorSettingsRepository, |
| 43 | + private val networkUtilsWrapper: NetworkUtilsWrapper, |
| 44 | + @Named(APPLICATION_SCOPE) private val appScope: CoroutineScope, |
| 45 | +) { |
| 46 | + private val states = |
| 47 | + ConcurrentHashMap<Int, MutableStateFlow<EditorCapabilityDetectionState>>() |
| 48 | + private val jobs = ConcurrentHashMap<Int, Job>() |
| 49 | + |
| 50 | + // Sites whose live probe succeeded this process — the dedup gate. Only a |
| 51 | + // successful fetch latches; a failed one is left to retry on the next |
| 52 | + // access, matching the connectivity banner's previous per-slice behaviour. |
| 53 | + // Reset by refresh / clear. |
| 54 | + private val probedOk = ConcurrentHashMap.newKeySet<Int>() |
| 55 | + |
| 56 | + /** |
| 57 | + * The shared detection state for [site]. The first call starts detection; |
| 58 | + * later calls return the same flow without re-probing once it has |
| 59 | + * succeeded. Collect it to react to capability changes. |
| 60 | + */ |
| 61 | + @Synchronized |
| 62 | + fun stateFor(site: SiteModel): StateFlow<EditorCapabilityDetectionState> { |
| 63 | + val flow = flowFor(site.id) |
| 64 | + if (shouldProbe(site.id)) launchDetection(site) |
| 65 | + return flow |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Ensures detection has run for [site] (so its capabilities are persisted) |
| 70 | + * and returns the settled state. Respects the once-per-site gate; call |
| 71 | + * [refresh] first to force a fresh probe. |
| 72 | + */ |
| 73 | + suspend fun awaitProbe(site: SiteModel): EditorCapabilityDetectionState { |
| 74 | + stateFor(site) |
| 75 | + jobs[site.id]?.join() |
| 76 | + return states[site.id]?.value ?: EditorCapabilityDetectionState.Pending |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Forces a re-probe for [site], bypassing the once-per-site gate. A no-op |
| 81 | + * while a probe is already in flight — that probe's result is fresh enough. |
| 82 | + */ |
| 83 | + @Synchronized |
| 84 | + fun refresh(site: SiteModel) { |
| 85 | + if (jobs[site.id]?.isActive == true) return |
| 86 | + probedOk.remove(site.id) |
| 87 | + launchDetection(site) |
| 88 | + } |
| 89 | + |
| 90 | + /** Cancels all in-flight detection and drops all cached state (sign-out). */ |
| 91 | + @Synchronized |
| 92 | + fun clear() { |
| 93 | + jobs.values.forEach { it.cancel() } |
| 94 | + jobs.clear() |
| 95 | + states.clear() |
| 96 | + probedOk.clear() |
| 97 | + } |
| 98 | + |
| 99 | + @Synchronized |
| 100 | + private fun launchDetection(site: SiteModel) { |
| 101 | + jobs[site.id]?.cancel() |
| 102 | + val flow = flowFor(site.id) |
| 103 | + jobs[site.id] = appScope.launch { |
| 104 | + flow.value = detect(site) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private fun flowFor(siteLocalId: Int): MutableStateFlow<EditorCapabilityDetectionState> = |
| 109 | + states.getOrPut(siteLocalId) { |
| 110 | + MutableStateFlow(EditorCapabilityDetectionState.Pending) |
| 111 | + } |
| 112 | + |
| 113 | + private fun shouldProbe(siteLocalId: Int): Boolean = |
| 114 | + jobs[siteLocalId]?.isActive != true && siteLocalId !in probedOk |
| 115 | + |
| 116 | + private suspend fun detect(site: SiteModel): EditorCapabilityDetectionState { |
| 117 | + val ok = editorSettingsRepository.fetchEditorCapabilitiesForSite(site) |
| 118 | + if (ok) probedOk.add(site.id) |
| 119 | + val hasCache = editorSettingsRepository.hasCachedCapabilities(site) |
| 120 | + return when { |
| 121 | + ok || hasCache -> EditorCapabilityDetectionState.Ready |
| 122 | + editorSettingsRepository.isAwaitingApplicationPassword(site) -> |
| 123 | + EditorCapabilityDetectionState.Pending |
| 124 | + !networkUtilsWrapper.isNetworkAvailable() -> |
| 125 | + EditorCapabilityDetectionState.TransientError |
| 126 | + else -> EditorCapabilityDetectionState.Unreachable |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Observable lifecycle of editor-capability detection for one site — distinct |
| 133 | + * from `org.wordpress.android.ui.posts.EditorCapabilityState`, which models a |
| 134 | + * resolved settings-row capability. This is the *detection* state the |
| 135 | + * connectivity banner and editor preloader subscribe to. |
| 136 | + */ |
| 137 | +sealed interface EditorCapabilityDetectionState { |
| 138 | + /** |
| 139 | + * Not determined yet — still probing, or waiting on an application password |
| 140 | + * being minted asynchronously. Consumers hold; the banner stays hidden. |
| 141 | + */ |
| 142 | + data object Pending : EditorCapabilityDetectionState |
| 143 | + |
| 144 | + /** |
| 145 | + * Capabilities are known (freshly detected, or cached from a prior run). |
| 146 | + * Read them via [EditorSettingsRepository]'s getters. |
| 147 | + */ |
| 148 | + data object Ready : EditorCapabilityDetectionState |
| 149 | + |
| 150 | + /** |
| 151 | + * Credentials are present but the transport probe failed — the site looks |
| 152 | + * unreachable. The only state that surfaces the connectivity banner. |
| 153 | + */ |
| 154 | + data object Unreachable : EditorCapabilityDetectionState |
| 155 | + |
| 156 | + /** A transient failure (e.g. device offline). Retried on the next probe. */ |
| 157 | + data object TransientError : EditorCapabilityDetectionState |
| 158 | +} |
0 commit comments