-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from ekonstantinidis/native-notifications
Native Notifications
- Loading branch information
Showing
11 changed files
with
196 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/*global jest, describe, it, expect, spyOn, beforeEach */ | ||
|
||
'use strict'; | ||
|
||
jest.dontMock('reflux'); | ||
jest.dontMock('../../stores/sound-notification.js'); | ||
jest.dontMock('../../utils/api-requests.js'); | ||
jest.dontMock('../../actions/actions.js'); | ||
|
||
describe('Tests for SoundNotificationStore', function () { | ||
|
||
var SoundNotificationStore, Actions; | ||
|
||
beforeEach(function () { | ||
|
||
// Mock Electron's window.require | ||
window.require = function () { | ||
return { | ||
sendChannel: function () { | ||
return; | ||
} | ||
}; | ||
}; | ||
|
||
// Mock localStorage | ||
window.localStorage = { | ||
item: false, | ||
getItem: function () { | ||
return this.item; | ||
}, | ||
setItem: function (item) { | ||
this.item = item; | ||
} | ||
}; | ||
|
||
// Mock Audio | ||
window.Audio = function () { | ||
return { | ||
play: function () {} | ||
}; | ||
}; | ||
|
||
// Mock Notifications | ||
window.Notification = function () { | ||
return { | ||
onClick: function () {} | ||
}; | ||
}; | ||
|
||
Actions = require('../../actions/actions.js'); | ||
SoundNotificationStore = require('../../stores/sound-notification.js'); | ||
}); | ||
|
||
it('should get a payload and check if it should play sound & show notification.', function () { | ||
|
||
spyOn(SoundNotificationStore, 'showNotification'); | ||
|
||
var payload = [{ | ||
'id': '1', | ||
'repository': { | ||
'id': 1296269, | ||
'full_name': 'octocat/Hello-World', | ||
'description': 'This your first repo!' | ||
}, | ||
'subject': { | ||
'title': 'Greetings', | ||
'url': 'https://api.github.com/repos/octokit/octokit.rb/issues/123' | ||
} | ||
}]; | ||
|
||
SoundNotificationStore.onIsNewNotification(payload); | ||
|
||
expect(SoundNotificationStore.showNotification).toHaveBeenCalled(); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
var ipc = window.require('ipc'); | ||
var Reflux = require('reflux'); | ||
var _ = require('underscore'); | ||
|
||
var Actions = require('../actions/actions'); | ||
var SettingsStore = require('../stores/settings'); | ||
|
||
var SoundNotificationStore = Reflux.createStore({ | ||
listenables: Actions, | ||
|
||
init: function () { | ||
this._previousNotifications = []; | ||
}, | ||
|
||
playSound: function () { | ||
var audio = new Audio('sounds/digi.wav'); | ||
audio.play(); | ||
}, | ||
|
||
showNotification: function (countNew, response, latestNotification) { | ||
var title = (countNew == 1 ? | ||
'Gitify - ' + latestNotification.full_name : | ||
'Gitify'); | ||
var body = (countNew == 1 ? | ||
latestNotification.subject : | ||
'You\'ve got ' + countNew + ' notifications.'); | ||
var nativeNotification = new Notification(title, { | ||
body: body | ||
}); | ||
nativeNotification.onclick = function () { | ||
ipc.sendChannel('reopen-window'); | ||
}; | ||
}, | ||
|
||
onIsNewNotification: function (response) { | ||
var self = this; | ||
var playSound = SettingsStore.getSettings().playSound; | ||
var showNotifications = SettingsStore.getSettings().showNotifications; | ||
|
||
if (!playSound && !showNotifications) { return; } | ||
|
||
// Check if notification is already in the store. | ||
var newNotifications = _.filter(response, function (obj) { | ||
return !_.contains(self._previousNotifications, obj.id); | ||
}); | ||
|
||
// Play Sound / Show Notification. | ||
if (newNotifications && newNotifications.length) { | ||
if (playSound) { | ||
self.playSound(); | ||
} | ||
if (showNotifications) { | ||
this.showNotification(newNotifications.length, response, { | ||
full_name: newNotifications[0].repository.full_name, | ||
subject: newNotifications[0].subject.title | ||
}); | ||
} | ||
} | ||
|
||
// Now Reset the previousNotifications array. | ||
self._previousNotifications = _.map(response, function (obj) { | ||
return obj.id; | ||
}); | ||
} | ||
|
||
}); | ||
|
||
module.exports = SoundNotificationStore; |