Skip to content

Commit 92868d0

Browse files
committed
feat(idleTimeTracking): rework idleTimeTracking
1 parent f21c365 commit 92868d0

File tree

11 files changed

+231
-159
lines changed

11 files changed

+231
-159
lines changed

app-src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
<script src="scripts/main/global-services/take-a-break-reminder-s.js"></script>
139139
<script src="scripts/main/global-services/tasks-s.js"></script>
140140
<script src="scripts/main/global-services/tasks-util-f.js"></script>
141+
<script src="scripts/main/global-services/time-tracking-s.js"></script>
141142
<script src="scripts/main/global-services/uid-s.js"></script>
142143
<script src="scripts/main/global-services/util-s.js"></script>
143144
<script src="scripts/main-header/main-header-d.js"></script>

app-src/scripts/_app.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
.run(initGlobalModels)
4040
.run(initPollJiraTaskUpdates)
4141
.run(initPollGitTaskUpdates)
42-
.run(initPollForSimpleTimeTracking)
42+
.run(initTimeTracking)
4343
.run(initMousewheelZoomForElectron)
4444
.run(initGlobalShortcuts)
4545
.run(initAutomaticSyncIfEnabled)
@@ -146,21 +146,8 @@
146146
}
147147

148148
/* @ngInject */
149-
function initPollForSimpleTimeTracking($rootScope, IS_ELECTRON, $interval, SIMPLE_TIME_TRACKING_INTERVAL, Tasks) {
150-
// if NOT in electron context
151-
if (!IS_ELECTRON) {
152-
let currentTrackingStart;
153-
$interval(() => {
154-
if ($rootScope.r.currentTask) {
155-
if (currentTrackingStart) {
156-
let now = moment();
157-
let realIdleTime = moment.duration(now.diff(currentTrackingStart)).asMilliseconds();
158-
Tasks.addTimeSpent($rootScope.r.currentTask, realIdleTime);
159-
}
160-
currentTrackingStart = moment();
161-
}
162-
}, SIMPLE_TIME_TRACKING_INTERVAL);
163-
}
149+
function initTimeTracking(TimeTracking) {
150+
TimeTracking.init();
164151
}
165152

166153
/* @ngInject */

app-src/scripts/constants.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
'data',
5151
])
5252
.constant('EV', {
53-
UPDATE_CURRENT_TASK: 'UPDATE_CURRENT_TASK'
53+
UPDATE_CURRENT_TASK: 'UPDATE_CURRENT_TASK',
54+
IS_IDLE: 'IS_IDLE',
55+
IS_BUSY: 'IS_BUSY',
5456
})
5557
.constant('EV_PROJECT_CHANGED', 'EV_PROJECT_CHANGED')
5658
.constant('LS_DEFAULTS', {
@@ -206,7 +208,7 @@
206208
.constant('WORKLOG_DATE_STR_FORMAT', 'YYYY-MM-DD')
207209
.constant('JIRA_UPDATE_POLL_INTERVAL', 60 * 1000 * 5)
208210
.constant('GIT_UPDATE_POLL_INTERVAL', 60 * 1000 * 0.25)
209-
.constant('SIMPLE_TIME_TRACKING_INTERVAL', 1000 * 5)
211+
.constant('TRACKING_INTERVAL', 1000 * 1)
210212
.constant('IS_ELECTRON', (typeof window.ipcRenderer !== 'undefined'))
211213
.constant('THEMES', [
212214
'red',

app-src/scripts/dialogs/was-idle/was-idle-c.js

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,23 @@
1414
.controller('WasIdleCtrl', WasIdleCtrl);
1515

1616
/* @ngInject */
17-
function WasIdleCtrl($mdDialog, $rootScope, $scope, Tasks, $window, idleTime, minIdleTimeInMs, theme, $filter) {
17+
function WasIdleCtrl($mdDialog, $rootScope, $scope, Tasks, $window, initialIdleTime, minIdleTimeInMs, theme, $filter, $interval) {
18+
const POLL_INTERVAL = 1000;
19+
1820
let vm = this;
1921
vm.theme = theme;
20-
const IPC_EVENT_IDLE = 'WAS_IDLE';
21-
const IPC_EVENT_UPDATE_TIME_SPEND_FOR_CURRENT = 'UPDATE_TIME_SPEND';
2222

23-
let realIdleTime = (idleTime + minIdleTimeInMs);
23+
let realIdleTime = initialIdleTime;
2424

2525
// used to display only; we add minIdleTimeInMs because that is idleTime too
2626
// even if it is tracked already
2727
vm.idleTime = $window.moment.duration(realIdleTime, 'milliseconds').format('hh:mm:ss');
2828

2929
vm.undoneTasks = Tasks.getUndoneToday(true);
30-
vm.selectedTask = $rootScope.r.currentTask || undefined;
30+
vm.selectedTask = $rootScope.r.currentTask || $rootScope.r.lastCurrentTask || undefined;
3131

3232
vm.trackIdleToTask = () => {
3333
if (vm.selectedTask) {
34-
if ($rootScope.r.currentTask) {
35-
// we need remove the possibly falsely tracked time from the previous current task
36-
Tasks.removeTimeSpent($rootScope.r.currentTask, minIdleTimeInMs);
37-
}
38-
3934
// add the idle time in milliseconds + the minIdleTime that was
4035
// not tracked or removed
4136
Tasks.addTimeSpent(vm.selectedTask, realIdleTime);
@@ -51,37 +46,23 @@
5146
};
5247

5348
vm.cancel = () => {
54-
// remove min idle time when it was tracked before
55-
if ($rootScope.r.currentTask) {
56-
Tasks.removeTimeSpent($rootScope.r.currentTask, minIdleTimeInMs);
57-
}
5849
$mdDialog.cancel();
5950
};
6051

61-
$scope.$on('$destroy', () => {
62-
window.ipcRenderer.removeListener(IPC_EVENT_UPDATE_TIME_SPEND_FOR_CURRENT, updateOnPing);
63-
window.ipcRenderer.removeListener(IPC_EVENT_IDLE, updateOnIdle);
64-
});
52+
let currentIdleStart = moment();
53+
const poll = $interval(() => {
54+
let now = moment();
6555

66-
function updateOnPing(ev, evData) {
67-
let timeSpentInMs = evData.timeSpentInMs;
68-
idleTime = idleTime + timeSpentInMs + 5000;
69-
realIdleTime = (idleTime + minIdleTimeInMs);
56+
realIdleTime += moment.duration(now.diff(currentIdleStart))
57+
.asMilliseconds();
7058
vm.idleTime = $window.moment.duration(realIdleTime, 'milliseconds').format('hh:mm:ss');
71-
$scope.$digest();
72-
}
7359

74-
function updateOnIdle(ev, params) {
75-
idleTime = idleTime + params.idleTimeInMs;
76-
realIdleTime = (idleTime + minIdleTimeInMs);
77-
vm.idleTime = $window.moment.duration(realIdleTime, 'milliseconds').format('hh:mm:ss');
78-
$scope.$digest();
79-
}
80-
81-
// add regular ping until response
82-
window.ipcRenderer.on(IPC_EVENT_UPDATE_TIME_SPEND_FOR_CURRENT, updateOnPing);
60+
// set to now
61+
currentIdleStart = moment();
62+
}, POLL_INTERVAL);
8363

84-
// add additional idle until response
85-
window.ipcRenderer.on(IPC_EVENT_IDLE, updateOnIdle);
64+
$scope.$on('$destroy', () => {
65+
$interval.cancel(poll);
66+
});
8667
}
8768
})();

app-src/scripts/main/global-services/take-a-break-reminder-s.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,43 @@
66
* Service in the superProductivity.
77
*/
88

9-
(function () {
9+
(function() {
1010
'use strict';
1111

1212
angular
1313
.module('superProductivity')
1414
.service('TakeABreakReminder', TakeABreakReminder);
1515

1616
/* @ngInject */
17-
function TakeABreakReminder($rootScope, Notifier, $mdToast, ParseDuration) {
18-
const MIN_IDLE_VAL_TO_TAKE_A_BREAK_FROM_TAKE_A_BREAK = 9999;
19-
17+
function TakeABreakReminder($rootScope, Notifier, $mdToast, ParseDuration, $timeout) {
2018
this.isShown = true;
2119

22-
this.check = (timeSpentInMs, idleTimeInMs) => {
20+
const TOAST_DISPLAY_TIME = 30000;
21+
let toast;
22+
23+
this.resetCounter = () => {
24+
this.lastCounterValBeforeReset = this.$rootScope.r.currentSession.timeWorkedWithoutBreak;
25+
$rootScope.r.currentSession.timeWorkedWithoutBreak = undefined;
26+
};
27+
28+
this.resetResetCounter = () => {
29+
$rootScope.r.currentSession.timeWorkedWithoutBreak = this.lastCounterValBeforeReset;
30+
this.lastCounterValBeforeReset = undefined;
31+
};
32+
33+
let timeoutRunning = false;
34+
this.notificationTimeout = () => {
35+
if (!timeoutRunning) {
36+
timeoutRunning = true;
37+
$timeout(() => {
38+
timeoutRunning = false;
39+
}, TOAST_DISPLAY_TIME);
40+
}
41+
42+
return timeoutRunning;
43+
};
44+
45+
this.update = (timeSpentInMs, isIdle) => {
2346
if ($rootScope.r.config && $rootScope.r.config.isTakeABreakEnabled) {
2447
if (!$rootScope.r.currentSession) {
2548
$rootScope.r.currentSession = {};
@@ -36,20 +59,21 @@
3659
if (moment.duration($rootScope.r.config.takeABreakMinWorkingTime)
3760
.asSeconds() < $rootScope.r.currentSession.timeWorkedWithoutBreak.asSeconds()) {
3861

39-
if (idleTimeInMs > MIN_IDLE_VAL_TO_TAKE_A_BREAK_FROM_TAKE_A_BREAK) {
62+
if (isIdle) {
4063
return;
4164
}
4265

43-
if (this.isShown) {
66+
if (this.isShown && !timeoutRunning) {
67+
this.notificationTimeout();
4468
const durationStr = ParseDuration.toString($rootScope.r.currentSession.timeWorkedWithoutBreak);
4569
const message = $rootScope.r.config && $rootScope.r.config.takeABreakMessage && $rootScope.r.config.takeABreakMessage.replace(/\$\{duration\}/gi, durationStr);
4670

47-
let toast = $mdToast.simple()
71+
toast = $mdToast.simple()
4872
.textContent(message)
4973
.action('I already did!')
50-
.hideDelay(20000)
74+
.hideDelay(TOAST_DISPLAY_TIME)
5175
.position('bottom');
52-
$mdToast.show(toast).then(function (response) {
76+
$mdToast.show(toast).then(function(response) {
5377
if (response === 'ok') {
5478
// re-add task on undo
5579
$rootScope.r.currentSession.timeWorkedWithoutBreak = undefined;
@@ -63,6 +87,7 @@
6387
wait: true
6488
});
6589
}
90+
6691
}
6792
}
6893
};

app-src/scripts/main/global-services/tasks-s.js

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
(function() {
1010
'use strict';
1111

12-
const IPC_EVENT_IDLE = 'WAS_IDLE';
13-
const IPC_EVENT_UPDATE_TIME_SPEND_FOR_CURRENT = 'UPDATE_TIME_SPEND';
1412
const IPC_EVENT_CURRENT_TASK_UPDATED = 'CHANGED_CURRENT_TASK';
1513
const IPC_EVENT_TASK_MARK_AS_DONE = 'TASK_MARK_AS_DONE';
1614
const IPC_EVENT_TASK_START = 'TASK_START';
@@ -35,57 +33,6 @@
3533
// SETUP HANDLERS FOR ELECTRON EVENTS
3634
if (IS_ELECTRON) {
3735
let that = this;
38-
let isIdleDialogOpen = false;
39-
40-
// handler for time spent tracking
41-
window.ipcRenderer.on(IPC_EVENT_UPDATE_TIME_SPEND_FOR_CURRENT, (ev, evData) => {
42-
if (!isIdleDialogOpen) {
43-
// only track if there is a task
44-
if (that.$rootScope.r.currentTask) {
45-
let timeSpentInMs = evData.timeSpentInMs;
46-
let idleTimeInMs = evData.idleTimeInMs;
47-
48-
TakeABreakReminder.check(timeSpentInMs, idleTimeInMs);
49-
50-
// track
51-
that.addTimeSpent(that.$rootScope.r.currentTask, timeSpentInMs);
52-
53-
// update indicator
54-
window.ipcRenderer.send(IPC_EVENT_CURRENT_TASK_UPDATED, {
55-
current: that.$rootScope.r.currentTask,
56-
lastCurrent: that.lastCurrentTask
57-
});
58-
59-
// we need to manually call apply as that is an outside event
60-
that.$rootScope.$apply();
61-
}
62-
}
63-
});
64-
65-
// handler for idle event
66-
window.ipcRenderer.on(IPC_EVENT_IDLE, (ev, params) => {
67-
const idleTime = params.idleTimeInMs;
68-
const minIdleTimeInMs = params.minIdleTimeInMs;
69-
70-
// do not show as long as the user hasn't decided
71-
TakeABreakReminder.isShown = false;
72-
73-
if (!isIdleDialogOpen) {
74-
isIdleDialogOpen = true;
75-
that.Dialogs('WAS_IDLE', { idleTime, minIdleTimeInMs })
76-
.then(() => {
77-
// if tracked
78-
TakeABreakReminder.isShown = true;
79-
isIdleDialogOpen = false;
80-
}, () => {
81-
// if not tracked
82-
// unset currentSession.timeWorkedWithoutBreak
83-
that.$rootScope.r.currentSession.timeWorkedWithoutBreak = undefined;
84-
TakeABreakReminder.isShown = true;
85-
isIdleDialogOpen = false;
86-
});
87-
}
88-
});
8936

9037
// handlers for dbus events
9138
window.ipcRenderer.on(IPC_EVENT_TASK_MARK_AS_DONE, () => {

0 commit comments

Comments
 (0)