Skip to content

Commit 06093ae

Browse files
author
Emmanouil Konstantinidis
committed
Split to 'sound-notification' store & get the correct full_name & subject title
1 parent c7629cc commit 06093ae

File tree

3 files changed

+76
-46
lines changed

3 files changed

+76
-46
lines changed

src/js/actions/actions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Actions = Reflux.createActions({
55
'login': {},
66
'logout': {},
77
'getNotifications': {asyncResult: true},
8+
'isNewNotification': {},
89
'updateSearchTerm': {},
910
'clearSearchTerm': {},
1011
'setSetting': {}

src/js/stores/notifications.js

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ var NotificationsStore = Reflux.createStore({
1111

1212
init: function () {
1313
this._notifications = [];
14-
this._previousNotifications = [];
1514
},
1615

1716
updateTrayIcon: function (notifications) {
@@ -22,50 +21,6 @@ var NotificationsStore = Reflux.createStore({
2221
}
2322
},
2423

25-
isNewNotification: function (response) {
26-
var self = this;
27-
var playSound = SettingsStore.getSettings().playSound;
28-
var showNotifications = SettingsStore.getSettings().showNotifications;
29-
30-
if (!playSound && !showNotifications) { return; }
31-
32-
// Check if notification is already in the store.
33-
var countNew = 0;
34-
_.map(response, function (obj) {
35-
if (!_.contains(self._previousNotifications, obj.id)) {
36-
countNew ++;
37-
}
38-
});
39-
40-
// Play Sound.
41-
if (countNew > 0) {
42-
if (playSound) {
43-
var audio = new Audio('sounds/digi.wav');
44-
audio.play();
45-
}
46-
if (showNotifications) {
47-
var title = (countNew == 1 ?
48-
'Gitify - ' + response[0].repository.full_name :
49-
'Gitify');
50-
var body = (countNew == 1 ?
51-
response[0].subject.title :
52-
'You\'ve got ' + countNew + ' notifications.');
53-
var nativeNotification = new Notification(title, {
54-
body: body
55-
});
56-
nativeNotification.onclick = function () {
57-
ipc.sendChannel('reopen-window');
58-
};
59-
}
60-
}
61-
62-
// Now Reset the previousNotifications array.
63-
self._previousNotifications = [];
64-
_.map(response, function (obj) {
65-
self._previousNotifications.push(obj.id);
66-
});
67-
},
68-
6924
onGetNotifications: function () {
7025
var self = this;
7126
var participating = SettingsStore.getSettings().participating;
@@ -78,7 +33,7 @@ var NotificationsStore = Reflux.createStore({
7833
// Success - Do Something.
7934
Actions.getNotifications.completed(response.body);
8035
self.updateTrayIcon(response.body);
81-
self.isNewNotification(response.body);
36+
Actions.isNewNotification(response.body);
8237
} else {
8338
// Error - Show messages.
8439
Actions.getNotifications.failed(err);

src/js/stores/sound-notification.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var ipc = window.require('ipc');
2+
var Reflux = require('reflux');
3+
var _ = require('underscore');
4+
5+
var Actions = require('../actions/actions');
6+
var SettingsStore = require('../settings');
7+
8+
var SoundNotificationStore = Reflux.createStore({
9+
listenables: Actions,
10+
11+
init: function () {
12+
this._previousNotifications = [];
13+
},
14+
15+
playSound: function () {
16+
var audio = new Audio('sounds/digi.wav');
17+
audio.play();
18+
},
19+
20+
showNotification: function (countNew, response, latestNotification) {
21+
var title = (countNew == 1 ?
22+
'Gitify - ' + latestNotification.full_name :
23+
'Gitify');
24+
var body = (countNew == 1 ?
25+
latestNotification.subject :
26+
'You\'ve got ' + countNew + ' notifications.');
27+
var nativeNotification = new Notification(title, {
28+
body: body
29+
});
30+
nativeNotification.onclick = function () {
31+
ipc.sendChannel('reopen-window');
32+
};
33+
},
34+
35+
onIsNewNotification: function (response) {
36+
var self = this;
37+
var playSound = SettingsStore.getSettings().playSound;
38+
var showNotifications = SettingsStore.getSettings().showNotifications;
39+
40+
if (!playSound && !showNotifications) { return; }
41+
42+
// Check if notification is already in the store.
43+
var countNew = 0;
44+
var latestNotification = {};
45+
_.map(response, function (obj) {
46+
if (!_.contains(self._previousNotifications, obj.id)) {
47+
countNew ++;
48+
latestNotification = {
49+
full_name: obj.repository.full_name,
50+
subject: obj.subject.title
51+
};
52+
}
53+
});
54+
55+
// Play Sound / Show Notification.
56+
if (countNew > 0) {
57+
if (playSound) {
58+
self.playSound();
59+
}
60+
if (showNotifications) {
61+
self.showNotification(countNew, response, latestNotification);
62+
}
63+
}
64+
65+
// Now Reset the previousNotifications array.
66+
self._previousNotifications = [];
67+
_.map(response, function (obj) {
68+
self._previousNotifications.push(obj.id);
69+
});
70+
}
71+
72+
});
73+
74+
module.exports = SoundNotificationStore;

0 commit comments

Comments
 (0)