Skip to main content

lingxia_update/
config.rs

1use serde::{Deserialize, Serialize};
2use std::sync::{OnceLock, RwLock};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
5#[serde(rename_all = "camelCase")]
6pub enum UpdateUiMode {
7    /// LingXia owns the host app update prompt, download progress UI, and install handoff.
8    #[default]
9    Builtin,
10    /// LingXia never shows built-in host app update UI or installs automatically.
11    /// The native host owns check/download/install UX explicitly.
12    Custom,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct UpdateConfig {
18    /// Whether startup should automatically check host app updates.
19    ///
20    /// - Builtin mode: auto check can show LingXia prompt/progress and request install.
21    /// - Custom mode: auto check only emits availability events; it never downloads or installs.
22    /// - Set this to false in custom mode when the native host wants full manual control.
23    #[serde(default = "default_enabled")]
24    pub auto_check_app: bool,
25    #[serde(default)]
26    pub ui_mode: UpdateUiMode,
27    #[serde(default = "default_enabled")]
28    pub force_update_gate: bool,
29}
30
31impl Default for UpdateConfig {
32    fn default() -> Self {
33        Self {
34            auto_check_app: true,
35            ui_mode: UpdateUiMode::Builtin,
36            force_update_gate: true,
37        }
38    }
39}
40
41fn default_enabled() -> bool {
42    true
43}
44
45fn config_store() -> &'static RwLock<UpdateConfig> {
46    static UPDATE_CONFIG: OnceLock<RwLock<UpdateConfig>> = OnceLock::new();
47    UPDATE_CONFIG.get_or_init(|| RwLock::new(UpdateConfig::default()))
48}
49
50pub fn update_config() -> UpdateConfig {
51    config_store()
52        .read()
53        .unwrap_or_else(|err| err.into_inner())
54        .clone()
55}
56
57pub fn configure_update(config: UpdateConfig) {
58    *config_store()
59        .write()
60        .unwrap_or_else(|err| err.into_inner()) = config;
61}