Skip to content

Commit

Permalink
feat: optionally notify when time estimate was exceeded #69
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Mar 29, 2018
1 parent 9fda280 commit 0f84ef8
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
1 change: 1 addition & 0 deletions app-src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<script src="scripts/main/global-filters/number-to-month-filter.js"></script>
<script src="scripts/main/global-services/app-storage-s.js"></script>
<script src="scripts/main/global-services/check-shortcut-key-combo-s.js"></script>
<script src="scripts/main/global-services/estimate-exceeded-checker-s.js"></script>
<script src="scripts/main/global-services/extension-interface-s.js"></script>
<script src="scripts/main/global-services/git-log-s.js"></script>
<script src="scripts/main/global-services/git-s.js"></script>
Expand Down
1 change: 1 addition & 0 deletions app-src/scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
isPlaySound: true,
isGoToWorkView: false,
},
isNotifyWhenTimeEstimateExceeded: false,
isBlockFinishDayUntilTimeTimeTracked: false,
isConfirmBeforeExit: false,
isShowTimeWorkedWithoutBreak: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @ngdoc service
* @name superProductivity.EstimateExceededChecker
* @description
* # EstimateExceededChecker
* Service in the superProductivity.
*/

(() => {
'use strict';

const EXTRA_TIME_BETWEEN_NOTIFICATIONS = 60 * 1000;

class EstimateExceededChecker {
/* @ngInject */
constructor($mdToast, Notifier, $rootScope, $timeout) {
this.$mdToast = $mdToast;
this.Notifier = Notifier;
this.$rootScope = $rootScope;
this.$timeout = $timeout;
this.currentToastPromise = undefined;
this.notificationTimeout = undefined;
this.isNotificationTimeoutRunning = false;
}

checkTaskAndNotify(task) {
if (!this.isEnabled()) {
return;
}

if (task.timeEstimate && task.timeSpent && moment.duration(task.timeSpent)
.asMinutes() > moment.duration(task.timeEstimate).asMinutes()) {
this.notify(task);
}
}

isEnabled() {
return this.$rootScope.r.config && this.$rootScope.r.config.isNotifyWhenTimeEstimateExceeded;
}

isToastOpen() {
return (this.currentToastPromise && this.currentToastPromise.$$state.status === 0);
}

reInitNotificationTimeout() {
if (this.notificationTimeout) {
this.$timeout.cancel(this.notificationTimeout);
}

this.isNotificationTimeoutRunning = true;
this.notificationTimeout = this.$timeout(() => {
this.isNotificationTimeoutRunning = false;
}, EXTRA_TIME_BETWEEN_NOTIFICATIONS)
}

notify(task) {
if (!this.isToastOpen() && !this.isNotificationTimeoutRunning) {
this.reInitNotificationTimeout();

const msg = `You exceeded your estimated time for "${task.title}".`;
const toast = this.$mdToast.simple()
.textContent(msg)
.action('Add 1/2 hour')
.hideDelay(10000)
.position('bottom');

this.currentToastPromise = this.$mdToast.show(toast)
.then((response) => {
if (response === 'ok') {
task.timeEstimate = moment.duration(task.timeEstimate);
task.timeEstimate.add(moment.duration({ minutes: 30 }));
}
});

this.Notifier({
title: 'Time estimate exceeded!',
message: msg,
sound: true,
wait: true
});
}
}
}

angular
.module('superProductivity')
.service('EstimateExceededChecker', EstimateExceededChecker);

// hacky fix for ff
EstimateExceededChecker.$$ngIsClass = true;
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

describe('Service: EstimateExceededChecker', () => {
// load the service's module
beforeEach(module('superProductivity'));

// instantiate service
let EstimateExceededChecker;
beforeEach(inject((_EstimateExceededChecker_) => {
EstimateExceededChecker = _EstimateExceededChecker_;
}));

it('should be defined', () => {
expect(true).toBe(true);
});

});
4 changes: 3 additions & 1 deletion app-src/scripts/main/global-services/time-tracking-s.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

class TimeTracking {
/* @ngInject */
constructor($rootScope, Tasks, Dialogs, TakeABreakReminder, TRACKING_INTERVAL, IS_ELECTRON, IS_EXTENSION, EV, $interval, ExtensionInterface) {
constructor($rootScope, Tasks, Dialogs, TakeABreakReminder, TRACKING_INTERVAL, IS_ELECTRON, IS_EXTENSION, EV, $interval, ExtensionInterface, EstimateExceededChecker) {
this.$rootScope = $rootScope;
this.$interval = $interval;
this.Tasks = Tasks;
this.Dialogs = Dialogs;
this.TakeABreakReminder = TakeABreakReminder;
this.ExtensionInterface = ExtensionInterface;
this.EstimateExceededChecker = EstimateExceededChecker;
this.TRACKING_INTERVAL = TRACKING_INTERVAL;
this.IS_ELECTRON = IS_ELECTRON;
this.IS_EXTENSION = IS_EXTENSION;
Expand Down Expand Up @@ -54,6 +55,7 @@
// only track if not idle and interval is smaller than threshold
if (!this.isIdle && realPeriodDuration <= MAX_TRACKING_PERIOD_VAL) {
this.Tasks.addTimeSpent(this.$rootScope.r.currentTask, realPeriodDuration);
this.EstimateExceededChecker.checkTaskAndNotify(this.$rootScope.r.currentTask);
}

// set to now
Expand Down
5 changes: 5 additions & 0 deletions app-src/scripts/settings/misc-settings/misc-settings-d.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
Show time worked without break in work view
</md-switch>

<md-switch ng-model="vm.settings.isNotifyWhenTimeEstimateExceeded"
aria-label="Notify when time estimate was exceeded for current task">
Notify when time estimate was exceeded for current task
</md-switch>

<md-switch ng-model="vm.settings.isTakeABreakEnabled"
aria-label="Enable take a break reminder">
Enable take a break reminder
Expand Down

0 comments on commit 0f84ef8

Please sign in to comment.