Custom updater code

This commit is contained in:
Gregory Schier
2023-11-12 21:16:33 -08:00
parent c554b73d48
commit aea4e961aa
3 changed files with 128 additions and 42 deletions

41
src-tauri/src/updates.rs Normal file
View File

@@ -0,0 +1,41 @@
use std::time::SystemTime;
use tauri::{AppHandle, updater, Window, Wry};
use tauri::api::dialog;
// Check for updates every 3 hours
const MAX_UPDATE_CHECK_SECONDS: u64 = 3600 * 3;
// Create updater struct
pub struct YaakUpdater {
last_update_check: SystemTime,
}
impl YaakUpdater {
pub fn new() -> Self {
Self {
last_update_check: SystemTime::UNIX_EPOCH,
}
}
pub async fn check(&mut self, app_handle: &AppHandle<Wry>) -> Result<(), updater::Error> {
if self.last_update_check.elapsed().unwrap().as_secs() < MAX_UPDATE_CHECK_SECONDS {
return Ok(());
}
self.last_update_check = SystemTime::now();
match app_handle.updater().check().await {
Ok(update) => {
if dialog::blocking::ask(
None::<&Window>,
"Update available",
"An update is available. Would you like to download and install it now?",
) {
_ = update.download_and_install().await;
}
Ok(())
}
Err(updater::Error::UpToDate) => Ok(()),
Err(e) => Err(e),
}
}
}