Refactor UpdateMode

This commit is contained in:
Gregory Schier
2024-05-13 07:28:45 -07:00
parent 967590c7ff
commit b640f0c357
2 changed files with 85 additions and 80 deletions
+2 -2
View File
@@ -57,7 +57,7 @@ use crate::models::{
}; };
use crate::plugin::{ImportResult, run_plugin_export_curl, run_plugin_import}; use crate::plugin::{ImportResult, run_plugin_export_curl, run_plugin_import};
use crate::render::render_request; use crate::render::render_request;
use crate::updates::{update_mode_from_str, UpdateMode, YaakUpdater}; use crate::updates::{UpdateMode, YaakUpdater};
use crate::window_menu::app_menu; use crate::window_menu::app_menu;
mod analytics; mod analytics;
@@ -1820,7 +1820,7 @@ fn create_window(handle: &AppHandle, url: Option<&str>) -> WebviewWindow {
async fn get_update_mode(h: &AppHandle) -> UpdateMode { async fn get_update_mode(h: &AppHandle) -> UpdateMode {
let settings = get_or_create_settings(h).await; let settings = get_or_create_settings(h).await;
update_mode_from_str(settings.update_channel.as_str()) UpdateMode::new(settings.update_channel.as_str())
} }
fn safe_uri(endpoint: &str) -> Result<Uri, InvalidUri> { fn safe_uri(endpoint: &str) -> Result<Uri, InvalidUri> {
+22 -17
View File
@@ -1,3 +1,4 @@
use std::fmt::{Display, Formatter};
use std::time::SystemTime; use std::time::SystemTime;
use log::info; use log::info;
@@ -20,6 +21,25 @@ pub enum UpdateMode {
Beta, Beta,
} }
impl Display for UpdateMode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let s = match self {
UpdateMode::Stable => "stable",
UpdateMode::Beta => "beta",
};
write!(f, "{}", s)
}
}
impl UpdateMode {
pub fn new(mode: &str) -> UpdateMode {
match mode {
"beta" => UpdateMode::Beta,
_ => UpdateMode::Stable,
}
}
}
impl YaakUpdater { impl YaakUpdater {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
@@ -33,11 +53,10 @@ impl YaakUpdater {
) -> Result<bool, tauri_plugin_updater::Error> { ) -> Result<bool, tauri_plugin_updater::Error> {
self.last_update_check = SystemTime::now(); self.last_update_check = SystemTime::now();
let update_mode = get_update_mode_str(mode);
let enabled = !is_dev(); let enabled = !is_dev();
info!( info!(
"Checking for updates mode={} enabled={}", "Checking for updates mode={} enabled={}",
update_mode, enabled mode, enabled
); );
if !enabled { if !enabled {
@@ -46,7 +65,7 @@ impl YaakUpdater {
match app_handle match app_handle
.updater_builder() .updater_builder()
.header("X-Update-Mode", update_mode)? .header("X-Update-Mode", mode.to_string())?
.build()? .build()?
.check() .check()
.await .await
@@ -102,17 +121,3 @@ impl YaakUpdater {
self.force_check(app_handle, mode).await self.force_check(app_handle, mode).await
} }
} }
pub fn update_mode_from_str(mode: &str) -> UpdateMode {
match mode {
"beta" => UpdateMode::Beta,
_ => UpdateMode::Stable,
}
}
fn get_update_mode_str(mode: UpdateMode) -> &'static str {
match mode {
UpdateMode::Stable => "stable",
UpdateMode::Beta => "beta",
}
}