Skip to content

Commit

Permalink
Fix timer resets on notification closes, add human readable timers
Browse files Browse the repository at this point in the history
  • Loading branch information
flakas committed Dec 26, 2016
1 parent c4d8a11 commit 1d8936f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ var notification, breakTime, workTime, rule, enableSound, iCloseNotification = 0
resetTimes();
$(document).ready(function () {
chrome.notifications.onButtonClicked.addListener(handleNotificationInteraction);
chrome.notifications.onClosed.addListener(handleNotificationClosed);
waitForNext();
updateBadge();
});
Expand Down Expand Up @@ -124,6 +125,12 @@ function handleNotificationInteraction(notificationId, buttonIndex) {
}
}

function handleNotificationClosed(notificationId, byUser) {
if (notificationId === 'bh-notification' && byUser && breakTimeLeft === 0) {
skipBreak();
}
}

function closeNotification() {
"use strict";
iCloseNotification = 1;
Expand Down Expand Up @@ -175,7 +182,7 @@ function updateBreakNotificationTimer() {
chrome.notifications.update('bh-notification', {
type: 'progress',
title: 'Break Helper',
message: "You're on a break.",
message: "You're on a break ($%1s).".replace("$%1s", secondsToClock(breakTimeLeft)),
iconUrl: 'icon128.png',
buttons: [],
progress: progress
Expand Down Expand Up @@ -276,7 +283,7 @@ function updateBadgeClock() {
if (left <= 60) {
chrome.browserAction.setBadgeBackgroundColor({color : [255, 0, 0, 255]});
}
chrome.browserAction.setBadgeText({text: m + ':' + s});
chrome.browserAction.setBadgeText({text: m.pad(2) + ':' + s.pad(2)});

} else {
chrome.browserAction.setBadgeBackgroundColor({color : [255, 0, 0, 255]});
Expand All @@ -292,3 +299,18 @@ function updateBadge() {
function playSound ( url ) {
(new Audio(url)).play();
}

function secondsToClock(leftSeconds) {
var h, m, s, tempLeft;
h = Math.floor(leftSeconds / 3600);
tempLeft = leftSeconds % 3600;
s = tempLeft % 60;
m = (tempLeft - s) / 60;

return h.pad(2) + ":" + m.pad(2) + ":" + s.pad(2);
}
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Break Helper",
"version": "1.5",
"version": "1.6",
"description": "Reminds you to take a break, because breaks are good for your health and productivity",
"icons": {
"16": "icon16.png",
Expand Down

0 comments on commit 1d8936f

Please sign in to comment.