Revert "feat(phone/settings): Add localStorage settings schema validation (#139)"

This reverts commit cb5a00e57e.
Reverted due a non-descript bug in CEF. Will be reapplied after fix.
This commit is contained in:
Taso committed 2021-03-05 12:38:22 -05:00
1 parent da5694863a
commit 6419245d93
10 files changed
+43 -136

No files matched your search

@@ -35,7 +35,7 @@ export const SettingsApp = () => {
const settingsApp = useApp('SETTINGS');
const [config] = useConfig();
const simcard = useSimcard();
const { settings, setSettings } = useSettings();
const [settings, setSettings] = useSettings();
const { t } = useTranslation();
// TODO: Uncomment when #135 is merged
// const { addAlert } = useSnackbar();
@@ -1,54 +0,0 @@
import config from '../../../../config/default.json';
import { PhoneSettings } from '../useSettings';
describe('useSettings', () => {
it('Creates settings and returns every needed key', () => {
const invalid = {};
const validated = new PhoneSettings(invalid, config.defaultSettings);
for (const key of Object.keys(config.defaultSettings)) {
expect(validated[key]).toBe(config.defaultSettings[key]);
}
});
it('Creates settings and adds missing key', () => {
const invalid = {};
const validated = new PhoneSettings(invalid, { ...config.defaultSettings, ringtoneVol: 2 });
expect(validated.ringtoneVol).toBe(2);
});
it('Creates settings and leave valid value as is', () => {
const valid = { ringtoneVol: 0 };
const validated = new PhoneSettings(valid, { ...config.defaultSettings, ringtoneVol: 50 });
expect(validated.ringtoneVol).toBe(0);
});
it('Creates settings and adds missing key for SettingOption', () => {
const invalid = {};
const validated = new PhoneSettings(invalid, {
...config.defaultSettings,
ringtone: { label: 'Bye', value: 'bye' },
});
expect(validated.ringtone.label).toBe('Bye');
expect(validated.ringtone.value).toBe('bye');
});
it('Creates settings and leave valid value as is for SettingOption', () => {
const valid = { ringtone: { label: 'Test', value: 'test' } };
const validated = new PhoneSettings(valid, {
...config.defaultSettings,
ringtone: { label: 'Bye', value: 'bye' },
});
expect(validated.ringtone.label).toBe('Test');
expect(validated.ringtone.value).toBe('test');
});
it('Creates settings and add default value when value typeof is not correct', () => {
const invalid = { ringtone: 'test' };
const validated = new PhoneSettings(invalid as any, {
...config.defaultSettings,
ringtone: { label: 'Bye', value: 'bye' },
});
expect(validated.ringtone.label).toBe('Bye');
expect(validated.ringtone.value).toBe('bye');
});
});
+22 -64
View File
@@ -2,6 +2,27 @@ import { atom, DefaultValue, useRecoilState } from 'recoil';
import config from '../../../config/default.json';
import { SettingOption } from '../../../ui/hooks/useContextMenu';
const localStorageEffect = (key) => ({ setSelf, onSet }) => {
const savedVal = localStorage.getItem(key);
if (savedVal != null) {
setSelf(JSON.parse(savedVal));
}
onSet((newValue) => {
if (newValue instanceof DefaultValue) {
localStorage.removeItem(key);
} else {
localStorage.setItem(key, JSON.stringify(newValue));
}
});
};
export const settingsState = atom({
key: 'settings',
default: config.defaultSettings,
effects_UNSTABLE: [localStorageEffect('npwd_settings')],
});
export interface IPhoneSettings {
wallpaper: SettingOption;
frame: SettingOption;
@@ -16,69 +37,6 @@ export interface IPhoneSettings {
TWITTER_notifyNewFeedTweet: boolean;
}
export class PhoneSettings implements IPhoneSettings {
wallpaper: SettingOption;
frame: SettingOption;
theme: SettingOption;
zoom: SettingOption;
ringtone: SettingOption;
ringtoneVol: number;
notiSound: SettingOption;
notiSoundVol: number;
TWITTER_notiSound: SettingOption;
TWITTER_notiSoundVol: number;
TWITTER_notifyNewFeedTweet: boolean;
constructor(value: Partial<IPhoneSettings>, defaultSettings: IPhoneSettings) {
return this.validate(value, defaultSettings);
}
validate(value, defaultSettings) {
Object.keys(this).map((key) => {
if (value[key] === undefined || typeof value[key] !== typeof defaultSettings[key]) {
this[key] = defaultSettings[key];
return key;
}
this[key] = value[key];
return key;
});
return this;
}
}
export const localStorageEffect = (key) => ({ setSelf, onSet }) => {
const storedSettings = localStorage.getItem(key);
if (storedSettings) {
try {
const validated = new PhoneSettings(JSON.parse(storedSettings), config.defaultSettings);
setSelf(validated);
} catch (e) {
console.error('Local storage parse error, restoring to defaults', e.message);
setSelf(config.defaultSettings);
}
}
onSet((settingValue) => {
if (settingValue instanceof DefaultValue) {
localStorage.removeItem(key);
} else {
localStorage.setItem(key, JSON.stringify(settingValue));
}
});
};
export const settingsState = atom({
key: 'settings',
default: config.defaultSettings,
effects_UNSTABLE: [localStorageEffect('npwd_settings')],
});
export const useSettings = () => {
const [settings, setSettings] = useRecoilState(settingsState);
const resetSettings = () => {
setSettings(config.defaultSettings);
};
return { settings, setSettings, resetSettings };
return useRecoilState(settingsState);
};