Import source: js/store
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
+130
@@ -0,0 +1,130 @@
|
||||
"use strict";
|
||||
|
||||
const Store = {
|
||||
KEY: "aumusic-v1",
|
||||
likes: new Set(),
|
||||
playlists: [],
|
||||
history: [],
|
||||
volume: 0.85,
|
||||
shuffle: false,
|
||||
repeat: 0,
|
||||
|
||||
load() {
|
||||
try {
|
||||
const raw = localStorage.getItem(this.KEY);
|
||||
if (!raw) return;
|
||||
const d = JSON.parse(raw);
|
||||
this.likes = new Set(d.likes || []);
|
||||
this.playlists = d.playlists || [];
|
||||
this.history = d.history || [];
|
||||
this.volume = d.volume != null ? d.volume : 0.85;
|
||||
this.shuffle = !!d.shuffle;
|
||||
this.repeat = d.repeat || 0;
|
||||
} catch (e) {}
|
||||
},
|
||||
|
||||
save() {
|
||||
try {
|
||||
localStorage.setItem(this.KEY, JSON.stringify({
|
||||
likes: Array.from(this.likes),
|
||||
playlists: this.playlists,
|
||||
history: this.history,
|
||||
volume: this.volume,
|
||||
shuffle: this.shuffle,
|
||||
repeat: this.repeat
|
||||
}));
|
||||
} catch (e) {}
|
||||
},
|
||||
|
||||
isLiked(id) {
|
||||
return this.likes.has(id);
|
||||
},
|
||||
|
||||
toggleLike(id) {
|
||||
if (this.likes.has(id)) {
|
||||
this.likes.delete(id);
|
||||
return false;
|
||||
}
|
||||
this.likes.add(id);
|
||||
return true;
|
||||
},
|
||||
|
||||
likedSongs() {
|
||||
return Array.from(this.likes).map(D.song);
|
||||
},
|
||||
|
||||
pushHistory(id) {
|
||||
this.history = [id, ...this.history.filter((x) => x !== id)].slice(0, 40);
|
||||
this.save();
|
||||
},
|
||||
|
||||
recentSongs() {
|
||||
const out = [];
|
||||
this.history.forEach((id) => {
|
||||
const s = D.song(id);
|
||||
if (s) out.push(s);
|
||||
});
|
||||
return out;
|
||||
},
|
||||
|
||||
createPlaylist(name, color) {
|
||||
const pl = {
|
||||
id: "pl" + Date.now() + Math.floor(Math.random() * 999),
|
||||
name: name || "New Playlist",
|
||||
color: color || "#fa233b",
|
||||
items: [],
|
||||
createdAt: Date.now()
|
||||
};
|
||||
this.playlists.unshift(pl);
|
||||
this.save();
|
||||
return pl;
|
||||
},
|
||||
|
||||
deletePlaylist(id) {
|
||||
this.playlists = this.playlists.filter((p) => p.id !== id);
|
||||
this.save();
|
||||
},
|
||||
|
||||
renamePlaylist(id, name) {
|
||||
const p = this.playlists.find((x) => x.id === id);
|
||||
if (p) {
|
||||
p.name = name || p.name;
|
||||
this.save();
|
||||
}
|
||||
return p;
|
||||
},
|
||||
|
||||
addToPlaylist(pid, songId) {
|
||||
const p = this.playlists.find((x) => x.id === pid);
|
||||
if (p && !p.items.includes(songId)) {
|
||||
p.items.push(songId);
|
||||
this.save();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
removeFromPlaylist(pid, songId) {
|
||||
const p = this.playlists.find((x) => x.id === pid);
|
||||
if (p) {
|
||||
p.items = p.items.filter((i) => i !== songId);
|
||||
this.save();
|
||||
}
|
||||
},
|
||||
|
||||
clearPlaylist(pid) {
|
||||
const p = this.playlists.find((x) => x.id === pid);
|
||||
if (p) {
|
||||
p.items = [];
|
||||
this.save();
|
||||
}
|
||||
},
|
||||
|
||||
playlistSongs(pid) {
|
||||
const p = this.playlists.find((x) => x.id === pid);
|
||||
if (!p) return [];
|
||||
return p.items.map(D.song).filter(Boolean);
|
||||
}
|
||||
};
|
||||
|
||||
Store.load();
|
||||
Reference in New Issue
Block a user