From 5d296153514330424751cbe4bc341811f47aaac7 Mon Sep 17 00:00:00 2001 From: Mubashar Hussain Date: Tue, 20 Oct 2026 13:16:07 +0500 Subject: [PATCH 1/9] store: prioritize primary mirror for module details --- .../vector/manager/data/repository/RepoRepository.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt index b54183dcd0..75c21c9c8a 100644 --- a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt +++ b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt @@ -30,9 +30,7 @@ import org.matrix.vector.manager.logW * The Store's data: the online catalogue, and what this device already has of it. * * **The mirror list is two lists, and that is not an oversight.** The full `modules.json` is served - * by exactly one host today: `modules.lsposed.org` answers that path with a 403. Per-module - * `module/.json` *is* served by both hosts, so the public site is a real fallback there - * and only there. Merging these two lists back into one would quietly take the Store offline. + * Per-module `module/.json` is served by both hosts, with `modules.lsposed.org` providing the latest data and `backup.modules.lsposed.org` serving as fallback. * * **Caching is declared, not hoped for.** Every request states its own freshness, so the 16 MB disk * cache in `HttpClientFactory` is actually used: the catalogue revalidates against the server's own @@ -298,9 +296,9 @@ class RepoRepository( */ val LIST_MIRRORS = listOf("https://backup.modules.lsposed.org/") - /** Detail is served by both hosts, so here the public site is a genuine fallback. */ + /** Detail is served by both hosts; primary site carries the latest data while backup is fallback. */ val DETAIL_MIRRORS = - listOf("https://backup.modules.lsposed.org/", "https://modules.lsposed.org/") + listOf("https://modules.lsposed.org/", "https://backup.modules.lsposed.org/") /** Matches the server's own `cache-control: max-age=600`, so revalidation stays free. */ const val CATALOG_MAX_AGE_MINUTES = 10 From 3c4107dde0a570aa0de26d9b972200f9037f7b03 Mon Sep 17 00:00:00 2001 From: Mubashar Hussain Date: Tue, 20 Oct 2026 13:22:55 +0500 Subject: [PATCH 2/9] store: integrate search.json for global real-time search suggestions --- .../org/matrix/vector/ui/store/RepoModels.kt | 25 +++++++ .../org/matrix/vector/ui/store/RepoScreen.kt | 74 ++++++++++++++++++- .../matrix/vector/ui/store/RepoViewModel.kt | 12 +++ .../matrix/vector/ui/store/StoreDataSource.kt | 4 + .../manager/data/repository/RepoRepository.kt | 40 ++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-) diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt index 059b2e5fc4..383430e3d3 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt @@ -50,6 +50,31 @@ data class OnlineModule( get() = url ?: "https://github.com/Xposed-Modules-Repo/$name" } +/** + * An entry from `https://modules.lsposed.org/search.json`. + * + * Provides a lightweight index of all available modules for instant client-side + * global search and auto-complete. + */ +data class StoreSearchItem( + @SerializedName("name") val name: String, + @SerializedName("description") val description: String? = null, + @SerializedName("summary") val summary: String? = null, + @SerializedName("readmeExcerpt") val readmeExcerpt: String? = null, + @SerializedName("authors") val authors: String? = null, +) { + val title: String + get() = description?.takeIf { it.isNotBlank() } ?: name + + fun matches(query: String): Boolean { + if (query.isBlank()) return false + return title.contains(query, ignoreCase = true) || + name.contains(query, ignoreCase = true) || + summary?.contains(query, ignoreCase = true) == true || + authors?.contains(query, ignoreCase = true) == true + } +} + data class Collaborator( @SerializedName("login") val login: String?, @SerializedName("name") val name: String?, diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt index 431484882d..35631db462 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt @@ -104,6 +104,8 @@ fun RepoScreen( val priorities by viewModel.priorities.collectAsState() val channel by viewModel.channel.collectAsState() + val searchSuggestions by viewModel.searchSuggestions.collectAsState() + Scaffold { innerPadding -> Column(modifier = Modifier.padding(innerPadding).fillMaxSize()) { StoreHeader( @@ -115,6 +117,33 @@ fun RepoScreen( Spacer(Modifier.height(4.dp)) + // When user is searching, display real-time global search suggestions from search.json + if (query.isNotBlank()) { + LazyColumn( + modifier = Modifier.fillMaxSize(), + contentPadding = PaddingValues(bottom = 20.dp), + ) { + if (searchSuggestions.isEmpty() && entries.isEmpty()) { + item { + EmptyState( + modifier = Modifier.fillParentMaxSize(), + catalog = catalog, + filtered = true, + ) + } + } else { + items(searchSuggestions, key = { it.name }) { item -> + SearchSuggestionRow( + item = item, + onClick = { onModuleClick(item.name) }, + ) + HorizontalDivider(color = MaterialTheme.colorScheme.surfaceVariant) + } + } + } + return@Column + } + // Nothing has ever loaded and a fetch is running: the one moment a spinner says // something the list could not say better itself. if (!catalog.loaded && entries.isEmpty()) { @@ -137,7 +166,7 @@ fun RepoScreen( EmptyState( modifier = Modifier.fillParentMaxSize(), catalog = catalog, - filtered = query.isNotBlank(), + filtered = false, ) } } else { @@ -427,6 +456,49 @@ private fun StoreRow(entry: StoreEntry, onClick: () -> Unit) { } } +@Composable +private fun SearchSuggestionRow(item: StoreSearchItem, onClick: () -> Unit) { + val colors = MaterialTheme.colorScheme + Column( + modifier = + Modifier.fillMaxWidth() + .clickable(onClick = onClick) + .padding(horizontal = 20.dp, vertical = 12.dp) + ) { + Text( + text = item.title, + style = MaterialTheme.typography.titleMedium, + maxLines = 2, + overflow = TextOverflow.Ellipsis, + ) + Text( + text = item.name, + style = Mono, + color = colors.onSurfaceVariant, + ) + if (!item.summary.isNullOrBlank()) { + Spacer(Modifier.height(6.dp)) + Text( + text = item.summary, + style = MaterialTheme.typography.bodyMedium, + color = colors.onSurfaceVariant, + maxLines = 2, + overflow = TextOverflow.Ellipsis, + ) + } + if (!item.authors.isNullOrBlank()) { + Spacer(Modifier.height(6.dp)) + Text( + text = item.authors, + style = MaterialTheme.typography.labelSmall, + color = colors.onSurfaceVariant, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + } + } +} + @Composable private fun RowBadge(icon: ImageVector, text: String, tint: Color, modifier: Modifier = Modifier) { Row(modifier = modifier, verticalAlignment = Alignment.CenterVertically) { diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt index 6f8f962534..e5080dcb9c 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt @@ -170,6 +170,18 @@ class RepoViewModel( .flowOn(Dispatchers.Default) .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList()) + /** + * Real-time search suggestions over the complete index of all ~1,180+ modules. + * When query is empty, suggestions are empty. + */ + val searchSuggestions: StateFlow> = + combine(dataSource.searchIndex, _query) { index, query -> + if (query.isBlank()) emptyList() + else index.filter { it.matches(query) }.take(20) + } + .flowOn(Dispatchers.Default) + .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList()) + init { // The catalogue outlives this ViewModel — switching tabs destroys the nav entry — so a // return to the Store paints from what is already in memory and only fetches when there is diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt index ff969b535f..7e50d2cb53 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt @@ -17,6 +17,10 @@ interface StoreDataSource { /** Installed module versions on this device, keyed by package name. */ val installedVersions: StateFlow> + /** Complete search index for all modules, used for instant suggestion and search. */ + val searchIndex: StateFlow> + get() = kotlinx.coroutines.flow.MutableStateFlow(emptyList()) + /** Reload the catalogue from the network; [force] bypasses any cache. */ suspend fun refresh(force: Boolean = false) diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt index 75c21c9c8a..6b2b6887e1 100644 --- a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt +++ b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt @@ -20,6 +20,7 @@ import org.matrix.vector.ui.store.StoreDataSource import org.matrix.vector.ui.store.OnlineModule import org.matrix.vector.ui.store.RepoVersion import org.matrix.vector.ui.store.StoreCatalog +import org.matrix.vector.ui.store.StoreSearchItem import org.matrix.vector.manager.data.model.versionCodeCompat import org.matrix.vector.manager.di.ServiceLocator import org.matrix.vector.manager.ipc.DaemonClient @@ -57,6 +58,9 @@ class RepoRepository( private val _isRefreshing = MutableStateFlow(false) override val isRefreshing: StateFlow = _isRefreshing.asStateFlow() + private val _searchIndex = MutableStateFlow>(emptyList()) + override val searchIndex: StateFlow> = _searchIndex.asStateFlow() + private val _installed = MutableStateFlow>(emptyMap()) /** @@ -102,6 +106,7 @@ class RepoRepository( .maxAge(CATALOG_MAX_AGE_MINUTES, TimeUnit.MINUTES) .build() + launch { fetchSearchIndex(freshness) } val fetched = LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, freshness) } if (fetched != null) { _catalog.value = fetched @@ -241,6 +246,41 @@ class RepoRepository( } } + private fun fetchSearchIndex(cacheControl: CacheControl) { + val url = "https://modules.lsposed.org/search.json" + try { + client.newCall(request(url, cacheControl)).execute().use { response -> + if (response.isSuccessful) { + val parsed = parseSearchIndex(response) + if (parsed.isNotEmpty()) { + logI("store: loaded ${parsed.size} search items from $url") + _searchIndex.value = parsed + } + } + } + } catch (e: Exception) { + logW("store: search index from $url unavailable", e) + // Try cache fallback + if (cacheControl != CacheControl.FORCE_CACHE) { + fetchSearchIndex(CacheControl.FORCE_CACHE) + } + } + } + + private fun parseSearchIndex(response: Response): List { + val items = ArrayList(1200) + JsonReader(response.body.charStream()).use { reader -> + reader.beginArray() + while (reader.hasNext()) { + val element = JsonParser.parseReader(reader) + val item = runCatching { gson.fromJson(element, StoreSearchItem::class.java) } + if (item.isSuccess) item.getOrNull()?.let(items::add) + } + reader.endArray() + } + return items + } + private fun request(url: String, cacheControl: CacheControl): Request = Request.Builder().url(url).cacheControl(cacheControl).build() From 1fe5ffa5ef50b5e9fc6ab7f304180f51ecc3c454 Mon Sep 17 00:00:00 2001 From: Mubashar Hussain Date: Tue, 20 Oct 2026 13:27:59 +0500 Subject: [PATCH 3/9] store: paginate module catalogue using module-list endpoint --- .../org/matrix/vector/ui/store/RepoModels.kt | 7 ++ .../org/matrix/vector/ui/store/RepoScreen.kt | 32 ++++++- .../matrix/vector/ui/store/RepoViewModel.kt | 4 + .../matrix/vector/ui/store/StoreDataSource.kt | 3 + .../manager/data/repository/RepoRepository.kt | 88 +++++++++++++++++-- 5 files changed, 126 insertions(+), 8 deletions(-) diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt index 383430e3d3..6a3ae7cc7d 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt @@ -293,7 +293,14 @@ data class StoreCatalog( val loaded: Boolean = false, val fromCache: Boolean = false, val loadedAtMillis: Long = 0L, + val page: Int = 1, + val pageCount: Int = 1, + val total: Int = 0, + val isLoadingMore: Boolean = false, ) { val isEmpty: Boolean get() = modules.isEmpty() + + val hasMore: Boolean + get() = page < pageCount } diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt index 35631db462..a8b87a02b3 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt @@ -28,6 +28,7 @@ import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.Check import androidx.compose.material.icons.rounded.CloudOff @@ -46,11 +47,13 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.pulltorefresh.PullToRefreshBox import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue +import androidx.compose.runtime.snapshotFlow import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -153,8 +156,22 @@ fun RepoScreen( return@Column } + val listState = rememberLazyListState() + LaunchedEffect(listState, catalog.hasMore, catalog.isLoadingMore) { + snapshotFlow { + val lastVisibleItem = listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: 0 + val totalItems = listState.layoutInfo.totalItemsCount + lastVisibleItem >= totalItems - 5 + }.collect { nearBottom -> + if (nearBottom && catalog.hasMore && !catalog.isLoadingMore) { + viewModel.loadMore() + } + } + } + PullToRefreshBox(isRefreshing = refreshing, onRefresh = viewModel::refresh) { LazyColumn( + state = listState, modifier = Modifier.fillMaxSize(), contentPadding = PaddingValues(bottom = 20.dp), ) { @@ -174,6 +191,16 @@ fun RepoScreen( StoreRow(entry = entry, onClick = { onModuleClick(entry.module.name) }) HorizontalDivider(color = MaterialTheme.colorScheme.surfaceVariant) } + if (catalog.isLoadingMore) { + item { + Box( + modifier = Modifier.fillMaxWidth().padding(16.dp), + contentAlignment = Alignment.Center, + ) { + CircularProgressIndicator(modifier = Modifier.size(24.dp), strokeWidth = 2.dp) + } + } + } } } } @@ -194,11 +221,12 @@ private fun StoreHeader( actions = actions, description = { if (catalog.modules.isNotEmpty()) { + val count = if (catalog.total > 0) catalog.total else catalog.modules.size val total = context.resources.getQuantityString( R.plurals.store_module_count, - catalog.modules.size, - catalog.modules.size, + count, + count, ) // "Up to date" is stated in words rather than as a zero, because a zero in a row // of counts reads as a failure to load rather than as good news. diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt index e5080dcb9c..b2e02e4568 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt @@ -213,6 +213,10 @@ class RepoViewModel( viewModelScope.launch { dataSource.refresh(force = true) } } + fun loadMore() { + viewModelScope.launch { dataSource.loadMore() } + } + /** The three controls that reorder the list, as one value so `combine` stays readable. */ private data class View( val sort: StoreSort, diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt index 7e50d2cb53..0d6a9f3fa0 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt @@ -24,6 +24,9 @@ interface StoreDataSource { /** Reload the catalogue from the network; [force] bypasses any cache. */ suspend fun refresh(force: Boolean = false) + /** Load the next page of modules in the catalogue. */ + suspend fun loadMore() {} + /** The full record for one module (README, all releases); null when unreachable. */ suspend fun details(packageName: String): OnlineModule? diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt index 6b2b6887e1..769f31d633 100644 --- a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt +++ b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt @@ -107,7 +107,8 @@ class RepoRepository( .build() launch { fetchSearchIndex(freshness) } - val fetched = LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, freshness) } + val fetched = fetchPaginatedCatalog(1, freshness) + ?: LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, freshness) } if (fetched != null) { _catalog.value = fetched return@withContext @@ -116,8 +117,8 @@ class RepoRepository( // Every mirror failed. Before reporting nothing, ask the cache — the bytes from // the last successful visit are usually still on disk, and a stale catalogue is // far more use than an empty screen. - val cached = - LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, CacheControl.FORCE_CACHE) } + val cached = fetchPaginatedCatalog(1, CacheControl.FORCE_CACHE) + ?: LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, CacheControl.FORCE_CACHE) } when { cached != null -> _catalog.value = cached.copy(fromCache = true) // Nothing on the network and nothing on disk. `loaded` still flips, so the @@ -135,6 +136,29 @@ class RepoRepository( } } + override suspend fun loadMore() { + val current = _catalog.value + if (!current.hasMore || current.isLoadingMore) return + _catalog.value = current.copy(isLoadingMore = true) + withContext(Dispatchers.IO) { + val nextPage = current.page + 1 + val freshness = CacheControl.Builder().maxAge(CATALOG_MAX_AGE_MINUTES, TimeUnit.MINUTES).build() + val result = fetchPaginatedCatalog(nextPage, freshness) + if (result != null) { + val combined = (current.modules + result.modules).distinctBy { it.name } + _catalog.value = current.copy( + modules = combined, + page = result.page, + pageCount = result.pageCount, + total = result.total, + isLoadingMore = false, + ) + } else { + _catalog.value = current.copy(isLoadingMore = false) + } + } + } + /** * The full record for one module: its README, and every release rather than only the newest. * @@ -281,6 +305,59 @@ class RepoRepository( return items } + private fun fetchPaginatedCatalog(page: Int, cacheControl: CacheControl): StoreCatalog? { + val url = "https://modules.lsposed.org/module-list/$page.json" + return try { + client.newCall(request(url, cacheControl)).execute().use { response -> + if (!response.isSuccessful) { + if (response.networkResponse != null) { + logW("store: $url returned HTTP ${response.code}") + } + return null + } + val parsed = parsePaginatedCatalog(response) ?: return null + logI("store: page ${parsed.page}/${parsed.pageCount} (${parsed.modules.size} modules) from $url") + StoreCatalog( + modules = usable(parsed.modules), + loaded = true, + loadedAtMillis = response.receivedResponseAtMillis, + page = parsed.page, + pageCount = parsed.pageCount, + total = parsed.total, + ) + } + } catch (e: Exception) { + logW("store: $url unavailable", e) + null + } + } + + private data class PaginatedResponse( + val modules: List = emptyList(), + val page: Int = 1, + val pageCount: Int = 1, + val total: Int = 0, + ) + + private fun parsePaginatedCatalog(response: Response): PaginatedResponse? { + return try { + val json = JsonParser.parseReader(response.body.charStream()).asJsonObject + val page = json.get("page")?.asInt ?: 1 + val pageCount = json.get("pageCount")?.asInt ?: 1 + val total = json.get("total")?.asInt ?: 0 + val modulesArray = json.getAsJsonArray("modules") ?: return null + val modules = ArrayList(modulesArray.size()) + for (element in modulesArray) { + val module = runCatching { gson.fromJson(element, OnlineModule::class.java) } + if (module.isSuccess) module.getOrNull()?.let(modules::add) + } + PaginatedResponse(modules = modules, page = page, pageCount = pageCount, total = total) + } catch (e: Exception) { + logW("store: failed to parse paginated catalog", e) + null + } + } + private fun request(url: String, cacheControl: CacheControl): Request = Request.Builder().url(url).cacheControl(cacheControl).build() @@ -318,14 +395,13 @@ class RepoRepository( * * `distinctBy` is not superstition about today's data — it is what stops a mirror serving the * same package twice from crashing the Store's `LazyColumn`, which is keyed by package name. - * Entries with no release at all are dropped because there is nothing to install and nothing to - * say about them. + * Entries with no release or latestRelease are dropped. */ private fun usable(parsed: List): List = parsed .asSequence() .filter { it.hide != true } - .filter { !it.releases.isNullOrEmpty() } + .filter { !it.releases.isNullOrEmpty() || !it.latestRelease.isNullOrBlank() } .distinctBy { it.name } .toList() From 0743f855706f3f5619cb3d39333fa1dcbaca848a Mon Sep 17 00:00:00 2001 From: Mubashar Hussain Date: Tue, 20 Oct 2026 14:06:58 +0500 Subject: [PATCH 4/9] store: implement sequential background sync with progress tracking and 1-hour cache --- .../org/matrix/vector/ui/store/RepoModels.kt | 7 +- .../org/matrix/vector/ui/store/RepoScreen.kt | 106 ++++++++++-------- .../matrix/vector/ui/store/RepoViewModel.kt | 4 - .../matrix/vector/ui/store/StoreDataSource.kt | 3 - .../manager/data/repository/RepoRepository.kt | 78 +++++++------ 5 files changed, 109 insertions(+), 89 deletions(-) diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt index 6a3ae7cc7d..99792865cf 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt @@ -296,11 +296,12 @@ data class StoreCatalog( val page: Int = 1, val pageCount: Int = 1, val total: Int = 0, - val isLoadingMore: Boolean = false, + val isSyncing: Boolean = false, + val syncProgress: Float = 0f, ) { val isEmpty: Boolean get() = modules.isEmpty() - val hasMore: Boolean - get() = page < pageCount + val isFullySynced: Boolean + get() = page >= pageCount && pageCount > 0 } diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt index a8b87a02b3..64356b8773 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt @@ -42,6 +42,7 @@ import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Icon import androidx.compose.material3.IconButton +import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.Text @@ -114,7 +115,7 @@ fun RepoScreen( StoreHeader( catalog = catalog, updates = updates, - search = { StoreSearch(query, viewModel, sort, priorities, channel) }, + search = { StoreSearch(query, viewModel, sort, priorities, channel, isSyncing = catalog.isSyncing) }, actions = actions, ) @@ -156,52 +157,63 @@ fun RepoScreen( return@Column } - val listState = rememberLazyListState() - LaunchedEffect(listState, catalog.hasMore, catalog.isLoadingMore) { - snapshotFlow { - val lastVisibleItem = listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: 0 - val totalItems = listState.layoutInfo.totalItemsCount - lastVisibleItem >= totalItems - 5 - }.collect { nearBottom -> - if (nearBottom && catalog.hasMore && !catalog.isLoadingMore) { - viewModel.loadMore() + Box(modifier = Modifier.weight(1f).fillMaxWidth()) { + PullToRefreshBox(isRefreshing = refreshing, onRefresh = viewModel::refresh) { + LazyColumn( + modifier = Modifier.fillMaxSize(), + contentPadding = PaddingValues(bottom = 20.dp), + ) { + if (entries.isEmpty()) { + // Inside the list rather than beside it: an empty Box has nothing to + // scroll, and pull-to-refresh is exactly what the reader wants when the + // reason the list is empty is that the network was down. + item { + EmptyState( + modifier = Modifier.fillParentMaxSize(), + catalog = catalog, + filtered = false, + ) + } + } else { + items(entries, key = { it.module.name }) { entry -> + StoreRow(entry = entry, onClick = { onModuleClick(entry.module.name) }) + HorizontalDivider(color = MaterialTheme.colorScheme.surfaceVariant) + } + } } } } - PullToRefreshBox(isRefreshing = refreshing, onRefresh = viewModel::refresh) { - LazyColumn( - state = listState, - modifier = Modifier.fillMaxSize(), - contentPadding = PaddingValues(bottom = 20.dp), + // Sync progress bar positioned above the bottom bar + if (catalog.isSyncing) { + Column( + modifier = + Modifier.fillMaxWidth() + .padding(horizontal = 20.dp, vertical = 6.dp) ) { - if (entries.isEmpty()) { - // Inside the list rather than beside it: an empty Box has nothing to - // scroll, and pull-to-refresh is exactly what the reader wants when the - // reason the list is empty is that the network was down. - item { - EmptyState( - modifier = Modifier.fillParentMaxSize(), - catalog = catalog, - filtered = false, - ) - } - } else { - items(entries, key = { it.module.name }) { entry -> - StoreRow(entry = entry, onClick = { onModuleClick(entry.module.name) }) - HorizontalDivider(color = MaterialTheme.colorScheme.surfaceVariant) - } - if (catalog.isLoadingMore) { - item { - Box( - modifier = Modifier.fillMaxWidth().padding(16.dp), - contentAlignment = Alignment.Center, - ) { - CircularProgressIndicator(modifier = Modifier.size(24.dp), strokeWidth = 2.dp) - } - } - } + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically, + ) { + Text( + text = "Syncing repository (${catalog.page}/${catalog.pageCount})...", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + Text( + text = "${(catalog.syncProgress * 100).toInt()}%", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.primary, + ) } + Spacer(Modifier.height(4.dp)) + LinearProgressIndicator( + progress = { catalog.syncProgress }, + modifier = Modifier.fillMaxWidth().height(3.dp), + color = MaterialTheme.colorScheme.primary, + trackColor = MaterialTheme.colorScheme.surfaceVariant, + ) } } } @@ -257,6 +269,7 @@ private fun StoreSearch( sort: StoreSort, priorities: List, channel: StoreChannel, + isSyncing: Boolean, ) { SearchField( query = query, @@ -270,6 +283,7 @@ private fun StoreSearch( onTogglePriority = viewModel::togglePriority, channel = channel, onChannelChange = viewModel::setChannel, + isSyncing = isSyncing, ) } } @@ -292,6 +306,7 @@ private fun StoreFilterButton( onTogglePriority: (StorePriority) -> Unit, channel: StoreChannel, onChannelChange: (StoreChannel) -> Unit, + isSyncing: Boolean = false, ) { var sheetOpen by remember { mutableStateOf(false) } val narrowed = @@ -299,14 +314,17 @@ private fun StoreFilterButton( channel != StoreChannel.Stable || priorities != listOf(StorePriority.Updates) - IconButton(onClick = { sheetOpen = true }) { + IconButton(onClick = { if (!isSyncing) sheetOpen = true }, enabled = !isSyncing) { BadgedBox(badge = { if (narrowed) Badge(modifier = Modifier.size(6.dp)) }) { Icon( Icons.Rounded.FilterList, contentDescription = stringResource(R.string.store_filter), tint = - if (narrowed) MaterialTheme.colorScheme.primary - else MaterialTheme.colorScheme.onSurfaceVariant, + when { + isSyncing -> MaterialTheme.colorScheme.outlineVariant + narrowed -> MaterialTheme.colorScheme.primary + else -> MaterialTheme.colorScheme.onSurfaceVariant + }, ) } } diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt index b2e02e4568..e5080dcb9c 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt @@ -213,10 +213,6 @@ class RepoViewModel( viewModelScope.launch { dataSource.refresh(force = true) } } - fun loadMore() { - viewModelScope.launch { dataSource.loadMore() } - } - /** The three controls that reorder the list, as one value so `combine` stays readable. */ private data class View( val sort: StoreSort, diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt index 0d6a9f3fa0..7e50d2cb53 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt @@ -24,9 +24,6 @@ interface StoreDataSource { /** Reload the catalogue from the network; [force] bypasses any cache. */ suspend fun refresh(force: Boolean = false) - /** Load the next page of modules in the catalogue. */ - suspend fun loadMore() {} - /** The full record for one module (README, all releases); null when unreachable. */ suspend fun details(packageName: String): OnlineModule? diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt index 769f31d633..7897f843d2 100644 --- a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt +++ b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt @@ -93,8 +93,7 @@ class RepoRepository( * because a user who pulls is telling us they think what they are looking at is stale. */ override suspend fun refresh(force: Boolean) { - // A second caller during a refresh is a no-op rather than a queued duplicate of a 1.2 MB - // download. + // A second caller during a refresh is a no-op rather than a queued duplicate if (!refreshing.tryLock()) return try { _isRefreshing.value = true @@ -107,56 +106,65 @@ class RepoRepository( .build() launch { fetchSearchIndex(freshness) } - val fetched = fetchPaginatedCatalog(1, freshness) + + // Fetch page 1 immediately to show the initial modules instantly + val firstPage = fetchPaginatedCatalog(1, freshness) ?: LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, freshness) } - if (fetched != null) { - _catalog.value = fetched + + if (firstPage != null) { + val initialCatalog = firstPage.copy( + isSyncing = firstPage.page < firstPage.pageCount, + syncProgress = if (firstPage.pageCount > 0) 1f / firstPage.pageCount else 1f, + ) + _catalog.value = initialCatalog + + // If more pages exist, sync them sequentially in the background + if (firstPage.page < firstPage.pageCount) { + syncRemainingPages(firstPage, freshness) + } return@withContext } - // Every mirror failed. Before reporting nothing, ask the cache — the bytes from - // the last successful visit are usually still on disk, and a stale catalogue is - // far more use than an empty screen. + // If network failed, check cache for page 1 val cached = fetchPaginatedCatalog(1, CacheControl.FORCE_CACHE) ?: LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, CacheControl.FORCE_CACHE) } + when { - cached != null -> _catalog.value = cached.copy(fromCache = true) - // Nothing on the network and nothing on disk. `loaded` still flips, so the - // screen can say the repository is unreachable instead of sitting forever on - // a spinner that means nothing. - else -> _catalog.value = _catalog.value.copy(loaded = true) + cached != null -> _catalog.value = cached.copy(fromCache = true, isSyncing = false) + else -> _catalog.value = _catalog.value.copy(loaded = true, isSyncing = false) } } } finally { - // In a `finally` rather than after the block: a cancelled `viewModelScope` — a - // rotation mid-refresh — would otherwise strand the flag at true, and with - // pull-to-refresh reading it that is a spinner that never stops. _isRefreshing.value = false refreshing.unlock() } } - override suspend fun loadMore() { - val current = _catalog.value - if (!current.hasMore || current.isLoadingMore) return - _catalog.value = current.copy(isLoadingMore = true) - withContext(Dispatchers.IO) { - val nextPage = current.page + 1 - val freshness = CacheControl.Builder().maxAge(CATALOG_MAX_AGE_MINUTES, TimeUnit.MINUTES).build() - val result = fetchPaginatedCatalog(nextPage, freshness) - if (result != null) { - val combined = (current.modules + result.modules).distinctBy { it.name } - _catalog.value = current.copy( - modules = combined, - page = result.page, - pageCount = result.pageCount, - total = result.total, - isLoadingMore = false, + private suspend fun syncRemainingPages(initial: StoreCatalog, cacheControl: CacheControl) { + var accumulated = initial.modules.toMutableList() + val totalPages = initial.pageCount + for (p in (initial.page + 1)..totalPages) { + val pageResult = fetchPaginatedCatalog(p, cacheControl) + if (pageResult != null) { + accumulated.addAll(pageResult.modules) + val distinct = usable(accumulated) + accumulated = distinct.toMutableList() + val progress = p.toFloat() / totalPages + _catalog.value = _catalog.value.copy( + modules = distinct, + page = p, + pageCount = totalPages, + total = pageResult.total, + isSyncing = p < totalPages, + syncProgress = progress, ) } else { - _catalog.value = current.copy(isLoadingMore = false) + // If a page fails, stop syncing and keep current state + _catalog.value = _catalog.value.copy(isSyncing = false) + break } } + _catalog.value = _catalog.value.copy(isSyncing = false, syncProgress = 1f) } /** @@ -416,8 +424,8 @@ class RepoRepository( val DETAIL_MIRRORS = listOf("https://modules.lsposed.org/", "https://backup.modules.lsposed.org/") - /** Matches the server's own `cache-control: max-age=600`, so revalidation stays free. */ - const val CATALOG_MAX_AGE_MINUTES = 10 + /** Matches 1 hour cache duration so full catalogue sync only happens once an hour or on pull-to-refresh. */ + const val CATALOG_MAX_AGE_MINUTES = 60 /** Longer: a module's release history changes far less often than the index does. */ const val DETAIL_MAX_AGE_MINUTES = 60 From 6b0553b0e4312a804e3494415f48ba47a50a0788 Mon Sep 17 00:00:00 2001 From: Mubashar Hussain Date: Tue, 20 Oct 2026 14:14:57 +0500 Subject: [PATCH 5/9] refactor: remove search index and suggestions feature from store repository and UI --- .../org/matrix/vector/ui/store/RepoModels.kt | 24 ------ .../org/matrix/vector/ui/store/RepoScreen.kt | 75 +------------------ .../matrix/vector/ui/store/RepoViewModel.kt | 12 --- .../matrix/vector/ui/store/StoreDataSource.kt | 4 - .../manager/data/repository/RepoRepository.kt | 40 ---------- 5 files changed, 2 insertions(+), 153 deletions(-) diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt index 99792865cf..c1ed022075 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt @@ -50,30 +50,6 @@ data class OnlineModule( get() = url ?: "https://github.com/Xposed-Modules-Repo/$name" } -/** - * An entry from `https://modules.lsposed.org/search.json`. - * - * Provides a lightweight index of all available modules for instant client-side - * global search and auto-complete. - */ -data class StoreSearchItem( - @SerializedName("name") val name: String, - @SerializedName("description") val description: String? = null, - @SerializedName("summary") val summary: String? = null, - @SerializedName("readmeExcerpt") val readmeExcerpt: String? = null, - @SerializedName("authors") val authors: String? = null, -) { - val title: String - get() = description?.takeIf { it.isNotBlank() } ?: name - - fun matches(query: String): Boolean { - if (query.isBlank()) return false - return title.contains(query, ignoreCase = true) || - name.contains(query, ignoreCase = true) || - summary?.contains(query, ignoreCase = true) == true || - authors?.contains(query, ignoreCase = true) == true - } -} data class Collaborator( @SerializedName("login") val login: String?, diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt index 64356b8773..4e540aa5d0 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt @@ -108,8 +108,6 @@ fun RepoScreen( val priorities by viewModel.priorities.collectAsState() val channel by viewModel.channel.collectAsState() - val searchSuggestions by viewModel.searchSuggestions.collectAsState() - Scaffold { innerPadding -> Column(modifier = Modifier.padding(innerPadding).fillMaxSize()) { StoreHeader( @@ -121,33 +119,6 @@ fun RepoScreen( Spacer(Modifier.height(4.dp)) - // When user is searching, display real-time global search suggestions from search.json - if (query.isNotBlank()) { - LazyColumn( - modifier = Modifier.fillMaxSize(), - contentPadding = PaddingValues(bottom = 20.dp), - ) { - if (searchSuggestions.isEmpty() && entries.isEmpty()) { - item { - EmptyState( - modifier = Modifier.fillParentMaxSize(), - catalog = catalog, - filtered = true, - ) - } - } else { - items(searchSuggestions, key = { it.name }) { item -> - SearchSuggestionRow( - item = item, - onClick = { onModuleClick(item.name) }, - ) - HorizontalDivider(color = MaterialTheme.colorScheme.surfaceVariant) - } - } - } - return@Column - } - // Nothing has ever loaded and a fetch is running: the one moment a spinner says // something the list could not say better itself. if (!catalog.loaded && entries.isEmpty()) { @@ -171,7 +142,7 @@ fun RepoScreen( EmptyState( modifier = Modifier.fillParentMaxSize(), catalog = catalog, - filtered = false, + filtered = query.isNotBlank(), ) } } else { @@ -197,7 +168,7 @@ fun RepoScreen( verticalAlignment = Alignment.CenterVertically, ) { Text( - text = "Syncing repository (${catalog.page}/${catalog.pageCount})...", + text = "Syncing modules (${catalog.page}/${catalog.pageCount})...", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) @@ -502,48 +473,6 @@ private fun StoreRow(entry: StoreEntry, onClick: () -> Unit) { } } -@Composable -private fun SearchSuggestionRow(item: StoreSearchItem, onClick: () -> Unit) { - val colors = MaterialTheme.colorScheme - Column( - modifier = - Modifier.fillMaxWidth() - .clickable(onClick = onClick) - .padding(horizontal = 20.dp, vertical = 12.dp) - ) { - Text( - text = item.title, - style = MaterialTheme.typography.titleMedium, - maxLines = 2, - overflow = TextOverflow.Ellipsis, - ) - Text( - text = item.name, - style = Mono, - color = colors.onSurfaceVariant, - ) - if (!item.summary.isNullOrBlank()) { - Spacer(Modifier.height(6.dp)) - Text( - text = item.summary, - style = MaterialTheme.typography.bodyMedium, - color = colors.onSurfaceVariant, - maxLines = 2, - overflow = TextOverflow.Ellipsis, - ) - } - if (!item.authors.isNullOrBlank()) { - Spacer(Modifier.height(6.dp)) - Text( - text = item.authors, - style = MaterialTheme.typography.labelSmall, - color = colors.onSurfaceVariant, - maxLines = 1, - overflow = TextOverflow.Ellipsis, - ) - } - } -} @Composable private fun RowBadge(icon: ImageVector, text: String, tint: Color, modifier: Modifier = Modifier) { diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt index e5080dcb9c..6f8f962534 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt @@ -170,18 +170,6 @@ class RepoViewModel( .flowOn(Dispatchers.Default) .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList()) - /** - * Real-time search suggestions over the complete index of all ~1,180+ modules. - * When query is empty, suggestions are empty. - */ - val searchSuggestions: StateFlow> = - combine(dataSource.searchIndex, _query) { index, query -> - if (query.isBlank()) emptyList() - else index.filter { it.matches(query) }.take(20) - } - .flowOn(Dispatchers.Default) - .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList()) - init { // The catalogue outlives this ViewModel — switching tabs destroys the nav entry — so a // return to the Store paints from what is already in memory and only fetches when there is diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt index 7e50d2cb53..ff969b535f 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt @@ -17,10 +17,6 @@ interface StoreDataSource { /** Installed module versions on this device, keyed by package name. */ val installedVersions: StateFlow> - /** Complete search index for all modules, used for instant suggestion and search. */ - val searchIndex: StateFlow> - get() = kotlinx.coroutines.flow.MutableStateFlow(emptyList()) - /** Reload the catalogue from the network; [force] bypasses any cache. */ suspend fun refresh(force: Boolean = false) diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt index 7897f843d2..c40eb61463 100644 --- a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt +++ b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt @@ -20,7 +20,6 @@ import org.matrix.vector.ui.store.StoreDataSource import org.matrix.vector.ui.store.OnlineModule import org.matrix.vector.ui.store.RepoVersion import org.matrix.vector.ui.store.StoreCatalog -import org.matrix.vector.ui.store.StoreSearchItem import org.matrix.vector.manager.data.model.versionCodeCompat import org.matrix.vector.manager.di.ServiceLocator import org.matrix.vector.manager.ipc.DaemonClient @@ -58,9 +57,6 @@ class RepoRepository( private val _isRefreshing = MutableStateFlow(false) override val isRefreshing: StateFlow = _isRefreshing.asStateFlow() - private val _searchIndex = MutableStateFlow>(emptyList()) - override val searchIndex: StateFlow> = _searchIndex.asStateFlow() - private val _installed = MutableStateFlow>(emptyMap()) /** @@ -105,8 +101,6 @@ class RepoRepository( .maxAge(CATALOG_MAX_AGE_MINUTES, TimeUnit.MINUTES) .build() - launch { fetchSearchIndex(freshness) } - // Fetch page 1 immediately to show the initial modules instantly val firstPage = fetchPaginatedCatalog(1, freshness) ?: LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, freshness) } @@ -278,40 +272,6 @@ class RepoRepository( } } - private fun fetchSearchIndex(cacheControl: CacheControl) { - val url = "https://modules.lsposed.org/search.json" - try { - client.newCall(request(url, cacheControl)).execute().use { response -> - if (response.isSuccessful) { - val parsed = parseSearchIndex(response) - if (parsed.isNotEmpty()) { - logI("store: loaded ${parsed.size} search items from $url") - _searchIndex.value = parsed - } - } - } - } catch (e: Exception) { - logW("store: search index from $url unavailable", e) - // Try cache fallback - if (cacheControl != CacheControl.FORCE_CACHE) { - fetchSearchIndex(CacheControl.FORCE_CACHE) - } - } - } - - private fun parseSearchIndex(response: Response): List { - val items = ArrayList(1200) - JsonReader(response.body.charStream()).use { reader -> - reader.beginArray() - while (reader.hasNext()) { - val element = JsonParser.parseReader(reader) - val item = runCatching { gson.fromJson(element, StoreSearchItem::class.java) } - if (item.isSuccess) item.getOrNull()?.let(items::add) - } - reader.endArray() - } - return items - } private fun fetchPaginatedCatalog(page: Int, cacheControl: CacheControl): StoreCatalog? { val url = "https://modules.lsposed.org/module-list/$page.json" From 40e3cc1ccbd4edaee32976284ff88fdd298e5bcc Mon Sep 17 00:00:00 2001 From: Mubashar Hussain Date: Tue, 20 Oct 2026 14:23:38 +0500 Subject: [PATCH 6/9] store: enforce 1-hour cache freshness and prevent automatic resync on tab switch --- .../manager/data/repository/RepoRepository.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt index c40eb61463..e977faaec4 100644 --- a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt +++ b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt @@ -58,6 +58,7 @@ class RepoRepository( override val isRefreshing: StateFlow = _isRefreshing.asStateFlow() private val _installed = MutableStateFlow>(emptyMap()) + private var lastFullSyncTimeMillis: Long = 0L /** * What each package on this device is at, keyed by package name. @@ -94,6 +95,17 @@ class RepoRepository( try { _isRefreshing.value = true withContext(Dispatchers.IO) { + // If not forcing a refresh, check if we already have a fully synced catalog within 1 hour + if (!force) { + val current = _catalog.value + val ageMillis = System.currentTimeMillis() - lastFullSyncTimeMillis + val isFresh = lastFullSyncTimeMillis > 0L && ageMillis < TimeUnit.MINUTES.toMillis(CATALOG_MAX_AGE_MINUTES.toLong()) + if (current.isFullySynced && isFresh) { + logI("store: catalog is fresh (age: ${TimeUnit.MILLISECONDS.toMinutes(ageMillis)}m), skipping sync") + return@withContext + } + } + val freshness = if (force) CacheControl.FORCE_NETWORK else @@ -115,6 +127,8 @@ class RepoRepository( // If more pages exist, sync them sequentially in the background if (firstPage.page < firstPage.pageCount) { syncRemainingPages(firstPage, freshness) + } else { + lastFullSyncTimeMillis = System.currentTimeMillis() } return@withContext } @@ -159,6 +173,9 @@ class RepoRepository( } } _catalog.value = _catalog.value.copy(isSyncing = false, syncProgress = 1f) + if (_catalog.value.isFullySynced) { + lastFullSyncTimeMillis = System.currentTimeMillis() + } } /** From 6d925b85cc3330511e24745eb10cf42888de714a Mon Sep 17 00:00:00 2001 From: JingMatrix Date: Tue, 10 Nov 2026 11:27:13 +0100 Subject: [PATCH 7/9] Correct the mirror's catalogue from the site's pages, not replace it The premise this branch started from holds only in part. The site does answer modules.json with a 403, and module-list/.json is current to the minute. But the mirror is not an archive: it is regenerated daily and runs about three days behind, which is all that keeps a new release out of the Store, and it is the only source whose entries carry a release with its APK. A page entry names latestRelease and nothing else, so a catalogue read from the pages alone left releasesOn() empty for every module: the Modules screen's update sheet could tick nothing, and a module's own sheet said "This release has no APK to install" of a release that has one. So the mirror stays the base and the pages correct it. They are sorted newest release first and the mirror is only ever behind, so what it lacks sits at the front: StoreApi.catalog() reads until a page agrees with the mirror entirely, five pages today out of 45. A corrected entry drops the mirror's releases instead of keeping an APK that is no longer the version it names, and RepoRepository fetches the detail record for the installed ones, which is what the two sheets read. A page corrects an entry only where what it states is no older than what the mirror states. That the mirror is the one behind is what was measured, not what is promised: a site that stopped being rebuilt would otherwise take back every version the mirror had learnt since. Pull-to-refresh still reads to the last page, and so does a start with the mirror out of reach. Those read every page after the first at once: the site spends a third of a second on each, which in a row was fourteen seconds under the spinner and together is about one. The count of pages is the site's to state, so it is bounded before it decides how many requests go out. refresh() replaced the catalogue with page one before reading the rest, so every pull collapsed the list to thirty rows and took the update badges of the Modules screen with it. Only the mirror's list is now published early, and only onto an empty screen. One page failing left a short catalogue marked as loaded, indistinguishable from a whole one and kept until the next pull; a read that breaks off without the mirror behind it is now a failure, and falls to the cache. Publishing the mirror's list first has a cost the screen has to answer. What the site then adds is the newest releases, which sort above everything showing, and a LazyColumn keeps its first visible row where it is when rows arrive above it. A reader who opened the Store while it loaded was left looking at the mirror's newest, with today's releases out of sight above. RepoScreen now asks for the top again whenever the first row changes under a list that has not been scrolled. That cache could not serve the pages. The site marks them must- revalidate, under which OkHttp refuses a FORCE_CACHE read however stale the caller allows, so the replay an offline start depends on came back empty. OkHttp also stores a 404 under the same key as the 200 before it, so a retired host would overwrite the last good catalogue with its error page. A network interceptor on the Store's client relaxes the one and keeps the other out. With the mirror's snapshot still fresh on disk and the site unreachable, the pages saved beside it are used, provided they were read no earlier than it was. The one-hour gate was a timestamp beside the catalogue; it is now read off loadedAtMillis, at the mirror's own ten minutes. It is still needed, because the pages never answer 304 and an unforced refresh runs on every launch. A beta published with no stable release beside it changes no latestRelease, so correct() compares latestBetaReleaseTime as well, which is all a page says of a beta. It cannot name the new one, so the entry is left naming none, and the detail record names it. releasesOn() returned a detail record's releases in the order they came on the prerelease channel. A detail record has no betaReleases: its betas sit in releases, flagged, and the whole list is by date, while latestOn() names the highest code. For io.github.wumisc.antiburn, stable code 10 published after beta code 45, the offer named one release and the first element was another. That channel is now ordered by code whichever shape the payload has. Also here: the progress display is gone. With the pages read together there is nothing left to step through, its label was English text in the composable, which the translation gate does not see, and the filter button it disabled had no reason to be. A detail record is accepted only when it names the package it was asked for. One gap: such a beta is listed by its stable release's date, so an unforced refresh, which stops at the first page that agrees, sees it only if it falls in the pages read. A pull finds it. Reported in discussion #1082. --- .../vector/ui/store/RepoDetailsViewModel.kt | 3 +- .../org/matrix/vector/ui/store/RepoModels.kt | 13 +- .../org/matrix/vector/ui/store/RepoScreen.kt | 109 ++--- .../matrix/vector/ui/store/RepoViewModel.kt | 23 +- .../manager/data/repository/RepoRepository.kt | 381 ++++++------------ .../manager/data/repository/StoreApi.kt | 333 +++++++++++++++ 6 files changed, 514 insertions(+), 348 deletions(-) create mode 100644 manager/src/main/kotlin/org/matrix/vector/manager/data/repository/StoreApi.kt diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoDetailsViewModel.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoDetailsViewModel.kt index 67a24d1613..640042faa2 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoDetailsViewModel.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoDetailsViewModel.kt @@ -27,7 +27,8 @@ enum class DetailFetch { * the whole design of this screen: the catalogue already carries the description, the summary, the * scope, the collaborators and the newest release *with its APK*, so the page can paint — and be * installed from — before any request is made, and a failed request costs the README rather than - * the page. + * the page. The exception is an entry the site has corrected since the mirror's snapshot: it names + * its version and carries no release, so that page installs once its record has arrived. */ data class RepoDetailsState( val module: OnlineModule? = null, diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt index c1ed022075..0e86e17603 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt @@ -17,6 +17,10 @@ import com.google.gson.annotations.SerializedName * **The list payload is nearly a detail payload.** Each list entry already carries exactly one * release — the newest — with its `.apk` asset and download URL. Installing the current version of * a module needs no second request, which is what lets the Store work on a bad connection. + * + * **Except where the list is the site's.** An entry from `module-list/.json` names its + * `latestRelease` and carries no release, scope or collaborators at all, so `releases` being empty + * means "ask for the detail record", not "nothing was ever published". */ data class OnlineModule( @SerializedName("name") val name: String, @@ -50,7 +54,6 @@ data class OnlineModule( get() = url ?: "https://github.com/Xposed-Modules-Repo/$name" } - data class Collaborator( @SerializedName("login") val login: String?, @SerializedName("name") val name: String?, @@ -269,15 +272,7 @@ data class StoreCatalog( val loaded: Boolean = false, val fromCache: Boolean = false, val loadedAtMillis: Long = 0L, - val page: Int = 1, - val pageCount: Int = 1, - val total: Int = 0, - val isSyncing: Boolean = false, - val syncProgress: Float = 0f, ) { val isEmpty: Boolean get() = modules.isEmpty() - - val isFullySynced: Boolean - get() = page >= pageCount && pageCount > 0 } diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt index 4e540aa5d0..d76c19bd31 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoScreen.kt @@ -42,19 +42,17 @@ import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Icon import androidx.compose.material3.IconButton -import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.pulltorefresh.PullToRefreshBox import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.SideEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue -import androidx.compose.runtime.snapshotFlow import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -113,7 +111,7 @@ fun RepoScreen( StoreHeader( catalog = catalog, updates = updates, - search = { StoreSearch(query, viewModel, sort, priorities, channel, isSyncing = catalog.isSyncing) }, + search = { StoreSearch(query, viewModel, sort, priorities, channel) }, actions = actions, ) @@ -128,63 +126,44 @@ fun RepoScreen( return@Column } - Box(modifier = Modifier.weight(1f).fillMaxWidth()) { - PullToRefreshBox(isRefreshing = refreshing, onRefresh = viewModel::refresh) { - LazyColumn( - modifier = Modifier.fillMaxSize(), - contentPadding = PaddingValues(bottom = 20.dp), - ) { - if (entries.isEmpty()) { - // Inside the list rather than beside it: an empty Box has nothing to - // scroll, and pull-to-refresh is exactly what the reader wants when the - // reason the list is empty is that the network was down. - item { - EmptyState( - modifier = Modifier.fillParentMaxSize(), - catalog = catalog, - filtered = query.isNotBlank(), - ) - } - } else { - items(entries, key = { it.module.name }) { entry -> - StoreRow(entry = entry, onClick = { onModuleClick(entry.module.name) }) - HorizontalDivider(color = MaterialTheme.colorScheme.surfaceVariant) - } - } - } + // A list holds its first visible row where it is when rows arrive above it, and rows do: + // the mirror's catalogue is shown before the site has corrected it, and what the site + // adds is the newest releases. A reader who has not scrolled is at the top to see what + // is newest, so the top is where they stay. + val listState = rememberLazyListState() + // Read out here on purpose: the rows are otherwise read only inside the list's own + // content, so a new first row would not recompose this scope and the effect, which is + // what has to run before the list is measured again, would not run at all. + val firstRow = entries.firstOrNull()?.module?.name + SideEffect { + if (firstRow != null && !listState.canScrollBackward) { + listState.requestScrollToItem(0) } } - // Sync progress bar positioned above the bottom bar - if (catalog.isSyncing) { - Column( - modifier = - Modifier.fillMaxWidth() - .padding(horizontal = 20.dp, vertical = 6.dp) + PullToRefreshBox(isRefreshing = refreshing, onRefresh = viewModel::refresh) { + LazyColumn( + state = listState, + modifier = Modifier.fillMaxSize(), + contentPadding = PaddingValues(bottom = 20.dp), ) { - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.SpaceBetween, - verticalAlignment = Alignment.CenterVertically, - ) { - Text( - text = "Syncing modules (${catalog.page}/${catalog.pageCount})...", - style = MaterialTheme.typography.labelSmall, - color = MaterialTheme.colorScheme.onSurfaceVariant, - ) - Text( - text = "${(catalog.syncProgress * 100).toInt()}%", - style = MaterialTheme.typography.labelSmall, - color = MaterialTheme.colorScheme.primary, - ) + if (entries.isEmpty()) { + // Inside the list rather than beside it: an empty Box has nothing to + // scroll, and pull-to-refresh is exactly what the reader wants when the + // reason the list is empty is that the network was down. + item { + EmptyState( + modifier = Modifier.fillParentMaxSize(), + catalog = catalog, + filtered = query.isNotBlank(), + ) + } + } else { + items(entries, key = { it.module.name }) { entry -> + StoreRow(entry = entry, onClick = { onModuleClick(entry.module.name) }) + HorizontalDivider(color = MaterialTheme.colorScheme.surfaceVariant) + } } - Spacer(Modifier.height(4.dp)) - LinearProgressIndicator( - progress = { catalog.syncProgress }, - modifier = Modifier.fillMaxWidth().height(3.dp), - color = MaterialTheme.colorScheme.primary, - trackColor = MaterialTheme.colorScheme.surfaceVariant, - ) } } } @@ -204,12 +183,11 @@ private fun StoreHeader( actions = actions, description = { if (catalog.modules.isNotEmpty()) { - val count = if (catalog.total > 0) catalog.total else catalog.modules.size val total = context.resources.getQuantityString( R.plurals.store_module_count, - count, - count, + catalog.modules.size, + catalog.modules.size, ) // "Up to date" is stated in words rather than as a zero, because a zero in a row // of counts reads as a failure to load rather than as good news. @@ -240,7 +218,6 @@ private fun StoreSearch( sort: StoreSort, priorities: List, channel: StoreChannel, - isSyncing: Boolean, ) { SearchField( query = query, @@ -254,7 +231,6 @@ private fun StoreSearch( onTogglePriority = viewModel::togglePriority, channel = channel, onChannelChange = viewModel::setChannel, - isSyncing = isSyncing, ) } } @@ -277,7 +253,6 @@ private fun StoreFilterButton( onTogglePriority: (StorePriority) -> Unit, channel: StoreChannel, onChannelChange: (StoreChannel) -> Unit, - isSyncing: Boolean = false, ) { var sheetOpen by remember { mutableStateOf(false) } val narrowed = @@ -285,17 +260,14 @@ private fun StoreFilterButton( channel != StoreChannel.Stable || priorities != listOf(StorePriority.Updates) - IconButton(onClick = { if (!isSyncing) sheetOpen = true }, enabled = !isSyncing) { + IconButton(onClick = { sheetOpen = true }) { BadgedBox(badge = { if (narrowed) Badge(modifier = Modifier.size(6.dp)) }) { Icon( Icons.Rounded.FilterList, contentDescription = stringResource(R.string.store_filter), tint = - when { - isSyncing -> MaterialTheme.colorScheme.outlineVariant - narrowed -> MaterialTheme.colorScheme.primary - else -> MaterialTheme.colorScheme.onSurfaceVariant - }, + if (narrowed) MaterialTheme.colorScheme.primary + else MaterialTheme.colorScheme.onSurfaceVariant, ) } } @@ -473,7 +445,6 @@ private fun StoreRow(entry: StoreEntry, onClick: () -> Unit) { } } - @Composable private fun RowBadge(icon: ImageVector, text: String, tint: Color, modifier: Modifier = Modifier) { Row(modifier = modifier, verticalAlignment = Alignment.CenterVertically) { diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt index 6f8f962534..6cfe003d3c 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt @@ -54,27 +54,28 @@ enum class StoreChannel(val preference: String) { /** * Every release the chosen channel admits, newest first. * - * **A beta is not a flagged element of `releases`; it is a different array.** In the live catalogue - * not one entry of any module's `releases` carries `isPrerelease`, and each of the 14 modules that - * publish a beta keeps it exclusively in `betaReleases`. Selecting the prerelease channel by - * filtering `releases` on `isPrerelease` therefore matches nothing at all, and the install bar - * takes the newest release with an APK straight off this list. + * **Where a beta lives depends on the payload.** In the catalogue it is not a flagged element of + * `releases` but a different array: not one list entry's `releases` carries `isPrerelease`, and + * each module that publishes a beta keeps it exclusively in `betaReleases`. In a detail record it + * is the reverse: there is no `betaReleases`, and the betas sit in `releases` with `isPrerelease` + * set. Both shapes are read here, because the install bar takes the newest release with an APK + * straight off this list whichever payload it came from. * - * Merged, the order has to come from the version code rather than from either array's position, - * because the two are sorted independently and a beta is not automatically newer: of today's 14, - * `com.luoshui.paycardeditor` publishes beta code 1 against stable code 8. + * On the prerelease channel the order has to come from the version code rather than from position + * in either shape. The two arrays are sorted independently, a detail record is sorted by date, and + * a beta is not automatically newer: `com.luoshui.paycardeditor` publishes beta code 1 against + * stable code 8, and `io.github.wumisc.antiburn` a stable code 10 two weeks after beta + * code 45. [latestOn] names the highest code, so the first element here has to be that release. */ public fun OnlineModule.releasesOn(channel: StoreChannel): List { val published = releases.orEmpty().filter { it.isDraft != true } if (channel == StoreChannel.Prerelease) { val beta = betaReleases.orEmpty().filter { it.isDraft != true } - if (beta.isEmpty()) return published return (published + beta) .distinctBy { it.tagName ?: it.id ?: it.name } .sortedByDescending { it.version?.versionCode ?: Long.MIN_VALUE } } - // Defensive rather than load-bearing, and it stays because the mirror's shape is not ours to - // promise: a module that has only ever prereleased still has releases worth listing. + // A module that has only ever prereleased still has releases worth listing. val stable = published.filter { it.isPrerelease != true } return stable.ifEmpty { published } } diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt index e977faaec4..9156aeb5b7 100644 --- a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt +++ b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt @@ -1,21 +1,20 @@ package org.matrix.vector.manager.data.repository -import com.google.gson.Gson -import com.google.gson.JsonParser -import com.google.gson.stream.JsonReader import java.util.concurrent.TimeUnit import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.withContext import okhttp3.CacheControl import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.Response import org.matrix.vector.ui.store.StoreDataSource import org.matrix.vector.ui.store.OnlineModule import org.matrix.vector.ui.store.RepoVersion @@ -29,28 +28,29 @@ import org.matrix.vector.manager.logW /** * The Store's data: the online catalogue, and what this device already has of it. * - * **The mirror list is two lists, and that is not an oversight.** The full `modules.json` is served - * Per-module `module/.json` is served by both hosts, with `modules.lsposed.org` providing the latest data and `backup.modules.lsposed.org` serving as fallback. + * Where the catalogue comes from, and why from two hosts, is [StoreApi]'s business. What is decided + * here is when to ask and what to do when asking fails. * * **Caching is declared, not hoped for.** Every request states its own freshness, so the 16 MB disk - * cache in `HttpClientFactory` is actually used: the catalogue revalidates against the server's own - * ten-minute `max-age` and its ETag, pull-to-refresh forces the network, and when every mirror - * fails the same request is replayed against the cache alone. That last step is why a cold start - * with no network renders the last known catalogue rather than an error, and it is also what gives - * the DNS-over-HTTPS setting an effect here, since the shared client is the one carrying the DoH - * resolver. + * cache in `HttpClientFactory` is actually used: the mirror's snapshot revalidates against the + * server's own ten-minute `max-age` and its ETag, pull-to-refresh forces the network, and when the + * network yields nothing the same requests are replayed against the cache alone. That last step is + * why a cold start with no network renders the last known catalogue rather than an error, and it is + * also what gives the DNS-over-HTTPS setting an effect here, since the shared client is the one + * carrying the DoH resolver. * * There is deliberately **no snapshot file** of our own, unlike `GitHubRepository`. The OkHttp - * cache already holds these exact bytes; a 1.2 MB duplicate in the same cache directory would buy + * cache already holds these exact bytes; a 2 MB duplicate in the same cache directory would buy * nothing but a second thing to keep in sync. */ class RepoRepository( - private val client: OkHttpClient, + client: OkHttpClient, private val daemon: DaemonClient, private val scope: CoroutineScope, - private val gson: Gson = Gson(), ) : StoreDataSource { + private val api = StoreApi(client) + private val _catalog = MutableStateFlow(StoreCatalog()) override val catalog: StateFlow = _catalog.asStateFlow() @@ -58,7 +58,9 @@ class RepoRepository( override val isRefreshing: StateFlow = _isRefreshing.asStateFlow() private val _installed = MutableStateFlow>(emptyMap()) - private var lastFullSyncTimeMillis: Long = 0L + + /** Detail records fetched for installed modules, by package name. See [completeInstalled]. */ + @Volatile private var records = emptyMap() /** * What each package on this device is at, keyed by package name. @@ -81,31 +83,26 @@ class RepoRepository( // because the daemon died would state something untrue. ServiceLocator.service.collect { service -> if (service != null) loadInstalled() } } + scope.launch { completeInstalled() } } /** * Reloads the catalogue. * - * [force] is pull-to-refresh: it bypasses the cache rather than revalidating against it, - * because a user who pulls is telling us they think what they are looking at is stale. + * [force] is pull-to-refresh: it bypasses the cache rather than revalidating against it, and + * reads the site to its last page, because a user who pulls is telling us they think what they + * are looking at is stale. */ override suspend fun refresh(force: Boolean) { - // A second caller during a refresh is a no-op rather than a queued duplicate + // A second caller during a refresh is a no-op rather than a queued duplicate of a 2 MB + // download. if (!refreshing.tryLock()) return try { + if (!force && isCurrent(_catalog.value)) return _isRefreshing.value = true + // Nothing in the block suspends, so a caller cancelled half way — a tab switch takes + // the Store's `viewModelScope` with it — still leaves a whole catalogue behind. withContext(Dispatchers.IO) { - // If not forcing a refresh, check if we already have a fully synced catalog within 1 hour - if (!force) { - val current = _catalog.value - val ageMillis = System.currentTimeMillis() - lastFullSyncTimeMillis - val isFresh = lastFullSyncTimeMillis > 0L && ageMillis < TimeUnit.MINUTES.toMillis(CATALOG_MAX_AGE_MINUTES.toLong()) - if (current.isFullySynced && isFresh) { - logI("store: catalog is fresh (age: ${TimeUnit.MILLISECONDS.toMinutes(ageMillis)}m), skipping sync") - return@withContext - } - } - val freshness = if (force) CacheControl.FORCE_NETWORK else @@ -113,88 +110,118 @@ class RepoRepository( .maxAge(CATALOG_MAX_AGE_MINUTES, TimeUnit.MINUTES) .build() - // Fetch page 1 immediately to show the initial modules instantly - val firstPage = fetchPaginatedCatalog(1, freshness) - ?: LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, freshness) } - - if (firstPage != null) { - val initialCatalog = firstPage.copy( - isSyncing = firstPage.page < firstPage.pageCount, - syncProgress = if (firstPage.pageCount > 0) 1f / firstPage.pageCount else 1f, - ) - _catalog.value = initialCatalog - - // If more pages exist, sync them sequentially in the background - if (firstPage.page < firstPage.pageCount) { - syncRemainingPages(firstPage, freshness) - } else { - lastFullSyncTimeMillis = System.currentTimeMillis() - } - return@withContext - } - - // If network failed, check cache for page 1 - val cached = fetchPaginatedCatalog(1, CacheControl.FORCE_CACHE) - ?: LIST_MIRRORS.firstNotNullOfOrNull { fetchCatalog(it, CacheControl.FORCE_CACHE) } - - when { - cached != null -> _catalog.value = cached.copy(fromCache = true, isSyncing = false) - else -> _catalog.value = _catalog.value.copy(loaded = true, isSyncing = false) - } + // The mirror's list goes up as soon as it is read, before the site has corrected + // it, but only onto an empty screen: over a list already showing it would take + // the corrections away, and update badges with them, only to bring them back. + val blank = _catalog.value.isEmpty + val paint = { base: StoreCatalog -> if (blank) _catalog.value = complete(base) } + + // Before reporting nothing, ask the cache — the bytes from the last successful + // visit are usually still on disk, and a stale catalogue is far more use than an + // empty screen. + val fetched = + api.catalog(freshness, everyPage = force, paint) + ?: api.catalog(CacheControl.FORCE_CACHE, everyPage = false, paint) + ?.copy(fromCache = true) + + // Nothing on the network and nothing on disk. `loaded` still flips, so the screen + // can say the repository is unreachable instead of sitting forever on a spinner + // that means nothing. + _catalog.value = fetched?.let(::complete) ?: _catalog.value.copy(loaded = true) } } finally { + // In a `finally` rather than after the block: a cancelled `viewModelScope` — a + // rotation mid-refresh — would otherwise strand the flag at true, and with + // pull-to-refresh reading it that is a spinner that never stops. _isRefreshing.value = false refreshing.unlock() } } - private suspend fun syncRemainingPages(initial: StoreCatalog, cacheControl: CacheControl) { - var accumulated = initial.modules.toMutableList() - val totalPages = initial.pageCount - for (p in (initial.page + 1)..totalPages) { - val pageResult = fetchPaginatedCatalog(p, cacheControl) - if (pageResult != null) { - accumulated.addAll(pageResult.modules) - val distinct = usable(accumulated) - accumulated = distinct.toMutableList() - val progress = p.toFloat() / totalPages - _catalog.value = _catalog.value.copy( - modules = distinct, - page = p, - pageCount = totalPages, - total = pageResult.total, - isSyncing = p < totalPages, - syncProgress = progress, - ) - } else { - // If a page fails, stop syncing and keep current state - _catalog.value = _catalog.value.copy(isSyncing = false) - break - } - } - _catalog.value = _catalog.value.copy(isSyncing = false, syncProgress = 1f) - if (_catalog.value.isFullySynced) { - lastFullSyncTimeMillis = System.currentTimeMillis() - } + /** + * Whether an unforced refresh would only fetch again what is already here. + * + * The mirror's snapshot answers that for itself through its `max-age`, but the site's pages + * carry no cache headers and never answer 304, so without this every launch and every visit to + * the Modules screen would download them again. + */ + private fun isCurrent(catalog: StoreCatalog): Boolean { + // Bounded below as well: a clock set back would otherwise hold a refresh off for as long + // as it was set back by. + val age = System.currentTimeMillis() - catalog.loadedAtMillis + val limit = TimeUnit.MINUTES.toMillis(CATALOG_MAX_AGE_MINUTES.toLong()) + return !catalog.isEmpty && !catalog.fromCache && age in 0 until limit } /** * The full record for one module: its README, and every release rather than only the newest. * - * Returns null when no mirror answers. Callers are expected to fall back to the catalogue entry - * they already hold, which carries the description, the scope, the collaborators and the newest - * release with its APK — a usable page, and much better than an error screen. + * Returns null when no host answers. Callers are expected to fall back to the catalogue entry + * they already hold, which carries the description and, where the mirror supplied it, the + * scope, the collaborators and the newest release with its APK — a usable page, and much better + * than an error screen. */ override suspend fun details(packageName: String): OnlineModule? = withContext(Dispatchers.IO) { val freshness = CacheControl.Builder().maxAge(DETAIL_MAX_AGE_MINUTES, TimeUnit.MINUTES).build() - DETAIL_MIRRORS.firstNotNullOfOrNull { fetchDetails(it, packageName, freshness) } - ?: DETAIL_MIRRORS.firstNotNullOfOrNull { - fetchDetails(it, packageName, CacheControl.FORCE_CACHE) - } + api.details(packageName, freshness) + ?: api.details(packageName, CacheControl.FORCE_CACHE) } + /** + * Gives every installed module its releases, where the catalogue came without them. + * + * An entry the site corrected, or one read from the site alone, names a version and carries no + * APK. For most of the catalogue that costs nothing, since the detail page fetches its own + * record. For an installed module it would leave the Modules screen offering an update it then + * cannot install, so those few are fetched here. + * + * Driven by the two flows rather than called from [refresh], because the daemon's package list + * and the catalogue arrive in either order. A record that fails to arrive leaves the names as + * they were, and `distinctUntilChanged` then keeps this from asking in a loop. + */ + private suspend fun completeInstalled() { + combine(_catalog, _installed) { catalog, installed -> + catalog.modules + .filter { it.releases.isNullOrEmpty() && it.name in installed } + .map { it.name } + } + .distinctUntilChanged() + .collectLatest { names -> + val fetched = names.mapNotNull { details(it) } + if (fetched.isEmpty()) return@collectLatest + logI("store: ${fetched.size} of ${names.size} installed modules given releases") + // Without the README: these stay in memory for as long as the process does. + records = records + fetched.associate { it.name to it.copy(readmeHTML = null) } + _catalog.update(::complete) + } + } + + /** + * [catalog] with the [records] laid over the entries that lack releases. + * + * Applied to everything published and not only once after a fetch, because every reload brings + * the same bare entries back. + * + * Only a record of the very releases the entry names: the mirror answers when the site does + * not, and its record may be older. + */ + private fun complete(catalog: StoreCatalog): StoreCatalog { + val records = records + if (records.isEmpty()) return catalog + return catalog.copy( + modules = + catalog.modules.map { module -> + records[module.name]?.takeIf { + module.releases.isNullOrEmpty() && + it.latestRelease == module.latestRelease && + it.latestBetaReleaseTime == module.latestBetaReleaseTime + } ?: module + } + ) + } + /** Re-reads installed versions; called on opening the Store and after an install lands. */ override fun refreshInstalled() { scope.launch { loadInstalled() } @@ -238,171 +265,9 @@ class RepoRepository( _installed.value = versions } - private fun fetchCatalog(baseUrl: String, cacheControl: CacheControl): StoreCatalog? { - val url = baseUrl + "modules.json" - return try { - // `use` covers the failure branch as well as the success one, and the failure branch is - // the one that runs whenever a mirror is down. - client.newCall(request(url, cacheControl)).execute().use { response -> - if (!response.isSuccessful) { - // The FORCE_CACHE replay synthesises 504 without contacting the mirror, so - // only report a status the network actually produced. - if (response.networkResponse != null) { - logW("store: $url returned HTTP ${response.code}") - } - return null - } - val parsed = parseCatalog(response) - if (parsed.isEmpty()) return null - logI("store: ${parsed.size} modules from $url") - // `fromCache` is deliberately *not* derived from `response.networkResponse`. A hit - // inside the ten-minute freshness window is served from disk without touching the - // network, and calling that "the saved catalogue" would put an offline notice on - // a perfectly current list. Staleness is a property of which branch produced this, - // so the caller sets the flag on the fallback path and only there. - StoreCatalog( - modules = usable(parsed), - loaded = true, - loadedAtMillis = response.receivedResponseAtMillis, - ) - } - } catch (e: Exception) { - logW("store: $url unavailable", e) - null - } - } - - private fun fetchDetails( - baseUrl: String, - packageName: String, - cacheControl: CacheControl, - ): OnlineModule? { - val url = "${baseUrl}module/$packageName.json" - return try { - client.newCall(request(url, cacheControl)).execute().use { response -> - if (!response.isSuccessful) return null - gson.fromJson(response.body.string(), OnlineModule::class.java) - } - } catch (e: Exception) { - logW("store: $url unavailable", e) - null - } - } - - - private fun fetchPaginatedCatalog(page: Int, cacheControl: CacheControl): StoreCatalog? { - val url = "https://modules.lsposed.org/module-list/$page.json" - return try { - client.newCall(request(url, cacheControl)).execute().use { response -> - if (!response.isSuccessful) { - if (response.networkResponse != null) { - logW("store: $url returned HTTP ${response.code}") - } - return null - } - val parsed = parsePaginatedCatalog(response) ?: return null - logI("store: page ${parsed.page}/${parsed.pageCount} (${parsed.modules.size} modules) from $url") - StoreCatalog( - modules = usable(parsed.modules), - loaded = true, - loadedAtMillis = response.receivedResponseAtMillis, - page = parsed.page, - pageCount = parsed.pageCount, - total = parsed.total, - ) - } - } catch (e: Exception) { - logW("store: $url unavailable", e) - null - } - } - - private data class PaginatedResponse( - val modules: List = emptyList(), - val page: Int = 1, - val pageCount: Int = 1, - val total: Int = 0, - ) - - private fun parsePaginatedCatalog(response: Response): PaginatedResponse? { - return try { - val json = JsonParser.parseReader(response.body.charStream()).asJsonObject - val page = json.get("page")?.asInt ?: 1 - val pageCount = json.get("pageCount")?.asInt ?: 1 - val total = json.get("total")?.asInt ?: 0 - val modulesArray = json.getAsJsonArray("modules") ?: return null - val modules = ArrayList(modulesArray.size()) - for (element in modulesArray) { - val module = runCatching { gson.fromJson(element, OnlineModule::class.java) } - if (module.isSuccess) module.getOrNull()?.let(modules::add) - } - PaginatedResponse(modules = modules, page = page, pageCount = pageCount, total = total) - } catch (e: Exception) { - logW("store: failed to parse paginated catalog", e) - null - } - } - - private fun request(url: String, cacheControl: CacheControl): Request = - Request.Builder().url(url).cacheControl(cacheControl).build() - - /** - * Reads the catalogue one entry at a time, and survives a bad one. - * - * Binding the whole array in a single `fromJson` call fails the entire Store on one unexpected - * field — `additionalAuthors` holds objects rather than the strings its name suggests, and - * `AdditionalAuthor` exists because of it. This is third-party data written by hundreds of - * authors, so an entry the model does not expect must cost that entry and nothing else. - * - * Streamed off the response rather than through a `String`, which also keeps the 1.2 MB body - * from being materialised twice. - */ - private fun parseCatalog(response: Response): List { - val modules = ArrayList(1024) - var rejected = 0 - JsonReader(response.body.charStream()).use { reader -> - reader.beginArray() - while (reader.hasNext()) { - // Parsing to a JsonElement first cannot fail on well-formed JSON, so a binding - // failure below leaves the reader cleanly positioned on the next entry. - val element = JsonParser.parseReader(reader) - val module = runCatching { gson.fromJson(element, OnlineModule::class.java) } - if (module.isSuccess) module.getOrNull()?.let(modules::add) else rejected++ - } - reader.endArray() - } - if (rejected > 0) logW("store: skipped $rejected unreadable entries") - return modules - } - - /** - * What is worth showing of a parsed catalogue. - * - * `distinctBy` is not superstition about today's data — it is what stops a mirror serving the - * same package twice from crashing the Store's `LazyColumn`, which is keyed by package name. - * Entries with no release or latestRelease are dropped. - */ - private fun usable(parsed: List): List = - parsed - .asSequence() - .filter { it.hide != true } - .filter { !it.releases.isNullOrEmpty() || !it.latestRelease.isNullOrBlank() } - .distinctBy { it.name } - .toList() - private companion object { - /** - * The only host serving the full list. Probed rather than assumed: roughly 1.2 MB and 800 - * entries here, against a 403 from `modules.lsposed.org`. - */ - val LIST_MIRRORS = listOf("https://backup.modules.lsposed.org/") - - /** Detail is served by both hosts; primary site carries the latest data while backup is fallback. */ - val DETAIL_MIRRORS = - listOf("https://modules.lsposed.org/", "https://backup.modules.lsposed.org/") - - /** Matches 1 hour cache duration so full catalogue sync only happens once an hour or on pull-to-refresh. */ - const val CATALOG_MAX_AGE_MINUTES = 60 + /** Matches the mirror's own `cache-control: max-age=600`, so revalidation stays free. */ + const val CATALOG_MAX_AGE_MINUTES = 10 /** Longer: a module's release history changes far less often than the index does. */ const val DETAIL_MAX_AGE_MINUTES = 60 diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/StoreApi.kt b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/StoreApi.kt new file mode 100644 index 0000000000..71df1ee2f0 --- /dev/null +++ b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/StoreApi.kt @@ -0,0 +1,333 @@ +package org.matrix.vector.manager.data.repository + +import com.google.gson.Gson +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.google.gson.stream.JsonReader +import java.net.HttpURLConnection +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.runBlocking +import okhttp3.CacheControl +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response +import org.matrix.vector.manager.logI +import org.matrix.vector.manager.logW +import org.matrix.vector.ui.store.OnlineModule +import org.matrix.vector.ui.store.StoreCatalog + +/** + * The module repository's two hosts, and how one catalogue is made of them. + * + * **Neither host serves what the Store needs on its own.** Probed rather than assumed: + * - the mirror, `backup.modules.lsposed.org`, serves `modules.json`: every module in one cacheable + * 2 MB response, each entry carrying its newest release with the APK. It is regenerated daily but + * ran three days behind when measured, so a release published since is not in it. + * - the site, `modules.lsposed.org`, answers `modules.json` with a 403 and serves the index only as + * `module-list/.json`: thirty entries a page, newest release first, current to the minute. + * Those entries state `latestRelease` but carry no release, scope or collaborators, and the pages + * answer no conditional request with a 304, so walking all of them costs 45 requests and 1.3 MB + * every time. + * + * So the mirror is the base and the site corrects it. Because the pages are ordered by release time + * and the mirror has only been seen *behind*, everything the mirror lacks sits at the front: the + * walk stops at the first page that agrees with the mirror entirely, typically the fifth. An entry + * the site knows better loses its now-outdated releases rather than keep an APK that is no longer + * the one its version names; [RepoRepository] fetches the detail record for the installed ones. + * + * Either host can vanish. Without the site the mirror is shown as it is, which is what the Store + * did before. Without the mirror every page is read, and the catalogue is the site's alone. + */ +internal class StoreApi( + client: OkHttpClient, + private val gson: Gson = Gson(), + private val site: String = "https://modules.lsposed.org/", + private val mirror: String = "https://backup.modules.lsposed.org/", +) { + + /** + * The shared client, with its cache made to hold what the replay against it needs. + * + * Two things otherwise empty it. OkHttp stores a 404 as readily as a 200, and under the same + * key, so a host that starts answering 404 — how a retired static site fails — would overwrite + * the last good catalogue with its own error page. And the site marks its pages + * `must-revalidate`, which forbids exactly the stale read a cold start with no network is: a + * `FORCE_CACHE` request for one is refused although the bytes are on disk. + */ + private val client = + client + .newBuilder() + .addNetworkInterceptor { chain -> + val response = chain.proceed(chain.request()) + val stored = + response.isSuccessful || response.code == HttpURLConnection.HTTP_NOT_MODIFIED + when { + !stored -> response.cacheControl("no-store") + // Still never fresh, so the network is asked whenever it can be. + response.cacheControl.mustRevalidate -> response.cacheControl("max-age=0") + else -> response + } + } + .build() + + private fun Response.cacheControl(value: String): Response = + newBuilder().header("Cache-Control", value).build() + + /** + * The catalogue, or null when [cacheControl] could not produce a whole one. + * + * [everyPage] reads the site to its last page even where the mirror already agrees. That finds + * what does not sit at the front — a release republished under its old date, a beta with no + * stable release beside it — and costs the whole index, so it is for a reader who asked. + * + * [onBase] receives the mirror's list as soon as it is read, before the site has corrected it. + */ + fun catalog( + cacheControl: CacheControl, + everyPage: Boolean, + onBase: (StoreCatalog) -> Unit = {}, + ): StoreCatalog? { + val base = fetchSnapshot(cacheControl)?.also(onBase) + val modules = LinkedHashMap() + base?.modules?.associateByTo(modules) { it.name } + // With nothing to compare against, there is no page at which to stop. + val toTheEnd = everyPage || base == null + + var loadedAtMillis = base?.loadedAtMillis ?: 0L + var complete = false + var number = 1 + var pageCount = 1 + var source = cacheControl + walk@ while (true) { + // The first page goes alone, being what says how many there are. After it, every + // remaining page at once where all are wanted, and otherwise a few: the page that + // agrees with the base ends the walk, and whatever was read past it is thrown away. + val reach = if (toTheEnd) pageCount else number + READ_AHEAD - 1 + val asked = source + for (fetched in fetchPages(number..reach.coerceIn(number, pageCount), asked)) { + var page = fetched + if (page == null && base != null && !asked.onlyIfCached) { + // The base can be current straight off the disk while the site is out of + // reach, and the corrections it had last time are then still on disk too. The + // rest of the walk stays there: offline, every further attempt is a timeout + // to sit out. + source = CacheControl.FORCE_CACHE + page = fetchPage(number, source) + } + // Only a page read since that base, though, which a saved one need not be: an + // older page laid over a newer base would take a version back. + if (page == null || (base != null && page.receivedAtMillis < base.loadedAtMillis)) { + break@walk + } + if (loadedAtMillis == 0L) loadedAtMillis = page.receivedAtMillis + // Bounded, because this number decides how many requests go out together. + pageCount = page.pageCount.coerceIn(number, MAX_PAGES) + val corrected = page.modules.count { correct(modules, it) } + complete = number == pageCount || (!toTheEnd && corrected == 0) + if (complete) break@walk + number++ + } + } + + // A walk that broke off is only a loss when it was the sole source, and then only on the + // network: the replay against the cache is the last resort, and part of a list beats none. + if (base == null && !complete && (modules.isEmpty() || !cacheControl.onlyIfCached)) { + return null + } + if (number > 1 || complete) logI("store: site read to page $number of $pageCount") + return StoreCatalog(usable(modules.values), loaded = true, loadedAtMillis = loadedAtMillis) + } + + /** + * The full record for one module, from the site first because the mirror's lags. + * + * Read as strictly as a catalogue entry, and only accepted as the module that was asked for: + * the caller files it under that name. + */ + fun details(packageName: String, cacheControl: CacheControl): OnlineModule? = + sequenceOf(site, mirror).firstNotNullOfOrNull { host -> + fetch("${host}module/$packageName.json", cacheControl) { response -> + val root = JsonParser.parseReader(response.body.charStream()).asJsonObject + bind(root)?.takeIf { it.name == packageName } + } + } + + private fun fetchSnapshot(cacheControl: CacheControl): StoreCatalog? { + val url = mirror + "modules.json" + return fetch(url, cacheControl) { response -> + val parsed = usable(parseSnapshot(response)) + if (parsed.isEmpty()) return@fetch null + logI("store: ${parsed.size} modules from $url") + // `fromCache` is deliberately *not* derived from `response.networkResponse`. A hit + // inside the ten-minute freshness window is served from disk without touching the + // network, and calling that "the saved catalogue" would put an offline notice on + // a perfectly current list. Staleness is a property of which branch produced this, + // so the caller sets the flag on the fallback path and only there. + StoreCatalog(parsed, loaded = true, loadedAtMillis = response.receivedResponseAtMillis) + } + } + + private class Page( + val modules: List, + val pageCount: Int, + val receivedAtMillis: Long, + ) + + /** + * [numbers] in order, each null where its page could not be read. + * + * Together rather than in turn, because the site takes a third of a second over each page + * however small it is: 45 of those in a row is a quarter of a minute under a spinner, and at + * once, down the one HTTP/2 connection, they are half a second. + */ + private fun fetchPages(numbers: IntRange, cacheControl: CacheControl): List = + if (numbers.first == numbers.last) { + listOf(fetchPage(numbers.first, cacheControl)) + } else { + runBlocking(Dispatchers.IO) { + numbers.map { async { fetchPage(it, cacheControl) } }.awaitAll() + } + } + + private fun fetchPage(number: Int, cacheControl: CacheControl): Page? = + fetch("${site}module-list/$number.json", cacheControl) { response -> + val root = JsonParser.parseReader(response.body.charStream()).asJsonObject + val modules = root.getAsJsonArray("modules").mapNotNull { bind(it.asJsonObject) } + // The walk counts for itself and only asks the page where the end is, so a page that + // misstates its own number cannot send it round again. + Page(modules, root.get("pageCount").asInt, response.receivedResponseAtMillis) + } + + /** + * One GET, with every way it can fail folded into null. + * + * [read] runs inside the same `try`, because a body that is not the JSON it was expected to be + * is one more way for a host to be down. + */ + private fun fetch(url: String, cacheControl: CacheControl, read: (Response) -> T?): T? = + try { + val request = Request.Builder().url(url).cacheControl(cacheControl).build() + // `use` covers the failure branch as well as the success one, and the failure branch is + // the one that runs whenever a host is down. + client.newCall(request).execute().use { response -> + if (response.isSuccessful) { + read(response) + } else { + // The FORCE_CACHE replay synthesises 504 without contacting the host, so only + // report a status the network actually produced. + if (response.networkResponse != null) { + logW("store: $url returned HTTP ${response.code}") + } + null + } + } + } catch (e: Exception) { + logW("store: $url unavailable", e) + null + } + + /** + * Reads the snapshot one entry at a time, and survives a bad one. + * + * Binding the whole array in a single `fromJson` call fails the entire Store on one unexpected + * field — `additionalAuthors` holds objects rather than the strings its name suggests, and + * `AdditionalAuthor` exists because of it. This is third-party data written by hundreds of + * authors, so an entry the model does not expect must cost that entry and nothing else. + * + * Streamed off the response rather than through a `String`, which also keeps the 2 MB body from + * being materialised twice. + */ + private fun parseSnapshot(response: Response): List { + val modules = ArrayList(1536) + var rejected = 0 + JsonReader(response.body.charStream()).use { reader -> + reader.beginArray() + while (reader.hasNext()) { + // Parsing to a JsonElement first cannot fail on well-formed JSON, so a binding + // failure below leaves the reader cleanly positioned on the next entry. + val module = bind(JsonParser.parseReader(reader).asJsonObject) + if (module != null) modules.add(module) else rejected++ + } + reader.endArray() + } + if (rejected > 0) logW("store: skipped $rejected unreadable entries") + return modules + } + + /** Null for an entry the model cannot hold, including one with no name to key it by. */ + private fun bind(entry: JsonObject): OnlineModule? { + val module = runCatching { gson.fromJson(entry, OnlineModule::class.java) }.getOrNull() + // Gson constructs through `Unsafe`, so the non-null type on `name` is not a promise. + @Suppress("SENSELESS_COMPARISON") + return module?.takeIf { it.name != null } + } + + companion object { + /** Pages read together past the first, where the walk expects to stop early. */ + const val READ_AHEAD = 4 + + /** Four times today's index; past it a `pageCount` is a mistake, not a catalogue. */ + const val MAX_PAGES = 200 + + /** + * Folds one page entry into [modules]; true when the page knew something they did not. + * + * The stable version and the time of the newest beta are the whole comparison. The two + * hosts word `summary` and `sourceUrl` differently for hundreds of unchanged modules, so + * comparing more would report a difference on every page and the walk would never stop + * early. + * + * **The later statement wins, not the site.** That the mirror is the one behind is what was + * measured, not what is promised, and a site that stopped being rebuilt would otherwise + * take every version the mirror has since learnt back to its own. The cost is a withdrawn + * release, which the site states by naming an older one: that waits for the mirror, as it + * did when the mirror was all there was. + * + * The beta is compared by its time because that is all a page says of it: there is no + * `latestBetaRelease` to read. A newer beta therefore leaves the entry naming none, and the + * detail record is what names it. + */ + fun correct(modules: MutableMap, entry: OnlineModule): Boolean { + val known = modules[entry.name] + // ISO-8601 in UTC on both hosts, so the text orders as the times do. + val newerBeta = + entry.latestBetaReleaseTime.orEmpty() > known?.latestBetaReleaseTime.orEmpty() + val newerRelease = + entry.latestRelease != known?.latestRelease && + entry.latestReleaseTime.orEmpty() >= known?.latestReleaseTime.orEmpty() + if (known != null && !newerRelease && !newerBeta) return false + modules[entry.name] = + known?.copy( + description = entry.description ?: known.description, + stargazerCount = entry.stargazerCount ?: known.stargazerCount, + updatedAt = entry.updatedAt ?: known.updatedAt, + latestRelease = entry.latestRelease, + latestReleaseTime = entry.latestReleaseTime, + latestBetaRelease = known.latestBetaRelease.takeUnless { newerBeta }, + latestBetaReleaseTime = entry.latestBetaReleaseTime, + // Both lists are the base's, and its newest is no longer the newest: left in + // place, a one-tap update would install the old APK under the new version. + releases = null, + betaReleases = null, + ) ?: entry + return true + } + + /** + * What is worth showing of a parsed catalogue. + * + * An entry is kept for having a release to install or, on a page entry, a version to name; + * one with neither has nothing to say. Keying by package name upstream is what stops a host + * serving the same package twice from crashing the Store's `LazyColumn`. + */ + fun usable(parsed: Collection): List = + parsed + .asSequence() + .filter { it.hide != true } + .filter { !it.releases.isNullOrEmpty() || !it.latestRelease.isNullOrBlank() } + .distinctBy { it.name } + .toList() + } +} From ce2cb334f407888efd55a31e214d9a72263ddb0e Mon Sep 17 00:00:00 2001 From: JingMatrix Date: Tue, 10 Nov 2026 14:38:13 +0100 Subject: [PATCH 8/9] Keep the Store's network code where both managers can reach it LSPatch hosts the same Store screen and reads the same two hosts, and did so through a copy of RepoRepository made by hand before the site's pages were read at all. So it still showed the mirror alone, three days behind, asked the mirror first for a module's detail, and never replayed the cache its client has. Every fix to this code had to be made twice, and had been made once. StoreApi moves to manager-ui, which LSPatch already depends on for the screen itself, and the rest of RepoRepository moves with it as StoreRepository: the refresh lock, the ten-minute gate, the replay against the cache and the detail records for installed modules. None of that is about the app. What is, arrives as two arguments. How the device is asked what is installed is a function, which for Vector is the daemon's getInstalledPackagesFromAllUsers and is all that RepoRepository now holds. Where a log line goes is a StoreLog, because Vector's lines have to pass through logI and logW to reach the Verbose tab, and manager-ui cannot see those. Nothing changes in what Vector does. --- .../org/matrix/vector/ui/store}/StoreApi.kt | 14 +- .../matrix/vector/ui/store/StoreRepository.kt | 250 +++++++++++++++ .../manager/data/repository/RepoRepository.kt | 287 +++--------------- 3 files changed, 298 insertions(+), 253 deletions(-) rename {manager/src/main/kotlin/org/matrix/vector/manager/data/repository => manager-ui/src/main/kotlin/org/matrix/vector/ui/store}/StoreApi.kt (97%) create mode 100644 manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/StoreApi.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreApi.kt similarity index 97% rename from manager/src/main/kotlin/org/matrix/vector/manager/data/repository/StoreApi.kt rename to manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreApi.kt index 71df1ee2f0..ebad280398 100644 --- a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/StoreApi.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreApi.kt @@ -1,5 +1,6 @@ -package org.matrix.vector.manager.data.repository +package org.matrix.vector.ui.store +import android.util.Log import com.google.gson.Gson import com.google.gson.JsonObject import com.google.gson.JsonParser @@ -13,10 +14,6 @@ import okhttp3.CacheControl import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response -import org.matrix.vector.manager.logI -import org.matrix.vector.manager.logW -import org.matrix.vector.ui.store.OnlineModule -import org.matrix.vector.ui.store.StoreCatalog /** * The module repository's two hosts, and how one catalogue is made of them. @@ -35,13 +32,14 @@ import org.matrix.vector.ui.store.StoreCatalog * and the mirror has only been seen *behind*, everything the mirror lacks sits at the front: the * walk stops at the first page that agrees with the mirror entirely, typically the fifth. An entry * the site knows better loses its now-outdated releases rather than keep an APK that is no longer - * the one its version names; [RepoRepository] fetches the detail record for the installed ones. + * the one its version names; [StoreRepository] fetches the detail record for the installed ones. * * Either host can vanish. Without the site the mirror is shown as it is, which is what the Store * did before. Without the mirror every page is read, and the catalogue is the site's alone. */ internal class StoreApi( client: OkHttpClient, + private val log: StoreLog, private val gson: Gson = Gson(), private val site: String = "https://modules.lsposed.org/", private val mirror: String = "https://backup.modules.lsposed.org/", @@ -256,6 +254,10 @@ internal class StoreApi( return modules } + private fun logI(message: String) = log.log(Log.INFO, message, null) + + private fun logW(message: String, cause: Throwable? = null) = log.log(Log.WARN, message, cause) + /** Null for an entry the model cannot hold, including one with no name to key it by. */ private fun bind(entry: JsonObject): OnlineModule? { val module = runCatching { gson.fromJson(entry, OnlineModule::class.java) }.getOrNull() diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt new file mode 100644 index 0000000000..50bd38eeb2 --- /dev/null +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt @@ -0,0 +1,250 @@ +package org.matrix.vector.ui.store + +import android.util.Log +import java.util.concurrent.TimeUnit +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.withContext +import okhttp3.CacheControl +import okhttp3.OkHttpClient + +/** + * Where the Store's shared code reports, as a `Log` priority; each app routes it to its own log. + */ +fun interface StoreLog { + fun log(priority: Int, message: String, cause: Throwable?) +} + +/** + * The Store's data: the online catalogue, and what this device already has of it. + * + * One implementation for every app that hosts the Store. What differs between them is how the + * device is asked what is installed, which arrives as a function, and where a log line goes, which + * is [log]. Everything about the network is the same, and has to be: the hosts, their quirks and + * the ways they fail are facts about the repository, not about the app reading it. + * + * Where the catalogue comes from, and why from two hosts, is [StoreApi]'s business. What is decided + * here is when to ask and what to do when asking fails. + * + * **Caching is declared, not hoped for.** Every request states its own freshness, so the client's + * disk cache is actually used: the mirror's snapshot revalidates against the server's own + * ten-minute `max-age` and its ETag, pull-to-refresh forces the network, and when the network + * yields nothing the same requests are replayed against the cache alone. That last step is why a + * cold start with no network renders the last known catalogue rather than an error, and it is also + * what gives the DNS-over-HTTPS setting an effect here, since the shared client is the one carrying + * the DoH resolver. + * + * There is deliberately **no snapshot file** of our own. The OkHttp cache already holds these exact + * bytes; a 2 MB duplicate in the same cache directory would buy nothing but a second thing to keep + * in sync. + */ +open class StoreRepository( + client: OkHttpClient, + private val scope: CoroutineScope, + private val log: StoreLog, + /** What each installed package is at, or null when the device could not be asked. */ + private val readInstalledVersions: suspend () -> Map?, +) : StoreDataSource { + + private val api = StoreApi(client, log) + + private val _catalog = MutableStateFlow(StoreCatalog()) + override val catalog: StateFlow = _catalog.asStateFlow() + + private val _isRefreshing = MutableStateFlow(false) + override val isRefreshing: StateFlow = _isRefreshing.asStateFlow() + + private val _installed = MutableStateFlow>(emptyMap()) + + /** Detail records fetched for installed modules, by package name. See [completeInstalled]. */ + @Volatile private var records = emptyMap() + + /** What each package on this device is at, keyed by package name. */ + override val installedVersions: StateFlow> = _installed.asStateFlow() + + /** Held for the length of a refresh; `tryLock` leaves no window between checking and taking. */ + private val refreshing = Mutex() + + init { + scope.launch { completeInstalled() } + } + + /** + * Reloads the catalogue. + * + * [force] is pull-to-refresh: it bypasses the cache rather than revalidating against it, and + * reads the site to its last page, because a user who pulls is telling us they think what they + * are looking at is stale. + */ + override suspend fun refresh(force: Boolean) { + // A second caller during a refresh is a no-op rather than a queued duplicate of a 2 MB + // download. + if (!refreshing.tryLock()) return + try { + if (!force && isCurrent(_catalog.value)) return + _isRefreshing.value = true + // Nothing in the block suspends, so a caller cancelled half way — a tab switch takes + // the Store's `viewModelScope` with it — still leaves a whole catalogue behind. + withContext(Dispatchers.IO) { + val freshness = + if (force) CacheControl.FORCE_NETWORK + else + CacheControl.Builder() + .maxAge(CATALOG_MAX_AGE_MINUTES, TimeUnit.MINUTES) + .build() + + // The mirror's list goes up as soon as it is read, before the site has corrected + // it, but only onto an empty screen: over a list already showing it would take + // the corrections away, and update badges with them, only to bring them back. + val blank = _catalog.value.isEmpty + val paint = { base: StoreCatalog -> if (blank) _catalog.value = complete(base) } + + // Before reporting nothing, ask the cache — the bytes from the last successful + // visit are usually still on disk, and a stale catalogue is far more use than an + // empty screen. + val fetched = + api.catalog(freshness, everyPage = force, paint) + ?: api.catalog(CacheControl.FORCE_CACHE, everyPage = false, paint) + ?.copy(fromCache = true) + + // Nothing on the network and nothing on disk. `loaded` still flips, so the screen + // can say the repository is unreachable instead of sitting forever on a spinner + // that means nothing. + _catalog.value = fetched?.let(::complete) ?: _catalog.value.copy(loaded = true) + } + } finally { + // In a `finally` rather than after the block: a cancelled `viewModelScope` — a + // rotation mid-refresh — would otherwise strand the flag at true, and with + // pull-to-refresh reading it that is a spinner that never stops. + _isRefreshing.value = false + refreshing.unlock() + } + } + + /** + * Whether an unforced refresh would only fetch again what is already here. + * + * The mirror's snapshot answers that for itself through its `max-age`, but the site's pages + * never answer 304, so without this every unforced refresh would download them again. + */ + private fun isCurrent(catalog: StoreCatalog): Boolean { + // Bounded below as well: a clock set back would otherwise hold a refresh off for as long + // as it was set back by. + val age = System.currentTimeMillis() - catalog.loadedAtMillis + val limit = TimeUnit.MINUTES.toMillis(CATALOG_MAX_AGE_MINUTES.toLong()) + return !catalog.isEmpty && !catalog.fromCache && age in 0 until limit + } + + /** + * The full record for one module: its README, and every release rather than only the newest. + * + * Returns null when no host answers. Callers are expected to fall back to the catalogue entry + * they already hold, which carries the description and, where the mirror supplied it, the + * scope, the collaborators and the newest release with its APK — a usable page, and much better + * than an error screen. + */ + override suspend fun details(packageName: String): OnlineModule? = + withContext(Dispatchers.IO) { + val freshness = + CacheControl.Builder().maxAge(DETAIL_MAX_AGE_MINUTES, TimeUnit.MINUTES).build() + api.details(packageName, freshness) + ?: api.details(packageName, CacheControl.FORCE_CACHE) + } + + /** + * Gives every installed module its releases, where the catalogue came without them. + * + * An entry the site corrected, or one read from the site alone, names a version and carries no + * APK. For most of the catalogue that costs nothing, since the detail page fetches its own + * record. For an installed module it would leave the app offering an update it then cannot + * install from where it offers it, so those few are fetched here. + * + * Driven by the two flows rather than called from [refresh], because the device's package list + * and the catalogue arrive in either order. A record that fails to arrive leaves the names as + * they were, and `distinctUntilChanged` then keeps this from asking in a loop. + */ + private suspend fun completeInstalled() { + combine(_catalog, _installed) { catalog, installed -> + catalog.modules + .filter { it.releases.isNullOrEmpty() && it.name in installed } + .map { it.name } + } + .distinctUntilChanged() + .collectLatest { names -> + val fetched = names.mapNotNull { details(it) } + if (fetched.isEmpty()) return@collectLatest + val message = + "store: ${fetched.size} of ${names.size} installed modules given releases" + log.log(Log.INFO, message, null) + // Without the README: these stay in memory for as long as the process does. + records = records + fetched.associate { it.name to it.copy(readmeHTML = null) } + _catalog.update(::complete) + } + } + + /** + * [catalog] with the [records] laid over the entries that lack releases. + * + * Applied to everything published and not only once after a fetch, because every reload brings + * the same bare entries back. + * + * Only a record of the very releases the entry names: the mirror answers when the site does + * not, and its record may be older. + */ + private fun complete(catalog: StoreCatalog): StoreCatalog { + val records = records + if (records.isEmpty()) return catalog + return catalog.copy( + modules = + catalog.modules.map { module -> + records[module.name]?.takeIf { + module.releases.isNullOrEmpty() && + it.latestRelease == module.latestRelease && + it.latestBetaReleaseTime == module.latestBetaReleaseTime + } ?: module + } + ) + } + + /** Re-reads installed versions; called on opening the Store and after an install lands. */ + override fun refreshInstalled() { + scope.launch { loadInstalled() } + } + + /** + * The same read, awaited and handed back, for a caller that has to act on what it finds. + * + * Which is how an install records what it produced: the note that suppresses a satisfied offer + * is compared against [installedVersions], so it has to be written from that same reading and + * not from one of the caller's own, which need not agree with it. + * + * Returns the last known map when the device cannot be asked, which is the safe direction: a + * note written from a stale version simply fails to match, and the offer stays. + */ + suspend fun readInstalled(): Map { + loadInstalled() + return _installed.value + } + + /** Keeps the last map when the device has no answer: what is installed did not stop being. */ + private suspend fun loadInstalled() { + readInstalledVersions()?.let { _installed.value = it } + } + + private companion object { + /** Matches the mirror's own `cache-control: max-age=600`, so revalidation stays free. */ + const val CATALOG_MAX_AGE_MINUTES = 10 + + /** Longer: a module's release history changes far less often than the index does. */ + const val DETAIL_MAX_AGE_MINUTES = 60 + } +} diff --git a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt index 9156aeb5b7..78cd61eb9f 100644 --- a/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt +++ b/manager/src/main/kotlin/org/matrix/vector/manager/data/repository/RepoRepository.kt @@ -1,79 +1,27 @@ package org.matrix.vector.manager.data.repository -import java.util.concurrent.TimeUnit +import android.util.Log import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.asStateFlow -import kotlinx.coroutines.flow.collectLatest -import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.withContext -import okhttp3.CacheControl import okhttp3.OkHttpClient -import org.matrix.vector.ui.store.StoreDataSource -import org.matrix.vector.ui.store.OnlineModule -import org.matrix.vector.ui.store.RepoVersion -import org.matrix.vector.ui.store.StoreCatalog import org.matrix.vector.manager.data.model.versionCodeCompat import org.matrix.vector.manager.di.ServiceLocator import org.matrix.vector.manager.ipc.DaemonClient import org.matrix.vector.manager.logI import org.matrix.vector.manager.logW +import org.matrix.vector.ui.store.RepoVersion +import org.matrix.vector.ui.store.StoreLog +import org.matrix.vector.ui.store.StoreRepository /** - * The Store's data: the online catalogue, and what this device already has of it. - * - * Where the catalogue comes from, and why from two hosts, is [StoreApi]'s business. What is decided - * here is when to ask and what to do when asking fails. + * The Store's data for Vector: [StoreRepository], with the daemon as what knows the device. * - * **Caching is declared, not hoped for.** Every request states its own freshness, so the 16 MB disk - * cache in `HttpClientFactory` is actually used: the mirror's snapshot revalidates against the - * server's own ten-minute `max-age` and its ETag, pull-to-refresh forces the network, and when the - * network yields nothing the same requests are replayed against the cache alone. That last step is - * why a cold start with no network renders the last known catalogue rather than an error, and it is - * also what gives the DNS-over-HTTPS setting an effect here, since the shared client is the one - * carrying the DoH resolver. - * - * There is deliberately **no snapshot file** of our own, unlike `GitHubRepository`. The OkHttp - * cache already holds these exact bytes; a 2 MB duplicate in the same cache directory would buy - * nothing but a second thing to keep in sync. + * Only the two things that are Vector's own live here. Installed versions come from the daemon and + * are re-read whenever its binder arrives, and log lines go through [logI] and [logW], which is + * what puts them in the Verbose tab and the exported zip. */ -class RepoRepository( - client: OkHttpClient, - private val daemon: DaemonClient, - private val scope: CoroutineScope, -) : StoreDataSource { - - private val api = StoreApi(client) - - private val _catalog = MutableStateFlow(StoreCatalog()) - override val catalog: StateFlow = _catalog.asStateFlow() - - private val _isRefreshing = MutableStateFlow(false) - override val isRefreshing: StateFlow = _isRefreshing.asStateFlow() - - private val _installed = MutableStateFlow>(emptyMap()) - - /** Detail records fetched for installed modules, by package name. See [completeInstalled]. */ - @Volatile private var records = emptyMap() - - /** - * What each package on this device is at, keyed by package name. - * - * One `getInstalledPackagesFromAllUsers` call and no `ModuleDetection`: the Store already knows - * that every name it asks about is a module, so it does not need the much more expensive - * discovery the Modules screen runs, which opens every APK to find out. - */ - override val installedVersions: StateFlow> = _installed.asStateFlow() - - - /** Held for the length of a refresh; `tryLock` leaves no window between checking and taking. */ - private val refreshing = Mutex() +class RepoRepository(client: OkHttpClient, daemon: DaemonClient, scope: CoroutineScope) : + StoreRepository(client, scope, VectorLog, { daemon.installedVersions() }) { init { scope.launch { @@ -81,195 +29,40 @@ class RepoRepository( // *not* cleared when the daemon goes away: which packages are installed is a fact // about the device, not about the framework, and dropping every "Installed" badge // because the daemon died would state something untrue. - ServiceLocator.service.collect { service -> if (service != null) loadInstalled() } - } - scope.launch { completeInstalled() } - } - - /** - * Reloads the catalogue. - * - * [force] is pull-to-refresh: it bypasses the cache rather than revalidating against it, and - * reads the site to its last page, because a user who pulls is telling us they think what they - * are looking at is stale. - */ - override suspend fun refresh(force: Boolean) { - // A second caller during a refresh is a no-op rather than a queued duplicate of a 2 MB - // download. - if (!refreshing.tryLock()) return - try { - if (!force && isCurrent(_catalog.value)) return - _isRefreshing.value = true - // Nothing in the block suspends, so a caller cancelled half way — a tab switch takes - // the Store's `viewModelScope` with it — still leaves a whole catalogue behind. - withContext(Dispatchers.IO) { - val freshness = - if (force) CacheControl.FORCE_NETWORK - else - CacheControl.Builder() - .maxAge(CATALOG_MAX_AGE_MINUTES, TimeUnit.MINUTES) - .build() - - // The mirror's list goes up as soon as it is read, before the site has corrected - // it, but only onto an empty screen: over a list already showing it would take - // the corrections away, and update badges with them, only to bring them back. - val blank = _catalog.value.isEmpty - val paint = { base: StoreCatalog -> if (blank) _catalog.value = complete(base) } - - // Before reporting nothing, ask the cache — the bytes from the last successful - // visit are usually still on disk, and a stale catalogue is far more use than an - // empty screen. - val fetched = - api.catalog(freshness, everyPage = force, paint) - ?: api.catalog(CacheControl.FORCE_CACHE, everyPage = false, paint) - ?.copy(fromCache = true) - - // Nothing on the network and nothing on disk. `loaded` still flips, so the screen - // can say the repository is unreachable instead of sitting forever on a spinner - // that means nothing. - _catalog.value = fetched?.let(::complete) ?: _catalog.value.copy(loaded = true) - } - } finally { - // In a `finally` rather than after the block: a cancelled `viewModelScope` — a - // rotation mid-refresh — would otherwise strand the flag at true, and with - // pull-to-refresh reading it that is a spinner that never stops. - _isRefreshing.value = false - refreshing.unlock() - } - } - - /** - * Whether an unforced refresh would only fetch again what is already here. - * - * The mirror's snapshot answers that for itself through its `max-age`, but the site's pages - * carry no cache headers and never answer 304, so without this every launch and every visit to - * the Modules screen would download them again. - */ - private fun isCurrent(catalog: StoreCatalog): Boolean { - // Bounded below as well: a clock set back would otherwise hold a refresh off for as long - // as it was set back by. - val age = System.currentTimeMillis() - catalog.loadedAtMillis - val limit = TimeUnit.MINUTES.toMillis(CATALOG_MAX_AGE_MINUTES.toLong()) - return !catalog.isEmpty && !catalog.fromCache && age in 0 until limit - } - - /** - * The full record for one module: its README, and every release rather than only the newest. - * - * Returns null when no host answers. Callers are expected to fall back to the catalogue entry - * they already hold, which carries the description and, where the mirror supplied it, the - * scope, the collaborators and the newest release with its APK — a usable page, and much better - * than an error screen. - */ - override suspend fun details(packageName: String): OnlineModule? = - withContext(Dispatchers.IO) { - val freshness = - CacheControl.Builder().maxAge(DETAIL_MAX_AGE_MINUTES, TimeUnit.MINUTES).build() - api.details(packageName, freshness) - ?: api.details(packageName, CacheControl.FORCE_CACHE) + ServiceLocator.service.collect { service -> if (service != null) refreshInstalled() } } - - /** - * Gives every installed module its releases, where the catalogue came without them. - * - * An entry the site corrected, or one read from the site alone, names a version and carries no - * APK. For most of the catalogue that costs nothing, since the detail page fetches its own - * record. For an installed module it would leave the Modules screen offering an update it then - * cannot install, so those few are fetched here. - * - * Driven by the two flows rather than called from [refresh], because the daemon's package list - * and the catalogue arrive in either order. A record that fails to arrive leaves the names as - * they were, and `distinctUntilChanged` then keeps this from asking in a loop. - */ - private suspend fun completeInstalled() { - combine(_catalog, _installed) { catalog, installed -> - catalog.modules - .filter { it.releases.isNullOrEmpty() && it.name in installed } - .map { it.name } - } - .distinctUntilChanged() - .collectLatest { names -> - val fetched = names.mapNotNull { details(it) } - if (fetched.isEmpty()) return@collectLatest - logI("store: ${fetched.size} of ${names.size} installed modules given releases") - // Without the README: these stay in memory for as long as the process does. - records = records + fetched.associate { it.name to it.copy(readmeHTML = null) } - _catalog.update(::complete) - } - } - - /** - * [catalog] with the [records] laid over the entries that lack releases. - * - * Applied to everything published and not only once after a fetch, because every reload brings - * the same bare entries back. - * - * Only a record of the very releases the entry names: the mirror answers when the site does - * not, and its record may be older. - */ - private fun complete(catalog: StoreCatalog): StoreCatalog { - val records = records - if (records.isEmpty()) return catalog - return catalog.copy( - modules = - catalog.modules.map { module -> - records[module.name]?.takeIf { - module.releases.isNullOrEmpty() && - it.latestRelease == module.latestRelease && - it.latestBetaReleaseTime == module.latestBetaReleaseTime - } ?: module - } - ) - } - - /** Re-reads installed versions; called on opening the Store and after an install lands. */ - override fun refreshInstalled() { - scope.launch { loadInstalled() } } +} - /** - * The same read, awaited and handed back, for a caller that has to act on what it finds. - * - * Which is how an install records what it produced: the note that suppresses a satisfied offer - * is compared against [installedVersions], so it has to be written from that same reading. A - * local `getPackageInfo` would answer for user 0 while this map answers with the highest version - * across every user, and on a device with a work profile the two differ — leaving a note that can - * never match and a row that nags for ever. - * - * Returns the last known map when the daemon cannot be reached, which is the safe direction: a - * note written from a stale version simply fails to match, and the offer stays. - */ - suspend fun readInstalled(): Map { - loadInstalled() - return _installed.value - } +private val VectorLog = StoreLog { priority, message, cause -> + if (priority >= Log.WARN) logW(message, cause) else logI(message, cause) +} - private suspend fun loadInstalled() { - val packages = - daemon - .getInstalledPackagesFromAllUsers(0, false) - .onFailure { e -> - logW("store: installed versions unavailable", e) - } - .getOrNull() ?: return - val versions = HashMap(packages.size) - for (info in packages) { - val version = RepoVersion(info.versionCodeCompat, info.versionName.orEmpty()) - // The daemon reports every user, so the same package arrives more than once. The - // highest version wins, because that is the one an update would have to beat. - val known = versions[info.packageName] - if (known == null || version.versionCode > known.versionCode) { - versions[info.packageName] = version - } +/** + * What each package is at, across every user, or null when the daemon cannot be reached. + * + * One `getInstalledPackagesFromAllUsers` call and no `ModuleDetection`: the Store already knows + * that every name it asks about is a module, so it does not need the much more expensive discovery + * the Modules screen runs, which opens every APK to find out. + * + * Every user rather than user 0, which matters to an install recording what it produced: on a + * device with a work profile a local `getPackageInfo` would answer differently from this map, and a + * note written from the one can never match the other. + */ +private suspend fun DaemonClient.installedVersions(): Map? { + val packages = + getInstalledPackagesFromAllUsers(0, false) + .onFailure { e -> logW("store: installed versions unavailable", e) } + .getOrNull() ?: return null + val versions = HashMap(packages.size) + for (info in packages) { + val version = RepoVersion(info.versionCodeCompat, info.versionName.orEmpty()) + // The daemon reports every user, so the same package arrives more than once. The highest + // version wins, because that is the one an update would have to beat. + val known = versions[info.packageName] + if (known == null || version.versionCode > known.versionCode) { + versions[info.packageName] = version } - _installed.value = versions - } - - private companion object { - /** Matches the mirror's own `cache-control: max-age=600`, so revalidation stays free. */ - const val CATALOG_MAX_AGE_MINUTES = 10 - - /** Longer: a module's release history changes far less often than the index does. */ - const val DETAIL_MAX_AGE_MINUTES = 60 } + return versions } From 6b896dce809fe9cc134d8ca80082d8ce253af462 Mon Sep 17 00:00:00 2001 From: JingMatrix Date: Sun, 15 Nov 2026 00:15:23 +0100 Subject: [PATCH 9/9] Correct an entry on the channel the page is later on, not both MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit correct() decides per channel whether the page or the base states the later release, then took both channels' fields from the page whichever answer it got. A page ahead on the beta and behind on the stable — a beta published since the rebuild, a stable release the page has yet to name — would carry its older latestRelease into the entry, which is the regression the >= guard on latestReleaseTime is there to refuse, and would drop the releases that install the newer one. A page stating an older beta time would take that too, leaving the entry naming a beta whose time no longer matches it, a pair complete() refuses to lay a record over, so an installed module stays without an APK until something else changes its name. Each channel now keeps what it holds unless this page states something newer on it. Both release lists still go together, since a detail record carries both. Also here: the Store's comments lose what cannot be checked and what dates. The site does not mark its index pages must-revalidate, and nothing either host serves does, so the interceptor is described by the response it answers rather than by a host that sends one. Entry counts, null-field tallies, asset censuses, page timings and the modules named for an odd release history are gone with them; the payload shapes they illustrated are what the code reads, and those are stated on their own. StoreDataSource no longer says it is where the two apps' network code differs, which StoreRepository ended. One gap: nothing the two hosts serve today reaches either branch of the first paragraph, which is why no report names it. --- .../org/matrix/vector/ui/store/RepoModels.kt | 33 ++++--- .../matrix/vector/ui/store/RepoViewModel.kt | 22 ++--- .../org/matrix/vector/ui/store/StoreApi.kt | 86 ++++++++++--------- .../matrix/vector/ui/store/StoreDataSource.kt | 7 +- .../matrix/vector/ui/store/StoreRepository.kt | 27 +++--- 5 files changed, 91 insertions(+), 84 deletions(-) diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt index 0e86e17603..761d3c85a0 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoModels.kt @@ -5,14 +5,13 @@ import com.google.gson.annotations.SerializedName /** * The module repository's JSON, as types. * - * These mirror what the server actually sends, measured against a live `modules.json` of 809 - * entries. Two things shape the file: + * These mirror what the server sends. Two things shape the file: * - * **Nullability is not decoration here.** `scope` is null on 506 of the 809 entries, `sourceUrl` on - * 369 and `summary` on 121. Gson constructs through `Unsafe` and runs neither Kotlin's - * default-argument logic nor its null checks, so a non-null type on a field the server omits yields - * a `null` that only explodes at the first dereference, far from the parse. Every field the payload - * does not guarantee is therefore declared optional. + * **Nullability is not decoration here.** Many entries leave `scope`, `sourceUrl` or `summary` + * null. Gson constructs through `Unsafe` and runs neither Kotlin's default-argument logic nor its + * null checks, so a non-null type on a field the server omits yields a `null` that only explodes at + * the first dereference, far from the parse. Every field the payload does not guarantee is + * therefore declared optional. * * **The list payload is nearly a detail payload.** Each list entry already carries exactly one * release — the newest — with its `.apk` asset and download URL. Installing the current version of @@ -62,8 +61,8 @@ data class Collaborator( /** * Someone credited beyond the repository's collaborators. * - * An object, not a bare name, though the field reads like a list of names: the 49 entries that - * carry one hold `{type, name, link}`. Typed as a list of strings it makes Gson throw `Expected a + * An object, not a bare name, though the field reads like a list of names: an entry that carries + * one holds `{type, name, link}`. Typed as a list of strings it makes Gson throw `Expected a * string but was BEGIN_OBJECT`, which takes the whole catalogue parse — and with it the entire * Store — rather than the one module. */ @@ -92,11 +91,11 @@ data class Release( /** * A stable identity for a lazy list. * - * Release *names* are not unique in real data: `com.rww.wetypeswipe` currently publishes two - * releases both named `1.11.4`, under tags `43-` and `42-`. `LazyColumn` throws - * `IllegalArgumentException` on a duplicate key, so identity comes from `id`, which is unique - * by construction, with the tag as fallback and the index as last resort — a malformed payload - * then degrades into an odd-looking list rather than a crash. + * Release *names* are not unique in real data: a module can publish two releases under one name + * and different tags. `LazyColumn` throws `IllegalArgumentException` on a duplicate key, so + * identity comes from `id`, which is unique by construction, with the tag as fallback and the + * index as last resort — a malformed payload then degrades into an odd-looking list rather than + * a crash. */ fun key(index: Int): String = id ?: tagName ?: "release:$index" @@ -120,10 +119,8 @@ data class ReleaseAsset( /** * Whether this asset is an APK. * - * Judged on the declared content type *and* the filename: 923 of the 946 assets in the - * catalogue declare `application/vnd.android.package-archive`, but a few authors upload theirs - * as `application/octet-stream`, and trusting the type alone would hide the only download those - * modules have. + * Judged on the declared content type *and* the filename: an APK uploaded under a generic + * content type is still the only download its module has, and the type alone would hide it. */ val isApk: Boolean get() = diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt index 6cfe003d3c..33c0d710ad 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/RepoViewModel.kt @@ -37,9 +37,9 @@ enum class StoreSort { /** * Which releases count. * - * Two options, not three. Of the 809 modules in the catalogue 14 publish a beta and **none** - * publishes a snapshot, so a snapshot channel would have no data behind it at all — and a control - * that can never change anything is a control that lies. + * Two options, not three. The payload carries a beta release list and nothing a snapshot channel + * could select from, so such a channel would have no data behind it — and a control that can never + * change anything is a control that lies. */ enum class StoreChannel(val preference: String) { Stable("stable"), @@ -63,9 +63,9 @@ enum class StoreChannel(val preference: String) { * * On the prerelease channel the order has to come from the version code rather than from position * in either shape. The two arrays are sorted independently, a detail record is sorted by date, and - * a beta is not automatically newer: `com.luoshui.paycardeditor` publishes beta code 1 against - * stable code 8, and `io.github.wumisc.antiburn` a stable code 10 two weeks after beta - * code 45. [latestOn] names the highest code, so the first element here has to be that release. + * a beta is not automatically newer: a module's newest beta can carry a lower code than its newest + * stable release. [latestOn] names the highest code, so the first element here has to be that + * release. */ public fun OnlineModule.releasesOn(channel: StoreChannel): List { val published = releases.orEmpty().filter { it.isDraft != true } @@ -84,8 +84,8 @@ public fun OnlineModule.releasesOn(channel: StoreChannel): List { * The version the channel says is current — which is what every "Update to …" label states. * * On the prerelease channel that is whichever of the two channels is genuinely newer, not the beta - * unconditionally. Advertising `latestBetaRelease` on its own offers `paycardeditor`'s readers a - * downgrade from code 8 to code 1 and calls it an update. + * unconditionally. Advertising `latestBetaRelease` on its own would offer a reader whose newest + * beta is older than their stable release a downgrade, and call it an update. */ public fun OnlineModule.latestOn(channel: StoreChannel): RepoVersion? { val stable = RepoVersion.parse(latestRelease) @@ -139,7 +139,7 @@ class RepoViewModel( * Every catalogue entry paired with what this device has, before any filtering. * * Shared rather than recomputed per consumer: both the list and the "n updates" count need it, - * and it walks 809 entries. + * and it walks the whole catalogue. */ private val allEntries: StateFlow> = combine( @@ -166,8 +166,8 @@ class RepoViewModel( entries.filter { it.matches(query) }.sortedWith(comparatorFor(view)) } // Off the main thread, for the reason ModulesViewModel records: stateIn on its own - // collects on Dispatchers.Main.immediate, which would put a filter and a sort over 809 - // entries on the UI thread on every keystroke. + // collects on Dispatchers.Main.immediate, which would put a filter and a sort over the + // whole catalogue on the UI thread on every keystroke. .flowOn(Dispatchers.Default) .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList()) diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreApi.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreApi.kt index ebad280398..d27e12c46b 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreApi.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreApi.kt @@ -18,24 +18,24 @@ import okhttp3.Response /** * The module repository's two hosts, and how one catalogue is made of them. * - * **Neither host serves what the Store needs on its own.** Probed rather than assumed: + * **Neither host serves what the Store needs on its own.** * - the mirror, `backup.modules.lsposed.org`, serves `modules.json`: every module in one cacheable - * 2 MB response, each entry carrying its newest release with the APK. It is regenerated daily but - * ran three days behind when measured, so a release published since is not in it. + * response, each entry carrying its newest release with the APK. It is a periodic rebuild, so a + * release published since the last one is not in it. * - the site, `modules.lsposed.org`, answers `modules.json` with a 403 and serves the index only as - * `module-list/.json`: thirty entries a page, newest release first, current to the minute. - * Those entries state `latestRelease` but carry no release, scope or collaborators, and the pages - * answer no conditional request with a 304, so walking all of them costs 45 requests and 1.3 MB - * every time. + * `module-list/.json`, a page of entries at a time, newest release first and current. Those + * entries state `latestRelease` but carry no release, scope or collaborators, and the pages + * answer a conditional request with the whole page rather than a 304, so walking all of them + * costs the whole index every time. * - * So the mirror is the base and the site corrects it. Because the pages are ordered by release time - * and the mirror has only been seen *behind*, everything the mirror lacks sits at the front: the - * walk stops at the first page that agrees with the mirror entirely, typically the fifth. An entry - * the site knows better loses its now-outdated releases rather than keep an APK that is no longer - * the one its version names; [StoreRepository] fetches the detail record for the installed ones. + * So the mirror is the base and the site corrects it. The pages are ordered by release time and the + * mirror only ever trails, so everything it lacks sits at the front: the walk stops at the first + * page that agrees with the mirror entirely. An entry the site knows better loses its now-outdated + * releases rather than keep an APK that is no longer the one its version names; [StoreRepository] + * fetches the detail record for the installed ones. * - * Either host can vanish. Without the site the mirror is shown as it is, which is what the Store - * did before. Without the mirror every page is read, and the catalogue is the site's alone. + * Either host can vanish. Without the site the mirror is shown as it is; without the mirror every + * page is read, and the catalogue is the site's alone. */ internal class StoreApi( client: OkHttpClient, @@ -50,9 +50,9 @@ internal class StoreApi( * * Two things otherwise empty it. OkHttp stores a 404 as readily as a 200, and under the same * key, so a host that starts answering 404 — how a retired static site fails — would overwrite - * the last good catalogue with its own error page. And the site marks its pages - * `must-revalidate`, which forbids exactly the stale read a cold start with no network is: a - * `FORCE_CACHE` request for one is refused although the bytes are on disk. + * the last good catalogue with its own error page. And `must-revalidate` forbids exactly the + * stale read a cold start with no network is: a `FORCE_CACHE` request for such a response is + * refused although the bytes are on disk. */ private val client = client @@ -158,11 +158,11 @@ internal class StoreApi( val parsed = usable(parseSnapshot(response)) if (parsed.isEmpty()) return@fetch null logI("store: ${parsed.size} modules from $url") - // `fromCache` is deliberately *not* derived from `response.networkResponse`. A hit - // inside the ten-minute freshness window is served from disk without touching the - // network, and calling that "the saved catalogue" would put an offline notice on - // a perfectly current list. Staleness is a property of which branch produced this, - // so the caller sets the flag on the fallback path and only there. + // `fromCache` is not `networkResponse == null`: a hit inside the freshness window + // is served from disk without touching the network, and calling that "the saved + // catalogue" would put an offline notice on a perfectly current list. Staleness is a + // property of the branch that produced the catalogue, so the caller sets the flag on + // the fallback path and only there. StoreCatalog(parsed, loaded = true, loadedAtMillis = response.receivedResponseAtMillis) } } @@ -176,9 +176,9 @@ internal class StoreApi( /** * [numbers] in order, each null where its page could not be read. * - * Together rather than in turn, because the site takes a third of a second over each page - * however small it is: 45 of those in a row is a quarter of a minute under a spinner, and at - * once, down the one HTTP/2 connection, they are half a second. + * Together rather than in turn, because a page costs a round trip whatever its size: in a row + * the whole index is a wait under a spinner, and at once, down the one HTTP/2 connection, it is + * about the cost of one. */ private fun fetchPages(numbers: IntRange, cacheControl: CacheControl): List = if (numbers.first == numbers.last) { @@ -231,11 +231,11 @@ internal class StoreApi( * * Binding the whole array in a single `fromJson` call fails the entire Store on one unexpected * field — `additionalAuthors` holds objects rather than the strings its name suggests, and - * `AdditionalAuthor` exists because of it. This is third-party data written by hundreds of - * authors, so an entry the model does not expect must cost that entry and nothing else. + * `AdditionalAuthor` exists because of it. This is third-party data, so an entry the model does + * not expect must cost that entry and nothing else. * - * Streamed off the response rather than through a `String`, which also keeps the 2 MB body from - * being materialised twice. + * Streamed off the response rather than through a `String`, which also keeps a body of this + * size from being materialised twice. */ private fun parseSnapshot(response: Response): List { val modules = ArrayList(1536) @@ -270,22 +270,25 @@ internal class StoreApi( /** Pages read together past the first, where the walk expects to stop early. */ const val READ_AHEAD = 4 - /** Four times today's index; past it a `pageCount` is a mistake, not a catalogue. */ + /** Well past what the index needs; beyond it a `pageCount` is a mistake, not a list. */ const val MAX_PAGES = 200 /** * Folds one page entry into [modules]; true when the page knew something they did not. * * The stable version and the time of the newest beta are the whole comparison. The two - * hosts word `summary` and `sourceUrl` differently for hundreds of unchanged modules, so + * hosts word `summary` and `sourceUrl` differently for modules that have not changed, so * comparing more would report a difference on every page and the walk would never stop * early. * - * **The later statement wins, not the site.** That the mirror is the one behind is what was - * measured, not what is promised, and a site that stopped being rebuilt would otherwise + * **The later statement wins, not the site, and one channel at a time.** The mirror is only + * known to trail, never promised to, so a site that stopped being rebuilt would otherwise * take every version the mirror has since learnt back to its own. The cost is a withdrawn - * release, which the site states by naming an older one: that waits for the mirror, as it - * did when the mirror was all there was. + * release, which the site states by naming an older one: that waits for the mirror. + * + * A page is the later of the two on one channel and the earlier on the other often enough — + * a beta published since the snapshot, a stable release the page has yet to name — so each + * channel keeps what it holds unless this page states something newer on it. * * The beta is compared by its time because that is all a page says of it: there is no * `latestBetaRelease` to read. A newer beta therefore leaves the entry naming none, and the @@ -305,12 +308,15 @@ internal class StoreApi( description = entry.description ?: known.description, stargazerCount = entry.stargazerCount ?: known.stargazerCount, updatedAt = entry.updatedAt ?: known.updatedAt, - latestRelease = entry.latestRelease, - latestReleaseTime = entry.latestReleaseTime, + latestRelease = if (newerRelease) entry.latestRelease else known.latestRelease, + latestReleaseTime = + if (newerRelease) entry.latestReleaseTime else known.latestReleaseTime, latestBetaRelease = known.latestBetaRelease.takeUnless { newerBeta }, - latestBetaReleaseTime = entry.latestBetaReleaseTime, - // Both lists are the base's, and its newest is no longer the newest: left in - // place, a one-tap update would install the old APK under the new version. + latestBetaReleaseTime = + if (newerBeta) entry.latestBetaReleaseTime else known.latestBetaReleaseTime, + // Both lists are the base's, and one of its newest is no longer the newest: + // left in place, a one-tap update would install the old APK under the new + // version. The detail record carries both channels, so both go together. releases = null, betaReleases = null, ) ?: entry diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt index ff969b535f..a0a1412268 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreDataSource.kt @@ -3,9 +3,10 @@ package org.matrix.vector.ui.store import kotlinx.coroutines.flow.StateFlow /** - * Everything the shared Store screen needs from its host to reach the catalogue and the device — the - * one place Vector's daemon+OkHttp and LSPatch's Shizuku+HttpURLConnection differ. Each app provides - * an implementation; the [RepoViewModel] and screen are otherwise identical. + * Everything the shared Store screen needs from its host to reach the catalogue and the device. + * [StoreRepository] implements it for every app that hosts the Store, since how the repository is + * read is a fact about the repository; what an app supplies is how the device is asked what is + * installed and where a log line goes. */ interface StoreDataSource { /** The online module catalogue, updated by [refresh]. */ diff --git a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt index 50bd38eeb2..353ec64dab 100644 --- a/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt +++ b/manager-ui/src/main/kotlin/org/matrix/vector/ui/store/StoreRepository.kt @@ -36,16 +36,15 @@ fun interface StoreLog { * here is when to ask and what to do when asking fails. * * **Caching is declared, not hoped for.** Every request states its own freshness, so the client's - * disk cache is actually used: the mirror's snapshot revalidates against the server's own - * ten-minute `max-age` and its ETag, pull-to-refresh forces the network, and when the network - * yields nothing the same requests are replayed against the cache alone. That last step is why a - * cold start with no network renders the last known catalogue rather than an error, and it is also - * what gives the DNS-over-HTTPS setting an effect here, since the shared client is the one carrying - * the DoH resolver. + * disk cache is actually used: the mirror's snapshot revalidates against the host's own `max-age` + * and its ETag, pull-to-refresh forces the network, and when the network yields nothing the same + * requests are replayed against the cache alone. That last step is why a cold start with no network + * renders the last known catalogue rather than an error, and it is also what gives the + * DNS-over-HTTPS setting an effect here, since the shared client is the one carrying the DoH + * resolver. * - * There is deliberately **no snapshot file** of our own. The OkHttp cache already holds these exact - * bytes; a 2 MB duplicate in the same cache directory would buy nothing but a second thing to keep - * in sync. + * **No snapshot file of its own.** The OkHttp cache already holds these exact bytes; a duplicate in + * the same cache directory would buy nothing but a second thing to keep in sync. */ open class StoreRepository( client: OkHttpClient, @@ -86,7 +85,7 @@ open class StoreRepository( * are looking at is stale. */ override suspend fun refresh(force: Boolean) { - // A second caller during a refresh is a no-op rather than a queued duplicate of a 2 MB + // A second caller during a refresh is a no-op rather than a queued duplicate of the whole // download. if (!refreshing.tryLock()) return try { @@ -241,10 +240,14 @@ open class StoreRepository( } private companion object { - /** Matches the mirror's own `cache-control: max-age=600`, so revalidation stays free. */ + /** Matches the mirror's own `max-age`, so an unforced refresh revalidates. */ const val CATALOG_MAX_AGE_MINUTES = 10 - /** Longer: a module's release history changes far less often than the index does. */ + /** + * A ceiling, not an extension: a host states its own `max-age` for a record and OkHttp + * takes whichever of the two is shorter. This only bounds a record that arrives without + * one. + */ const val DETAIL_MAX_AGE_MINUTES = 60 } }