Skip to content

Commit

Permalink
feat: mfsdk
Browse files Browse the repository at this point in the history
  • Loading branch information
lovegaoshi committed Dec 18, 2024
1 parent ab5af17 commit 4070a5d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 deletions.
29 changes: 7 additions & 22 deletions src/components/setting/developer/plugins/musicfree/Searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,25 @@ import { useTranslation } from 'react-i18next';
import { useNoxSetting } from '@stores/useApp';
import logger from '@utils/Logger';
import SearchBar from '@components/commonui/SearchBar';
import { getUniqObjects } from '@utils/Utils';
import { fetchMFsdk } from '@utils/mfsdk';

export default () => {
const { t } = useTranslation();
const playerStyles = useNoxSetting(state => state.playerStyles);
const setPlayerStyles = useNoxSetting(state => state.setPlayerStyles);
const loadCustomSkin = (skins: NoxTheme.Style[]) => {
// skins MUST BE an array of objects
if (!Array.isArray(skins)) {
throw new Error('requested skin URL is not an array. aborting.');
}
const uniqueSkins = getUniqObjects(
skins.filter(skin => skin.metaData).concat(playerStyles),
() => '',
);
setPlayerStyles(uniqueSkins);
};

const addMFsdks = useNoxSetting(state => state.addMFsdks);
return (
<SearchBar
defaultSearchText="https://raw.githubusercontent.com/lovegaoshi/azusa-player-mobile/master/src/components/styles/steria.json"
defaultSearchText=""
onSearch={async ({ v, setSnack }) => {
try {
const res = await fetch(v);
const searchedResult = await res.json();
loadCustomSkin(searchedResult);
addMFsdks(await fetchMFsdk(v));
} catch (e) {
logger.warn(`[SkinSearchbar] failed to search ${e}`);
logger.warn(`[mfsdk] failed to search ${v}: ${e}`);
setSnack({
snackMsg: { success: t('CustomSkin.SearchFailMsg') },
snackMsg: { success: t('MFSDK.SearchFailMsg') },
});
}
}}
placeholder={t('CustomSkin.SearchBarLabel')}
placeholder={t('MFSDK.url')}
/>
);
};
8 changes: 8 additions & 0 deletions src/components/setting/developer/plugins/musicfree/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ import { FlashList } from '@shopify/flash-list';

import { styles } from '@components/style';
import Searchbar from './Searchbar';
import { useNoxSetting } from '@stores/useApp';

const MFSettings = () => {
const MFsdks = useNoxSetting(state => state.MFsdks);
return (
<SafeAreaView style={styles.flex}>
<Searchbar />
<FlashList
data={MFsdks}
renderItem={({ item }) => (
<Text> {`${item.platform} ${item.version}`} </Text>
)}
/>
</SafeAreaView>
);
};
Expand Down
11 changes: 7 additions & 4 deletions src/stores/useMFsdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ export interface MFsdkStore {
MFsdks: MFsdk[];
setMFsdks: (mf: MFsdk[]) => void;
addMFsdks: (mf: MFsdk[]) => void;
removeMFsdk: (mf: MFsdk) => void;
rmMFsdks: (mf: MFsdk[]) => void;
}

const store: StateCreator<MFsdkStore, [], [], MFsdkStore> = (set, get) => ({
MFsdks: [],
setMFsdks: mf => set({ MFsdks: mf }),
addMFsdks: mf => {
if (mf.length === 0) return;
addMFsdks(mf.map(v => v.path));
set(s => ({
MFsdks: getUniqObjects([...mf, ...s.MFsdks], sdk => sdk.srcUrl),
}));
},
removeMFsdk: mf => {
rmMFsdks: mf => {
if (mf.length === 0) return;
const { MFsdks } = get();
rmMFsdks(MFsdks.filter(v => v.srcUrl === mf.srcUrl).map(v => v.path));
const rmUrls = mf.map(v => v.srcUrl);
rmMFsdks(MFsdks.filter(v => rmUrls.includes(v.srcUrl)).map(v => v.path));
set(s => ({
MFsdks: s.MFsdks.filter(v => v.srcUrl !== mf.srcUrl),
MFsdks: s.MFsdks.filter(v => !rmUrls.includes(v.srcUrl)),
}));
},
});
Expand Down
5 changes: 3 additions & 2 deletions src/utils/mfsdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const rmMFsdks = async (paths: string[]) => {

export const addMFsdks = async (paths: string[]) => {
const mfsdkPaths = await getMFsdk();
saveItem(StorageKeys.MFSDK_PATHS, [...mfsdkPaths, ...paths]);
saveItem(StorageKeys.MFSDK_PATHS, [...new Set([...mfsdkPaths, ...paths])]);
};

export const initMFsdk = async () => {
Expand All @@ -47,8 +47,9 @@ export const fetchMFsdk = async (url: string) => {
const sdkLocalPath = `${loadedSDK.platform}.${loadedSDK.version}.js`;
loadedSDK.path = sdkLocalPath;
writeTxtFile(sdkLocalPath, [text], mfsdkSubFolder);
return [loadedSDK];
} catch {
logger.warn(`[mfsdk] failed to fetch and parse ${url}`);
}
return;
return [];
};

0 comments on commit 4070a5d

Please sign in to comment.