From 9298065934cb8bc291062d99d79b252c10434ac4 Mon Sep 17 00:00:00 2001 From: advplyr Date: Mon, 11 Dec 2023 18:11:17 -0600 Subject: [PATCH] Add:Theme option setting #916 --- components/ui/LoadingIndicator.vue | 4 ++-- pages/settings.vue | 35 ++++++++++++++++++++++++++++++ plugins/init.client.js | 7 ++++++ plugins/localStore.js | 19 ++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/components/ui/LoadingIndicator.vue b/components/ui/LoadingIndicator.vue index 9762fde7..0879d338 100644 --- a/components/ui/LoadingIndicator.vue +++ b/components/ui/LoadingIndicator.vue @@ -1,13 +1,13 @@ diff --git a/pages/settings.vue b/pages/settings.vue index f510bc04..53cd8f6b 100644 --- a/pages/settings.vue +++ b/pages/settings.vue @@ -27,6 +27,12 @@ +
+

{{ $strings.LabelTheme }}

+
+ +
+

{{ $strings.HeaderPlaybackSettings }}

@@ -165,6 +171,7 @@ export default { autoSleepTimerAutoRewindTime: 300000, // 5 minutes languageCode: 'en-us' }, + theme: 'dark', lockCurrentOrientation: false, settingInfo: { disableShakeToResetSleepTimer: { @@ -256,6 +263,18 @@ export default { languageOptionItems() { return this.$languageCodeOptions || [] }, + themeOptionItems() { + return [ + { + text: this.$strings.LabelThemeDark, + value: 'dark' + }, + { + text: this.$strings.LabelThemeLight, + value: 'light' + } + ] + }, currentJumpForwardTimeIcon() { return this.jumpForwardItems[this.currentJumpForwardTimeIndex].icon }, @@ -281,6 +300,9 @@ export default { languageOption() { return this.languageOptionItems.find((i) => i.value === this.settings.languageCode)?.text || '' }, + themeOption() { + return this.themeOptionItems.find((i) => i.value === this.theme)?.text || '' + }, sleepTimerLengthOption() { if (!this.settings.sleepTimerLength) return this.$strings.LabelEndOfChapter const minutes = Number(this.settings.sleepTimerLength) / 1000 / 60 @@ -294,6 +316,7 @@ export default { if (this.moreMenuSetting === 'shakeSensitivity') return this.shakeSensitivityItems else if (this.moreMenuSetting === 'hapticFeedback') return this.hapticFeedbackItems else if (this.moreMenuSetting === 'language') return this.languageOptionItems + else if (this.moreMenuSetting === 'theme') return this.themeOptionItems return [] } }, @@ -324,6 +347,10 @@ export default { this.moreMenuSetting = 'language' this.showMoreMenuDialog = true }, + showThemeOptions() { + this.moreMenuSetting = 'theme' + this.showMoreMenuDialog = true + }, clickMenuAction(action) { this.showMoreMenuDialog = false if (this.moreMenuSetting === 'shakeSensitivity') { @@ -335,8 +362,15 @@ export default { } else if (this.moreMenuSetting === 'language') { this.settings.languageCode = action this.saveSettings() + } else if (this.moreMenuSetting === 'theme') { + this.theme = action + this.saveTheme(action) } }, + saveTheme(theme) { + document.documentElement.dataset.theme = theme + this.$localStore.setTheme(theme) + }, autoSleepTimerTimeUpdated(val) { if (!val) return // invalid times return falsy this.saveSettings() @@ -461,6 +495,7 @@ export default { }, async init() { this.loading = true + this.theme = (await this.$localStore.getTheme()) || 'dark' this.deviceData = await this.$db.getDeviceData() this.$store.commit('setDeviceData', this.deviceData) this.setDeviceSettings() diff --git a/plugins/init.client.js b/plugins/init.client.js index 8eea07ee..09c9b34c 100644 --- a/plugins/init.client.js +++ b/plugins/init.client.js @@ -245,6 +245,13 @@ export default ({ store, app }, inject) => { const eventBus = new Vue() inject('eventBus', eventBus) + // Set theme + app.$localStore?.getTheme()?.then((theme) => { + if (theme) { + document.documentElement.dataset.theme = theme + } + }) + // iOS Only // backButton event does not work with iOS swipe navigation so use this workaround if (app.router && Capacitor.getPlatform() === 'ios') { diff --git a/plugins/localStore.js b/plugins/localStore.js index 5a9e86f2..89d8b7f2 100644 --- a/plugins/localStore.js +++ b/plugins/localStore.js @@ -141,6 +141,25 @@ class LocalStorage { return false } } + + async setTheme(theme) { + try { + await Preferences.set({ key: 'theme', value: theme }) + console.log('[LocalStorage] Set theme', theme) + } catch (error) { + console.error('[LocalStorage] Failed to set theme', error) + } + } + + async getTheme() { + try { + var obj = await Preferences.get({ key: 'theme' }) || {} + return obj.value || null + } catch (error) { + console.error('[LocalStorage] Failed to get theme', error) + return false + } + } }