mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-09-03 06:25:34 +08:00
fix(local-music): 歌曲总数不再是历史累计值 (#742)
This commit is contained in:
@@ -108,12 +108,47 @@ export const useLocalMusicStore = defineStore(
|
||||
|
||||
/**
|
||||
* 移除文件夹路径
|
||||
* 同时清理该文件夹下、且不再属于任何已配置文件夹的缓存条目,
|
||||
* 让"歌曲总数"立即反映当前实际的本地音乐库,而不是历史累计值(#742)
|
||||
* @param path 要移除的文件夹路径
|
||||
*/
|
||||
function removeFolder(path: string): void {
|
||||
async function removeFolder(path: string): Promise<void> {
|
||||
const index = folderPaths.value.indexOf(path);
|
||||
if (index !== -1) {
|
||||
folderPaths.value.splice(index, 1);
|
||||
if (index === -1) {
|
||||
return;
|
||||
}
|
||||
folderPaths.value.splice(index, 1);
|
||||
|
||||
try {
|
||||
const localDB = await getDB();
|
||||
const entries = await localDB.getAllData(LOCAL_MUSIC_STORE);
|
||||
for (const entry of entries) {
|
||||
const stillConfigured = folderPaths.value.some((folder) =>
|
||||
isUnderFolder(entry.filePath, folder)
|
||||
);
|
||||
if (isUnderFolder(entry.filePath, path) && !stillConfigured) {
|
||||
await localDB.deleteData(LOCAL_MUSIC_STORE, entry.id);
|
||||
}
|
||||
}
|
||||
musicList.value = await localDB.getAllData(LOCAL_MUSIC_STORE);
|
||||
} catch (error) {
|
||||
console.error('移除文件夹后清理缓存失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空本地音乐缓存(没有配置任何文件夹时使用)
|
||||
*/
|
||||
async function clearAllEntries(): Promise<void> {
|
||||
try {
|
||||
const localDB = await getDB();
|
||||
const entries = await localDB.getAllData(LOCAL_MUSIC_STORE);
|
||||
for (const entry of entries) {
|
||||
await localDB.deleteData(LOCAL_MUSIC_STORE, entry.id);
|
||||
}
|
||||
musicList.value = [];
|
||||
} catch (error) {
|
||||
console.error('清空本地音乐缓存失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +157,14 @@ export const useLocalMusicStore = defineStore(
|
||||
* 流程:IPC 扫描文件 → 增量对比 → 解析变更文件元数据 → 存入 IndexedDB → 更新列表
|
||||
*/
|
||||
async function scanFolders(): Promise<void> {
|
||||
if (scanning.value || folderPaths.value.length === 0) {
|
||||
if (scanning.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 没有配置任何文件夹时,本地音乐库就应该是空的:
|
||||
// 直接清空缓存,避免"歌曲总数"永远停留在历史累计值(#742)
|
||||
if (folderPaths.value.length === 0) {
|
||||
await clearAllEntries();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -144,7 +186,7 @@ export const useLocalMusicStore = defineStore(
|
||||
// 目录枚举失败的文件夹:其下的缓存条目不参与"已删除清理",
|
||||
// 避免移动盘/网络盘暂时不可用时整个文件夹的歌曲被误删(#713)。
|
||||
// 注意只收录"枚举失败",元数据解析/写库失败不算 —— 那时磁盘文件列表已经拿全了,
|
||||
// 若一并跳过清理,删掉的歌曲会一直留在列表里(#713)
|
||||
// 若一并跳过清理,删掉的歌曲会一直留在列表里(#742)
|
||||
const unreadableFolders: string[] = [];
|
||||
|
||||
// 遍历每个文件夹进行扫描
|
||||
@@ -210,8 +252,9 @@ export const useLocalMusicStore = defineStore(
|
||||
const isUnderUnreadableFolder = (filePath: string): boolean =>
|
||||
unreadableFolders.some((folder) => isUnderFolder(filePath, folder));
|
||||
|
||||
// 4. 清理已删除文件:从 IndexedDB 移除磁盘上不存在的条目
|
||||
// (目录枚举失败的文件夹跳过清理,其文件未被枚举并不代表已删除)
|
||||
// 4. 清理:从 IndexedDB 移除磁盘上不存在的条目
|
||||
// - 目录枚举失败的文件夹跳过,其文件未被枚举并不代表已删除(#713)
|
||||
// - 不属于任何已配置文件夹的历史残留条目直接删除(#742)
|
||||
for (const [filePath, entry] of cachedMap) {
|
||||
if (diskFilePaths.has(filePath) || isUnderUnreadableFolder(filePath)) {
|
||||
continue;
|
||||
|
||||
@@ -251,11 +251,11 @@ async function handleAddFolder(): Promise<void> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除文件夹
|
||||
* 移除文件夹(同时清理该文件夹下的缓存条目,歌曲总数立即更新)
|
||||
* @param folder 要移除的文件夹路径
|
||||
*/
|
||||
function handleRemoveFolder(folder: string): void {
|
||||
localMusicStore.removeFolder(folder);
|
||||
async function handleRemoveFolder(folder: string): Promise<void> {
|
||||
await localMusicStore.removeFolder(folder);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,7 +263,8 @@ function handleRemoveFolder(folder: string): void {
|
||||
*/
|
||||
async function handleScan(): Promise<void> {
|
||||
if (localMusicStore.folderPaths.length === 0) {
|
||||
// 没有配置文件夹时,引导用户先添加文件夹
|
||||
// 没有配置文件夹:先清掉可能残留的历史条目(#742),再引导用户添加文件夹
|
||||
await localMusicStore.scanFolders();
|
||||
await handleAddFolder();
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user