grid: packing, pinning, resize steps (port of Layout.js and app.py's grid logic). gesture: pointer state machine (tap, long press, drag, release, swipe). updater: release/version parsing and the update decision. vdom: pure virtual DOM with an HTML-text renderer for tests. app: Elm-style Model/Msg/update/view with effects as data, persistence (JSON layout, import of the old WebView build's localStorage), drawer/search, menus, drag and resize. 60 headless tests drive it through pointer input. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
112 lines
2.9 KiB
MoonBit
112 lines
2.9 KiB
MoonBit
///|
|
|
/// Self-update logic, pure. The shell does the network and the install; this decides
|
|
/// what the releases it fetched mean.
|
|
///
|
|
/// CI publishes a Gitea release `build-<versionCode>` holding `ultra-launcher.apk` and
|
|
/// `version.json` (`{"versionCode": N, "commit": SHA}`), with N also the APK's
|
|
/// versionCode. The latest-release API names the download URLs of both assets.
|
|
|
|
///|
|
|
pub let release_api : String =
|
|
"https://git.site.latha.org/api/v1/repos/experiments/launcher/releases/latest"
|
|
|
|
///|
|
|
pub let apk_asset : String = "ultra-launcher.apk"
|
|
|
|
///|
|
|
pub let version_asset : String = "version.json"
|
|
|
|
///|
|
|
/// How often the home screen checks by itself (6 hours).
|
|
pub let check_every_ms : Int64 = 6L * 60L * 60L * 1000L
|
|
|
|
///|
|
|
pub(all) struct Release {
|
|
tag : String
|
|
version_url : String?
|
|
apk_url : String?
|
|
}
|
|
|
|
///|
|
|
/// The latest-release JSON from the Gitea API.
|
|
pub fn parse_release(text : String) -> Release? {
|
|
let json = @jsonx.parse(text)
|
|
guard json is Some(json) else { return None }
|
|
let mut version_url = None
|
|
let mut apk_url = None
|
|
for a in @jsonx.arr(json, "assets") {
|
|
let name = @jsonx.str(a, "name")
|
|
let url = @jsonx.str(a, "browser_download_url")
|
|
if url != "" {
|
|
if name == version_asset {
|
|
version_url = Some(url)
|
|
} else if name == apk_asset {
|
|
apk_url = Some(url)
|
|
}
|
|
}
|
|
}
|
|
Some({ tag: @jsonx.str(json, "tag_name"), version_url, apk_url })
|
|
}
|
|
|
|
///|
|
|
/// The versionCode in a `version.json`.
|
|
pub fn parse_version(text : String) -> Int? {
|
|
guard @jsonx.parse(text) is Some(json) else { return None }
|
|
match @jsonx.get(json, "versionCode") {
|
|
Some(Number(n, ..)) => Some(n.to_int())
|
|
_ => None
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// The versionCode a release tag `build-<N>` stands for.
|
|
pub fn tag_version(tag : String) -> Int? {
|
|
guard tag.has_prefix("build-") else { return None }
|
|
let rest = tag.clamped_view(start=6)
|
|
if rest.length() == 0 {
|
|
return None
|
|
}
|
|
let mut n = 0
|
|
for c in rest {
|
|
if c < '0' || c > '9' {
|
|
return None
|
|
}
|
|
n = n * 10 + (c.to_int() - '0'.to_int())
|
|
}
|
|
Some(n)
|
|
}
|
|
|
|
///|
|
|
#derive(Eq, Debug)
|
|
pub(all) enum Decision {
|
|
UpToDate(Int)
|
|
/// A newer build exists and should be installed.
|
|
Install(Int)
|
|
/// The user already cancelled this build's install; not offered again unless they ask.
|
|
Declined(Int)
|
|
}
|
|
|
|
///|
|
|
/// `latest` is the newest published versionCode, `installed` the running one, `declined`
|
|
/// the last build whose install the user cancelled (-1: none).
|
|
pub fn decide(
|
|
latest : Int,
|
|
installed : Int,
|
|
declined : Int,
|
|
manual~ : Bool,
|
|
) -> Decision {
|
|
if latest <= installed {
|
|
UpToDate(installed)
|
|
} else if !manual && latest == declined {
|
|
Declined(latest)
|
|
} else {
|
|
Install(latest)
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// An automatic check runs at most every `check_every_ms`; a manual one always runs.
|
|
pub fn should_check(now : Int64, last : Int64, manual~ : Bool) -> Bool {
|
|
manual || now - last >= check_every_ms
|
|
}
|