Skip to content

Notifications / Sounds #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"grunt-contrib-copy": "=0.8.0",
"grunt-contrib-less": "=1.0.1",
"grunt-contrib-watch": "=0.6.1",
"jest-cli": "=0.4.5",
"jest-cli": "=0.4.12",
"jscs": "^1.13.1",
"jshint-stylish": "=1.0.2",
"jsxhint": "=0.15.0",
Expand Down
Binary file added sounds/click.wav
Binary file not shown.
Binary file added sounds/digi.wav
Binary file not shown.
4 changes: 2 additions & 2 deletions src/js/__tests__/components/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ describe('Test for Settings Component', function () {
var instance = TestUtils.renderIntoDocument(<Settings />);

expect(instance.state.participating).toBeFalsy();
expect(instance.toggleParticipating).toBeDefined();
expect(instance.toggleSetting).toBeDefined();
expect(instance.appQuit).toBeDefined();

instance.toggleParticipating({
instance.toggleSetting('participating', {
target: {
checked: true
}
Expand Down
8 changes: 8 additions & 0 deletions src/js/__tests__/stores/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ describe('Tests for NotificationsStore', function () {
}
};

// Mock Audio
window.Audio = function (src) {
console.log('Loading Audio: ' + src);
return {
play: function () {}
};
};

Actions = require('../../actions/actions.js');
apiRequests = require('../../utils/api-requests.js');
NotificationsStore = require('../../stores/notifications.js');
Expand Down
18 changes: 14 additions & 4 deletions src/js/components/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ var SettingsStore = require('../stores/settings');

var SettingsPage = React.createClass({
getInitialState: function () {
var settings = SettingsStore.getSettings();
return {
participating: SettingsStore.getSettings().participating
participating: settings.participating,
playSound: settings.playSound
};
},

toggleParticipating: function (event) {
Actions.setSetting('participating', event.target.checked);
toggleSetting: function (key, event) {
Actions.setSetting(key, event.target.checked);
},

appQuit: function () {
Expand All @@ -29,7 +31,15 @@ var SettingsPage = React.createClass({
<div className='col-xs-4'>
<Toggle
defaultChecked={this.state.participating}
onChange={this.toggleParticipating} />
onChange={this.toggleSetting.bind(this, 'participating')} />
</div>
</div>
<div className='row'>
<div className='col-xs-8'>Play sound</div>
<div className='col-xs-4'>
<Toggle
defaultChecked={this.state.playSound}
onChange={this.toggleSetting.bind(this, 'playSound')} />
</div>
</div>
<div className='row'>
Expand Down
31 changes: 31 additions & 0 deletions src/js/stores/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var NotificationsStore = Reflux.createStore({

init: function () {
this._notifications = [];
this._previousNotifications = [];
},

updateTrayIcon: function (notifications) {
Expand All @@ -21,6 +22,35 @@ var NotificationsStore = Reflux.createStore({
}
},

isNewNotification: function (response) {
var self = this;
var playSound = SettingsStore.getSettings().playSound;

if (!playSound) { return; }

// Check if notification is already in the store.
var isNew = false;
_.map(response, function (obj) {
if (!_.contains(self._previousNotifications, obj.id)) {
isNew = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep this function pure you should use _.some (also has alias _.any)...

var isNew = _.some(response, function (obj) {
  return !_.contains(self._previousNotifications, obj.id);
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed will have a further look in cleaning it up on #57 .

}
});

// Play Sound.
if (isNew) {
if (playSound) {
var audio = new Audio('sounds/digi.wav');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would load this when the app first loads. :) Perhaps have a store for sounds / notifications.

audio.play();
}
}

// Now Reset the previousNotifications array.
self._previousNotifications = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self._previousNotifications = _.map(response, function (obj) {
  return obj.id;
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should clean the 'previousNotifications' every time it receives a new payload so that's why..

_.map(response, function (obj) {
self._previousNotifications.push(obj.id);
});
},

onGetNotifications: function () {
var self = this;
var participating = SettingsStore.getSettings().participating;
Expand All @@ -33,6 +63,7 @@ var NotificationsStore = Reflux.createStore({
// Success - Do Something.
Actions.getNotifications.completed(response.body);
self.updateTrayIcon(response.body);
self.isNewNotification(response.body);
} else {
// Error - Show messages.
Actions.getNotifications.failed(err);
Expand Down
3 changes: 2 additions & 1 deletion src/js/stores/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var SettingsStore = Reflux.createStore({

if (!settings) {
settings = {
'participating': false
'participating': false,
'playSound': true
};
}

Expand Down