1
1
/*
2
2
Copyright 2017 Travis Ralston
3
3
Copyright 2019 New Vector Ltd.
4
- Copyright 2019 The Matrix.org Foundation C.I.C.
4
+ Copyright 2019 - 2022 The Matrix.org Foundation C.I.C.
5
5
6
6
Licensed under the Apache License, Version 2.0 (the "License");
7
7
you may not use this file except in compliance with the License.
@@ -16,17 +16,17 @@ See the License for the specific language governing permissions and
16
16
limitations under the License.
17
17
*/
18
18
19
- import SettingsHandler from "./SettingsHandler" ;
20
19
import { MatrixClientPeg } from "../../MatrixClientPeg" ;
21
20
import { SettingLevel } from "../SettingLevel" ;
22
21
import { CallbackFn , WatchManager } from "../WatchManager" ;
22
+ import AbstractLocalStorageSettingsHandler from "./AbstractLocalStorageSettingsHandler" ;
23
23
24
24
/**
25
25
* Gets and sets settings at the "device" level for the current device.
26
26
* This handler does not make use of the roomId parameter. This handler
27
27
* will special-case features to support legacy settings.
28
28
*/
29
- export default class DeviceSettingsHandler extends SettingsHandler {
29
+ export default class DeviceSettingsHandler extends AbstractLocalStorageSettingsHandler {
30
30
/**
31
31
* Creates a new device settings handler
32
32
* @param {string[] } featureNames The names of known features.
@@ -43,15 +43,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
43
43
44
44
// Special case notifications
45
45
if ( settingName === "notificationsEnabled" ) {
46
- const value = localStorage . getItem ( "notifications_enabled" ) ;
46
+ const value = this . getItem ( "notifications_enabled" ) ;
47
47
if ( typeof ( value ) === "string" ) return value === "true" ;
48
48
return null ; // wrong type or otherwise not set
49
49
} else if ( settingName === "notificationBodyEnabled" ) {
50
- const value = localStorage . getItem ( "notifications_body_enabled" ) ;
50
+ const value = this . getItem ( "notifications_body_enabled" ) ;
51
51
if ( typeof ( value ) === "string" ) return value === "true" ;
52
52
return null ; // wrong type or otherwise not set
53
53
} else if ( settingName === "audioNotificationsEnabled" ) {
54
- const value = localStorage . getItem ( "audio_notifications_enabled" ) ;
54
+ const value = this . getItem ( "audio_notifications_enabled" ) ;
55
55
if ( typeof ( value ) === "string" ) return value === "true" ;
56
56
return null ; // wrong type or otherwise not set
57
57
}
@@ -68,15 +68,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
68
68
69
69
// Special case notifications
70
70
if ( settingName === "notificationsEnabled" ) {
71
- localStorage . setItem ( "notifications_enabled" , newValue ) ;
71
+ this . setItem ( "notifications_enabled" , newValue ) ;
72
72
this . watchers . notifyUpdate ( settingName , null , SettingLevel . DEVICE , newValue ) ;
73
73
return Promise . resolve ( ) ;
74
74
} else if ( settingName === "notificationBodyEnabled" ) {
75
- localStorage . setItem ( "notifications_body_enabled" , newValue ) ;
75
+ this . setItem ( "notifications_body_enabled" , newValue ) ;
76
76
this . watchers . notifyUpdate ( settingName , null , SettingLevel . DEVICE , newValue ) ;
77
77
return Promise . resolve ( ) ;
78
78
} else if ( settingName === "audioNotificationsEnabled" ) {
79
- localStorage . setItem ( "audio_notifications_enabled" , newValue ) ;
79
+ this . setItem ( "audio_notifications_enabled" , newValue ) ;
80
80
this . watchers . notifyUpdate ( settingName , null , SettingLevel . DEVICE , newValue ) ;
81
81
return Promise . resolve ( ) ;
82
82
}
@@ -87,15 +87,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
87
87
88
88
delete settings [ "useIRCLayout" ] ;
89
89
settings [ "layout" ] = newValue ;
90
- localStorage . setItem ( "mx_local_settings" , JSON . stringify ( settings ) ) ;
90
+ this . setObject ( "mx_local_settings" , settings ) ;
91
91
92
92
this . watchers . notifyUpdate ( settingName , null , SettingLevel . DEVICE , newValue ) ;
93
93
return Promise . resolve ( ) ;
94
94
}
95
95
96
96
const settings = this . getSettings ( ) || { } ;
97
97
settings [ settingName ] = newValue ;
98
- localStorage . setItem ( "mx_local_settings" , JSON . stringify ( settings ) ) ;
98
+ this . setObject ( "mx_local_settings" , settings ) ;
99
99
this . watchers . notifyUpdate ( settingName , null , SettingLevel . DEVICE , newValue ) ;
100
100
101
101
return Promise . resolve ( ) ;
@@ -105,10 +105,6 @@ export default class DeviceSettingsHandler extends SettingsHandler {
105
105
return true ; // It's their device, so they should be able to
106
106
}
107
107
108
- public isSupported ( ) : boolean {
109
- return localStorage !== undefined && localStorage !== null ;
110
- }
111
-
112
108
public watchSetting ( settingName : string , roomId : string , cb : CallbackFn ) {
113
109
this . watchers . watchSetting ( settingName , roomId , cb ) ;
114
110
}
@@ -118,9 +114,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
118
114
}
119
115
120
116
private getSettings ( ) : any { // TODO: [TS] Type return
121
- const value = localStorage . getItem ( "mx_local_settings" ) ;
122
- if ( ! value ) return null ;
123
- return JSON . parse ( value ) ;
117
+ return this . getObject ( "mx_local_settings" ) ;
124
118
}
125
119
126
120
// Note: features intentionally don't use the same key as settings to avoid conflicts
@@ -132,15 +126,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
132
126
return false ;
133
127
}
134
128
135
- const value = localStorage . getItem ( "mx_labs_feature_" + featureName ) ;
129
+ const value = this . getItem ( "mx_labs_feature_" + featureName ) ;
136
130
if ( value === "true" ) return true ;
137
131
if ( value === "false" ) return false ;
138
132
// Try to read the next config level for the feature.
139
133
return null ;
140
134
}
141
135
142
136
private writeFeature ( featureName : string , enabled : boolean | null ) {
143
- localStorage . setItem ( "mx_labs_feature_" + featureName , `${ enabled } ` ) ;
137
+ this . setItem ( "mx_labs_feature_" + featureName , `${ enabled } ` ) ;
144
138
this . watchers . notifyUpdate ( featureName , null , SettingLevel . DEVICE , enabled ) ;
145
139
}
146
140
}
0 commit comments