diff --git a/src/settingsManager.ts b/src/settingsManager.ts index 7cbb23f..3a6e254 100644 --- a/src/settingsManager.ts +++ b/src/settingsManager.ts @@ -8,70 +8,127 @@ export const setLanguage = (selectedLanguage: string, i18n: any) => { i18n.locale = locale } -export function getDefaultSettings() { +export interface SettingsCheckbox { + name: string + selected: boolean, + type: 'checkbox' +} + +export interface SettingsOptions { + name: string + options: string[] + selected: string + type: 'option' +} + +export interface Settings { + version: string + settings: { + audioOnly: SettingsCheckbox + defaultQuality: SettingsOptions + language: SettingsOptions + chatVisible: SettingsCheckbox + streamTagsVisible: SettingsCheckbox + streamerAboutSectionVisible: SettingsCheckbox + autoplay: SettingsCheckbox + } +} + + +export function getDefaultSettings(): Settings { return { version: import.meta.env.SAFETWITCH_TAG, - audioOnly: { - name: 'audioOnly', - selected: false, - type: 'checkbox' - }, - defaultQuality: { - name: 'defaultQuality', - options: ['160p', '360p', '480p', '720p', '1080p'], - selected: '480p', - type: 'option' - }, - language: { - name: 'language', - options: languages, - selected: 'English', - type: 'option' - }, - chatVisible: { - name: 'chatVisible', - selected: false, - type: 'checkbox' - }, - streamTagsVisible: { - name: 'streamTagsVisible', - selected: true, - type: 'checkbox' - }, - streamerAboutSectionVisible: { - name: 'streamerAboutSectionVisible', - selected: true, - type: 'checkbox' - }, - autoplay: { - name: 'autoplay', - selected: false, - type: 'checkbox' + settings: { + audioOnly: { + name: 'audioOnly', + selected: false, + type: 'checkbox' + }, + defaultQuality: { + name: 'defaultQuality', + options: ['160p', '360p', '480p', '720p', '1080p'], + selected: '480p', + type: 'option' + }, + language: { + name: 'language', + options: languages, + selected: 'English', + type: 'option' + }, + chatVisible: { + name: 'chatVisible', + selected: false, + type: 'checkbox' + }, + streamTagsVisible: { + name: 'streamTagsVisible', + selected: true, + type: 'checkbox' + }, + streamerAboutSectionVisible: { + name: 'streamerAboutSectionVisible', + selected: true, + type: 'checkbox' + }, + autoplay: { + name: 'autoplay', + selected: false, + type: 'checkbox' + } } } } -export function syncUserSettings() { +/** + * Syncs older user setting to the latest version + * @param settings Settings of the Settings type + * @returns The synced settings and a boolean stating if the settings were modified + */ +export function syncUserSettings(settings: Settings): {settings: Settings, changed: boolean}{ const defaultSettings = getDefaultSettings() - const userSettings = localStorage.getItem('settings') - if (!userSettings) return - const parsedUserSettings = JSON.parse(userSettings) + let userSettings = settings - if (parsedUserSettings.version === import.meta.env.SAFETWITCH_TAG) { + // converting settings storage from versions older + // than 2.4.1 + let oldMigration = false + + + if (userSettings.version === import.meta.env.SAFETWITCH_TAG) { console.log('Settings up to date!') - return + return { settings: userSettings, changed: false } } else { console.log('Settings outdated... Migrating') + // converts v2.4.1 to 241 + let settingsVersion = Number(userSettings.version.slice(1, defaultSettings.version.length).split(".").join("")) + + if (settingsVersion < 241) { + oldMigration = true + } } + if (oldMigration) { + let oldSettings: any = userSettings + delete oldSettings.version + let migrated: Settings = { + version: defaultSettings.version, + settings: { + ...oldSettings + } + } - const synced = { ...defaultSettings, ...parsedUserSettings } + userSettings = migrated + } + console.log(userSettings) + + const synced = { ...defaultSettings, ...userSettings } // update avaliable languages - synced.language.options = defaultSettings.language.options + synced.settings.language.options = defaultSettings.settings.language.options synced.version = import.meta.env.SAFETWITCH_TAG localStorage.setItem('settings', JSON.stringify(synced)) console.log('Migrated!') + return { settings: synced, changed: true } } export function getSetting(key: string): boolean | string { diff --git a/src/views/SettingsView.vue b/src/views/SettingsView.vue index 126489d..f66ae83 100644 --- a/src/views/SettingsView.vue +++ b/src/views/SettingsView.vue @@ -1,12 +1,11 @@ @@ -69,7 +78,7 @@ export default {