Skip to content

Commit

Permalink
save a cached config when closing without saving
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Nov 7, 2024
1 parent 6cc1e1b commit f0f6dc6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
28 changes: 22 additions & 6 deletions data/src/dir_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct ConfigNames {
pub struct DirManager {
pub config_dir_path: PathBuf,
pub state_dir_path: PathBuf,
pub cache_dir_path: PathBuf,
pub config_names: ConfigNames,
settings: Settings,
state: SettingsState,
Expand All @@ -48,6 +49,7 @@ type Result<T> = std::result::Result<T, ConfigError>;
static SETTINGS_FILENAME: &str = "settings.toml";
static STATE_FILENAME: &str = "state.toml";
static HARDWARE_FILENAME: &str = "hardware.toml";
static CACHED_CONFIG_FILENAME: &str = "cached_config.toml";

impl DirManager {
pub fn new(
Expand Down Expand Up @@ -119,12 +121,6 @@ impl DirManager {
project_dirs.data_local_dir().to_path_buf()
};

if !state_dir_path.exists() {
if let Err(e) = fs::create_dir_all(&state_dir_path) {
error!("Can't create config directories: {e}.")
}
}

let state = {
let state_file_path = state_dir_path.join(STATE_FILENAME);

Expand All @@ -147,6 +143,7 @@ impl DirManager {
settings,
state,
state_dir_path,
cache_dir_path: project_dirs.cache_dir().to_path_buf(),
}
}

Expand Down Expand Up @@ -204,6 +201,20 @@ impl DirManager {
}
}

pub fn get_config_cached(&self) -> Option<Config> {
deserialize::<Config>(&self.cache_dir_path.join(CACHED_CONFIG_FILENAME)).ok()
}

pub fn save_config_cached(&self, config: &Config) -> Result<()> {
serialize(&self.cache_dir_path.join(CACHED_CONFIG_FILENAME), config)?;

Ok(())
}

pub fn remove_config_cached(&self) {
let _ = fs::remove_file(self.cache_dir_path.join(CACHED_CONFIG_FILENAME));
}

pub fn serialize_hardware(&self, hardware: &Hardware) {
let hardware_file_path = self.hardware_file_path();

Expand Down Expand Up @@ -446,6 +457,11 @@ mod helper {
}

pub fn serialize<T: Serialize>(path: &Path, rust_struct: &T) -> super::Result<()> {
let parent = path.parent().unwrap();
if !parent.exists() {
fs::create_dir_all(parent)?
}

let str = toml::to_string_pretty(rust_struct)?;
fs::write(path, str)?;
Ok(())
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ fn try_run() -> Result<()> {
return Ok(());
}

let app_graph = match dir_manager.get_config() {
let app_graph = match dir_manager
.get_config_cached()
.or_else(|| dir_manager.get_config())
{
Some(config) => AppGraph::from_config(config, hardware),
None => AppGraph::default(hardware),
};
Expand Down
23 changes: 19 additions & 4 deletions ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,11 +593,26 @@ impl<H: HardwareBridge + 'static> cosmic::Application for Ui<H> {
if let Err(e) = self.app_state.bridge.shutdown() {
error!("shutdown hardware: {}", e);
}
None
}

fn on_close_requested(&self, _id: iced::window::Id) -> Option<Self::Message> {
// todo: pop up. Need to use settings to not close auto
let runtime_config = Config::from_app_graph(&self.app_state.app_graph);

if match self.app_state.dir_manager.get_config() {
Some(saved_config) => saved_config != runtime_config,
None => true,
} {
if let Err(err) = self
.app_state
.dir_manager
.save_config_cached(&runtime_config)
{
error!("{err}")
} else {
info!("cached config saved successfully");
}
} else {
self.app_state.dir_manager.remove_config_cached();
}

None
}

Expand Down

0 comments on commit f0f6dc6

Please sign in to comment.