Skip to content

Commit dd0f498

Browse files
committed
fmt
1 parent c074309 commit dd0f498

File tree

12 files changed

+236
-127
lines changed

12 files changed

+236
-127
lines changed

src/app.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use dioxus::prelude::*;
21
use crate::models::{MyLifeApp, Yaml};
2+
use crate::ui::{BottomPanel, CentralPanel, TopPanel};
33
use crate::yaml_manager::get_yaml;
4-
use crate::ui::{TopPanel, BottomPanel, CentralPanel};
54
#[cfg(not(target_arch = "wasm32"))]
6-
use crate::yaml_manager::{get_yaml_manager, get_available_yamls};
5+
use crate::yaml_manager::{get_available_yamls, get_yaml_manager};
6+
use dioxus::prelude::*;
77

88
#[cfg(not(target_arch = "wasm32"))]
99
use dioxus_logger::tracing::error;
@@ -36,14 +36,12 @@ pub fn App() -> Element {
3636
let yamls = get_yaml_manager().get_available_yamls();
3737
app_state.write().yaml_files = yamls.unwrap_or_default();
3838
}
39-
39+
4040
#[cfg(not(target_arch = "wasm32"))]
4141
{
4242
let loaded_yamls = load_yamls();
4343
app_state.write().loaded_yamls = loaded_yamls;
4444
}
45-
46-
4745
});
4846

4947
use_context_provider(|| app_state);
@@ -83,4 +81,4 @@ fn load_yamls() -> Vec<(String, Yaml)> {
8381
.collect()
8482
})
8583
.unwrap_or_default()
86-
}
84+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![warn(clippy::all, rust_2018_idioms)]
22

33
mod app;
4-
mod yaml_manager;
54
mod models;
65
mod ui;
76
mod utils;
7+
mod yaml_manager;
88

99
pub use models::MyLifeApp;

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#![allow(non_snake_case)]
22

3-
use dioxus_logger::tracing::{Level, info};
3+
use dioxus_logger::tracing::{info, Level};
44

55
use dioxus::prelude::*;
66

77
mod app;
8-
mod utils;
8+
mod models;
99
mod ui;
10+
mod utils;
1011
mod yaml_manager;
11-
mod models;
1212

1313
fn main() {
1414
dioxus_logger::init(Level::INFO).expect("logger failed to init");

src/ui/bottom_panel.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use dioxus::prelude::*;
21
use crate::models::{LegendItem, MyLifeApp};
2+
use crate::ui::{EditLegendItem, Legend};
33
use chrono::{Datelike, Local};
4+
use dioxus::prelude::*;
45
use uuid::Uuid;
5-
use crate::ui::{Legend, EditLegendItem};
66

77
#[component]
88
pub fn BottomPanel() -> Element {
@@ -15,7 +15,12 @@ pub fn BottomPanel() -> Element {
1515
} else {
1616
// For EventView, use the start date of the selected life period
1717
if let Some(period_id) = app_state().selected_life_period {
18-
if let Some(period) = app_state().yaml.life_periods.iter().find(|p| p.id == period_id) {
18+
if let Some(period) = app_state()
19+
.yaml
20+
.life_periods
21+
.iter()
22+
.find(|p| p.id == period_id)
23+
{
1924
format!("{}-01", period.start) // Append -01 to the YYYY-MM format
2025
} else {
2126
// Fallback to current date if period not found
@@ -28,7 +33,7 @@ pub fn BottomPanel() -> Element {
2833
format!("{}-{:02}-{:02}", now.year(), now.month(), now.day())
2934
}
3035
};
31-
36+
3237
let new_item = if *current_view == "Lifetime" {
3338
LegendItem {
3439
id: Uuid::new_v4(),
@@ -46,7 +51,7 @@ pub fn BottomPanel() -> Element {
4651
is_event: true,
4752
}
4853
};
49-
54+
5055
app_state.write().item_state = Some(new_item);
5156
app_state.write().temp_start_date = default_start;
5257
};
@@ -69,7 +74,7 @@ pub fn BottomPanel() -> Element {
6974
"Add New Item"
7075
}
7176
}
72-
77+
7378
div {
7479
class: "legend-items",
7580
Legend {}
@@ -78,4 +83,4 @@ pub fn BottomPanel() -> Element {
7883
EditLegendItem {}
7984
}
8085
}
81-
}
86+
}

src/ui/central_panel.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::models::MyLifeApp;
2+
use crate::ui::{EventView, LifetimeView};
13
use dioxus::prelude::*;
2-
use crate::ui::{LifetimeView, EventView};
34
use uuid::Uuid;
4-
use crate::models::MyLifeApp;
55

66
#[component]
77
pub fn CentralPanel() -> Element {
@@ -45,4 +45,4 @@ pub fn CentralPanel() -> Element {
4545
}
4646
}
4747
}
48-
}
48+
}

