From 39808373cbf46288b4cd5911d322e7901222c98e Mon Sep 17 00:00:00 2001 From: dragongoose Date: Fri, 18 Aug 2023 14:40:25 -0400 Subject: [PATCH] Sync user settings with newer versions on changes --- src/settingsManager.ts | 74 +++++++++++++++++++++++--------------- src/views/SettingsView.vue | 3 +- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/src/settingsManager.ts b/src/settingsManager.ts index 6d6276d..5cf31a2 100644 --- a/src/settingsManager.ts +++ b/src/settingsManager.ts @@ -1,42 +1,60 @@ export function getDefaultSettings() { return { - version: import.meta.env.SAFETWITCH_TAG, - audioOnly: { - name: 'Audio Only', - selected: false, - type: 'checkbox' - }, - defaultQuality: { - name: 'Default Quality', - options: ['160p', '360p', '480p', '720p', '1080p'], - selected: '480p', - type: 'option' - }, - chatVisible: { - name: 'Hide Chat', - selected: false, - type: 'checkbox' - } + version: import.meta.env.SAFETWITCH_TAG, + audioOnly: { + name: 'Audio Only', + selected: false, + type: 'checkbox' + }, + defaultQuality: { + name: 'Default Quality', + options: ['160p', '360p', '480p', '720p', '1080p'], + selected: '480p', + type: 'option' + }, + chatVisible: { + name: 'Hide Chat', + selected: false, + type: 'checkbox' + } } - } - - export function getSetting(key: string): boolean | string { +} + +export function syncUserSettings() { + const defaultSettings = getDefaultSettings() + let userSettings = localStorage.getItem('settings') + if (!userSettings) return + const parsedUserSettings = JSON.parse(userSettings) + + if(parsedUserSettings.version === import.meta.env.SAFETWITCH_TAG) { + console.log('Settings up to date!') + return + } else { + console.log('Settings outdated... Migrating') + } + + + const synced = {...defaultSettings, ...parsedUserSettings} + localStorage.setItem('settings', JSON.stringify(synced)) + console.log('Migrated!') +} + +export function getSetting(key: string): boolean | string { const storage = localStorage.getItem('settings') let parsed if (!storage) { - parsed = getDefaultSettings() + parsed = getDefaultSettings() } else { - parsed = JSON.parse(storage) + parsed = JSON.parse(storage) } - + return parsed[key].selected - } - - export function chatVisible() { +} + +export function chatVisible() { const p = getSetting('chatVisible') // Flip becuase on the setting page it's // displayed as "Hide Chat", but the value // is chatVisible return !p - } - \ No newline at end of file +} diff --git a/src/views/SettingsView.vue b/src/views/SettingsView.vue index e718934..0e52f7c 100644 --- a/src/views/SettingsView.vue +++ b/src/views/SettingsView.vue @@ -1,9 +1,10 @@