diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index 6e467cf..c1e92a7 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -91,7 +91,7 @@ impl Core { // timer initialize let mut timer = self.timer.lock(); timer.set_core(self.clone()); - log_if_err!(timer.refresh()); + log_if_err!(timer.restore()); } /// save the window instance diff --git a/src-tauri/src/core/profiles.rs b/src-tauri/src/core/profiles.rs index 16b75ec..6b4d6e1 100644 --- a/src-tauri/src/core/profiles.rs +++ b/src-tauri/src/core/profiles.rs @@ -121,7 +121,7 @@ impl Profiles { } } - bail!("failed to get the item by \"{}\"", uid); + bail!("failed to get the profile item \"uid:{uid}\""); } /// append new item @@ -178,7 +178,7 @@ impl Profiles { } self.items = Some(items); - bail!("failed to found the uid \"{uid}\"") + bail!("failed to find the profile item \"uid:{uid}\"") } /// be used to update the remote item @@ -286,7 +286,7 @@ impl Profiles { return Ok(config::read_yaml::(file_path.clone())); } } - bail!("failed to found the uid \"{current}\""); + bail!("failed to find current profile \"uid:{current}\""); } /// generate the data for activate clash config diff --git a/src-tauri/src/core/timer.rs b/src-tauri/src/core/timer.rs index 3e5ccb9..181c0d5 100644 --- a/src-tauri/src/core/timer.rs +++ b/src-tauri/src/core/timer.rs @@ -1,5 +1,6 @@ use super::Core; use crate::log_if_err; +use crate::utils::help::get_now; use anyhow::{bail, Context, Result}; use delay_timer::prelude::{DelayTimer, DelayTimerBuilder, TaskBuilder}; use std::collections::HashMap; @@ -63,6 +64,34 @@ impl Timer { Ok(()) } + /// restore timer + pub fn restore(&mut self) -> Result<()> { + self.refresh()?; + + let cur_timestamp = get_now(); // seconds + let profiles = self.core.as_ref().unwrap().profiles.lock(); + + profiles + .get_items() + .unwrap_or(&vec![]) + .iter() + .filter(|item| item.uid.is_some() && item.updated.is_some() && item.option.is_some()) + .filter(|item| { + // mins to seconds + let interval = item.option.as_ref().unwrap().update_interval.unwrap_or(0) as usize * 60; + let updated = item.updated.unwrap(); + return interval > 0 && cur_timestamp - updated >= interval; + }) + .for_each(|item| { + let uid = item.uid.as_ref().unwrap(); + if let Some((task_id, _)) = self.timer_map.get(uid) { + log_if_err!(self.delay_timer.advance_task(*task_id)); + } + }); + + Ok(()) + } + /// generate a uid -> update_interval map fn gen_map(&self) -> HashMap { let profiles = self.core.as_ref().unwrap().profiles.lock();