Skip to content

Commit c1de5e9

Browse files
committed
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.
1 parent ed738b6 commit c1de5e9

5 files changed

Lines changed: 92 additions & 35 deletions

File tree

‎skins/base/views/molecules/MatrixToolbar.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ module.exports = React.createClass({
3232
render: function() {
3333
return (
3434
<div className="mx_MatrixToolbar">
35-
<LogoutButton />
36-
<EnableNotificationsButton />
35+
You are not receiving desktop notifications. <EnableNotificationsButton />
3736
</div>
3837
);
3938
}

‎skins/base/views/pages/MatrixChat.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var UserSettings = ComponentBroker.get('organisms/UserSettings');
2727
var Register = ComponentBroker.get('templates/Register');
2828
var CreateRoom = ComponentBroker.get('organisms/CreateRoom');
2929
var RoomDirectory = ComponentBroker.get('organisms/RoomDirectory');
30+
var Notifier = ComponentBroker.get('organisms/Notifier');
31+
var MatrixToolbar = ComponentBroker.get('molecules/MatrixToolbar');
3032

3133
var MatrixChatController = require("../../../../src/controllers/pages/MatrixChat");
3234

@@ -52,6 +54,11 @@ module.exports = React.createClass({
5254

5355
var page_element;
5456
var right_panel = "";
57+
var top_bar;
58+
59+
if (!Notifier.isEnabled()) {
60+
top_bar = <MatrixToolbar />;
61+
}
5562

5663
switch (this.state.page_type) {
5764
case this.PageTypes.RoomView:
@@ -74,6 +81,7 @@ module.exports = React.createClass({
7481

7582
return (
7683
<div className="mx_MatrixChat">
84+
{top_bar}
7785
<LeftPanel selectedRoom={this.state.currentRoom} />
7886
<div className="mx_MatrixChat_middlePanel">
7987
{page_element}

‎src/controllers/atoms/EnableNotificationsButton.js‎

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,43 @@ limitations under the License.
1515
*/
1616

1717
'use strict';
18+
var ComponentBroker = require("../../ComponentBroker");
19+
var Notifier = ComponentBroker.get('organisms/Notifier');
20+
var dis = require("../../dispatcher");
1821

1922
module.exports = {
20-
notificationsAvailable: function() {
21-
return !!global.Notification;
22-
},
2323

24-
havePermission: function() {
25-
return global.Notification.permission == 'granted';
24+
componentDidMount: function() {
25+
this.dispatcherRef = dis.register(this.onAction);
2626
},
2727

28-
enabled: function() {
29-
if (!this.havePermission()) return false;
30-
31-
if (!global.localStorage) return true;
32-
33-
var enabled = global.localStorage.getItem('notifications_enabled');
34-
if (enabled === null) return true;
35-
return enabled === 'true';
28+
componentWillUnmount: function() {
29+
dis.unregister(this.dispatcherRef);
3630
},
3731

38-
disable: function() {
39-
if (!global.localStorage) return;
40-
global.localStorage.setItem('notifications_enabled', 'false');
32+
onAction: function(payload) {
33+
if (payload.action !== "notifier_enabled") {
34+
return;
35+
}
4136
this.forceUpdate();
4237
},
4338

44-
enable: function() {
45-
if (!this.havePermission()) {
46-
var self = this;
47-
global.Notification.requestPermission(function() {
48-
self.forceUpdate();
49-
});
50-
}
51-
52-
if (!global.localStorage) return;
53-
global.localStorage.setItem('notifications_enabled', 'true');
54-
this.forceUpdate();
39+
enabled: function() {
40+
return Notifier.isEnabled();
5541
},
5642

5743
onClick: function() {
58-
if (!this.notificationsAvailable()) {
44+
var self = this;
45+
if (!Notifier.supportsDesktopNotifications()) {
5946
return;
6047
}
61-
if (!this.enabled()) {
62-
this.enable();
48+
if (!Notifier.isEnabled()) {
49+
Notifier.setEnabled(true, function() {
50+
self.forceUpdate();
51+
});
6352
} else {
64-
this.disable();
53+
Notifier.setEnabled(false);
6554
}
55+
this.forceUpdate();
6656
},
6757
};

‎src/controllers/organisms/Notifier.js‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ limitations under the License.
1717
'use strict';
1818

1919
var MatrixClientPeg = require("../../MatrixClientPeg");
20+
var dis = require("../../dispatcher");
21+
22+
/*
23+
* Dispatches:
24+
* {
25+
* action: "notifier_enabled",
26+
* value: boolean
27+
* }
28+
*/
2029

2130
module.exports = {
2231
start: function() {
@@ -30,12 +39,60 @@ module.exports = {
3039
}
3140
},
3241

42+
supportsDesktopNotifications: function() {
43+
return !!global.Notification;
44+
},
45+
46+
havePermission: function() {
47+
return global.Notification.permission == 'granted';
48+
},
49+
50+
setEnabled: function(enable, callback) {
51+
console.log("Notifier.setEnabled => %s", enable);
52+
if(enable) {
53+
if (!this.havePermission()) {
54+
var self = this;
55+
global.Notification.requestPermission(function() {
56+
if (callback) {
57+
callback();
58+
}
59+
});
60+
}
61+
62+
if (!global.localStorage) return;
63+
global.localStorage.setItem('notifications_enabled', 'true');
64+
dis.dispatch({
65+
action: "notifier_enabled",
66+
value: true
67+
});
68+
}
69+
else {
70+
if (!global.localStorage) return;
71+
global.localStorage.setItem('notifications_enabled', 'false');
72+
dis.dispatch({
73+
action: "notifier_enabled",
74+
value: false
75+
});
76+
}
77+
},
78+
79+
isEnabled: function() {
80+
if (!this.havePermission()) return false;
81+
82+
if (!global.localStorage) return true;
83+
84+
var enabled = global.localStorage.getItem('notifications_enabled');
85+
if (enabled === null) return true;
86+
return enabled === 'true';
87+
},
88+
3389
onRoomTimeline: function(ev, room, toStartOfTimeline) {
3490
if (toStartOfTimeline) return;
3591
if (ev.sender && ev.sender.userId == MatrixClientPeg.get().credentials.userId) return;
3692

37-
var enabled = global.localStorage.getItem('notifications_enabled');
38-
if (enabled === 'false') return;
93+
if (!this.isEnabled()) {
94+
return;
95+
}
3996

4097
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
4198
if (actions && actions.notify) {

‎src/controllers/pages/MatrixChat.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ module.exports = {
163163
page_type: this.PageTypes.RoomDirectory,
164164
});
165165
break;
166+
case'notifier_enabled':
167+
this.forceUpdate();
168+
break;
166169
}
167170
},
168171

0 commit comments

Comments
 (0)