feat(event) persist state (#129)

* feat(event) persist state

* `Event::default()`
This commit is contained in:
Jens Krause 2025-10-14 10:18:53 +02:00 committed by GitHub
parent d2f41e04e2
commit e11dcaa913
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 27 additions and 14 deletions

1
Cargo.lock generated
View File

@ -642,6 +642,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a41953f86f8a05768a6cda24def994fd2f424b04ec5c719cf89989779f199071"
dependencies = [
"powerfmt",
"serde_core",
]
[[package]]

View File

@ -35,7 +35,7 @@ tracing = "0.1.41"
tracing-subscriber = { version = "0.3.20", features = ["env-filter"] }
directories = "5.0.1"
clap = { version = "4.5.48", features = ["derive"] }
time = { version = "0.3.44", features = ["formatting", "local-offset", "parsing", "macros"] }
time = { version = "0.3.44", features = ["formatting", "local-offset", "parsing", "macros", "serde"] }
notify-rust = "4.11.7"
rodio = { version = "0.20.1", features = [
"symphonia-mp3",

View File

@ -2,7 +2,7 @@ use crate::{
args::Args,
common::{AppEditMode, AppTime, AppTimeFormat, ClockTypeId, Content, Style, Toggle},
constants::TICK_VALUE_MS,
event::{Event, get_default_event},
event::Event,
events::{self, TuiEventHandler},
storage::AppStorage,
terminal::Terminal,
@ -135,7 +135,7 @@ impl From<FromAppArgs> for App {
None => stg.elapsed_value_countdown,
},
current_value_timer: stg.current_value_timer,
event: args.event.unwrap_or_else(get_default_event),
event: args.event.unwrap_or(stg.event),
app_tx,
#[cfg(feature = "sound")]
sound_path: args.sound,
@ -470,6 +470,7 @@ impl App {
),
elapsed_value_countdown: Duration::from(*self.countdown.get_elapsed_value()),
current_value_timer: Duration::from(*self.timer.get_clock().get_current_value()),
event: self.event.get_event(),
footer_app_time: self.footer.app_time_format().is_some().into(),
}
}

View File

@ -1,21 +1,20 @@
use time::macros::format_description;
use serde::{Deserialize, Serialize};
use time::macros::{datetime, format_description};
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Event {
pub date_time: time::PrimitiveDateTime,
pub title: Option<String>,
}
pub fn get_default_event() -> Event {
Event {
date_time: time::PrimitiveDateTime::parse(
impl Default for Event {
fn default() -> Self {
Self {
// Mario Bros. "...entered mass production in Japan on June 21" 1983
// https://en.wikipedia.org/wiki/Mario_Bros.#Release
"1983-06-21 00:00",
format_description!("[year]-[month]-[day] [hour]:[minute]"),
)
.unwrap(),
title: Some("Release date of Mario Bros. in Japan".into()),
date_time: datetime!(1983-06-21 00:00),
title: Some("Release date of Mario Bros. in Japan".into()),
}
}
}

View File

@ -1,5 +1,6 @@
use crate::{
common::{AppTimeFormat, Content, Style, Toggle},
event::Event,
widgets::pomodoro::Mode as PomodoroMode,
};
use color_eyre::eyre::Result;
@ -44,6 +45,8 @@ pub struct AppStorage {
pub elapsed_value_countdown: Duration,
// timer
pub current_value_timer: Duration,
// event
pub event: Event,
// footer
pub footer_app_time: Toggle,
}
@ -75,6 +78,8 @@ impl Default for AppStorage {
elapsed_value_countdown: Duration::ZERO,
// timer
current_value_timer: Duration::ZERO,
// event
event: Event::default(),
// footer
footer_app_time: Toggle::Off,
}

View File

@ -203,7 +203,7 @@ fn test_format_by_duration_boundaries() {
Format::YyyDddHhMmSs
);
// YyyDdHhMmSs
// YyyyDdHhMmSs
assert_eq!(
format_by_duration::<DurationEx>(&(1000 * ONE_YEAR + 10 * ONE_DAY).into()),
Format::YyyyDdHhMmSs

View File

@ -73,6 +73,13 @@ impl EventState {
self.with_decis = with_decis;
}
pub fn get_event(&self) -> Event {
Event {
title: self.title.clone(),
date_time: time::PrimitiveDateTime::new(self.event_time.date(), self.event_time.time()),
}
}
pub fn get_percentage_done(&self) -> u16 {
get_percentage(self.start_time, self.event_time, self.app_time)
}