1
- use dioxus:: prelude:: * ;
2
- use crate :: models:: { MyLifeApp , LifePeriod , LifePeriodEvent } ;
1
+ use crate :: models:: { LifePeriod , LifePeriodEvent , MyLifeApp } ;
3
2
use crate :: utils:: date_utils:: is_valid_date;
4
3
use crate :: yaml_manager:: save_yaml;
5
- use chrono:: { NaiveDate , Local } ;
4
+ use chrono:: { Local , NaiveDate } ;
5
+ use dioxus:: prelude:: * ;
6
6
use dioxus_logger:: tracing:: { debug, warn} ;
7
7
8
8
fn is_valid_hex_color ( color : & str ) -> bool {
@@ -19,11 +19,27 @@ pub fn EditLegendItem() -> Element {
19
19
let ( min_date, max_date) = use_memo ( move || {
20
20
if let Some ( item) = & app_state ( ) . item_state {
21
21
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 ( )
25
35
. 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
+ } )
27
43
. unwrap_or_else ( || chrono:: Local :: now ( ) . date_naive ( ) ) ;
28
44
return ( Some ( period_start) , Some ( period_end) ) ;
29
45
}
@@ -40,18 +56,37 @@ pub fn EditLegendItem() -> Element {
40
56
} else {
41
57
let default_date = if item. is_event {
42
58
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 ( )
46
72
. 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
+ } )
48
80
. unwrap_or_else ( || chrono:: Local :: now ( ) . date_naive ( ) ) ;
49
-
81
+
50
82
// Choose a date in the middle of the period
51
83
let days_in_period = ( period_end - period_start) . num_days ( ) ;
52
84
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
+ ) ;
55
90
Some ( middle_date. format ( "%Y-%m-%d" ) . to_string ( ) )
56
91
} else {
57
92
None
@@ -65,25 +100,30 @@ pub fn EditLegendItem() -> Element {
65
100
String :: new ( )
66
101
} )
67
102
} ;
68
-
103
+
69
104
debug ! ( "Initial date set to: {}" , initial_date) ;
70
105
current_date. set ( initial_date. clone ( ) ) ;
71
-
106
+
72
107
// Check if the initial date is valid
73
108
let is_event = item. is_event ;
74
109
if is_valid_date ( & initial_date, !is_event) {
75
110
if is_event {
76
111
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 ( ) ;
78
114
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
+ ) ) ;
80
120
}
81
121
}
82
122
}
83
123
} else {
84
124
date_error. set ( "Invalid date format" . to_string ( ) ) ;
85
125
}
86
-
126
+
87
127
color_input. set ( item. color . clone ( ) ) ;
88
128
} else {
89
129
debug ! ( "No item state" ) ;
@@ -97,7 +137,11 @@ pub fn EditLegendItem() -> Element {
97
137
debug ! ( "Updating item: {:?}" , item) ;
98
138
let mut new_yaml = app_state ( ) . yaml . clone ( ) ;
99
139
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
+ {
101
145
if let Some ( event) = period. events . iter_mut ( ) . find ( |e| e. id == item. id ) {
102
146
event. name = item. name . clone ( ) ;
103
147
event. color = item. color . clone ( ) ;
@@ -113,7 +157,8 @@ pub fn EditLegendItem() -> Element {
113
157
period. events . sort_by ( |a, b| a. start . cmp ( & b. start ) ) ;
114
158
}
115
159
} 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
+ {
117
162
period. name = item. name . clone ( ) ;
118
163
period. start = item. start . clone ( ) ;
119
164
period. color = item. color . clone ( ) ;
@@ -160,8 +205,11 @@ pub fn EditLegendItem() -> Element {
160
205
let new_date = evt. value ( ) . to_string ( ) ;
161
206
debug ! ( "Updating date: {}" , new_date) ;
162
207
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 ) ;
165
213
if is_valid_date ( & new_date, !is_event) {
166
214
if is_event {
167
215
if let ( Some ( min) , Some ( max) ) = ( min_date, max_date) {
@@ -174,7 +222,11 @@ pub fn EditLegendItem() -> Element {
174
222
date_error. set ( String :: new ( ) ) ;
175
223
debug ! ( "Valid event date set" ) ;
176
224
} 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
+ ) ) ;
178
230
warn ! ( "Invalid event date: {}" , new_date) ;
179
231
}
180
232
}
@@ -196,15 +248,22 @@ pub fn EditLegendItem() -> Element {
196
248
if is_valid_hex_color ( & color_input ( ) ) {
197
249
color_input ( ) . to_string ( )
198
250
} 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 ( ) )
200
255
}
201
256
} ;
202
257
203
258
let delete_item = move |_| {
204
259
if let Some ( item) = app_state ( ) . item_state {
205
260
let mut new_yaml = app_state ( ) . yaml . clone ( ) ;
206
261
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
+ {
208
267
period. events . retain ( |e| e. id != item. id ) ;
209
268
}
210
269
} else {
@@ -277,4 +336,4 @@ pub fn EditLegendItem() -> Element {
277
336
}
278
337
} ) }
279
338
}
280
- }
339
+ }
0 commit comments