1- use tauri :: { AppHandle , Manager } ;
1+ use std :: time :: Duration ;
22
3- use crate :: {
4- state:: { Pomodoro , TimerMode } ,
5- PomodoroState , SettingsState , TimerState , TimerStatePayload , MAIN_WINDOW_LABEL ,
6- } ;
3+ use tauri:: { AppHandle , Manager } ;
74
8- fn update_pomodoro_state ( state : & Pomodoro ) -> Pomodoro {
9- match state. mode {
10- TimerMode :: Relax => Pomodoro {
11- mode : TimerMode :: Work ,
12- cycles : state. cycles + 1 ,
13- } ,
14- TimerMode :: Work => Pomodoro {
15- mode : TimerMode :: Relax ,
16- cycles : state. cycles ,
17- } ,
18- }
19- }
5+ use crate :: { PomodoroState , TimerStatePayload , MAIN_WINDOW_LABEL } ;
206
21- pub fn setup_timer_listener ( app_handle : & AppHandle ) -> impl Fn ( ) {
22- let window = app_handle
23- . get_window ( MAIN_WINDOW_LABEL )
24- . expect ( "Unable to retrieve main window" ) ;
25- let update_receiver = app_handle. state :: < TimerState > ( ) . update_receiver . clone ( ) ;
7+ pub fn create_timer_listener ( app_handle : & AppHandle ) -> impl Fn ( Duration ) {
268 let app_handle = app_handle. clone ( ) ;
9+ let main_window = app_handle. get_window ( MAIN_WINDOW_LABEL ) . unwrap ( ) ;
2710
28- move || loop {
29- let remaining = update_receiver
30- . recv ( )
31- . expect ( "Failed to receive timer tick" ) ;
11+ move |remaining : Duration | {
3212 let remaining_secs = remaining. as_secs ( ) ;
3313
3414 #[ cfg( target_os = "macos" ) ]
@@ -47,25 +27,17 @@ pub fn setup_timer_listener(app_handle: &AppHandle) -> impl Fn() {
4727 app_handle. emit_all ( "timer-tick" , & remaining_secs) . unwrap ( ) ;
4828
4929 if remaining. is_zero ( ) {
50- let settings = app_handle. state :: < SettingsState > ( ) ;
5130 let pomodoro_state = app_handle. state :: < PomodoroState > ( ) ;
52- let mut pomodoro_state = pomodoro_state. lock ( ) . unwrap ( ) ;
53-
54- * pomodoro_state = update_pomodoro_state ( & pomodoro_state) ;
55- let new_duration = pomodoro_state. duration ( & settings. read ( ) . unwrap ( ) ) ;
56- app_handle
57- . state :: < TimerState > ( )
58- . reset ( new_duration)
59- . unwrap ( ) ;
31+ let pomodoro_state = pomodoro_state. lock ( ) . unwrap ( ) ;
6032
61- window
33+ main_window
6234 . emit :: < TimerStatePayload > (
6335 "timer-state" ,
6436 TimerStatePayload {
6537 mode : pomodoro_state. mode ,
6638 cycle : pomodoro_state. cycles ,
6739 is_ended : true ,
68- duration_secs : new_duration . as_secs ( ) as u32 ,
40+ duration_secs : remaining . as_secs ( ) as u32 ,
6941 } ,
7042 )
7143 . unwrap ( ) ;
0 commit comments