@@ -88,6 +88,10 @@ export class PomodoroTimer {
8888 skip : "CommandOrControl+Alt+S" // Save Session
8989 } ;
9090
91+ // Midnight monitoring for daily reset
92+ this . midnightMonitorInterval = null ;
93+ this . currentDateString = new Date ( ) . toDateString ( ) ;
94+
9195 this . init ( ) ;
9296 }
9397
@@ -182,6 +186,9 @@ export class PomodoroTimer {
182186
183187 // Initialize tray menu state
184188 this . updateTrayMenu ( ) ;
189+
190+ // Start midnight monitoring for daily reset
191+ this . startMidnightMonitoring ( ) ;
185192 }
186193
187194 setupEventListeners ( ) {
@@ -836,6 +843,82 @@ export class PomodoroTimer {
836843 ) ;
837844 }
838845
846+ // Midnight monitoring methods for daily reset
847+ startMidnightMonitoring ( ) {
848+ // Clear any existing monitoring
849+ this . stopMidnightMonitoring ( ) ;
850+
851+ // Check for date change every minute
852+ this . midnightMonitorInterval = setInterval ( ( ) => {
853+ this . checkForMidnightReset ( ) ;
854+ } , 60000 ) ; // Check every minute
855+
856+ console . log ( '🌙 Midnight monitoring started' ) ;
857+ }
858+
859+ stopMidnightMonitoring ( ) {
860+ if ( this . midnightMonitorInterval ) {
861+ clearInterval ( this . midnightMonitorInterval ) ;
862+ this . midnightMonitorInterval = null ;
863+ console . log ( '🌙 Midnight monitoring stopped' ) ;
864+ }
865+ }
866+
867+ checkForMidnightReset ( ) {
868+ const newDateString = new Date ( ) . toDateString ( ) ;
869+
870+ if ( newDateString !== this . currentDateString ) {
871+ console . log ( '🌙 Date change detected:' , this . currentDateString , '→' , newDateString ) ;
872+ this . currentDateString = newDateString ;
873+ this . performMidnightReset ( ) ;
874+ }
875+ }
876+
877+ async performMidnightReset ( ) {
878+ console . log ( '🌅 Performing midnight reset...' ) ;
879+
880+ // Store previous session count for notification
881+ const previousPomodoros = this . completedPomodoros ;
882+
883+ // Use enhanced loadSessionData with force reset to ensure clean state
884+ await this . loadSessionData ( true ) ;
885+
886+ // Update display to show the reset state
887+ this . updateDisplay ( ) ;
888+
889+ // Save the reset state
890+ await this . saveSessionData ( ) ;
891+
892+ // Show user notification about the reset
893+ if ( previousPomodoros > 0 ) {
894+ NotificationUtils . showNotificationPing (
895+ `New day! Yesterday's ${ previousPomodoros } sessions have been saved. Fresh start! 🌅` ,
896+ 'info'
897+ ) ;
898+ } else {
899+ NotificationUtils . showNotificationPing (
900+ 'Good morning! Ready for a productive new day? 🌅' ,
901+ 'info'
902+ ) ;
903+ }
904+
905+ // Update all visual elements
906+ this . updateTrayIcon ( ) ;
907+
908+ // Refresh navigation charts if available
909+ if ( window . navigationManager ) {
910+ try {
911+ await window . navigationManager . updateDailyChart ( ) ;
912+ await window . navigationManager . updateFocusSummary ( ) ;
913+ await window . navigationManager . updateWeeklySessionsChart ( ) ;
914+ } catch ( error ) {
915+ console . error ( 'Failed to update navigation charts after midnight reset:' , error ) ;
916+ }
917+ }
918+
919+ console . log ( '✅ Midnight reset completed successfully' ) ;
920+ }
921+
839922 async skipSession ( ) {
840923 this . isRunning = false ;
841924 this . isPaused = false ;
@@ -2005,49 +2088,57 @@ export class PomodoroTimer {
20052088 }
20062089 }
20072090
2008- async loadSessionData ( ) {
2091+ async loadSessionData ( forceReset = false ) {
20092092 const today = new Date ( ) . toDateString ( ) ;
20102093
2094+ // Update current date string for midnight monitoring
2095+ this . currentDateString = today ;
2096+
20112097 try {
20122098 const data = await invoke ( 'load_session_data' ) ;
2013- if ( data && data . date === today ) {
2099+ if ( data && data . date === today && ! forceReset ) {
20142100 // Load today's session data
20152101 this . completedPomodoros = data . completed_pomodoros || 0 ;
20162102 this . totalFocusTime = data . total_focus_time || 0 ;
20172103 this . currentSession = data . current_session || 1 ;
20182104 this . updateProgressDots ( ) ;
2105+ console . log ( '📊 Loaded existing session data for today' ) ;
20192106 } else {
2020- // Reset to default values for new day or no data
2107+ // Reset to default values for new day, no data, or forced reset
20212108 this . completedPomodoros = 0 ;
20222109 this . totalFocusTime = 0 ;
20232110 this . currentSession = 1 ;
20242111 this . updateProgressDots ( ) ;
2112+ console . log ( '🌅 Reset session data for new day or forced reset' ) ;
20252113 }
20262114 } catch ( error ) {
20272115 console . error ( 'Failed to load session data from Tauri, using localStorage:' , error ) ;
20282116 const saved = localStorage . getItem ( 'pomodoro-session' ) ;
20292117
20302118 if ( saved ) {
20312119 const data = JSON . parse ( saved ) ;
2032- if ( data . date === today ) {
2120+ if ( data . date === today && ! forceReset ) {
20332121 // Load today's session data from localStorage
20342122 this . completedPomodoros = data . completedPomodoros || 0 ;
20352123 this . totalFocusTime = data . totalFocusTime || 0 ;
20362124 this . currentSession = data . currentSession || 1 ;
20372125 this . updateProgressDots ( ) ;
2126+ console . log ( '📊 Loaded existing session data from localStorage' ) ;
20382127 } else {
2039- // Reset to default values for new day or no data
2128+ // Reset to default values for new day, no data, or forced reset
20402129 this . completedPomodoros = 0 ;
20412130 this . totalFocusTime = 0 ;
20422131 this . currentSession = 1 ;
20432132 this . updateProgressDots ( ) ;
2133+ console . log ( '🌅 Reset session data from localStorage for new day or forced reset' ) ;
20442134 }
20452135 } else {
20462136 // No saved data at all, reset to defaults
20472137 this . completedPomodoros = 0 ;
20482138 this . totalFocusTime = 0 ;
20492139 this . currentSession = 1 ;
20502140 this . updateProgressDots ( ) ;
2141+ console . log ( '🌅 No saved data found, using defaults' ) ;
20512142 }
20522143 }
20532144 }
@@ -2259,6 +2350,9 @@ export class PomodoroTimer {
22592350 this . smartPauseEnabled = false ;
22602351 this . inactivityThreshold = 30000 ; // Reset to default 30 seconds
22612352
2353+ // Clear midnight monitoring
2354+ this . stopMidnightMonitoring ( ) ;
2355+
22622356 // Reset all counters and state
22632357 this . completedPomodoros = 0 ;
22642358 this . generateProgressDots ( ) ;
@@ -2307,6 +2401,9 @@ export class PomodoroTimer {
23072401 this . updateTrayIcon ( ) ;
23082402 this . updateSettingIndicators ( ) ;
23092403
2404+ // Restart midnight monitoring
2405+ this . startMidnightMonitoring ( ) ;
2406+
23102407 console . log ( 'Timer reset to initial state' ) ;
23112408 }
23122409}
0 commit comments