Skip to content

Commit 20c99f8

Browse files
committed
fix: adjust time continuously
1 parent 785862a commit 20c99f8

File tree

1 file changed

+86
-9
lines changed

1 file changed

+86
-9
lines changed

src/main.rs

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,30 @@ impl App {
141141
self.cancel();
142142
}
143143

144+
fn adjust_time_by_seconds(&mut self, delta_seconds: i64) {
145+
let round_total_seconds = self.round_time.total_seconds() as i64;
146+
let new_round_seconds = (round_total_seconds + delta_seconds).max(0);
147+
self.round_time.minutes = (new_round_seconds / 60) as usize;
148+
self.round_time.seconds = (new_round_seconds % 60) as usize;
149+
self.round_time.tenths = 0;
150+
151+
let current_total_tenths = (self.timer.current_time.total_seconds() as i64) * 10
152+
+ self.timer.current_time.tenths as i64;
153+
let mut new_current_tenths = current_total_tenths + delta_seconds * 10;
154+
if new_current_tenths < 0 {
155+
new_current_tenths = 0;
156+
}
157+
let max_tenths = new_round_seconds * 10;
158+
if new_current_tenths > max_tenths {
159+
new_current_tenths = max_tenths;
160+
}
161+
162+
let new_current_seconds = new_current_tenths / 10;
163+
self.timer.current_time.minutes = (new_current_seconds / 60) as usize;
164+
self.timer.current_time.seconds = (new_current_seconds % 60) as usize;
165+
self.timer.current_time.tenths = (new_current_tenths % 10) as usize;
166+
}
167+
144168
fn max_seconds(&self) -> usize {
145169
if self.round_time.minutes > 0 {
146170
60
@@ -247,30 +271,25 @@ impl Component for App {
247271
}
248272
Msg::IncrementSecond => {
249273
info!("incrementing seconds");
250-
self.round_time.increment_seconds();
251-
self.timer.current_time = self.round_time;
274+
self.adjust_time_by_seconds(1);
252275
self.clear_blink_state();
253276
true
254277
}
255278
Msg::DecrementSecond => {
256279
info!("decrementing seconds");
257-
let max_seconds = self.max_seconds();
258-
self.round_time.decrement_seconds(max_seconds);
259-
self.timer.current_time = self.round_time;
280+
self.adjust_time_by_seconds(-1);
260281
self.clear_blink_state();
261282
true
262283
}
263284
Msg::IncrementQuarter => {
264285
info!("incrementing 15");
265-
self.round_time.increment_quarter();
266-
self.timer.current_time = self.round_time;
286+
self.adjust_time_by_seconds(15);
267287
self.clear_blink_state();
268288
true
269289
}
270290
Msg::DecrementQuarter => {
271291
info!("decrementing 15");
272-
self.round_time.decrement_quarter();
273-
self.timer.current_time = self.round_time;
292+
self.adjust_time_by_seconds(-15);
274293
self.clear_blink_state();
275294
true
276295
}
@@ -431,6 +450,64 @@ mod tests {
431450
assert_eq!(app.max_seconds(), 1);
432451
}
433452

453+
#[test]
454+
fn test_adjust_time_by_seconds_decrement_continuous() {
455+
let mut app = App {
456+
round_time: Time {
457+
seconds: 0,
458+
minutes: 1,
459+
tenths: 0,
460+
},
461+
timer: Timer {
462+
current_time: Time {
463+
seconds: 50,
464+
minutes: 0,
465+
tenths: 0,
466+
},
467+
rounds: 1,
468+
current_round: 1,
469+
running: true,
470+
},
471+
blink_state: BlinkState::None,
472+
countdown_timer: None,
473+
};
474+
app.adjust_time_by_seconds(-15);
475+
assert_eq!(app.round_time.minutes, 0);
476+
assert_eq!(app.round_time.seconds, 45);
477+
assert_eq!(app.timer.current_time.minutes, 0);
478+
assert_eq!(app.timer.current_time.seconds, 35);
479+
assert_eq!(app.timer.current_time.tenths, 0);
480+
}
481+
482+
#[test]
483+
fn test_adjust_time_by_seconds_increment_continuous() {
484+
let mut app = App {
485+
round_time: Time {
486+
seconds: 0,
487+
minutes: 1,
488+
tenths: 0,
489+
},
490+
timer: Timer {
491+
current_time: Time {
492+
seconds: 50,
493+
minutes: 0,
494+
tenths: 7,
495+
},
496+
rounds: 1,
497+
current_round: 1,
498+
running: true,
499+
},
500+
blink_state: BlinkState::None,
501+
countdown_timer: None,
502+
};
503+
app.adjust_time_by_seconds(15);
504+
assert_eq!(app.round_time.minutes, 1);
505+
assert_eq!(app.round_time.seconds, 15);
506+
assert_eq!(app.timer.current_time.minutes, 1);
507+
assert_eq!(app.timer.current_time.seconds, 5);
508+
assert_eq!(app.timer.current_time.tenths, 7);
509+
}
510+
434511
#[test]
435512
fn test_max_seconds_one_minute() {
436513
let app = App {

0 commit comments

Comments
 (0)