///| /// 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-` 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) -> Int32? { 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-` stands for. pub fn tag_version(tag : String) -> Int32? { 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(Int32) /// A newer build exists and should be installed. Install(Int32) /// The user already cancelled this build's install; not offered again unless they ask. Declined(Int32) } ///| /// `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 : Int32, installed : Int32, declined : Int32, 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 }