Skip to content

Commit

Permalink
fix: workaround for update current metadata
Browse files Browse the repository at this point in the history
 welp it works
  • Loading branch information
lovegaoshi committed Oct 12, 2023
1 parent 81a5002 commit 5f30196
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
19 changes: 16 additions & 3 deletions src/components/player/TrackInfo/SongMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useSongOperations from '@hooks/useSongOperations';
import { addR128Gain, getR128Gain } from '@stores/appStore';
import ABSliderMenu from './ABSliderMenu';
import { songlistToTracklist } from '@utils/RNTPUtils';
import useActiveTrack from '@hooks/useActiveTrack';

enum ICONS {
SEND_TO = 'playlist-plus',
Expand Down Expand Up @@ -54,6 +55,8 @@ export default ({
const setCurrentPlayingList = useNoxSetting(
state => state.setCurrentPlayingList
);
const updateTrack = useNoxSetting(state => state.updateTrack);

const { updateSongIndex, updateSongMetadata } = useUpdatePlaylist();
const { startRadio, radioAvailable } = useSongOperations();

Expand All @@ -71,19 +74,29 @@ export default ({
};
};

const renameSong = (rename: string) => {
const renameSong = async (rename: string) => {
const currentPlaylist2 = playlists[currentPlaylist.id];
updateSongIndex(
songMenuSongIndexes[0],
{ name: rename, parsedName: rename },
currentPlaylist2
);
const index = await TrackPlayer.getActiveTrackIndex();
index !== undefined && (await TrackPlayer.updateMetadataForTrack(index, {
title: rename,
}));
updateTrack();
};

const reloadSong = async () => {
const currentPlaylist2 = playlists[currentPlaylist.id];
const metadata = updateSongMetadata(songMenuSongIndexes[0], currentPlaylist2);
// TODO: call TP.updateMetadatabyindex something something
const metadata = await updateSongMetadata(songMenuSongIndexes[0], currentPlaylist2);
const index = await TrackPlayer.getActiveTrackIndex();
index !== undefined && (await TrackPlayer.updateMetadataForTrack(index, {
title: metadata.name,
artwork: metadata.cover,
}));
updateTrack();
return metadata;
};

Expand Down
9 changes: 6 additions & 3 deletions src/components/player/View.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from 'react';
import TrackPlayer, { useActiveTrack } from 'react-native-track-player';
import TrackPlayer from 'react-native-track-player';
import {
SafeAreaView,
StatusBar,
Expand All @@ -20,6 +20,7 @@ import { initCache } from '@utils/Cache';
import { getCurrentTPQueue, initializePlaybackMode } from '@stores/playingList';
import useVersionCheck from '@hooks/useVersionCheck';
import { songlistToTracklist } from '@utils/RNTPUtils';
import useActiveTrack from '@hooks/useActiveTrack';

const { NoxAndroidAutoModule } = NativeModules;

Expand All @@ -28,9 +29,11 @@ interface Props {
}

export function Player({ navigation }: Props) {
const track = useActiveTrack();
const { track, updateTrack } = useActiveTrack();
const playerStyle = useNoxSetting(state => state.playerStyle);
// TODO: component
const setUpdateTrack = useNoxSetting(state => state.setUpdateTrack);

useEffect(() => setUpdateTrack(updateTrack), []);

return (
<SafeAreaView style={playerStyle.screenContainer}>
Expand Down
22 changes: 22 additions & 0 deletions src/hooks/useActiveTrack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import TrackPlayer, { Track, useActiveTrack } from 'react-native-track-player';

const useTrack = () => {
const activeTrack = useActiveTrack();
const [track, setTrack] = React.useState<Track | undefined>(activeTrack);

const updateTrack = async () => {
const index = await TrackPlayer.getActiveTrackIndex();
if (index === undefined) return;
const queue = await TrackPlayer.getQueue();
setTrack({ ...queue[index] });
};

React.useEffect(() => setTrack(activeTrack), [activeTrack]);

React.useEffect(() => console.log(track), [track]);

return { track, updateTrack };
};

export default useTrack;
5 changes: 5 additions & 0 deletions src/hooks/useSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ interface initializedResults {
}

interface NoxSetting {
updateTrack: () => void;
setUpdateTrack: (val: () => void) => void;
appRefresh: boolean;
setAppRefresh: () => void;
playlistSearchAutoFocus: boolean;
Expand Down Expand Up @@ -133,6 +135,9 @@ interface NoxSetting {
* as well as saving and loading states to/from asyncStorage.
*/
export const useNoxSetting = create<NoxSetting>((set, get) => ({
updateTrack: () => undefined,
setUpdateTrack: updateTrack => set({ updateTrack }),

appRefresh: false,
setAppRefresh: () => set({ appRefresh: true }),
playlistSearchAutoFocus: true,
Expand Down

0 comments on commit 5f30196

Please sign in to comment.