From e0f03ccb931fb7d66ecdf57b57d691fd5a060351 Mon Sep 17 00:00:00 2001 From: lostb1t Date: Mon, 10 Feb 2025 17:38:01 +0100 Subject: [PATCH] feat: Allow plugin override defaults (#508) --- utils/atoms/settings.ts | 94 ++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 49 deletions(-) diff --git a/utils/atoms/settings.ts b/utils/atoms/settings.ts index cf849e15..91a54969 100644 --- a/utils/atoms/settings.ts +++ b/utils/atoms/settings.ts @@ -157,53 +157,53 @@ export type StreamyfinPluginConfig = { settings: PluginLockableSettings; }; -const loadSettings = (): Settings => { - const defaultValues: Settings = { - home: null, - autoRotate: true, - forceLandscapeInVideoPlayer: false, - deviceProfile: "Expo", - mediaListCollectionIds: [], - preferedLanguage: undefined, - searchEngine: "Jellyfin", - marlinServerUrl: "", - openInVLC: false, - downloadQuality: DownloadOptions[0], - libraryOptions: { - display: "list", - cardStyle: "detailed", - imageStyle: "cover", - showTitles: true, - showStats: true, - }, - defaultAudioLanguage: null, - playDefaultAudioTrack: true, - rememberAudioSelections: true, - defaultSubtitleLanguage: null, - subtitleMode: SubtitlePlaybackMode.Default, - rememberSubtitleSelections: true, - showHomeTitles: true, - defaultVideoOrientation: ScreenOrientation.OrientationLock.DEFAULT, - forwardSkipTime: 30, - rewindSkipTime: 10, - optimizedVersionsServerUrl: null, - downloadMethod: DownloadMethod.Remux, - autoDownload: false, - showCustomMenuLinks: false, - disableHapticFeedback: false, - subtitleSize: Platform.OS === "ios" ? 60 : 100, - remuxConcurrentLimit: 1, - safeAreaInControlsEnabled: true, - jellyseerrServerUrl: undefined, - hiddenLibraries: [], - }; +const defaultValues: Settings = { + home: null, + autoRotate: true, + forceLandscapeInVideoPlayer: false, + deviceProfile: "Expo", + mediaListCollectionIds: [], + preferedLanguage: undefined, + searchEngine: "Jellyfin", + marlinServerUrl: "", + openInVLC: false, + downloadQuality: DownloadOptions[0], + libraryOptions: { + display: "list", + cardStyle: "detailed", + imageStyle: "cover", + showTitles: true, + showStats: true, + }, + defaultAudioLanguage: null, + playDefaultAudioTrack: true, + rememberAudioSelections: true, + defaultSubtitleLanguage: null, + subtitleMode: SubtitlePlaybackMode.Default, + rememberSubtitleSelections: true, + showHomeTitles: true, + defaultVideoOrientation: ScreenOrientation.OrientationLock.DEFAULT, + forwardSkipTime: 30, + rewindSkipTime: 10, + optimizedVersionsServerUrl: null, + downloadMethod: DownloadMethod.Remux, + autoDownload: false, + showCustomMenuLinks: false, + disableHapticFeedback: false, + subtitleSize: Platform.OS === "ios" ? 60 : 100, + remuxConcurrentLimit: 1, + safeAreaInControlsEnabled: true, + jellyseerrServerUrl: undefined, + hiddenLibraries: [], +}; +const loadSettings = (): Partial => { try { const jsonValue = storage.getString("settings"); const loadedValues: Partial = jsonValue != null ? JSON.parse(jsonValue) : {}; - return { ...defaultValues, ...loadedValues }; + return loadedValues; } catch (error) { console.error("Failed to load settings:", error); return defaultValues; @@ -222,7 +222,7 @@ const saveSettings = (settings: Settings) => { storage.set("settings", jsonValue); }; -export const settingsAtom = atom(null); +export const settingsAtom = atom | null>(null); export const pluginSettingsAtom = atom( storage.get(STREAMYFIN_PLUGIN_SETTINGS) ); @@ -262,7 +262,7 @@ export const useSettings = () => { const updateSettings = (update: Partial) => { if (settings) { - const newSettings = { ...settings, ...update }; + const newSettings = { ..._settings, ...update }; setSettings(newSettings); saveSettings(newSettings); @@ -271,7 +271,7 @@ export const useSettings = () => { // We do not want to save over users pre-existing settings in case admin ever removes/unlocks a setting. // If admin sets locked to false but provides a value, - // use user settings first and fallback on admin setting if required. + // use user settings first and fallback on admin setting if required. const settings: Settings = useMemo(() => { let unlockedPluginDefaults = {} as Settings; const overrideSettings = Object.entries(pluginSettings || {}).reduce( @@ -300,12 +300,8 @@ export const useSettings = () => { {} as Settings ); - // Update settings with plugin defined defaults - if (Object.keys(unlockedPluginDefaults).length > 0) { - updateSettings(unlockedPluginDefaults); - } - return { + ...defaultValues, ..._settings, ...overrideSettings, };