Add Preview update history

This commit is contained in:
xXJsonDeruloXx committed 2026-10-03 00:06:09 -04:00
1 parent abdc46c7a7
commit 3c2064f30c
7 files changed
+363 -9

No files matched your search

@@ -116,12 +116,12 @@ internal fun UpdatesPage(s: FrontEndState, a: FrontEndActions, modifier: Modifie
StatusPanel(s, u, ua, me)
Box(Modifier.height(18.dp))
ChannelLabel()
ChannelPicker(u, ua)
ChannelPicker(u, ua, me)
} else {
// The label sits over both columns, so the status card lines up with the first channel.
ChannelLabel()
Row(horizontalArrangement = Arrangement.spacedBy(18.dp)) {
Box(Modifier.weight(1f)) { ChannelPicker(u, ua) }
Box(Modifier.weight(1f)) { ChannelPicker(u, ua, me) }
Box(Modifier.weight(1.15f)) { StatusPanel(s, u, ua, me) }
}
}
@@ -260,7 +260,7 @@ private fun ChannelLabel() {
}
@Composable
private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions) {
private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions, me: AppUpdates.Installed) {
val colors = MaterialTheme.colorScheme
val catalog = u.catalog
val tests = catalog?.tests.orEmpty()
@@ -273,6 +273,7 @@ private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions) {
Icons.Outlined.Bolt, stringResource(R.string.upd_preview), stringResource(R.string.upd_preview_hint),
catalog?.preview?.let { ago(it.publishedAt) }, u.follow.channel == Channel.NIGHTLY,
) { ua.onFollow(Follow(Channel.NIGHTLY)) }
PreviewHistory(catalog, me)
ChannelCard(
Icons.Outlined.Science, stringResource(R.string.upd_tests), stringResource(R.string.upd_tests_hint),
if (tests.isEmpty()) stringResource(R.string.upd_tests_none) else pluralStringResource(R.plurals.upd_tests_count, tests.size, tests.size),
@@ -286,6 +287,63 @@ private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions) {
}
}
/** Compact, read-only history under Preview; only the latest Preview build remains installable. */
@Composable
private fun PreviewHistory(catalog: AppUpdates.Catalog?, me: AppUpdates.Installed) {
val history = catalog?.let { AppUpdates.previewHistory(it, me) } ?: return
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
val key = catalog?.recentPreviews?.firstOrNull()?.commit
var expanded by rememberSaveable(key) { mutableStateOf(false) }
val heading = when (history.kind) {
AppUpdates.PreviewHistoryKind.CURRENT -> stringResource(R.string.upd_preview_current)
AppUpdates.PreviewHistoryKind.BEHIND -> pluralStringResource(
R.plurals.upd_preview_behind, history.buildsBehind, history.buildsBehind,
)
AppUpdates.PreviewHistoryKind.RECENT -> pluralStringResource(
R.plurals.upd_preview_recent, history.changes.size, history.changes.size,
)
}
Column(Modifier.fillMaxWidth().padding(start = 66.dp, end = 14.dp, bottom = 5.dp)) {
Text(heading, fontSize = 11.5.sp, fontWeight = FontWeight.Medium, color = colors.onSurfaceVariant)
if (history.changes.isNotEmpty()) {
val visible = if (expanded) history.changes else history.changes.take(3)
Column(Modifier.padding(top = 3.dp), verticalArrangement = Arrangement.spacedBy(3.dp)) {
visible.forEach { change ->
Column {
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) {
Text(
changeTitle(change.title.ifBlank { change.commit.take(7) }),
fontSize = 12.5.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground,
maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f),
)
Text(ago(change.publishedAt), fontSize = 11.sp, color = colors.onSurfaceVariant)
}
if (change.summary.isNotBlank()) Text(
change.summary, fontSize = 11.5.sp, color = colors.onSurfaceVariant,
maxLines = 1, overflow = TextOverflow.Ellipsis,
)
}
}
}
if (history.changes.size > 3 || expanded) {
val src = remember { MutableInteractionSource() }
val hot = rememberHot(src)
val toggle = { expanded = !expanded }
Text(
if (expanded) stringResource(R.string.upd_less) else stringResource(R.string.upd_more),
fontSize = 12.sp, fontWeight = FontWeight.SemiBold, color = pal.signal,
modifier = Modifier.padding(top = 1.dp).offset(x = (-6).dp).paneItem("preview:history:more")
.clip(Shape12).glideBorder(hot, Shape12, pal.signal)
.hoverable(src).clickable(interactionSource = src, indication = null, role = Role.Button, onClick = toggle)
.controllerConfirm(onClick = toggle)
.padding(horizontal = 6.dp, vertical = 4.dp),
)
}
}
}
}
@Composable
private fun ChannelCard(
icon: ImageVector, title: String, hint: String, latest: String?, selected: Boolean,
@@ -54,11 +54,19 @@ object AppUpdates {
val apk: Apk?,
)
class PreviewChange(
val commit: String,
val title: String,
val summary: String,
val publishedAt: Long,
)
class Catalog(
val stable: Release?,
val preview: Release?,
val tests: List<Release>,
val checkedAt: Long,
val recentPreviews: List<PreviewChange> = emptyList(),
)
class Installed(
@@ -68,6 +76,15 @@ object AppUpdates {
val versionCode: Int,
val updatable: Boolean,
val committedAt: Long = 0L,
val ciBuild: Boolean = BuildConfig.CI_BUILD,
)
enum class PreviewHistoryKind { CURRENT, BEHIND, RECENT }
class PreviewHistory(
val kind: PreviewHistoryKind,
val buildsBehind: Int,
val changes: List<PreviewChange>,
)
enum class Offer {
@@ -146,6 +163,37 @@ object AppUpdates {
r.commit.startsWith(me.commit, ignoreCase = true)
}
/** Recent Preview changes relative to a CI-installed commit, when its position is known. */
fun previewHistory(catalog: Catalog, me: Installed = installed()): PreviewHistory? {
if (!me.ciBuild || catalog.recentPreviews.isEmpty()) return null
val stable = catalog.stable
if (
me.pr != 0 || stable?.let { isRunning(it, me) } == true ||
stable?.version?.let { compareVersions(me.version, it) <= 0 } == true
) {
return PreviewHistory(PreviewHistoryKind.RECENT, 0, catalog.recentPreviews)
}
val matching = catalog.recentPreviews.indices.filter {
matchesInstalledCommit(catalog.recentPreviews[it].commit, me.commit)
}
if (matching.size != 1) {
return PreviewHistory(PreviewHistoryKind.RECENT, 0, catalog.recentPreviews)
}
val index = matching.single()
return if (index == 0) {
PreviewHistory(PreviewHistoryKind.CURRENT, 0, emptyList())
} else {
PreviewHistory(PreviewHistoryKind.BEHIND, index, catalog.recentPreviews.take(index))
}
}
private fun matchesInstalledCommit(published: String, installed: String): Boolean {
if (!Regex("^[0-9a-fA-F]{7,40}$").matches(installed)) return false
return published.equals(installed, ignoreCase = true) ||
(installed.length < 40 && published.startsWith(installed, ignoreCase = true))
}
/** Why Android cannot install this published build over [me], before downloading anything. */
fun installBlock(r: Release, me: Installed = installed()): String? = when {
r.apk == null -> "This build has no download for this copy of DroidDeck."
@@ -218,6 +266,7 @@ object AppUpdates {
)
}
val tests = root.optJSONArray("tests") ?: JSONArray()
val recentPreviews = root.optJSONArray("recentPreviews") ?: JSONArray()
return Catalog(
root.optJSONObject("stable")?.let { readPublishedRelease(it, packageName) },
root.optJSONObject("preview")?.let { readPublishedRelease(it, packageName) },
@@ -226,9 +275,20 @@ object AppUpdates {
.filter { it.pr > 0 }
.sortedByDescending { it.publishedAt },
checkedAt,
(0 until recentPreviews.length())
.mapNotNull { recentPreviews.optJSONObject(it)?.let(::readPublishedPreviewChange) }
.sortedByDescending { it.publishedAt }
.distinctBy { it.commit.lowercase() }
.take(10),
)
}
private fun readPublishedPreviewChange(o: JSONObject): PreviewChange? {
val commit = o.optString("commit")
if (!Regex("^[0-9a-fA-F]{40}$").matches(commit)) return null
return PreviewChange(commit.lowercase(), o.optString("title"), o.optString("summary"), o.optLong("publishedAt"))
}
private fun readPublishedRelease(o: JSONObject, packageName: String): Release {
val commit = o.getString("commit")
if (!Regex("^[0-9a-fA-F]{7,40}$").matches(commit)) {
@@ -358,23 +418,42 @@ object AppUpdates {
},
)
private fun writeCatalog(c: Catalog): JSONObject = JSONObject()
internal fun writeCatalog(c: Catalog): JSONObject = JSONObject()
.put("stable", c.stable?.let(::writeRelease))
.put("preview", c.preview?.let(::writeRelease))
.put("tests", JSONArray().apply { c.tests.forEach { put(writeRelease(it)) } })
.put("recentPreviews", JSONArray().apply { c.recentPreviews.forEach { put(writePreviewChange(it)) } })
.put("checkedAt", c.checkedAt)
private fun readCachedCatalog(o: JSONObject): Catalog {
private fun writePreviewChange(c: PreviewChange): JSONObject = JSONObject()
.put("commit", c.commit)
.put("title", c.title)
.put("summary", c.summary)
.put("publishedAt", c.publishedAt)
internal fun readCachedCatalog(o: JSONObject): Catalog {
val tests = o.optJSONArray("tests") ?: JSONArray()
val preview = o.optJSONObject("preview") ?: o.optJSONObject("nightly")
val recentPreviews = o.optJSONArray("recentPreviews") ?: JSONArray()
return Catalog(
o.optJSONObject("stable")?.let(::readCachedRelease),
preview?.let(::readCachedRelease),
(0 until tests.length()).map { readCachedRelease(tests.getJSONObject(it)) },
o.optLong("checkedAt"),
(0 until recentPreviews.length())
.mapNotNull { recentPreviews.optJSONObject(it)?.let(::readCachedPreviewChange) }
.sortedByDescending { it.publishedAt }
.distinctBy { it.commit.lowercase() }
.take(10),
)
}
private fun readCachedPreviewChange(o: JSONObject): PreviewChange? {
val commit = o.optString("commit")
if (!Regex("^[0-9a-fA-F]{40}$").matches(commit)) return null
return PreviewChange(commit.lowercase(), o.optString("title"), o.optString("summary"), o.optLong("publishedAt"))
}
private fun prefs(context: Context) =
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
}