Skip to content

Commit

Permalink
Add autorefresh on startup option
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Sep 12, 2024
1 parent ae08239 commit 44a08a4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src-tauri/src/plugins/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>, // Not used yet
pub library_folders: Vec<PathBuf>,
pub library_autorefresh: bool,
pub sleepblocker: bool,
pub auto_update_checker: bool,
pub minimize_to_tray: bool,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/generated/typings/index.ts
Original file line number Diff line number Diff line change
@@ -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<string>, 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<string>, library_autorefresh: boolean, sleepblocker: boolean, auto_update_checker: boolean, minimize_to_tray: boolean, notifications: boolean, track_view_density: string, };

export type DefaultView = "Library" | "Playlists";

Expand Down
67 changes: 46 additions & 21 deletions src/stores/SettingsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
// 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<void> => {
Expand Down Expand Up @@ -48,7 +64,7 @@ async function setTracksDensity(
density: Config['track_view_density'],
): Promise<void> {
await config.set('track_view_density', density);
router.revalidate();
invalidate();
}

/**
Expand All @@ -63,7 +79,9 @@ async function checkSleepBlocker(): Promise<void> {
/**
* Check if a new release is available
*/
async function checkForUpdate(options: UpdateCheckOptions = {}): Promise<void> {
async function checkForUpdate(
options: { silentFail?: boolean } = {},
): Promise<void> {
const shouldCheck = await config.get('auto_update_checker');

if (!shouldCheck) {
Expand Down Expand Up @@ -116,17 +134,6 @@ async function checkForUpdate(options: UpdateCheckOptions = {}): Promise<void> {
}
}

/**
* Init all settings
*/
async function checkAllSettings(): Promise<void> {
await Promise.allSettled([
checkTheme(),
checkSleepBlocker(),
checkForUpdate({ silentFail: true }),
]);
}

/**
* Toggle sleep blocker
*/
Expand All @@ -136,7 +143,7 @@ async function toggleSleepBlocker(value: boolean): Promise<void> {
} else {
await invoke('plugin:sleepblocker|disable');
}
router.revalidate();
invalidate();
}

/**
Expand All @@ -146,34 +153,52 @@ async function setDefaultView(defaultView: DefaultView): Promise<void> {
await invoke('plugin:default-view|set', {
defaultView,
});
router.revalidate();
invalidate();
}

/**
* Toggle library refresh on startup
*/
async function toggleLibraryAutorefresh(value: boolean): Promise<void> {
await config.set('library_autorefresh', value);
invalidate();
}

async function checkForLibraryRefresh(): Promise<void> {
const autorefreshEnabled = await config.getInitial('library_autorefresh');

console.log('autorefresh', autorefreshEnabled);
if (autorefreshEnabled) {
useLibraryStore.getState().api.refresh();
}
}

/**
* Toggle update check on startup
*/
async function toggleAutoUpdateChecker(value: boolean): Promise<void> {
await config.set('auto_update_checker', value);
router.revalidate();
invalidate();
}

/**
* Toggle native notifications display
*/
async function toggleDisplayNotifications(value: boolean): Promise<void> {
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,
};
Expand Down
6 changes: 1 addition & 5 deletions src/views/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getCurrentWindow } from '@tauri-apps/api/window';
import { Suspense, useEffect } from 'react';
import { Outlet } from 'react-router-dom';

Expand All @@ -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 (
Expand Down
12 changes: 11 additions & 1 deletion src/views/ViewSettingsLibrary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -18,7 +20,7 @@ export default function ViewSettingsLibrary() {
<Setting.Title>Files</Setting.Title>
{config.library_folders.length === 0 && (
<Setting.Description>
There are no folders in your library
There are no folders in your library.
</Setting.Description>
)}
{config.library_folders.length > 0 && (
Expand Down Expand Up @@ -57,6 +59,14 @@ export default function ViewSettingsLibrary() {
<code>.m3u</code> files will also be imported as playlists.
</Setting.Description>
</Setting.Section>
<Setting.Section>
<CheckboxSetting
slug="library-autorefresh"
title="Automatically refresh library on startup"
value={config.library_autorefresh}
onChange={SettingsAPI.toggleLibraryAutorefresh}
/>
</Setting.Section>
<Setting.Section>
<Setting.Title>Danger zone</Setting.Title>
<Setting.Description>
Expand Down

0 comments on commit 44a08a4

Please sign in to comment.