Skip to content

Commit b1cde51

Browse files
committed
feat: implement session synchronization events for added, updated, and deleted sessions
1 parent a5c9916 commit b1cde51

File tree

3 files changed

+93
-17
lines changed

3 files changed

+93
-17
lines changed

src/core/pomodoro-timer.js

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class PomodoroTimer {
110110
// Generate initial progress dots
111111
this.generateProgressDots();
112112
this.updateDisplay();
113-
this.updateProgressDots();
113+
await this.updateProgressDots();
114114
this.updateStopUndoButton(); // Initialize stop/undo button state
115115
this.updateSkipIcon(); // Initialize skip button icon
116116
this.updateSmartIndicator(); // Initialize smart pause indicator
@@ -190,6 +190,42 @@ export class PomodoroTimer {
190190

191191
// Start midnight monitoring for daily reset
192192
this.startMidnightMonitoring();
193+
194+
// Setup event listeners for session synchronization
195+
this.setupSessionEventListeners();
196+
}
197+
198+
setupSessionEventListeners() {
199+
// Listen for session changes from SessionManager to keep dots synchronized
200+
window.addEventListener('sessionAdded', async (event) => {
201+
const { date } = event.detail;
202+
const today = new Date().toDateString();
203+
204+
// Only update dots if the session was added for today
205+
if (date === today) {
206+
await this.updateProgressDots();
207+
}
208+
});
209+
210+
window.addEventListener('sessionDeleted', async (event) => {
211+
const { date } = event.detail;
212+
const today = new Date().toDateString();
213+
214+
// Only update dots if the session was deleted from today
215+
if (date === today) {
216+
await this.updateProgressDots();
217+
}
218+
});
219+
220+
window.addEventListener('sessionUpdated', async (event) => {
221+
const { date } = event.detail;
222+
const today = new Date().toDateString();
223+
224+
// Only update dots if the session was updated for today
225+
if (date === today) {
226+
await this.updateProgressDots();
227+
}
228+
});
193229
}
194230

195231
setupEventListeners() {
@@ -1020,7 +1056,7 @@ export class PomodoroTimer {
10201056
if (this.currentMode === 'focus') {
10211057
if (!this.sessionCompletedButNotSaved) {
10221058
this.completedPomodoros++;
1023-
this.updateProgressDots();
1059+
await this.updateProgressDots();
10241060
const actualElapsedTime = this.currentSessionElapsedTime || (this.durations.focus - this.timeRemaining);
10251061
this.totalFocusTime += actualElapsedTime;
10261062
this.lastCompletedSessionTime = actualElapsedTime;
@@ -1113,7 +1149,7 @@ export class PomodoroTimer {
11131149

11141150
if (this.currentMode === 'focus') {
11151151
this.completedPomodoros++;
1116-
this.updateProgressDots();
1152+
await this.updateProgressDots();
11171153

11181154
// Calculate actual elapsed time for focus sessions
11191155
const actualElapsedTime = this.currentSessionElapsedTime || (this.durations.focus - this.timeRemaining);
@@ -1273,7 +1309,7 @@ export class PomodoroTimer {
12731309
// Update completed sessions count for focus sessions
12741310
if (this.currentMode === 'focus') {
12751311
this.completedPomodoros++;
1276-
this.updateProgressDots();
1312+
await this.updateProgressDots();
12771313

12781314
// Calculate actual elapsed time for focus sessions
12791315
const actualElapsedTime = this.currentSessionElapsedTime || this.durations.focus;
@@ -1798,7 +1834,7 @@ export class PomodoroTimer {
17981834

17991835
// Update all displays
18001836
this.updateDisplay();
1801-
this.updateProgressDots();
1837+
await this.updateProgressDots();
18021838
this.updateButtons();
18031839
await this.saveSessionData();
18041840
this.updateTrayIcon();
@@ -1826,7 +1862,7 @@ export class PomodoroTimer {
18261862
}
18271863

18281864
// Progress dots update
1829-
updateProgressDots() {
1865+
async updateProgressDots() {
18301866
const dots = this.progressDots.querySelectorAll('.dot');
18311867

18321868
// Remove any existing overflow indicator
@@ -1835,20 +1871,23 @@ export class PomodoroTimer {
18351871
existingOverflow.remove();
18361872
}
18371873

1838-
// Update each dot based on completed pomodoros and current session
1874+
// Get actual completed sessions count from SessionManager
1875+
const actualCompletedSessions = await this.getCompletedSessionsToday();
1876+
1877+
// Update each dot based on actual completed sessions and current session
18391878
dots.forEach((dot, index) => {
18401879
// Remove all classes first
18411880
dot.classList.remove('completed', 'current');
18421881

1843-
if (index < this.completedPomodoros) {
1882+
if (index < actualCompletedSessions) {
18441883
dot.classList.add('completed');
1845-
} else if (index === this.completedPomodoros && this.currentMode === 'focus') {
1884+
} else if (index === actualCompletedSessions && this.currentMode === 'focus') {
18461885
dot.classList.add('current');
18471886
}
18481887
});
18491888

1850-
if (this.completedPomodoros > this.totalSessions) {
1851-
const overflowCount = this.completedPomodoros - this.totalSessions;
1889+
if (actualCompletedSessions > this.totalSessions) {
1890+
const overflowCount = actualCompletedSessions - this.totalSessions;
18521891
const overflowIndicator = document.createElement('div');
18531892
overflowIndicator.className = 'overflow-indicator';
18541893
overflowIndicator.textContent = `+${overflowCount}`;
@@ -2103,6 +2142,21 @@ export class PomodoroTimer {
21032142
}
21042143
}
21052144

2145+
async getCompletedSessionsToday() {
2146+
if (!window.sessionManager) {
2147+
return this.completedPomodoros; // Fallback to internal counter
2148+
}
2149+
2150+
try {
2151+
const today = new Date(); // Pass Date object instead of string
2152+
const todaySessions = await window.sessionManager.getSessionsForDate(today);
2153+
return todaySessions ? todaySessions.length : 0;
2154+
} catch (error) {
2155+
console.error('Failed to get completed sessions from SessionManager:', error);
2156+
return this.completedPomodoros; // Fallback to internal counter
2157+
}
2158+
}
2159+
21062160
async saveCompletedFocusSession() {
21072161
if (!window.sessionManager) {
21082162
console.log('SessionManager not available, skipping individual session save');
@@ -2196,14 +2250,14 @@ export class PomodoroTimer {
21962250
this.completedPomodoros = data.completed_pomodoros || 0;
21972251
this.totalFocusTime = data.total_focus_time || 0;
21982252
this.currentSession = data.current_session || 1;
2199-
this.updateProgressDots();
2253+
await this.updateProgressDots();
22002254
console.log('📊 Loaded existing session data for today');
22012255
} else {
22022256
// Reset to default values for new day, no data, or forced reset
22032257
this.completedPomodoros = 0;
22042258
this.totalFocusTime = 0;
22052259
this.currentSession = 1;
2206-
this.updateProgressDots();
2260+
await this.updateProgressDots();
22072261
console.log('🌅 Reset session data for new day or forced reset');
22082262
}
22092263
} catch (error) {
@@ -2217,22 +2271,22 @@ export class PomodoroTimer {
22172271
this.completedPomodoros = data.completedPomodoros || 0;
22182272
this.totalFocusTime = data.totalFocusTime || 0;
22192273
this.currentSession = data.currentSession || 1;
2220-
this.updateProgressDots();
2274+
await this.updateProgressDots();
22212275
console.log('📊 Loaded existing session data from localStorage');
22222276
} else {
22232277
// Reset to default values for new day, no data, or forced reset
22242278
this.completedPomodoros = 0;
22252279
this.totalFocusTime = 0;
22262280
this.currentSession = 1;
2227-
this.updateProgressDots();
2281+
await this.updateProgressDots();
22282282
console.log('🌅 Reset session data from localStorage for new day or forced reset');
22292283
}
22302284
} else {
22312285
// No saved data at all, reset to defaults
22322286
this.completedPomodoros = 0;
22332287
this.totalFocusTime = 0;
22342288
this.currentSession = 1;
2235-
this.updateProgressDots();
2289+
await this.updateProgressDots();
22362290
console.log('🌅 No saved data found, using defaults');
22372291
}
22382292
}
@@ -2383,7 +2437,7 @@ export class PomodoroTimer {
23832437

23842438
// Regenerate progress dots when total sessions change
23852439
this.generateProgressDots();
2386-
this.updateProgressDots();
2440+
await this.updateProgressDots();
23872441

23882442
// Update notification preferences
23892443
this.enableDesktopNotifications = settings.notifications.desktop_notifications;

src/managers/navigation-manager.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,11 +1707,13 @@ true // All sessions are focus sessions now
17071707
let sessionFound = false;
17081708

17091709
// Find and delete the session
1710+
let deletedFromDate = null;
17101711
for (const [dateString, sessions] of Object.entries(window.sessionManager.sessions)) {
17111712
const sessionIndex = sessions.findIndex(s => s.id === sessionId);
17121713
if (sessionIndex !== -1) {
17131714
sessions.splice(sessionIndex, 1);
17141715
sessionFound = true;
1716+
deletedFromDate = dateString;
17151717
console.log('Session deleted successfully:', sessionId);
17161718
break;
17171719
}
@@ -1724,6 +1726,11 @@ true // All sessions are focus sessions now
17241726

17251727
// Save the updated sessions
17261728
await window.sessionManager.saveSessionsToStorage();
1729+
1730+
// Dispatch session deleted event for synchronization with other components
1731+
window.dispatchEvent(new CustomEvent('sessionDeleted', {
1732+
detail: { sessionId, date: deletedFromDate }
1733+
}));
17271734

17281735
// Refresh the table
17291736
const currentDate = this.selectedDate || this.currentDate;

src/managers/session-manager.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ export class SessionManager {
330330

331331
// Save to storage (Tauri backend or localStorage)
332332
await this.saveSessionsToStorage();
333+
334+
// Dispatch session added event for synchronization with other components
335+
window.dispatchEvent(new CustomEvent('sessionAdded', {
336+
detail: { sessionData, date: dateString }
337+
}));
333338
}
334339

335340
async updateSession(sessionData) {
@@ -345,6 +350,11 @@ export class SessionManager {
345350

346351
// Save to storage (Tauri backend or localStorage)
347352
await this.saveSessionsToStorage();
353+
354+
// Dispatch session updated event for synchronization with other components
355+
window.dispatchEvent(new CustomEvent('sessionUpdated', {
356+
detail: { sessionData, date: dateString }
357+
}));
348358
}
349359

350360
async deleteCurrentSession() {
@@ -361,6 +371,11 @@ export class SessionManager {
361371
// Save to storage (Tauri backend or localStorage)
362372
await this.saveSessionsToStorage();
363373

374+
// Dispatch session deleted event for synchronization with other components
375+
window.dispatchEvent(new CustomEvent('sessionDeleted', {
376+
detail: { sessionId: this.currentEditingSession.id, date: dateString }
377+
}));
378+
364379
// Store the selected date before closing modal (as closeModal sets it to null)
365380
const dateForRefresh = this.selectedDate;
366381
this.closeModal();

0 commit comments

Comments
 (0)