mirror of
https://github.com/markuryy/shark.git
synced 2025-12-13 03:51:02 +00:00
feat(device): add device sync button
This commit is contained in:
81
src/lib/stores/deviceSync.ts
Normal file
81
src/lib/stores/deviceSync.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { LazyStore } from '@tauri-apps/plugin-store';
|
||||
import { writable, type Writable } from 'svelte/store';
|
||||
|
||||
// Device sync settings interface
|
||||
export type OverwriteMode = 'skip' | 'different' | 'always';
|
||||
|
||||
export interface DeviceSyncSettings {
|
||||
musicPath: string | null;
|
||||
playlistsPath: string | null;
|
||||
overwriteMode: OverwriteMode;
|
||||
}
|
||||
|
||||
// Initialize the store with device-sync.json
|
||||
const store = new LazyStore('device-sync.json');
|
||||
|
||||
// Default settings
|
||||
const defaultSettings: DeviceSyncSettings = {
|
||||
musicPath: null,
|
||||
playlistsPath: null,
|
||||
overwriteMode: 'different'
|
||||
};
|
||||
|
||||
// Create a writable store for reactive UI updates
|
||||
export const deviceSyncSettings: Writable<DeviceSyncSettings> = writable(defaultSettings);
|
||||
|
||||
// Load settings from store
|
||||
export async function loadDeviceSyncSettings(): Promise<void> {
|
||||
const musicPath = await store.get<string>('musicPath');
|
||||
const playlistsPath = await store.get<string>('playlistsPath');
|
||||
const overwriteMode = await store.get<OverwriteMode>('overwriteMode');
|
||||
|
||||
deviceSyncSettings.set({
|
||||
musicPath: musicPath ?? null,
|
||||
playlistsPath: playlistsPath ?? null,
|
||||
overwriteMode: overwriteMode ?? 'different'
|
||||
});
|
||||
}
|
||||
|
||||
// Save device music path setting
|
||||
export async function setMusicPath(path: string | null): Promise<void> {
|
||||
if (path) {
|
||||
await store.set('musicPath', path);
|
||||
} else {
|
||||
await store.delete('musicPath');
|
||||
}
|
||||
await store.save();
|
||||
|
||||
deviceSyncSettings.update(s => ({
|
||||
...s,
|
||||
musicPath: path
|
||||
}));
|
||||
}
|
||||
|
||||
// Save device playlists path setting
|
||||
export async function setPlaylistsPath(path: string | null): Promise<void> {
|
||||
if (path) {
|
||||
await store.set('playlistsPath', path);
|
||||
} else {
|
||||
await store.delete('playlistsPath');
|
||||
}
|
||||
await store.save();
|
||||
|
||||
deviceSyncSettings.update(s => ({
|
||||
...s,
|
||||
playlistsPath: path
|
||||
}));
|
||||
}
|
||||
|
||||
// Save overwrite mode setting
|
||||
export async function setOverwriteMode(mode: OverwriteMode): Promise<void> {
|
||||
await store.set('overwriteMode', mode);
|
||||
await store.save();
|
||||
|
||||
deviceSyncSettings.update(s => ({
|
||||
...s,
|
||||
overwriteMode: mode
|
||||
}));
|
||||
}
|
||||
|
||||
// Initialize settings on import
|
||||
loadDeviceSyncSettings();
|
||||
Reference in New Issue
Block a user