Skip to content

Commit b5c8b18

Browse files
committed
Refactor web-app
Ref. #75
1 parent 487d0b1 commit b5c8b18

File tree

3 files changed

+82
-76
lines changed

3 files changed

+82
-76
lines changed

crates/web-app/src/lib.rs

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,14 @@
11
#![warn(clippy::pedantic)]
22
#![allow(clippy::too_many_lines)]
33

4-
use chrono::{DateTime, Utc};
5-
64
pub mod chart;
75
pub mod log;
86
pub mod service_worker;
97

10-
#[allow(async_fn_in_trait)]
11-
pub trait SettingsRepository {
12-
async fn read_settings(&self) -> Result<Settings, String>;
13-
async fn write_settings(&self, settings: Settings) -> Result<(), String>;
14-
}
15-
16-
#[allow(async_fn_in_trait)]
17-
pub trait OngoingTrainingSessionRepository {
18-
async fn read_ongoing_training_session(&self)
19-
-> Result<Option<OngoingTrainingSession>, String>;
20-
async fn write_ongoing_training_session(
21-
&self,
22-
ongoing_training_session: Option<OngoingTrainingSession>,
23-
) -> Result<(), String>;
24-
}
25-
26-
#[derive(serde::Serialize, serde::Deserialize, Clone)]
27-
#[allow(clippy::struct_excessive_bools)]
28-
pub struct Settings {
29-
pub beep_volume: u8,
30-
pub theme: Theme,
31-
pub automatic_metronome: bool,
32-
pub notifications: bool,
33-
pub show_rpe: bool,
34-
pub show_tut: bool,
35-
}
36-
37-
impl Default for Settings {
38-
fn default() -> Self {
39-
Self {
40-
beep_volume: 80,
41-
theme: Theme::Light,
42-
automatic_metronome: false,
43-
notifications: false,
44-
show_rpe: true,
45-
show_tut: true,
46-
}
47-
}
48-
}
49-
50-
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq)]
51-
pub enum Theme {
52-
System,
53-
Light,
54-
Dark,
55-
}
56-
57-
#[derive(serde::Serialize, serde::Deserialize, Clone)]
58-
pub struct OngoingTrainingSession {
59-
pub training_session_id: u128,
60-
pub start_time: DateTime<Utc>,
61-
pub element_idx: usize,
62-
pub element_start_time: DateTime<Utc>,
63-
pub timer_state: TimerState,
64-
}
65-
66-
impl OngoingTrainingSession {
67-
#[must_use]
68-
pub fn new(training_session_id: u128) -> Self {
69-
Self {
70-
training_session_id,
71-
start_time: Utc::now(),
72-
element_idx: 0,
73-
element_start_time: Utc::now(),
74-
timer_state: TimerState::Unset,
75-
}
76-
}
77-
}
8+
pub use ongoing_training_session::{
9+
OngoingTrainingSession, OngoingTrainingSessionRepository, TimerState,
10+
};
11+
pub use settings::{Settings, SettingsRepository, Theme};
7812

79-
#[derive(serde::Serialize, serde::Deserialize, Clone, Copy)]
80-
pub enum TimerState {
81-
Unset,
82-
Active { target_time: DateTime<Utc> },
83-
Paused { time: i64 },
84-
}
13+
mod ongoing_training_session;
14+
mod settings;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use chrono::{DateTime, Utc};
2+
3+
#[allow(async_fn_in_trait)]
4+
pub trait OngoingTrainingSessionRepository {
5+
async fn read_ongoing_training_session(&self)
6+
-> Result<Option<OngoingTrainingSession>, String>;
7+
async fn write_ongoing_training_session(
8+
&self,
9+
ongoing_training_session: Option<OngoingTrainingSession>,
10+
) -> Result<(), String>;
11+
}
12+
13+
#[derive(serde::Serialize, serde::Deserialize, Clone)]
14+
pub struct OngoingTrainingSession {
15+
pub training_session_id: u128,
16+
pub start_time: DateTime<Utc>,
17+
pub element_idx: usize,
18+
pub element_start_time: DateTime<Utc>,
19+
pub timer_state: TimerState,
20+
}
21+
22+
impl OngoingTrainingSession {
23+
#[must_use]
24+
pub fn new(training_session_id: u128) -> Self {
25+
Self {
26+
training_session_id,
27+
start_time: Utc::now(),
28+
element_idx: 0,
29+
element_start_time: Utc::now(),
30+
timer_state: TimerState::Unset,
31+
}
32+
}
33+
}
34+
35+
#[derive(serde::Serialize, serde::Deserialize, Clone, Copy)]
36+
pub enum TimerState {
37+
Unset,
38+
Active { target_time: DateTime<Utc> },
39+
Paused { time: i64 },
40+
}

crates/web-app/src/settings.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#[allow(async_fn_in_trait)]
2+
pub trait SettingsRepository {
3+
async fn read_settings(&self) -> Result<Settings, String>;
4+
async fn write_settings(&self, settings: Settings) -> Result<(), String>;
5+
}
6+
7+
#[derive(serde::Serialize, serde::Deserialize, Clone)]
8+
#[allow(clippy::struct_excessive_bools)]
9+
pub struct Settings {
10+
pub beep_volume: u8,
11+
pub theme: Theme,
12+
pub automatic_metronome: bool,
13+
pub notifications: bool,
14+
pub show_rpe: bool,
15+
pub show_tut: bool,
16+
}
17+
18+
impl Default for Settings {
19+
fn default() -> Self {
20+
Self {
21+
beep_volume: 80,
22+
theme: Theme::Light,
23+
automatic_metronome: false,
24+
notifications: false,
25+
show_rpe: true,
26+
show_tut: true,
27+
}
28+
}
29+
}
30+
31+
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq)]
32+
pub enum Theme {
33+
System,
34+
Light,
35+
Dark,
36+
}

0 commit comments

Comments
 (0)