Skip to content

Commit

Permalink
🔧 chore: store ids in songlist
Browse files Browse the repository at this point in the history
Signed-off-by: SimonShiki <[email protected]>
  • Loading branch information
SimonShiki committed Aug 12, 2024
1 parent c1c371c commit 9432a02
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/local_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn process_file(path: &Path) -> Option<Song> {
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,
Expand Down
31 changes: 27 additions & 4 deletions src/jotais/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,36 @@ export interface Songlist {
songs: Song<string>[];
}

interface CachedSonglist {
name: string;
songs: (string | number)[];
}

export const songlistsJotai = atom<Songlist[]>([]);
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);
});
2 changes: 1 addition & 1 deletion src/storages/ncm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9432a02

Please sign in to comment.