@@ -105,15 +105,36 @@ interface IState {
105
105
} ;
106
106
pushers ?: IPusher [ ] ;
107
107
threepids ?: IThreepid [ ] ;
108
+
109
+ desktopNotifications : boolean ;
110
+ desktopShowBody : boolean ;
111
+ audioNotifications : boolean ;
108
112
}
109
113
110
114
export default class Notifications extends React . PureComponent < IProps , IState > {
115
+ private settingWatchers : string [ ] ;
116
+
111
117
public constructor ( props : IProps ) {
112
118
super ( props ) ;
113
119
114
120
this . state = {
115
121
phase : Phase . Loading ,
122
+ desktopNotifications : SettingsStore . getValue ( "notificationsEnabled" ) ,
123
+ desktopShowBody : SettingsStore . getValue ( "notificationBodyEnabled" ) ,
124
+ audioNotifications : SettingsStore . getValue ( "audioNotificationsEnabled" ) ,
116
125
} ;
126
+
127
+ this . settingWatchers = [
128
+ SettingsStore . watchSetting ( "notificationsEnabled" , null , ( ...[ , , , , value ] ) =>
129
+ this . setState ( { desktopNotifications : value as boolean } ) ,
130
+ ) ,
131
+ SettingsStore . watchSetting ( "notificationBodyEnabled" , null , ( ...[ , , , , value ] ) =>
132
+ this . setState ( { desktopShowBody : value as boolean } ) ,
133
+ ) ,
134
+ SettingsStore . watchSetting ( "audioNotificationsEnabled" , null , ( ...[ , , , , value ] ) =>
135
+ this . setState ( { audioNotifications : value as boolean } ) ,
136
+ ) ,
137
+ ] ;
117
138
}
118
139
119
140
private get isInhibited ( ) : boolean {
@@ -129,6 +150,10 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
129
150
this . refreshFromServer ( ) ;
130
151
}
131
152
153
+ public componentWillUnmount ( ) {
154
+ this . settingWatchers . forEach ( watcher => SettingsStore . unwatchSetting ( watcher ) ) ;
155
+ }
156
+
132
157
private async refreshFromServer ( ) {
133
158
try {
134
159
const newState = ( await Promise . all ( [
@@ -137,7 +162,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
137
162
this . refreshThreepids ( ) ,
138
163
] ) ) . reduce ( ( p , c ) => Object . assign ( c , p ) , { } ) ;
139
164
140
- this . setState ( {
165
+ this . setState < keyof Omit < IState , "desktopNotifications" | "desktopShowBody" | "audioNotifications" > > ( {
141
166
...newState ,
142
167
phase : Phase . Ready ,
143
168
} ) ;
@@ -308,17 +333,14 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
308
333
309
334
private onDesktopNotificationsChanged = async ( checked : boolean ) => {
310
335
await SettingsStore . setValue ( "notificationsEnabled" , null , SettingLevel . DEVICE , checked ) ;
311
- this . forceUpdate ( ) ; // the toggle is controlled by SettingsStore#getValue()
312
336
} ;
313
337
314
338
private onDesktopShowBodyChanged = async ( checked : boolean ) => {
315
339
await SettingsStore . setValue ( "notificationBodyEnabled" , null , SettingLevel . DEVICE , checked ) ;
316
- this . forceUpdate ( ) ; // the toggle is controlled by SettingsStore#getValue()
317
340
} ;
318
341
319
342
private onAudioNotificationsChanged = async ( checked : boolean ) => {
320
343
await SettingsStore . setValue ( "audioNotificationsEnabled" , null , SettingLevel . DEVICE , checked ) ;
321
- this . forceUpdate ( ) ; // the toggle is controlled by SettingsStore#getValue()
322
344
} ;
323
345
324
346
private onRadioChecked = async ( rule : IVectorPushRule , checkedState : VectorState ) => {
@@ -499,23 +521,23 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
499
521
500
522
< LabelledToggleSwitch
501
523
data-test-id = 'notif-setting-notificationsEnabled'
502
- value = { SettingsStore . getValue ( "notificationsEnabled" ) }
524
+ value = { this . state . desktopNotifications }
503
525
onChange = { this . onDesktopNotificationsChanged }
504
526
label = { _t ( 'Enable desktop notifications for this session' ) }
505
527
disabled = { this . state . phase === Phase . Persisting }
506
528
/>
507
529
508
530
< LabelledToggleSwitch
509
531
data-test-id = 'notif-setting-notificationBodyEnabled'
510
- value = { SettingsStore . getValue ( "notificationBodyEnabled" ) }
532
+ value = { this . state . desktopShowBody }
511
533
onChange = { this . onDesktopShowBodyChanged }
512
534
label = { _t ( 'Show message in desktop notification' ) }
513
535
disabled = { this . state . phase === Phase . Persisting }
514
536
/>
515
537
516
538
< LabelledToggleSwitch
517
539
data-test-id = 'notif-setting-audioNotificationsEnabled'
518
- value = { SettingsStore . getValue ( "audioNotificationsEnabled" ) }
540
+ value = { this . state . audioNotifications }
519
541
onChange = { this . onAudioNotificationsChanged }
520
542
label = { _t ( 'Enable audible notifications for this session' ) }
521
543
disabled = { this . state . phase === Phase . Persisting }
0 commit comments