src/ui/edit_legend_item.rs

Lines changed: 86 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use dioxus::prelude::*;
2-
use crate::models::{MyLifeApp, LifePeriod, LifePeriodEvent};
1+
use crate::models::{LifePeriod, LifePeriodEvent, MyLifeApp};
32
use crate::utils::date_utils::is_valid_date;
43
use crate::yaml_manager::save_yaml;
5-
use chrono::{NaiveDate, Local};
4+
use chrono::{Local, NaiveDate};
5+
use dioxus::prelude::*;
66
use dioxus_logger::tracing::{debug, warn};
77

88
fn is_valid_hex_color(color: &str) -> bool {
@@ -19,11 +19,27 @@ pub fn EditLegendItem() -> Element {
1919
let (min_date, max_date) = use_memo(move || {
2020
if let Some(item) = &app_state().item_state {
2121
if item.is_event {
22-
if let Some(period) = app_state().yaml.life_periods.iter().find(|p| p.id == app_state().selected_life_period.unwrap()) {
23-
let period_start = NaiveDate::parse_from_str(&format!("{}-01", period.start), "%Y-%m-%d").unwrap_or_default();
24-
let period_end = app_state().yaml.life_periods.iter()
22+
if let Some(period) = app_state()
23+
.yaml
24+
.life_periods
25+
.iter()
26+
.find(|p| p.id == app_state().selected_life_period.unwrap())
27+
{
28+
let period_start =
29+
NaiveDate::parse_from_str(&format!("{}-01", period.start), "%Y-%m-%d")
30+
.unwrap_or_default();
31+
let period_end = app_state()
32+
.yaml
33+
.life_periods
34+
.iter()
2535
.find(|p| p.start > period.start)
26-
.map(|next_period| NaiveDate::parse_from_str(&format!("{}-01", next_period.start), "%Y-%m-%d").unwrap_or_default())
36+
.map(|next_period| {
37+
NaiveDate::parse_from_str(
38+
&format!("{}-01", next_period.start),
39+
"%Y-%m-%d",
40+
)
41+
.unwrap_or_default()
42+
})
2743
.unwrap_or_else(|| chrono::Local::now().date_naive());
2844
return (Some(period_start), Some(period_end));
2945
}
@@ -40,18 +56,37 @@ pub fn EditLegendItem() -> Element {
4056
} else {
4157
let default_date = if item.is_event {
4258
debug!("Getting default date for new event");
43-
if let Some(period) = app_state().yaml.life_periods.iter().find(|p| p.id == app_state().selected_life_period.unwrap()) {
44-
let period_start = NaiveDate::parse_from_str(&format!("{}-01", period.start), "%Y-%m-%d").unwrap_or_default();
45-
let period_end = app_state().yaml.life_periods.iter()
59+
if let Some(period) = app_state()
60+
.yaml
61+
.life_periods
62+
.iter()
63+
.find(|p| p.id == app_state().selected_life_period.unwrap())
64+
{
65+
let period_start =
66+
NaiveDate::parse_from_str(&format!("{}-01", period.start), "%Y-%m-%d")
67+
.unwrap_or_default();
68+
let period_end = app_state()
69+
.yaml
70+
.life_periods
71+
.iter()
4672
.find(|p| p.start > period.start)
47-
.map(|next_period| NaiveDate::parse_from_str(&format!("{}-01", next_period.start), "%Y-%m-%d").unwrap_or_default())
73+
.map(|next_period| {
74+
NaiveDate::parse_from_str(
75+
&format!("{}-01", next_period.start),
76+
"%Y-%m-%d",
77+
)
78+
.unwrap_or_default()
79+
})
4880
.unwrap_or_else(|| chrono::Local::now().date_naive());
49-
81+
5082
// Choose a date in the middle of the period
5183
let days_in_period = (period_end - period_start).num_days();
5284
let middle_date = period_start + chrono::Duration::days(days_in_period / 2);
53-
54-
debug!("Found period start date: {}, end date: {}, chosen date: {}", period_start, period_end, middle_date);
85+
86+
debug!(
87+
"Found period start date: {}, end date: {}, chosen date: {}",
88+
period_start, period_end, middle_date
89+
);
5590
Some(middle_date.format("%Y-%m-%d").to_string())
5691
} else {
5792
None
@@ -65,25 +100,30 @@ pub fn EditLegendItem() -> Element {
65100
String::new()
66101
})
67102
};
68-
103+
69104
debug!("Initial date set to: {}", initial_date);
70105
current_date.set(initial_date.clone());
71-
106+
72107
// Check if the initial date is valid
73108
let is_event = item.is_event;
74109
if is_valid_date(&initial_date, !is_event) {
75110
if is_event {
76111
if let (Some(min), Some(max)) = (min_date, max_date) {
77-
let input_date = NaiveDate::parse_from_str(&initial_date, "%Y-%m-%d").unwrap();
112+
let input_date =
113+
NaiveDate::parse_from_str(&initial_date, "%Y-%m-%d").unwrap();
78114
if input_date < min || input_date >= max {
79-
date_error.set(format!("Date must be between {} and {}", min.format("%Y-%m-%d"), max.format("%Y-%m-%d")));
115+
date_error.set(format!(
116+
"Date must be between {} and {}",
117+
min.format("%Y-%m-%d"),
118+
max.format("%Y-%m-%d")
119+
));
80120
}
81121
}
82122
}
83123
} else {
84124
date_error.set("Invalid date format".to_string());
85125
}
86-
126+
87127
color_input.set(item.color.clone());
88128
} else {
89129
debug!("No item state");
@@ -97,7 +137,11 @@ pub fn EditLegendItem() -> Element {
97137
debug!("Updating item: {:?}", item);
98138
let mut new_yaml = app_state().yaml.clone();
99139
if item.is_event {
100-
if let Some(period) = new_yaml.life_periods.iter_mut().find(|p| p.id == app_state().selected_life_period.unwrap()) {
140+
if let Some(period) = new_yaml
141+
.life_periods
142+
.iter_mut()
143+
.find(|p| p.id == app_state().selected_life_period.unwrap())
144+
{
101145
if let Some(event) = period.events.iter_mut().find(|e| e.id == item.id) {
102146
event.name = item.name.clone();
103147
event.color = item.color.clone();
@@ -113,7 +157,8 @@ pub fn EditLegendItem() -> Element {
113157
period.events.sort_by(|a, b| a.start.cmp(&b.start));
114158
}
115159
} else {
116-
if let Some(period) = new_yaml.life_periods.iter_mut().find(|p| p.id == item.id) {
160+
if let Some(period) = new_yaml.life_periods.iter_mut().find(|p| p.id == item.id)
161+
{
117162
period.name = item.name.clone();
118163
period.start = item.start.clone();
119164
period.color = item.color.clone();
@@ -160,8 +205,11 @@ pub fn EditLegendItem() -> Element {
160205
let new_date = evt.value().to_string();
161206
debug!("Updating date: {}", new_date);
162207
current_date.set(new_date.clone());
163-
164-
let is_event = app_state().item_state.as_ref().map_or(false, |item| item.is_event);
208+
209+
let is_event = app_state()
210+
.item_state
211+
.as_ref()
212+
.map_or(false, |item| item.is_event);
165213
if is_valid_date(&new_date, !is_event) {
166214
if is_event {
167215
if let (Some(min), Some(max)) = (min_date, max_date) {
@@ -174,7 +222,11 @@ pub fn EditLegendItem() -> Element {
174222
date_error.set(String::new());
175223
debug!("Valid event date set");
176224
} else {
177-
date_error.set(format!("Date must be between {} and {}", min.format("%Y-%m-%d"), max.format("%Y-%m-%d")));
225+
date_error.set(format!(
226+
"Date must be between {} and {}",
227+
min.format("%Y-%m-%d"),
228+
max.format("%Y-%m-%d")
229+
));
178230
warn!("Invalid event date: {}", new_date);
179231
}
180232
}
@@ -196,15 +248,22 @@ pub fn EditLegendItem() -> Element {
196248
if is_valid_hex_color(&color_input()) {
197249
color_input().to_string()
198250
} else {
199-
app_state().item_state.as_ref().map_or("#000000".to_string(), |item| item.color.clone())
251+
app_state()
252+
.item_state
253+
.as_ref()
254+
.map_or("#000000".to_string(), |item| item.color.clone())
200255
}
201256
};
202257

203258
let delete_item = move |_| {
204259
if let Some(item) = app_state().item_state {
205260
let mut new_yaml = app_state().yaml.clone();
206261
if item.is_event {
207-
if let Some(period) = new_yaml.life_periods.iter_mut().find(|p| p.id == app_state().selected_life_period.unwrap()) {
262+
if let Some(period) = new_yaml
263+
.life_periods
264+
.iter_mut()
265+
.find(|p| p.id == app_state().selected_life_period.unwrap())
266+
{
208267
period.events.retain(|e| e.id != item.id);
209268
}
210269
} else {
@@ -277,4 +336,4 @@ pub fn EditLegendItem() -> Element {
277336
}
278337
})}
279338
}
280-
}
339+
}

0 commit comments

Comments
 (0)