From c4d8a11b944576a81eb0c613c8c4c663a4a91351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tautvidas=20Sipavi=C4=8Dius?= Date: Tue, 4 Oct 2016 23:34:37 +0300 Subject: [PATCH] Add a break confirmation and break progress to notifications --- js/background.js | 42 ++++++++++++++++++++--- js/notification.js | 85 ---------------------------------------------- manifest.json | 2 +- notification.html | 21 ------------ 4 files changed, 38 insertions(+), 112 deletions(-) delete mode 100644 js/notification.js delete mode 100644 notification.html diff --git a/js/background.js b/js/background.js index b144248..2367a1e 100644 --- a/js/background.js +++ b/js/background.js @@ -78,9 +78,10 @@ if (typeof localStorage.statistics === "undefined") { } else { statistics = $.parseJSON(localStorage["statistics"]); } -var notification, breakTime, workTime, rule, enableSound, iCloseNotification = 0, timer, badgeTimer, nextBreak; +var notification, breakTime, workTime, rule, enableSound, iCloseNotification = 0, timer, nextBreak, breakTimeLeft; resetTimes(); $(document).ready(function () { + chrome.notifications.onButtonClicked.addListener(handleNotificationInteraction); waitForNext(); updateBadge(); }); @@ -101,14 +102,28 @@ function displayNotification() { notification = chrome.notifications.create('bh-notification', { type: 'basic', title: 'Break Helper', - message: "You've been working for quite a while, please take a break", - iconUrl: 'icon48.png', - }, function (notificationId) { - doBreak(); + message: "You've been working for quite a while, please take a break.", + iconUrl: 'icon128.png', + buttons: [ + { title: "Take a break" }, + { title: "Skip this break" } + ], + requireInteraction: true, }); log("Displaying notification"); } +function handleNotificationInteraction(notificationId, buttonIndex) { + if (notificationId === 'bh-notification') { + if (buttonIndex === 0) { + doBreak(); + } else if (buttonIndex === 1) { + skipBreak(); + closeNotification(); + } + } +} + function closeNotification() { "use strict"; iCloseNotification = 1; @@ -147,10 +162,27 @@ function skipFor4Hours() { } function doBreak() { + breakTimeLeft = breakTime; timer = setTimeout(waitAndClose, 1000 * breakTime); + updateBreakNotificationTimer(); log("Doing a break"); } +function updateBreakNotificationTimer() { + if (breakTimeLeft <= 0) return; + breakTimeLeft -= 1; + var progress = breakTimeLeft / breakTime * 100; + chrome.notifications.update('bh-notification', { + type: 'progress', + title: 'Break Helper', + message: "You're on a break.", + iconUrl: 'icon128.png', + buttons: [], + progress: progress + }); + setTimeout(updateBreakNotificationTimer, 1000); +}; + function waitAndClose() { closeNotification(); var currentTime = new Date(); diff --git a/js/notification.js b/js/notification.js deleted file mode 100644 index 94a10a6..0000000 --- a/js/notification.js +++ /dev/null @@ -1,85 +0,0 @@ -$(document).ready(function () { - var backgroundPage = chrome.extension.getBackgroundPage(); - $("#takeBreak").click(function () { - backgroundPage._gaq.push(['_trackEvent', 'notification.button#takeBreak', 'clicked']); - switch (backgroundPage.settings.rule) { - case "20-20-20": - $("#notificationContent").html("

You're on a break

Look at an object that is at least 20 feet away.

"); - break; - case "50-10": - $("#notificationContent").html("

You're on a break

Do some exercises for your eyes or look through the window

"); - break; - case "pomodoro": - $("#notificationContent").html("

You're on a break

Do some exercises for your eyes or look through the window

"); - break; - case "custom": - $("#notificationContent").html("

You're on a break

Remember to get up and move around a bit

"); - break; - } - drawTimer(); - backgroundPage.doBreak(); - }); - - $("#skipBreak").click(function () { - backgroundPage.skipBreak(); - backgroundPage._gaq.push(['_trackEvent', 'notification.button#skipBreak', 'clicked']); - }); - - $("#skipForAnHour").click(function () { - backgroundPage.skipForAnHour(); - backgroundPage._gaq.push(['_trackEvent', 'notification.button#skipForAnHour', 'clicked']); - }); - - $("#skipFor4Hours").click(function () { - backgroundPage.skipFor4Hours(); - backgroundPage._gaq.push(['_trackEvent', 'notification.button#skipFor4Hours', 'clicked']); - }); - - switch (backgroundPage.settings.rule) { - case "20-20-20": - $("h1").html("20 minutes are up
take a break!"); - break; - case "50-10": - $("h1").html("50 minutes are up
take a break!"); - break; - case "pomodoro": - $("h1").html("25 minutes are up
take a break!"); - break; - case "custom": - $("h1").html(backgroundPage.settings.workTime + " seconds are up
take a break!"); - break; - } -}); -$(window).unload(function () { - var backgroundPage = chrome.extension.getBackgroundpage(); - if (backgroundPage.iCloseNotification === 0) { - backgroundPage.skipBreak(); - } -}); -var backgroundPage = chrome.extension.getBackgroundPage(); -var timerLeft = backgroundPage.rules[backgroundPage.settings.rule].breakTime; -function drawTimer() { - "use strict"; - var timer = $("#timer"), - minutes, - seconds; - timerLeft -= 1; - if (timerLeft <= 0) { - if (parseInt(backgroundPage.settings.playSound, 10) === 1) { - backgroundPage.playSound('notification.wav'); - } - timer.html('Finished!'); - } else { - seconds = timerLeft % 60; - minutes = (timerLeft - seconds) / 60; - if (seconds < 10) { - seconds = '0' + seconds; - } - if (minutes < 10) { - minutes = '0' + minutes; - } - timer.html(minutes + ':' + seconds); - - } - setTimeout(drawTimer, 1000); -} diff --git a/manifest.json b/manifest.json index 7646033..b50a0d3 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Break Helper", - "version": "1.4.4", + "version": "1.5", "description": "Reminds you to take a break, because breaks are good for your health and productivity", "icons": { "16": "icon16.png", diff --git a/notification.html b/notification.html deleted file mode 100644 index 467669b..0000000 --- a/notification.html +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - -
-

Time is up
take a break!

-

- -
- - -
- - -