Skip to content

Commit

Permalink
feat: 歌单排序
Browse files Browse the repository at this point in the history
  • Loading branch information
maotoumao committed May 21, 2023
1 parent 9801b15 commit 3499559
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/core/musicSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {useEffect, useState} from 'react';
import {nanoid} from 'nanoid';
import {getStorage, setStorage} from '@/utils/storage';
import {isSameMediaItem} from '@/utils/mediaItem';
import shuffle from 'lodash.shuffle';

const defaultSheet: IMusic.IMusicSheetItemBase = {
id: 'favorite',
Expand Down Expand Up @@ -250,6 +251,28 @@ function getSheetItems(): IMusic.IMusicSheetItem[] {
});
}

function sortMusicList(
type: undefined | 'a2z' | 'z2a' | 'random' | 'arta2z' | 'artz2a',
musicSheet: IMusic.IMusicSheetItem,
) {
let musicList = [...(musicSheet.musicList ?? [])];
if (type === 'a2z') {
musicList.sort((a, b) => a.title.localeCompare(b.title));
} else if (type === 'z2a') {
musicList.sort((b, a) => a.title.localeCompare(b.title));
} else if (type === 'random') {
musicList = shuffle(musicList);
} else if (type === 'arta2z') {
musicList.sort((a, b) => a.artist?.localeCompare?.(b.artist));
} else if (type === 'artz2a') {
musicList.sort((b, a) => a.artist?.localeCompare?.(b.artist));
}

updateAndSaveSheet(musicSheet.id, {
musicList: type ? musicList : undefined,
});
}

function useSheets(): IMusic.IMusicSheet;
function useSheets(sheetId: string): IMusic.IMusicSheetItem;
function useSheets(sheetId?: string) {
Expand Down Expand Up @@ -286,6 +309,7 @@ const MusicSheet = {
updateAndSaveSheet,
removeMusic,
useUserSheets,
sortMusicList,
};

export default MusicSheet;
37 changes: 37 additions & 0 deletions src/pages/sheetDetail/components/navBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,43 @@ export default function () {
});
},
},
{
icon: 'sort',
title: '排序',
onPress() {
showDialog('RadioDialog', {
content: [
{
value: 'random',
key: '随机顺序',
},
{
value: 'a2z',
key: '歌曲名A-Z',
},
{
value: 'z2a',
key: '歌曲名Z-A',
},
{
value: 'arta2z',
key: '作者名A-Z',
},
{
value: 'artz2a',
key: '作者名Z-A',
},
],
title: '排序',
async onOk(value) {
MusicSheet.sortMusicList(
value as any,
musicSheet,
);
},
});
},
},
]}
/>
);
Expand Down

0 comments on commit 3499559

Please sign in to comment.