Skip to content

Commit 195345e

Browse files
committed
change repeat data storage to improve extensibility
1 parent 36b0a31 commit 195345e

File tree

4 files changed

+99
-65
lines changed

4 files changed

+99
-65
lines changed

scripts/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async function wakeMeUp(tabs) {
7777
bgLog(['Waking up tabs', tabsToWakeUp.map(t => t.id).join(', ')], ['', 'green'], 'yellow');
7878
tabs.filter(wakingUp).filter(t => !t.repeat).forEach(t => t.opened = now);
7979
for (var s of tabs.filter(wakingUp).filter(t => t.repeat)) {
80-
var next = await calculateNextSnoozeTime(s.repeat, s.timeCreated, s.gap);
80+
var next = await calculateNextSnoozeTime(s.repeat);
8181
s.wakeUpTime = next.valueOf();
8282
}
8383
await saveTabs(tabs);

scripts/common.js

Lines changed: 81 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ async function openWindow(t, automatic = false) {
199199

200200
async function dupeSnoozedTab(tabId, snoozeTime) {
201201
var t = await getSnoozedTabs(tabId);
202-
['startUp', 'opened', 'deleted', 'repeat', 'paused', 'gap'].forEach(prop => delete t[prop]);
202+
['startUp', 'opened', 'deleted', 'repeat', 'paused'].forEach(prop => delete t[prop]);
203203
t.wakeUpTime = snoozeTime === 'startup' ? dayjs().add(20, 'y').valueOf() : dayjs(snoozeTime).valueOf(),
204204
t.timeCreated = dayjs().valueOf();
205205
t.id = getRandomId();
@@ -210,7 +210,7 @@ async function dupeSnoozedTab(tabId, snoozeTime) {
210210

211211
async function editSnoozeTime(tabId, snoozeTime) {
212212
var t = await getSnoozedTabs(tabId);
213-
['startUp', 'opened', 'deleted', 'repeat', 'paused', 'gap'].forEach(prop => delete t[prop]);
213+
['startUp', 'opened', 'deleted', 'repeat', 'paused'].forEach(prop => delete t[prop]);
214214
t.wakeUpTime = snoozeTime === 'startup' ? dayjs().add(20, 'y').valueOf() : dayjs(snoozeTime).valueOf(),
215215
t.modifiedTime = dayjs().valueOf();
216216
if (snoozeTime === 'startup') t.startUp = true;
@@ -271,11 +271,11 @@ async function snoozeWindow(snoozeTime, isASelection) {
271271
return isASelection ? {tabId: tabsInWindow.filter(t => t.highlighted).map(t => t.id)} : {windowId: tabsInWindow.find(w => w.active).windowId};
272272
}
273273

274-
async function snoozeRecurring(target, time, repeat, repeatData) {
274+
async function snoozeRecurring(target, data) {
275275
var sleepyObj = {
276276
id: getRandomId(),
277277
timeCreated: dayjs().valueOf(),
278-
repeat: repeat,
278+
repeat: data,
279279
paused: false,
280280
}
281281

@@ -287,12 +287,10 @@ async function snoozeRecurring(target, time, repeat, repeatData) {
287287
sleepyObj.selection = true;
288288
}
289289

290-
if (repeat === 'startup') sleepyObj.startUp = true;
291-
if (Object.keys(repeatData).length) sleepyObj.gap = repeatData;
290+
if (data.repeat === 'startup') sleepyObj.startUp = true;
292291

293-
var next = await calculateNextSnoozeTime(repeat, time, repeatData);
294-
sleepyObj.wakeUpTime = next.valueOf();
295-
console.log(next.format('DD/MM/YY HH:mm'));
292+
sleepyObj.wakeUpTime = await calculateNextSnoozeTime(data);
293+
console.log(dayjs(sleepyObj.wakeUpTime).format('DD/MM/YY HH:mm'));
296294

297295
if (validTabs.length === 0) return {};
298296
if (validTabs.length === 1 || target === 'tab') {
@@ -314,9 +312,8 @@ async function snoozeRecurring(target, time, repeat, repeatData) {
314312
})
315313
}
316314
console.log(sleepyObj);
317-
// return;
318315
await saveTab(sleepyObj);
319-
chrome.runtime.sendMessage({logOptions: [target, sleepyObj, next.valueOf()]});
316+
chrome.runtime.sendMessage({logOptions: [target, sleepyObj, sleepyObj.wakeUpTime]});
320317
if (target === 'tab') return {tabId: activeTab.id};
321318
if (target === 'window') return {windowId: validTabs.find(w => w.active).windowId};
322319
if (target === 'selection') return {tabId: validTabs.map(t => t.id)};
@@ -343,7 +340,7 @@ async function getChoices(which) {
343340
startUp: true,
344341
time: NOW.add(20, 'y'),
345342
timeString: '',
346-
repeatTime: '',
343+
repeatTime: NOW.add(20, 'y'),
347344
repeatTimeString: '',
348345
repeat_id: 'startup',
349346
menuLabel: 'till next startup'
@@ -365,9 +362,9 @@ async function getChoices(which) {
365362
timeString: 'Today',
366363
repeatTime: '',
367364
repeatTimeString: '',
365+
menuLabel: 'till this morning',
368366
disabled: NOW.startOf('d').add(config.morning[0], 'h').add(config.morning[1], 'm').valueOf() < dayjs(),
369367
repeatDisabled: true,
370-
menuLabel: 'till this morning'
371368
},
372369
'today-evening': {
373370
label: `Today ${getEveningLabel(config.evening[0])}`,
@@ -377,8 +374,8 @@ async function getChoices(which) {
377374
repeatTime: NOW.format(getHourFormat(true)),
378375
repeatTimeString: 'Starts Tom at',
379376
repeat_id: 'daily',
377+
menuLabel: 'till this evening',
380378
disabled: NOW.startOf('d').add(config.evening[0], 'h').add(config.evening[1], 'm').valueOf() < dayjs(),
381-
menuLabel: 'till this evening'
382379
},
383380
'tom-morning': {
384381
label: 'Tomorrow Morning',
@@ -408,8 +405,8 @@ async function getChoices(which) {
408405
repeatTime: NOW.startOf('d').format(getHourFormat(true)),
409406
repeatTimeString: `${NOW.weekday(6).format('dddd')}s at`,
410407
repeat_id: 'weekends',
408+
menuLabel: 'till the weekend',
411409
disabled: NOW.day() === 6,
412-
menuLabel: 'till the weekend'
413410
},
414411
'monday': {
415412
label: 'Next Monday',
@@ -429,15 +426,15 @@ async function getChoices(which) {
429426
repeatTime: NOW.format(getHourFormat(true)),
430427
repeatTimeString: `${NOW.format('dddd')}s at`,
431428
repeat_id: 'weekly',
429+
menuLabel: 'for a week',
432430
disabled: NOW.day() === 1,
433431
repeatDisabled: NOW.day() === 1 || NOW.day() === 6,
434-
menuLabel: 'for a week'
435432
},
436433
'month': {
437434
label: 'Next Month',
438435
repeatLabel: 'Every Month',
439436
time: NOW.startOf('d').add(1, 'M'),
440-
timeString: NOW.startOf('d').add(1, 'M').format('D MMM'),
437+
timeString: NOW.startOf('d').add(1, 'M').format('ddd, D MMM'),
441438
repeatTime: NOW.format(getHourFormat(true)),
442439
repeatTimeString: `${getOrdinal(NOW.format('D'))} of Month`,
443440
repeat_id: 'monthly',
@@ -447,51 +444,83 @@ async function getChoices(which) {
447444
return which && all[which] ? all[which] : all;
448445
}
449446

450-
async function calculateNextSnoozeTime(repeat, start, data) {
451-
var NOW = dayjs(), start = dayjs(start);
452-
if (repeat === 'custom' && data) {
447+
async function calculateNextSnoozeTime(data) {
448+
console.log(data);
449+
var NOW = dayjs(), TYPE = data.type, [HOUR, MINUTE] = data.time;
450+
if (TYPE === 'hourly') {
451+
var isNextHour = NOW.minute() >= MINUTE ? 1 : 0;
452+
return NOW.startOf('h').add(isNextHour, 'h').minute(MINUTE).valueOf();
453+
} else if (TYPE === 'daily') {
454+
var isNextDay = NOW.hour() > HOUR || (NOW.hour() === HOUR && NOW.minute() >= MINUTE) ? 1 : 0;
455+
return NOW.startOf('d').add(isNextDay, 'd').hour(HOUR).minute(MINUTE).valueOf();
456+
} else if (TYPE === 'daily_morning') {
457+
var [m_hour, m_minute] = await getOptions('morning');
458+
var isNextDay = NOW.hour() > m_hour || (NOW.hour() === m_hour && NOW.minute() >= m_minute) ? 1 : 0;
459+
return NOW.startOf('d').add(isNextDay, 'd').hour(m_hour).minute(m_minute).valueOf();
460+
} else if (TYPE === 'daily_evening') {
461+
var [e_hour, e_minute] = await getOptions('evening');
462+
var isNextDay = NOW.hour() > e_hour || (NOW.hour() === e_hour && NOW.minute() >= e_minute) ? 1 : 0;
463+
return NOW.startOf('d').add(isNextDay, 'd').hour(e_hour).minute(e_minute).valueOf();
464+
} else if (['weekends', 'mondays', 'weekly', 'monthly', 'custom'].includes(TYPE)) {
453465
var days = [];
454466
if (data.weekly) {
455467
var thisWeek = data.weekly, nextWeek = data.weekly.map(day => day + 7);
456-
days = nextWeek.concat(thisWeek).map(day => dayjs().startOf('w').add(day, 'd').add(start.hour(), 'h').add(start.minute(), 'm'));
468+
days = nextWeek.concat(thisWeek).map(day => dayjs().startOf('w').add(day, 'd').hour(HOUR).minute(MINUTE));
457469
} else if (data.monthly) {
458-
var thisMonth = data.monthly.filter(d => d <= dayjs().daysInMonth()).map(d => dayjs().startOf('M').date(d).add(start.hour(), 'h').add(start.minute(), 'm'));
459-
var nextMonth = data.monthly.filter(d => d <= dayjs().add(1, 'M').daysInMonth()).map(d => dayjs().startOf('M').add(1, 'M').date(d).add(start.hour(), 'h').add(start.minute(), 'm'));
470+
var thisMonth = data.monthly.filter(d => d <= dayjs().daysInMonth()).map(d => dayjs().startOf('M').date(d).hour(HOUR).minute(MINUTE));
471+
var nextMonth = data.monthly.filter(d => d <= dayjs().add(1, 'M').daysInMonth()).map(d => dayjs().startOf('M').add(1, 'M').date(d).hour(HOUR).minute(MINUTE));
460472
days = nextMonth.concat(thisMonth);
461473
}
462-
return days.filter(d => d > NOW).pop();
463-
} else if (repeat === 'startup') {
464-
return NOW.add(20, 'y');
465-
} else if (repeat === 'hourly') {
466-
var isThisHour = NOW.minute() < start.minute();
467-
return NOW.startOf('h').add(isThisHour ? 0 : 1, 'h').minute(start.minute());
468-
} else if (repeat === 'daily') {
469-
return dayjs().startOf('d').add(1, 'd').hour(NOW.hour()).minute(NOW.minute());
470-
} else if (repeat === 'daily_morning') {
471-
var morning = await getOptions('morning');
472-
var isToday = NOW.hour() < morning[0] || (NOW.hour() === morning[0] && NOW.minute() < morning[1]);
473-
return NOW.startOf('d').add(isToday ? 0 : 1, 'd').hour(morning[0]).minute(morning[1]);
474-
} else if (repeat === 'daily_evening') {
475-
var evening = await getOptions('evening');
476-
var isToday = NOW.hour() < evening[0] || (NOW.hour() === evening[0] && NOW.minute() < evening[1]);
477-
return NOW.startOf('d').add(isToday ? 0 : 1, 'd').hour(evening[0]).minute(evening[1]);
478-
} else if (repeat === 'weekends') {
479-
var isThisWeek = NOW.day() < 6 || (NOW.day() === 6 && (NOW.hour() < start.hour() || (NOW.hour() === start.hour() && NOW.minute() < start.minute())));
480-
return NOW.startOf('w').add(isThisWeek ? 0 : 1, 'w').day(6).hour(start.hour()).minute(start.minute());
481-
} else if (repeat === 'mondays') {
482-
var isThisWeek = NOW.day() < 1 || (NOW.day() === 1 && (NOW.hour() < start.hour() || (NOW.hour() === start.hour() && NOW.minute() < start.minute())));
483-
return NOW.startOf('w').add(isThisWeek ? 0 : 1, 'w').day(1).hour(start.hour()).minute(start.minute());
484-
} else if (repeat === 'weekly') {
485-
var isThisWeek = NOW.day() < start.day() || (NOW.day() === start.day() && (NOW.hour() < start.hour() || (NOW.hour() === start.hour() && NOW.minute() < start.minute())));
486-
return NOW.startOf('w').add(isThisWeek ? 0 : 1, 'w').day(start.day()).hour(start.hour()).minute(start.minute());
487-
} else if (repeat === 'monthly') {
488-
var isThisMonth = NOW.date() < start.date() || (NOW.date() === start.date() && (NOW.hour() < start.hour() ||( NOW.hour() === start.hour() && NOW.minute() < start.minute()))) ? 0 : 1;
489-
var isMonthValid = start.date() <= NOW.daysInMonth(isThisMonth, 'M') ? 0 : 1;
490-
return NOW.startOf('M').add(isThisMonth + isMonthValid, 'M').date(start.date()).hour(start.hour()).minute(start.minute());
474+
return days.filter(d => d > NOW).pop().valueOf();
491475
}
492476
return false;
493477
}
494478

479+
// async function calculateNextSnoozeTime(repeat, start, data) {
480+
// var NOW = dayjs(), start = dayjs(start);
481+
// if (repeat === 'custom' && data) {
482+
// var days = [];
483+
// if (data.weekly) {
484+
// var thisWeek = data.weekly, nextWeek = data.weekly.map(day => day + 7);
485+
// days = nextWeek.concat(thisWeek).map(day => dayjs().startOf('w').add(day, 'd').add(start.hour(), 'h').add(start.minute(), 'm'));
486+
// } else if (data.monthly) {
487+
// var thisMonth = data.monthly.filter(d => d <= dayjs().daysInMonth()).map(d => dayjs().startOf('M').date(d).add(start.hour(), 'h').add(start.minute(), 'm'));
488+
// var nextMonth = data.monthly.filter(d => d <= dayjs().add(1, 'M').daysInMonth()).map(d => dayjs().startOf('M').add(1, 'M').date(d).add(start.hour(), 'h').add(start.minute(), 'm'));
489+
// days = nextMonth.concat(thisMonth);
490+
// }
491+
// return days.filter(d => d > NOW).pop();
492+
// } else if (repeat === 'startup') {
493+
// return NOW.add(20, 'y');
494+
// } else if (repeat === 'hourly') {
495+
// var isThisHour = NOW.minute() < start.minute();
496+
// return NOW.startOf('h').add(isThisHour ? 0 : 1, 'h').minute(start.minute());
497+
// } else if (repeat === 'daily') {
498+
// return dayjs().startOf('d').add(1, 'd').hour(NOW.hour()).minute(NOW.minute());
499+
// } else if (repeat === 'daily_morning') {
500+
// var morning = await getOptions('morning');
501+
// var isToday = NOW.hour() < morning[0] || (NOW.hour() === morning[0] && NOW.minute() < morning[1]);
502+
// return NOW.startOf('d').add(isToday ? 0 : 1, 'd').hour(morning[0]).minute(morning[1]);
503+
// } else if (repeat === 'daily_evening') {
504+
// var evening = await getOptions('evening');
505+
// var isToday = NOW.hour() < evening[0] || (NOW.hour() === evening[0] && NOW.minute() < evening[1]);
506+
// return NOW.startOf('d').add(isToday ? 0 : 1, 'd').hour(evening[0]).minute(evening[1]);
507+
// } else if (repeat === 'weekends') {
508+
// var isThisWeek = NOW.day() < 6 || (NOW.day() === 6 && (NOW.hour() < start.hour() || (NOW.hour() === start.hour() && NOW.minute() < start.minute())));
509+
// return NOW.startOf('w').add(isThisWeek ? 0 : 1, 'w').day(6).hour(start.hour()).minute(start.minute());
510+
// } else if (repeat === 'mondays') {
511+
// var isThisWeek = NOW.day() < 1 || (NOW.day() === 1 && (NOW.hour() < start.hour() || (NOW.hour() === start.hour() && NOW.minute() < start.minute())));
512+
// return NOW.startOf('w').add(isThisWeek ? 0 : 1, 'w').day(1).hour(start.hour()).minute(start.minute());
513+
// } else if (repeat === 'weekly') {
514+
// var isThisWeek = NOW.day() < start.day() || (NOW.day() === start.day() && (NOW.hour() < start.hour() || (NOW.hour() === start.hour() && NOW.minute() < start.minute())));
515+
// return NOW.startOf('w').add(isThisWeek ? 0 : 1, 'w').day(start.day()).hour(start.hour()).minute(start.minute());
516+
// } else if (repeat === 'monthly') {
517+
// var isThisMonth = NOW.date() < start.date() || (NOW.date() === start.date() && (NOW.hour() < start.hour() ||( NOW.hour() === start.hour() && NOW.minute() < start.minute()))) ? 0 : 1;
518+
// var isMonthValid = start.date() <= NOW.daysInMonth(isThisMonth, 'M') ? 0 : 1;
519+
// return NOW.startOf('M').add(isThisMonth + isMonthValid, 'M').date(start.date()).hour(start.hour()).minute(start.minute());
520+
// }
521+
// return false;
522+
// }
523+
495524
/* END ASYNC FUNCTIONS */
496525

497526
// var getFaviconUrl = url => `https://icons.duckduckgo.com/ip3/${getHostname(url)}.ico`

scripts/nap_room.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,7 @@ function closeOverflows() {
417417
async function togglePause(t, state) {
418418
t.paused = state === 'pause'
419419
if (state === 'resume') {
420-
var next = await calculateNextSnoozeTime(t.repeat, t.timeCreated, t.gap)
421-
t.wakeUpTime = next.valueOf();
420+
t.wakeUpTime = await calculateNextSnoozeTime(t.repeat)
422421
}
423422
await saveTab(t);
424423
closeOverflows();
@@ -467,7 +466,7 @@ async function sendTabsToHistory(ids) {
467466
console.log('hi');
468467
if (!ids) return;
469468
CACHED_TABS.filter(t => ids.includes(t.id)).forEach(t => {
470-
['startUp', 'repeat', 'paused', 'gap'].forEach(prop => delete t[prop]);
469+
['startUp', 'paused'].forEach(prop => delete t[prop]);
471470
t.opened = dayjs().valueOf();
472471
t.deleted = true;
473472
});

scripts/popup.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,13 @@ async function buildRepeatCustomChoice() {
291291
a.classList.toggle('disabled', dayjs(time.selectedDates).format('HHmm') === actionValue.format('HHmm'));
292292
});
293293
if (isValid) {
294-
var data = {}, start = dayjs(time.selectedDates);
294+
var data = {type: 'custom', time: [dayjs(time.selectedDates).hour(), dayjs(time.selectedDates).minute()]};
295295
if (document.querySelector('.repeat-interval.active').getAttribute('data-type') === 'weekly') {
296296
data.weekly = Array.from(document.querySelectorAll('.day-choice span.active')).map(d => parseInt(d.getAttribute('data-value'))).sort(desc);
297297
} else {
298298
data.monthly = document.getElementById('monthly')._flatpickr.selectedDates.map(d => dayjs(d).date()).sort(desc);
299299
}
300-
var wakeUpTime = await calculateNextSnoozeTime('custom', start, data);
300+
var wakeUpTime = await calculateNextSnoozeTime(data);
301301
document.getElementById('next-wakeup').innerText = formatSnoozedUntil({wakeUpTime});
302302
document.querySelector('.submit-btn').classList.toggle('disabled', false);
303303
}
@@ -485,19 +485,25 @@ async function snooze(time, choice) {
485485
if (!['tab', 'window', 'selection', 'group'].includes(target.id)) return;
486486

487487
if (document.getElementById('repeat').checked) {
488-
var t, data = {}, repeat = choice.getAttribute('data-repeat-id');
489-
if (repeat === 'custom') {
490-
var pickr = dayjs(document.getElementById('repeat-time')._flatpickr.selectedDates), time = time.second(0).minute(pickr.minute()).hour(pickr.hour());
488+
var t, data = {type: choice.getAttribute('data-repeat-id'), time: [time.hour(), time.minute()]};
489+
if (data.type === 'daily') data.time = [dayjs().hour(), dayjs().minute()];
490+
if (data.type === 'weekends') data.weekly = [6];
491+
if (data.type === 'mondays') data.weekly = [1];
492+
if (data.type === 'weekly') data.weekly = [dayjs().day()];
493+
if (data.type === 'monthly') data.monthly = [dayjs().date()];
494+
if (data.type === 'custom') {
495+
var pickr = dayjs(document.getElementById('repeat-time')._flatpickr.selectedDates);
496+
data.time = [pickr.hour(), pickr.minute()];
497+
console.log(data, document.querySelector('.repeat-interval.active').getAttribute('data-type'));
491498
if (document.querySelector('.repeat-interval.active').getAttribute('data-type') === 'weekly') {
492499
data.weekly = Array.from(document.querySelectorAll('.day-choice span.active')).map(d => parseInt(d.getAttribute('data-value'))).sort(desc);
493500
}
494501
if (document.querySelector('.repeat-interval.active').getAttribute('data-type') === 'monthly') {
495502
data.monthly = document.getElementById('monthly')._flatpickr.selectedDates.map(d => dayjs(d).date()).sort(desc);
496503
}
497504
}
498-
if (repeat === 'weekly') time = time.subtract(1, 'w')
499-
if (repeat === 'monthly') time = time.subtract(1, 'M');
500-
response = await snoozeRecurring(target.id, time, repeat, data);
505+
response = await snoozeRecurring(target.id, data);
506+
// response = await snoozeRecurring(target.id, time, repeat, data);
501507
} else {
502508
if (target.id === 'tab') {
503509
response = await snoozeTab(time);
@@ -507,7 +513,7 @@ async function snooze(time, choice) {
507513
response = await snoozeWindow(time, true);
508514
}
509515
}
510-
if (!response.tabId && !response.windowId) return;
516+
if (!response || (!response.tabId && !response.windowId)) return;
511517
await chrome.runtime.sendMessage(Object.assign(response, {close: true, delay: closeDelay}));
512518
await displayPreviewAnimation(choice, time.format ? time.format('.HHmm') : '', `Snoozing ${target.id}`)
513519
}

0 commit comments

Comments
 (0)