-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
255 lines (214 loc) · 7.75 KB
/
background.js
File metadata and controls
255 lines (214 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Background service worker for the Breathe extension
class BreathingBackground {
constructor() {
this.setupEventListeners();
this.initializeNotifications();
}
setupEventListeners() {
// Handle extension installation
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
this.handleFirstInstall();
}
});
// Handle alarm events for session reminders
chrome.alarms.onAlarm.addListener((alarm) => {
this.handleAlarm(alarm);
});
// Handle messages from popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
this.handleMessage(message, sender, sendResponse);
return true; // Indicates async response
});
}
async handleFirstInstall() {
// Initialize default settings
const defaultSettings = {
breathingPattern: '478',
sessionDuration: 1,
soundEnabled: true,
sessions: [],
todaySessions: [],
reminderEnabled: false,
reminderInterval: 60 // minutes
};
// This part was missing!
await chrome.storage.local.set(defaultSettings);
// Show welcome notification after a short delay
setTimeout(() => {
this.showNotification(
'Welcome to Breathe!',
'Start your first breathing session to reduce stress and improve focus.',
'icons/icon48.png'
);
}, 1000);
}
async handleAlarm(alarm) {
switch (alarm.name) {
case 'breathingReminder':
await this.showBreathingReminder();
break;
case 'sessionComplete':
await this.handleSessionComplete();
break;
}
}
async handleMessage(message, sender, sendResponse) {
switch (message.action) {
case 'startSession':
await this.startSessionTracking(message.data);
sendResponse({ success: true });
break;
case 'completeSession':
await this.completeSessionTracking(message.data);
sendResponse({ success: true });
break;
case 'setReminder':
await this.setBreathingReminder(message.interval);
sendResponse({ success: true });
break;
case 'clearReminder':
await this.clearBreathingReminder();
sendResponse({ success: true });
break;
}
}
async startSessionTracking(sessionData) {
// Set alarm for session completion
chrome.alarms.create('sessionComplete', {
delayInMinutes: sessionData.duration
});
// Store session start time
await chrome.storage.local.set({
currentSession: {
...sessionData,
startTime: Date.now()
}
});
}
async completeSessionTracking(sessionData) {
// Clear any pending session alarms
chrome.alarms.clear('sessionComplete');
// Update session statistics
const result = await chrome.storage.local.get(['sessions', 'todaySessions', 'weeklyGoal']);
const sessions = result.sessions || [];
const todaySessions = result.todaySessions || [];
const today = new Date().toISOString().split('T')[0];
const completedSession = {
...sessionData,
date: today,
completedAt: Date.now()
};
sessions.push(completedSession);
todaySessions.push(completedSession);
await chrome.storage.local.set({
sessions,
todaySessions,
currentSession: null
});
// Check for achievements
await this.checkAchievements(sessions, todaySessions);
}
async checkAchievements(allSessions, todaySessions) {
const today = new Date().toISOString().split('T')[0];
const todayCount = todaySessions.filter(s => s.date === today).length;
// First session achievement
if (allSessions.length === 1) {
this.showNotification(
'First Session Complete! 🎉',
'You\'ve taken your first step towards better breathing habits.',
'icons/icon48.png'
);
}
// Daily milestones
if (todayCount === 3) {
this.showNotification(
'Daily Goal Reached! ⭐',
'You\'ve completed 3 sessions today. Keep up the great work!',
'icons/icon48.png'
);
}
// Weekly streak
const weeklyStreak = this.calculateWeeklyStreak(allSessions);
if (weeklyStreak === 7) {
this.showNotification(
'Weekly Streak! 🔥',
'You\'ve practiced breathing exercises for 7 days straight!',
'icons/icon48.png'
);
}
}
calculateWeeklyStreak(sessions) {
const today = new Date();
let streak = 0;
for (let i = 0; i < 7; i++) {
const checkDate = new Date(today);
checkDate.setDate(today.getDate() - i);
const dateStr = checkDate.toISOString().split('T')[0];
const hasSessionOnDate = sessions.some(s => s.date === dateStr);
if (hasSessionOnDate) {
streak++;
} else {
break;
}
}
return streak;
}
async setBreathingReminder(intervalMinutes) {
chrome.alarms.create('breathingReminder', {
delayInMinutes: intervalMinutes,
periodInMinutes: intervalMinutes
});
await chrome.storage.local.set({
reminderEnabled: true,
reminderInterval: intervalMinutes
});
}
async clearBreathingReminder() {
chrome.alarms.clear('breathingReminder');
await chrome.storage.local.set({
reminderEnabled: false
});
}
async showBreathingReminder() {
const { reminderEnabled } = await chrome.storage.local.get(['reminderEnabled']);
if (reminderEnabled) {
this.showNotification(
'Time to Breathe! 🧘♀️',
'Take a moment for a quick breathing session to refresh your mind.',
'icons/icon48.png'
);
}
}
async handleSessionComplete() {
const { currentSession } = await chrome.storage.local.get(['currentSession']);
if (currentSession) {
this.showNotification(
'Session Complete! ✨',
`Great job! You completed your ${currentSession.duration}-minute breathing session.`,
'icons/icon48.png'
);
}
}
showNotification(title, message, iconPath = 'icons/icon48.png') {
chrome.notifications.create({
type: 'basic',
iconUrl: iconPath,
title: title,
message: message,
priority: 1
});
}
async initializeNotifications() {
// Check if we should show daily reminder
const { reminderEnabled, reminderInterval } = await chrome.storage.local.get([
'reminderEnabled',
'reminderInterval'
]);
if (reminderEnabled && reminderInterval) {
this.setBreathingReminder(reminderInterval);
}
}
}
// Initialize background service
const breathingBackground = new BreathingBackground();