Persist app state (#21)

This commit is contained in:
Jens K.
2024-12-22 18:56:55 +01:00
committed by GitHub
parent 2cf411e2ae
commit c9b444e91a
12 changed files with 336 additions and 112 deletions

82
src/storage.rs Normal file
View File

@@ -0,0 +1,82 @@
use crate::{
app::Content,
constants::APP_NAME,
widgets::{clock::Style, pomodoro::Mode as PomodoroMode},
};
use color_eyre::eyre::Result;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use std::time::Duration;
#[derive(Debug, Serialize, Deserialize)]
pub struct AppStorage {
pub content: Content,
pub show_menu: bool,
pub style: Style,
pub with_decis: bool,
pub pomodoro_mode: PomodoroMode,
// pomodoro -> work
pub inital_value_work: Duration,
pub current_value_work: Duration,
// pomodoro -> pause
pub inital_value_pause: Duration,
pub current_value_pause: Duration,
// countdown
pub inital_value_countdown: Duration,
pub current_value_countdown: Duration,
// timer
pub current_value_timer: Duration,
}
impl Default for AppStorage {
fn default() -> Self {
const DEFAULT_WORK: Duration = Duration::from_secs(60 * 25); /* 25min */
const DEFAULT_PAUSE: Duration = Duration::from_secs(60 * 5); /* 5min */
const DEFAULT_COUNTDOWN: Duration = Duration::from_secs(60 * 10); /* 10min */
AppStorage {
content: Content::default(),
show_menu: false,
style: Style::default(),
with_decis: false,
pomodoro_mode: PomodoroMode::Work,
// pomodoro -> work
inital_value_work: DEFAULT_WORK,
current_value_work: DEFAULT_WORK,
// pomodoro -> pause
inital_value_pause: DEFAULT_PAUSE,
current_value_pause: DEFAULT_PAUSE,
// countdown
inital_value_countdown: DEFAULT_COUNTDOWN,
current_value_countdown: DEFAULT_COUNTDOWN,
// timer
current_value_timer: Duration::ZERO,
}
}
}
pub struct Storage {
data_dir: PathBuf,
}
impl Storage {
pub fn new(data_dir: PathBuf) -> Self {
Self { data_dir }
}
fn get_storage_path(&self) -> PathBuf {
self.data_dir.join(format!("{}.data", APP_NAME))
}
pub fn save(&self, data: AppStorage) -> Result<()> {
let file = fs::File::create(self.get_storage_path())?;
serde_json::to_writer(file, &data)?;
Ok(())
}
pub fn load(&self) -> Result<AppStorage> {
let file = fs::File::open(self.get_storage_path())?;
let data = serde_json::from_reader(file)?;
Ok(data)
}
}