diff --git a/src-tauri/src/local_scanner.rs b/src-tauri/src/local_scanner.rs index f368690..be867eb 100644 --- a/src-tauri/src/local_scanner.rs +++ b/src-tauri/src/local_scanner.rs @@ -73,7 +73,7 @@ fn process_file(path: &Path) -> Option { let mtime = metadata.modified().ok()?.duration_since(std::time::UNIX_EPOCH).ok()?.as_secs(); Some(Song { - id: path.to_string_lossy().into_owned(), + id: format!("local-{name}-{album}-{artist}", album = album.clone().unwrap(), artist = artist.clone().unwrap()), name, artist, album, diff --git a/src/jotais/library.ts b/src/jotais/library.ts index 925ed74..384fe9f 100644 --- a/src/jotais/library.ts +++ b/src/jotais/library.ts @@ -83,13 +83,36 @@ export interface Songlist { songs: Song[]; } +interface CachedSonglist { + name: string; + songs: (string | number)[]; +} + export const songlistsJotai = atom([]); -backendStorage.get('songlists').then((songlists: Songlist[] | undefined) => { - if (!songlists) return; - sharedStore.set(songlistsJotai, songlists); +sharedStore.sub(libraryJotai, async () => { + const songs = sharedStore.get(libraryJotai); + if (!songs.length) return; + + const cachedSonglists: CachedSonglist[] | undefined = await backendStorage.get('songlists'); + if (!cachedSonglists) return; + const mappedSonglist : Songlist[] = []; + for (const {name, songs: ids} of cachedSonglists) { + mappedSonglist.push({ + name, + songs: ids.map(id => songs.find(song => song.id === id)).filter(song => !!song) + }); + } + sharedStore.set(songlistsJotai, mappedSonglist); }); sharedStore.sub(songlistsJotai, () => { const songlists = sharedStore.get(songlistsJotai); - backendStorage.set('songlists', songlists); + const cachedSonglists: CachedSonglist[] = []; + for (const {name, songs} of songlists) { + cachedSonglists.push({ + name, + songs: songs.map((song) => song.id) + }); + } + backendStorage.set('songlists', cachedSonglists); }); diff --git a/src/storages/ncm.ts b/src/storages/ncm.ts index e258ae2..d4ed69b 100644 --- a/src/storages/ncm.ts +++ b/src/storages/ncm.ts @@ -423,7 +423,7 @@ export class NCM implements AbstractStorage { const res = await fetch(`${this.config.api}album?id=${song.album.id}`); const { album } = await res.json(); return ({ - id: song.id, + id: `ncm-${song.id}`, name: song.name, duration: song.duration, mtime: song.album.publishTime ?? 0,