Skip to content

Commit 2b95620

Browse files
committed
chore: 歌曲详情demo
1 parent e4a856f commit 2b95620

File tree

3 files changed

+92
-31
lines changed

3 files changed

+92
-31
lines changed

src/screens/Home/PlayList/PlayListDetail.tsx

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,35 @@ import {
88
Image,
99
Text,
1010
View,
11+
Heading,
1112
} from 'native-base';
12-
import React, { useCallback } from 'react';
13+
import React, { useCallback, useMemo } from 'react';
1314
import { TouchableOpacity } from 'react-native-gesture-handler';
1415
import AppBar from 'src/components/AppBar';
15-
import { GetPlayListDetail } from 'src/services/queries/music';
16+
import { GetPlayListDetail, GetSongDetail } from 'src/services/queries/music';
17+
import { map } from 'lodash';
1618

1719
function PlayListDetail({ route }: { route: Record<string, any> }) {
1820
const { id } = route.params;
1921

2022
const { isLoading, isFetching, data }: any = GetPlayListDetail(id);
2123

24+
const songIds: string[] = map(data?.playlist?.trackIds, 'id');
25+
26+
const {
27+
isLoading: songLoading,
28+
isFetching: songFetching,
29+
data: songData,
30+
}: any = GetSongDetail(songIds.join(','));
31+
2232
const renderItem = useCallback(
2333
({ item, index }: any) => {
2434
return (
2535
<TouchableOpacity onPress={() => {}}>
2636
<Box h="10" py="2" mt="2">
2737
<HStack>
2838
<HStack pt="2">
29-
<Text color="coolGray.600">{index}</Text>
39+
<Text color="coolGray.600">{index + 1}</Text>
3040
</HStack>
3141
<VStack mx="3">
3242
<Text>{item.name}</Text>
@@ -48,34 +58,65 @@ function PlayListDetail({ route }: { route: Record<string, any> }) {
4858
[data],
4959
);
5060

61+
const avatarUrl = useMemo(() => {
62+
const url = data?.playlist?.creator?.avatarUrl.replace('http:', 'https:');
63+
return url;
64+
}, [data?.playlist?.creator]);
65+
66+
/* const backgroundUrl = useMemo(() => {
67+
const url = data?.playlist?.creator?.backgroundUrl.replace(
68+
'http:',
69+
'https:',
70+
);
71+
return url;
72+
}, [data?.playlist?.creator]);
73+
*/
74+
5175
return (
52-
<View>
76+
<Box>
5377
<AppBar title="歌单详情" />
54-
<HStack h="200" p="4" bg="lightBlue.300">
55-
<VStack>
56-
<Image
57-
h="120"
58-
w="120"
59-
borderRadius="20"
60-
source={{
61-
uri: data?.playlist?.coverImgUrl,
62-
}}
63-
alt="cover"
64-
/>
65-
</VStack>
66-
<VStack mx="2">
67-
<Text>{data?.playlist?.name}</Text>
68-
<Text>{data?.playlist?.creator?.nickname}</Text>
69-
<Text isTruncated>{data?.playlist?.description}</Text>
78+
<HStack h="150" p="4" bg="lightBlue.300">
79+
<VStack h="120" w="120">
80+
{data?.playlist?.coverImgUrl && (
81+
<Image
82+
h="120"
83+
w="120"
84+
borderRadius="20"
85+
source={{
86+
uri: data?.playlist?.coverImgUrl,
87+
}}
88+
alt="cover"
89+
/>
90+
)}
7091
</VStack>
92+
<Box ml="3" flex="1">
93+
<Heading size="sm">{data?.playlist?.name}</Heading>
94+
<HStack mt="4">
95+
{avatarUrl && (
96+
<Image
97+
h="6"
98+
w="6"
99+
borderRadius="16"
100+
source={{
101+
uri: avatarUrl,
102+
}}
103+
alt="avatar"
104+
/>
105+
)}
106+
<Text mx="2">{data?.playlist?.creator?.nickname}</Text>
107+
</HStack>
108+
<Text mt="4" isTruncated>
109+
{data?.playlist?.description}
110+
</Text>
111+
</Box>
71112
</HStack>
72113
<FlatList
73114
p="4"
74-
data={data?.playlist?.tracks || []}
115+
data={songData?.songs || []}
75116
renderItem={renderItem}
76117
keyExtractor={(item: any) => item.id}
77118
/>
78-
</View>
119+
</Box>
79120
);
80121
}
81122

src/screens/Home/PlayList/RecommendPlaylist.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { Box, Spinner, Text, Button, ScrollView } from 'native-base';
1+
import { Box, ScrollView, Spinner } from 'native-base';
22
import React, { useCallback, useState } from 'react';
3+
import { RefreshControl } from 'react-native';
34
import { useInfiniteQuery } from 'react-query';
45
import { doGet } from 'src/services/http-client';
56
import PlayListCard from './PlayListCard';
6-
import { throttle } from 'lodash';
7-
8-
import { RefreshControl } from 'react-native';
97

108
function PlayList() {
119
const [refreshing, setRefreshing] = React.useState(false);
@@ -91,7 +89,6 @@ function PlayList() {
9189
);
9290
})
9391
)}
94-
<Text>{data?.pages.length}</Text>
9592
</Box>
9693
</ScrollView>
9794
);

src/services/queries/music.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,31 @@ export const GetTopPlayList = () => {
1919
* @returns
2020
*/
2121
export const GetPlayListDetail = (id: string) => {
22-
return useQuery(['PlayListDetail', id], async () => {
23-
const res = await doGet('/playlist/detail', { id });
24-
return res;
25-
});
22+
return useQuery(
23+
['PlayListDetail', id],
24+
async () => {
25+
const res = await doGet('/playlist/detail', { id });
26+
return res;
27+
},
28+
{
29+
enabled: !!id,
30+
},
31+
);
32+
};
33+
/**
34+
* 批量获取歌曲详情
35+
* /song/detail
36+
* @returns
37+
*/
38+
export const GetSongDetail = (ids: string) => {
39+
return useQuery(
40+
['PlayListDetail', ids],
41+
async () => {
42+
const res = await doGet('/song/detail', { ids });
43+
return res;
44+
},
45+
{
46+
enabled: !!ids,
47+
},
48+
);
2649
};

0 commit comments

Comments
 (0)