Skip to main content

lingxia_platform/traits/
update.rs

1use std::path::Path;
2
3use crate::error::PlatformError;
4
5pub trait UpdateService: Send + Sync + 'static {
6    /// Whether this platform installs host-app updates itself (download +
7    /// in-place install/relaunch). Store-delivered platforms (iOS App Store,
8    /// HarmonyOS AppGallery) return `false`: they must update through the
9    /// store, so the update flow never downloads or self-installs there.
10    ///
11    /// Defaults to `false` — opt in per platform (macOS, Android).
12    fn self_update_supported(&self) -> bool {
13        false
14    }
15
16    /// Whether this process was installed by a platform store (Play, App
17    /// Store, MAS, Microsoft Store, AppGallery). Store-installed binaries
18    /// never self-update, even when this build's yaml still says `direct`.
19    fn installed_from_store(&self) -> bool {
20        false
21    }
22
23    /// Open the platform app-store page for this app so the user can update
24    /// through the store. Used on store-delivered platforms when an update is
25    /// available. Returns `true` if a store page was opened. Defaults to
26    /// `false` (no in-app redirect; rely on the store's own update prompts).
27    fn open_update_store(&self, _update_info_json: &str) -> Result<bool, PlatformError> {
28        Ok(false)
29    }
30
31    /// Show a "new version — open the store" prompt (card / callout / tray /
32    /// alert). Confirm opens the store; the package is never downloaded.
33    /// Returns `true` when a UI was presented.
34    fn present_store_update(&self, _update_info_json: &str) -> Result<bool, PlatformError> {
35        Ok(false)
36    }
37
38    /// Requests installation of an application update from a local package file.
39    ///
40    /// This starts the platform-specific apply flow and returns once the request
41    /// is handed off to the updater helper.
42    ///
43    /// # Arguments
44    /// * `package_path` - Local, readable update package path (e.g. .apk on Android)
45    /// * `info_json` - Prompt metadata `{version, releaseNotes}` shown in the
46    ///   dismissible "ready to update" prompt.
47    ///
48    /// # Platform Support / Notes
49    /// - Android: Shows the post-download "ready to install" prompt (with
50    ///   release notes), then launches the system installer on confirm.
51    ///   Requires `REQUEST_INSTALL_PACKAGES` and a `FileProvider` for APK sharing.
52    /// - macOS: Stages a prepared `.zip` or `.app` update, shows the
53    ///   "ready to update" callout, and relaunches on the user's click.
54    /// - iOS: Not supported (App Store only).
55    /// - HarmonyOS: Not implemented (returns error).
56    fn install_update(&self, package_path: &Path, info_json: &str) -> Result<(), PlatformError> {
57        let _ = (package_path, info_json);
58        Err(PlatformError::NotSupported(
59            "install_update not implemented for this platform".to_string(),
60        ))
61    }
62}
63
64/// `storeUrl` from the store-channel prompt JSON.
65pub fn store_url_in_update_info(info_json: &str) -> Option<String> {
66    serde_json::from_str::<serde_json::Value>(info_json)
67        .ok()
68        .and_then(|value| {
69            value
70                .get("storeUrl")
71                .and_then(|v| v.as_str())
72                .map(str::trim)
73                .filter(|s| !s.is_empty())
74                .map(str::to_string)
75        })
76}