From 44a08a49e585e27fc3084029a3ca009eefa482e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20de=20la=20Martini=C3=A8re?= Date: Thu, 12 Sep 2024 15:33:42 +0200 Subject: [PATCH] Add autorefresh on startup option --- src-tauri/src/plugins/config.rs | 4 +- src/generated/typings/index.ts | 2 +- src/stores/SettingsAPI.ts | 67 +++++++++++++++++++++---------- src/views/Root.tsx | 6 +-- src/views/ViewSettingsLibrary.tsx | 12 +++++- 5 files changed, 62 insertions(+), 29 deletions(-) diff --git a/src-tauri/src/plugins/config.rs b/src-tauri/src/plugins/config.rs index 660d1a87..0bc0144c 100644 --- a/src-tauri/src/plugins/config.rs +++ b/src-tauri/src/plugins/config.rs @@ -57,7 +57,8 @@ pub struct Config { pub default_view: DefaultView, pub library_sort_by: SortBy, pub library_sort_order: SortOrder, - pub library_folders: Vec, // Not used yet + pub library_folders: Vec, + pub library_autorefresh: bool, pub sleepblocker: bool, pub auto_update_checker: bool, pub minimize_to_tray: bool, @@ -79,6 +80,7 @@ impl Config { library_sort_by: SortBy::Artist, library_sort_order: SortOrder::Asc, library_folders: vec![], + library_autorefresh: false, sleepblocker: false, auto_update_checker: true, minimize_to_tray: false, diff --git a/src/generated/typings/index.ts b/src/generated/typings/index.ts index 507d2903..97b6e55d 100644 --- a/src/generated/typings/index.ts +++ b/src/generated/typings/index.ts @@ -1,6 +1,6 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -export type Config = { theme: string, audio_volume: number, audio_playback_rate: number | null, audio_output_device: string, audio_muted: boolean, audio_shuffle: boolean, audio_repeat: Repeat, default_view: DefaultView, library_sort_by: SortBy, library_sort_order: SortOrder, library_folders: Array, sleepblocker: boolean, auto_update_checker: boolean, minimize_to_tray: boolean, notifications: boolean, track_view_density: string, }; +export type Config = { theme: string, audio_volume: number, audio_playback_rate: number | null, audio_output_device: string, audio_muted: boolean, audio_shuffle: boolean, audio_repeat: Repeat, default_view: DefaultView, library_sort_by: SortBy, library_sort_order: SortOrder, library_folders: Array, library_autorefresh: boolean, sleepblocker: boolean, auto_update_checker: boolean, minimize_to_tray: boolean, notifications: boolean, track_view_density: string, }; export type DefaultView = "Library" | "Playlists"; diff --git a/src/stores/SettingsAPI.ts b/src/stores/SettingsAPI.ts index 6fd85b9e..701fd723 100644 --- a/src/stores/SettingsAPI.ts +++ b/src/stores/SettingsAPI.ts @@ -7,12 +7,28 @@ import config from '../lib/config'; import { getTheme } from '../lib/themes'; import { logAndNotifyError } from '../lib/utils'; +import { getCurrentWindow } from '@tauri-apps/api/window'; import { invalidate } from '../lib/query'; -import router from '../views/router'; +import useLibraryStore from './useLibraryStore'; import useToastsStore from './useToastsStore'; -interface UpdateCheckOptions { - silentFail?: boolean; +/** + * Init all settings, then show the app + */ +async function init(): Promise { + // This is non-blocking + checkForLibraryRefresh().catch(logAndNotifyError); + + // Blocking (the window should not be shown until it's done) + await Promise.allSettled([ + checkTheme(), + checkSleepBlocker(), + checkForUpdate({ silentFail: true }), + ]); + + // Show the app once everything is loaded + const currentWindow = await getCurrentWindow(); + await currentWindow.show(); } const setTheme = async (themeID: string): Promise => { @@ -48,7 +64,7 @@ async function setTracksDensity( density: Config['track_view_density'], ): Promise { await config.set('track_view_density', density); - router.revalidate(); + invalidate(); } /** @@ -63,7 +79,9 @@ async function checkSleepBlocker(): Promise { /** * Check if a new release is available */ -async function checkForUpdate(options: UpdateCheckOptions = {}): Promise { +async function checkForUpdate( + options: { silentFail?: boolean } = {}, +): Promise { const shouldCheck = await config.get('auto_update_checker'); if (!shouldCheck) { @@ -116,17 +134,6 @@ async function checkForUpdate(options: UpdateCheckOptions = {}): Promise { } } -/** - * Init all settings - */ -async function checkAllSettings(): Promise { - await Promise.allSettled([ - checkTheme(), - checkSleepBlocker(), - checkForUpdate({ silentFail: true }), - ]); -} - /** * Toggle sleep blocker */ @@ -136,7 +143,7 @@ async function toggleSleepBlocker(value: boolean): Promise { } else { await invoke('plugin:sleepblocker|disable'); } - router.revalidate(); + invalidate(); } /** @@ -146,7 +153,24 @@ async function setDefaultView(defaultView: DefaultView): Promise { await invoke('plugin:default-view|set', { defaultView, }); - router.revalidate(); + invalidate(); +} + +/** + * Toggle library refresh on startup + */ +async function toggleLibraryAutorefresh(value: boolean): Promise { + await config.set('library_autorefresh', value); + invalidate(); +} + +async function checkForLibraryRefresh(): Promise { + const autorefreshEnabled = await config.getInitial('library_autorefresh'); + + console.log('autorefresh', autorefreshEnabled); + if (autorefreshEnabled) { + useLibraryStore.getState().api.refresh(); + } } /** @@ -154,7 +178,7 @@ async function setDefaultView(defaultView: DefaultView): Promise { */ async function toggleAutoUpdateChecker(value: boolean): Promise { await config.set('auto_update_checker', value); - router.revalidate(); + invalidate(); } /** @@ -162,18 +186,19 @@ async function toggleAutoUpdateChecker(value: boolean): Promise { */ async function toggleDisplayNotifications(value: boolean): Promise { await config.set('notifications', value); - router.revalidate(); + invalidate(); } // Should we use something else to harmonize between zustand and non-store APIs? const SettingsAPI = { + init, setTheme, applyThemeToUI, setTracksDensity, - checkAllSettings, checkForUpdate, toggleSleepBlocker, setDefaultView, + toggleLibraryAutorefresh, toggleAutoUpdateChecker, toggleDisplayNotifications, }; diff --git a/src/views/Root.tsx b/src/views/Root.tsx index 164eb7d4..e542f267 100644 --- a/src/views/Root.tsx +++ b/src/views/Root.tsx @@ -1,4 +1,3 @@ -import { getCurrentWindow } from '@tauri-apps/api/window'; import { Suspense, useEffect } from 'react'; import { Outlet } from 'react-router-dom'; @@ -20,10 +19,7 @@ import type { LoaderData } from './router'; export default function ViewRoot() { useEffect(() => { - SettingsAPI.checkAllSettings() - // Show the app once everything is loaded - .then(() => getCurrentWindow()) - .then((window) => window.show()); + SettingsAPI.init(); }, []); return ( diff --git a/src/views/ViewSettingsLibrary.tsx b/src/views/ViewSettingsLibrary.tsx index 263da08d..6f9760e5 100644 --- a/src/views/ViewSettingsLibrary.tsx +++ b/src/views/ViewSettingsLibrary.tsx @@ -5,6 +5,8 @@ import Flexbox from '../elements/Flexbox/Flexbox'; import useLibraryStore, { useLibraryAPI } from '../stores/useLibraryStore'; import type { SettingsLoaderData } from './ViewSettings'; +import CheckboxSetting from '../components/SettingCheckbox/SettingCheckbox'; +import SettingsAPI from '../stores/SettingsAPI'; import styles from './ViewSettingsLibrary.module.css'; export default function ViewSettingsLibrary() { @@ -18,7 +20,7 @@ export default function ViewSettingsLibrary() { Files {config.library_folders.length === 0 && ( - There are no folders in your library + There are no folders in your library. )} {config.library_folders.length > 0 && ( @@ -57,6 +59,14 @@ export default function ViewSettingsLibrary() { .m3u files will also be imported as playlists. + + + Danger zone