Skip to content

Commit

Permalink
refactor: useActiveTrack hook
Browse files Browse the repository at this point in the history
  • Loading branch information
lovegaoshi committed Dec 10, 2023
1 parent 361cf16 commit 71b6325
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
21 changes: 8 additions & 13 deletions src/components/player/ThumbsUpButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { Linking, StyleSheet, View } from 'react-native';
import TrackPlayer, { Track } from 'react-native-track-player';
import { IconButton } from 'react-native-paper';
import RNSvgaPlayer from 'react-native-svga-player';

Expand All @@ -11,6 +10,7 @@ import {
} from '@utils/Bilibili/BiliOperate';
import { useNoxSetting } from '@stores/useApp';
import { logger } from '@utils/Logger';
import useActiveTrack from '@hooks/useActiveTrack';

enum THUMBUPSTATUS {
notLoggedIn = 'web-cancel',
Expand Down Expand Up @@ -41,44 +41,39 @@ interface Props {
iconSize?: number;
}
const ThumbsUpButton = ({ iconSize = 30 }: Props) => {
const { track } = useActiveTrack();
const playerStyle = useNoxSetting(state => state.playerStyle);
const currentPlayingId = useNoxSetting(state => state.currentPlayingId);
const [status, setStatus] = React.useState<THUMBUPSTATUS>(
THUMBUPSTATUS.notLoggedIn
);
const [currentTrack, setCurrentTrack] = React.useState<Track | undefined>(
undefined
);
const [svgaVisible, setSvgaVisible] = React.useState(false);

const onClick = async (triple = false) => {
if (!currentTrack?.song) return;
if (!track?.song) return;
setSvgaVisible(true);
switch (status) {
case THUMBUPSTATUS.notThumbedUp:
sendBVLike(currentTrack.song.bvid).then(res => {
sendBVLike(track.song.bvid).then(res => {
if (res.code === 0) setStatus(THUMBUPSTATUS.ThumbedUp);
});
break;
case THUMBUPSTATUS.ThumbedUp:
if (triple) {
// TODO: use that starred lottie animation
sendBVTriple(currentTrack.song.bvid).then(res => {
sendBVTriple(track.song.bvid).then(res => {
if (res.code === 0) setStatus(THUMBUPSTATUS.Tripled);
});
}
break;
default:
if (currentTrack?.song) {
go2SongURL(currentTrack?.song);
if (track?.song) {
go2SongURL(track?.song);
}
}
};

React.useEffect(() => {
const setLikedStatus = async () => {
const track = await TrackPlayer.getActiveTrack();
setCurrentTrack(track);
if (!track || !track.song) {
setStatus(THUMBUPSTATUS.notLoggedIn);
return;
Expand All @@ -94,7 +89,7 @@ const ThumbsUpButton = ({ iconSize = 30 }: Props) => {
}
};
setLikedStatus();
}, [currentPlayingId]);
}, [track]);

return (
<View>
Expand Down
2 changes: 0 additions & 2 deletions src/hooks/useActiveTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const useTrack = () => {

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

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

return { track, updateTrack };
};

Expand Down

0 comments on commit 71b6325

Please sign in to comment.