11/*
22Copyright 2017 Travis Ralston
33Copyright 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.
55
66Licensed under the Apache License, Version 2.0 (the "License");
77you 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
1616limitations under the License.
1717*/
1818
19- import SettingsHandler from "./SettingsHandler" ;
2019import { MatrixClientPeg } from "../../MatrixClientPeg" ;
2120import { SettingLevel } from "../SettingLevel" ;
2221import { CallbackFn , WatchManager } from "../WatchManager" ;
22+ import AbstractLocalStorageSettingsHandler from "./AbstractLocalStorageSettingsHandler" ;
2323
2424/**
2525 * Gets and sets settings at the "device" level for the current device.
2626 * This handler does not make use of the roomId parameter. This handler
2727 * will special-case features to support legacy settings.
2828 */
29- export default class DeviceSettingsHandler extends SettingsHandler {
29+ export default class DeviceSettingsHandler extends AbstractLocalStorageSettingsHandler {
3030 /**
3131 * Creates a new device settings handler
3232 * @param {string[] } featureNames The names of known features.
@@ -43,15 +43,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
4343
4444 // Special case notifications
4545 if ( settingName === "notificationsEnabled" ) {
46- const value = localStorage . getItem ( "notifications_enabled" ) ;
46+ const value = this . getItem ( "notifications_enabled" ) ;
4747 if ( typeof ( value ) === "string" ) return value === "true" ;
4848 return null ; // wrong type or otherwise not set
4949 } else if ( settingName === "notificationBodyEnabled" ) {
50- const value = localStorage . getItem ( "notifications_body_enabled" ) ;
50+ const value = this . getItem ( "notifications_body_enabled" ) ;
5151 if ( typeof ( value ) === "string" ) return value === "true" ;
5252 return null ; // wrong type or otherwise not set
5353 } else if ( settingName === "audioNotificationsEnabled" ) {
54- const value = localStorage . getItem ( "audio_notifications_enabled" ) ;
54+ const value = this . getItem ( "audio_notifications_enabled" ) ;
5555 if ( typeof ( value ) === "string" ) return value === "true" ;
5656 return null ; // wrong type or otherwise not set
5757 }
@@ -68,15 +68,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
6868
6969 // Special case notifications
7070 if ( settingName === "notificationsEnabled" ) {
71- localStorage . setItem ( "notifications_enabled" , newValue ) ;
71+ this . setItem ( "notifications_enabled" , newValue ) ;
7272 this . watchers . notifyUpdate ( settingName , null , SettingLevel . DEVICE , newValue ) ;
7373 return Promise . resolve ( ) ;
7474 } else if ( settingName === "notificationBodyEnabled" ) {
75- localStorage . setItem ( "notifications_body_enabled" , newValue ) ;
75+ this . setItem ( "notifications_body_enabled" , newValue ) ;
7676 this . watchers . notifyUpdate ( settingName , null , SettingLevel . DEVICE , newValue ) ;
7777 return Promise . resolve ( ) ;
7878 } else if ( settingName === "audioNotificationsEnabled" ) {
79- localStorage . setItem ( "audio_notifications_enabled" , newValue ) ;
79+ this . setItem ( "audio_notifications_enabled" , newValue ) ;
8080 this . watchers . notifyUpdate ( settingName , null , SettingLevel . DEVICE , newValue ) ;
8181 return Promise . resolve ( ) ;
8282 }
@@ -87,15 +87,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
8787
8888 delete settings [ "useIRCLayout" ] ;
8989 settings [ "layout" ] = newValue ;
90- localStorage . setItem ( "mx_local_settings" , JSON . stringify ( settings ) ) ;
90+ this . setObject ( "mx_local_settings" , settings ) ;
9191
9292 this . watchers . notifyUpdate ( settingName , null , SettingLevel . DEVICE , newValue ) ;
9393 return Promise . resolve ( ) ;
9494 }
9595
9696 const settings = this . getSettings ( ) || { } ;
9797 settings [ settingName ] = newValue ;
98- localStorage . setItem ( "mx_local_settings" , JSON . stringify ( settings ) ) ;
98+ this . setObject ( "mx_local_settings" , settings ) ;
9999 this . watchers . notifyUpdate ( settingName , null , SettingLevel . DEVICE , newValue ) ;
100100
101101 return Promise . resolve ( ) ;
@@ -105,10 +105,6 @@ export default class DeviceSettingsHandler extends SettingsHandler {
105105 return true ; // It's their device, so they should be able to
106106 }
107107
108- public isSupported ( ) : boolean {
109- return localStorage !== undefined && localStorage !== null ;
110- }
111-
112108 public watchSetting ( settingName : string , roomId : string , cb : CallbackFn ) {
113109 this . watchers . watchSetting ( settingName , roomId , cb ) ;
114110 }
@@ -118,9 +114,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
118114 }
119115
120116 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" ) ;
124118 }
125119
126120 // Note: features intentionally don't use the same key as settings to avoid conflicts
@@ -132,15 +126,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
132126 return false ;
133127 }
134128
135- const value = localStorage . getItem ( "mx_labs_feature_" + featureName ) ;
129+ const value = this . getItem ( "mx_labs_feature_" + featureName ) ;
136130 if ( value === "true" ) return true ;
137131 if ( value === "false" ) return false ;
138132 // Try to read the next config level for the feature.
139133 return null ;
140134 }
141135
142136 private writeFeature ( featureName : string , enabled : boolean | null ) {
143- localStorage . setItem ( "mx_labs_feature_" + featureName , `${ enabled } ` ) ;
137+ this . setItem ( "mx_labs_feature_" + featureName , `${ enabled } ` ) ;
144138 this . watchers . notifyUpdate ( featureName , null , SettingLevel . DEVICE , enabled ) ;
145139 }
146140}
0 commit comments