Factor out logic from EnableNotificationsButton(!) and reuse MatrixToolbar.

Added notification logic to Notifier; dispatch notifier_enabled when toggled
so the toolbar can be shown/hidden and the button text can be kept in sync.
Add MatrixToolbar back into MatrixChat for notification nagging.
pull/1/head
Kegan Dougal 2015-07-21 15:47:56 +01:00
parent ed738b6398
commit c1de5e9e95
5 changed files with 95 additions and 38 deletions

View File

@ -32,8 +32,7 @@ module.exports = React.createClass({
render: function() { render: function() {
return ( return (
<div className="mx_MatrixToolbar"> <div className="mx_MatrixToolbar">
<LogoutButton /> You are not receiving desktop notifications. <EnableNotificationsButton />
<EnableNotificationsButton />
</div> </div>
); );
} }

View File

@ -27,6 +27,8 @@ var UserSettings = ComponentBroker.get('organisms/UserSettings');
var Register = ComponentBroker.get('templates/Register'); var Register = ComponentBroker.get('templates/Register');
var CreateRoom = ComponentBroker.get('organisms/CreateRoom'); var CreateRoom = ComponentBroker.get('organisms/CreateRoom');
var RoomDirectory = ComponentBroker.get('organisms/RoomDirectory'); var RoomDirectory = ComponentBroker.get('organisms/RoomDirectory');
var Notifier = ComponentBroker.get('organisms/Notifier');
var MatrixToolbar = ComponentBroker.get('molecules/MatrixToolbar');
var MatrixChatController = require("../../../../src/controllers/pages/MatrixChat"); var MatrixChatController = require("../../../../src/controllers/pages/MatrixChat");
@ -52,6 +54,11 @@ module.exports = React.createClass({
var page_element; var page_element;
var right_panel = ""; var right_panel = "";
var top_bar;
if (!Notifier.isEnabled()) {
top_bar = <MatrixToolbar />;
}
switch (this.state.page_type) { switch (this.state.page_type) {
case this.PageTypes.RoomView: case this.PageTypes.RoomView:
@ -74,6 +81,7 @@ module.exports = React.createClass({
return ( return (
<div className="mx_MatrixChat"> <div className="mx_MatrixChat">
{top_bar}
<LeftPanel selectedRoom={this.state.currentRoom} /> <LeftPanel selectedRoom={this.state.currentRoom} />
<div className="mx_MatrixChat_middlePanel"> <div className="mx_MatrixChat_middlePanel">
{page_element} {page_element}

View File

@ -15,53 +15,43 @@ limitations under the License.
*/ */
'use strict'; 'use strict';
var ComponentBroker = require("../../ComponentBroker");
var Notifier = ComponentBroker.get('organisms/Notifier');
var dis = require("../../dispatcher");
module.exports = { module.exports = {
notificationsAvailable: function() {
return !!global.Notification; componentDidMount: function() {
this.dispatcherRef = dis.register(this.onAction);
}, },
havePermission: function() { componentWillUnmount: function() {
return global.Notification.permission == 'granted'; dis.unregister(this.dispatcherRef);
},
onAction: function(payload) {
if (payload.action !== "notifier_enabled") {
return;
}
this.forceUpdate();
}, },
enabled: function() { enabled: function() {
if (!this.havePermission()) return false; return Notifier.isEnabled();
if (!global.localStorage) return true;
var enabled = global.localStorage.getItem('notifications_enabled');
if (enabled === null) return true;
return enabled === 'true';
},
disable: function() {
if (!global.localStorage) return;
global.localStorage.setItem('notifications_enabled', 'false');
this.forceUpdate();
},
enable: function() {
if (!this.havePermission()) {
var self = this;
global.Notification.requestPermission(function() {
self.forceUpdate();
});
}
if (!global.localStorage) return;
global.localStorage.setItem('notifications_enabled', 'true');
this.forceUpdate();
}, },
onClick: function() { onClick: function() {
if (!this.notificationsAvailable()) { var self = this;
if (!Notifier.supportsDesktopNotifications()) {
return; return;
} }
if (!this.enabled()) { if (!Notifier.isEnabled()) {
this.enable(); Notifier.setEnabled(true, function() {
self.forceUpdate();
});
} else { } else {
this.disable(); Notifier.setEnabled(false);
} }
this.forceUpdate();
}, },
}; };

View File

@ -17,6 +17,15 @@ limitations under the License.
'use strict'; 'use strict';
var MatrixClientPeg = require("../../MatrixClientPeg"); var MatrixClientPeg = require("../../MatrixClientPeg");
var dis = require("../../dispatcher");
/*
* Dispatches:
* {
* action: "notifier_enabled",
* value: boolean
* }
*/
module.exports = { module.exports = {
start: function() { start: function() {
@ -30,12 +39,60 @@ module.exports = {
} }
}, },
supportsDesktopNotifications: function() {
return !!global.Notification;
},
havePermission: function() {
return global.Notification.permission == 'granted';
},
setEnabled: function(enable, callback) {
console.log("Notifier.setEnabled => %s", enable);
if(enable) {
if (!this.havePermission()) {
var self = this;
global.Notification.requestPermission(function() {
if (callback) {
callback();
}
});
}
if (!global.localStorage) return;
global.localStorage.setItem('notifications_enabled', 'true');
dis.dispatch({
action: "notifier_enabled",
value: true
});
}
else {
if (!global.localStorage) return;
global.localStorage.setItem('notifications_enabled', 'false');
dis.dispatch({
action: "notifier_enabled",
value: false
});
}
},
isEnabled: function() {
if (!this.havePermission()) return false;
if (!global.localStorage) return true;
var enabled = global.localStorage.getItem('notifications_enabled');
if (enabled === null) return true;
return enabled === 'true';
},
onRoomTimeline: function(ev, room, toStartOfTimeline) { onRoomTimeline: function(ev, room, toStartOfTimeline) {
if (toStartOfTimeline) return; if (toStartOfTimeline) return;
if (ev.sender && ev.sender.userId == MatrixClientPeg.get().credentials.userId) return; if (ev.sender && ev.sender.userId == MatrixClientPeg.get().credentials.userId) return;
var enabled = global.localStorage.getItem('notifications_enabled'); if (!this.isEnabled()) {
if (enabled === 'false') return; return;
}
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev); var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
if (actions && actions.notify) { if (actions && actions.notify) {

View File

@ -163,6 +163,9 @@ module.exports = {
page_type: this.PageTypes.RoomDirectory, page_type: this.PageTypes.RoomDirectory,
}); });
break; break;
case'notifier_enabled':
this.forceUpdate();
break;
} }
}, },