-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
266 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
"Prefs", | ||
"reduxjs", | ||
"temurin", | ||
"tlyric" | ||
"tlyric", | ||
"userprofiles" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
import React from 'react'; | ||
import { ToastAndroid } from 'react-native'; | ||
import HapticFeedback, { HapticFeedbackTypes } from 'react-native-haptic-feedback'; | ||
import { Avatar, List } from 'react-native-paper'; | ||
import { StorageKeys } from '../App'; | ||
import { useAppDispatch } from '../hook'; | ||
import { setUser } from '../redux/slices'; | ||
import { Userprofile } from '../types/searchUsers'; | ||
|
||
export const UserItem = ({ item }: { item: Userprofile; }) => { | ||
const dispatch = useAppDispatch(); | ||
|
||
const login = () => { | ||
const user = { | ||
username: item.nickname, | ||
id: item.userId | ||
}; | ||
|
||
dispatch(setUser(user)); | ||
AsyncStorage.setItem( | ||
StorageKeys.User, JSON.stringify(user) | ||
); | ||
|
||
HapticFeedback.trigger( | ||
HapticFeedbackTypes.effectHeavyClick | ||
); | ||
ToastAndroid.show( | ||
`Logged in as ${item.nickname}`, | ||
ToastAndroid.SHORT | ||
); | ||
}; | ||
|
||
return ( | ||
<List.Item | ||
title={item.nickname} | ||
description={item.signature} | ||
onPress={login} | ||
left={(props) => ( | ||
<Avatar.Image {...props} | ||
size={40} | ||
source={{ uri: item.avatarUrl }} /> | ||
)} /> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React, { useMemo, useState } from 'react'; | ||
import { FlatList, StyleSheet, ToastAndroid } from 'react-native'; | ||
import { ActivityIndicator, List, useTheme } from 'react-native-paper'; | ||
import useSWRInfinite from 'swr/infinite'; | ||
import { UserItem } from '.'; | ||
import { useDebounce } from '../hook'; | ||
import { Main } from '../types/searchUsers'; | ||
|
||
export const UserList = ({ searchQuery }: { searchQuery: string; }) => { | ||
const appTheme = useTheme(); | ||
|
||
const [refreshing, setRefreshing] = useState(false); | ||
const { data, setSize, error, mutate } = useSWRInfinite<Main>( | ||
(index) => `https://music.163.com/api/search/get/web?s=${searchQuery}&type=1002&offset=${index * 15}&limit=15`, | ||
{ suspense: true } | ||
); | ||
|
||
const users = useMemo(() => { | ||
if (data && data?.[0].code === 200) { | ||
return data?.map((item) => item.result.userprofiles).flat(); | ||
} else { | ||
ToastAndroid.show( | ||
`Error: ${data?.[0].code} ${data?.[0].message}`, | ||
ToastAndroid.SHORT | ||
); | ||
return []; | ||
} | ||
}, [data]); | ||
|
||
const hasMore = useMemo(() => data && data[0].result && users.length !== data[0].result.userprofileCount, | ||
[data, users]); | ||
|
||
const loadMore = useDebounce(() => { | ||
if (hasMore) { | ||
setSize(prev => prev + 1); | ||
} | ||
}, 1000); | ||
|
||
if (error) { | ||
return ( | ||
<List.Item | ||
title={error.message} | ||
titleStyle={{ color: appTheme.colors.error }} | ||
description="Try to search later or with another query" /> | ||
); | ||
} | ||
|
||
return ( | ||
<FlatList | ||
data={users} | ||
renderItem={({ item }) => <UserItem item={item} />} | ||
keyExtractor={(item) => item.userId.toString()} | ||
initialNumToRender={7} | ||
onEndReachedThreshold={0.1} | ||
onEndReached={loadMore} | ||
onRefresh={async () => { | ||
setRefreshing(true); | ||
await mutate(); | ||
setRefreshing(false); | ||
}} | ||
refreshing={refreshing} | ||
ListFooterComponent={hasMore && users.length | ||
? <ActivityIndicator | ||
style={styles.footerLoading} /> : null} | ||
ListEmptyComponent={<List.Item | ||
title="No users found" | ||
description={data?.[0].message || 'Try another search query'} />} /> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
footerLoading: { | ||
marginVertical: '5%' | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React, { Suspense, useState } from 'react'; | ||
import { StatusBar, StyleSheet } from 'react-native'; | ||
import { ActivityIndicator, Searchbar } from 'react-native-paper'; | ||
import { BlurBackground, UserList } from '../components'; | ||
import { useAppSelector } from '../hook'; | ||
import { selectUser } from '../redux/slices'; | ||
|
||
export const Login = () => { | ||
const user = useAppSelector(selectUser); | ||
|
||
const [showQuery, setShowQuery] = useState(user.username); | ||
const [searchQuery, setSearchQuery] = useState(user.username); | ||
|
||
const search = () => { | ||
if (showQuery && searchQuery !== showQuery) { | ||
setSearchQuery(showQuery); | ||
} | ||
}; | ||
|
||
return ( | ||
<BlurBackground> | ||
<Searchbar | ||
style={styles.searchbar} | ||
placeholder="Search for a user" | ||
onChangeText={setShowQuery} | ||
value={showQuery} | ||
onIconPress={search} | ||
onSubmitEditing={search} | ||
selectTextOnFocus | ||
/> | ||
|
||
<Suspense fallback={ | ||
<ActivityIndicator | ||
size="large" | ||
style={styles.loading} | ||
/> | ||
}> | ||
<UserList searchQuery={searchQuery} /> | ||
</Suspense> | ||
</BlurBackground> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
searchbar: { | ||
marginTop: StatusBar.currentHeight, | ||
marginHorizontal: '2%', | ||
backgroundColor: 'transparent' | ||
}, | ||
loading: { | ||
marginTop: '20%' | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export interface Main { | ||
result: Result; | ||
code: number; | ||
message: string; | ||
} | ||
|
||
export interface Result { | ||
userprofiles: Userprofile[]; | ||
userprofileCount: number; | ||
} | ||
|
||
export interface Userprofile { | ||
defaultAvatar: boolean; | ||
province: number; | ||
authStatus: number; | ||
followed: boolean; | ||
avatarUrl: string; | ||
accountStatus: number; | ||
gender: number; | ||
city: number; | ||
birthday: number; | ||
userId: number; | ||
userType: number; | ||
nickname: string; | ||
signature: string; | ||
description: string; | ||
detailDescription: string; | ||
avatarImgId: number; | ||
backgroundImgId: number; | ||
backgroundUrl: string; | ||
authority: number; | ||
mutual: boolean; | ||
expertTags: null; | ||
experts: null; | ||
djStatus: number; | ||
vipType: number; | ||
remarkName: null; | ||
authenticationTypes: number; | ||
avatarDetail: null; | ||
avatarImgIdStr: string; | ||
backgroundImgIdStr: string; | ||
anchor: boolean; | ||
avatarImgId_str: string; | ||
followeds: number; | ||
follows: number; | ||
alg: Alg; | ||
playlistCount: number; | ||
playlistBeSubscribedCount: number; | ||
} | ||
|
||
export enum Alg { | ||
AlgUserBasic = "alg_user_basic", | ||
} |