From c12ff2c08ca9e174c1e6538e0f9fc7b1a23151ea Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Mon, 26 Jul 2021 13:44:55 +0900 Subject: [PATCH] serach debouncing, hiding result categories --- app/components/ListItem.tsx | 4 +- app/components/NothingHere.tsx | 7 +- app/models/music.ts | 2 + app/navigation/BottomTabBar.tsx | 17 ++--- app/screens/Search.tsx | 115 ++++++++++++++++++++++++-------- app/state/music.ts | 2 +- package.json | 2 + yarn.lock | 12 ++++ 8 files changed, 119 insertions(+), 42 deletions(-) diff --git a/app/components/ListItem.tsx b/app/components/ListItem.tsx index 28c2e6b..c8f2f0a 100644 --- a/app/components/ListItem.tsx +++ b/app/components/ListItem.tsx @@ -1,4 +1,4 @@ -import { AlbumListItem, Artist, PlaylistListItem, Song } from '@app/models/music' +import { ListableItem } from '@app/models/music' import { currentTrackAtom } from '@app/state/trackplayer' import colors from '@app/styles/colors' import font from '@app/styles/font' @@ -37,7 +37,7 @@ const TitleText = React.memo<{ }) const ListItem: React.FC<{ - item: Song | AlbumListItem | Artist | PlaylistListItem + item: ListableItem onPress?: (event: GestureResponderEvent) => void showArt?: boolean showStar?: boolean diff --git a/app/components/NothingHere.tsx b/app/components/NothingHere.tsx index 42f2274..0cf468a 100644 --- a/app/components/NothingHere.tsx +++ b/app/components/NothingHere.tsx @@ -1,17 +1,18 @@ import font from '@app/styles/font' import React from 'react' -import { Text, View, StyleSheet } from 'react-native' +import { Text, View, StyleSheet, ViewStyle } from 'react-native' import Icon from 'react-native-vector-icons/MaterialCommunityIcons' const NothingHere = React.memo<{ height?: number width?: number -}>(({ height, width }) => { + style?: ViewStyle +}>(({ height, width, style }) => { height = height || 200 width = width || 200 return ( - + Nothing here... diff --git a/app/models/music.ts b/app/models/music.ts index 26946dd..609971c 100644 --- a/app/models/music.ts +++ b/app/models/music.ts @@ -67,6 +67,8 @@ export interface Song { coverArt?: string } +export type ListableItem = Song | AlbumListItem | Artist | PlaylistListItem + export type DownloadedSong = { id: string type: 'song' diff --git a/app/navigation/BottomTabBar.tsx b/app/navigation/BottomTabBar.tsx index bf4e3f3..9af3d63 100644 --- a/app/navigation/BottomTabBar.tsx +++ b/app/navigation/BottomTabBar.tsx @@ -1,12 +1,14 @@ -import React from 'react' -import { Text, View, StyleSheet } from 'react-native' -import { BottomTabBarProps } from '@react-navigation/bottom-tabs' -import colors from '@app/styles/colors' -import FastImage from 'react-native-fast-image' import NowPlayingBar from '@app/components/NowPlayingBar' import PressableOpacity from '@app/components/PressableOpacity' -import font from '@app/styles/font' +import colors from '@app/styles/colors' import dimensions from '@app/styles/dimensions' +import font from '@app/styles/font' +import { BottomTabBarProps } from '@react-navigation/bottom-tabs' +import { BottomTabNavigationEventMap } from '@react-navigation/bottom-tabs/lib/typescript/src/types' +import { NavigationHelpers, ParamListBase } from '@react-navigation/native' +import React from 'react' +import { StyleSheet, Text, View } from 'react-native' +import FastImage from 'react-native-fast-image' type TabButtonImage = { regular: number @@ -38,7 +40,7 @@ const BottomTabButton = React.memo<{ name: string isFocused: boolean img: TabButtonImage - navigation: any + navigation: NavigationHelpers }>(({ routeKey, label, name, isFocused, img, navigation }) => { const onPress = () => { const event = navigation.emit({ @@ -53,7 +55,6 @@ const BottomTabButton = React.memo<{ } return ( - // (({ name, items }) => { + if (items.length === 0) { + return <> + } + + return ( + <> +
{name}
+ {items.map(a => ( + + ))} + + ) +}) + +const Results = React.memo<{ + results: SearchResults +}>(({ results }) => { + return ( + <> + + + + + ) +}) const Search = () => { const [text, setText] = useState('') const updateSearch = useUpdateSearchResults() + const updating = useAtomValue(searchResultsUpdatingAtom) const results = useAtomValue(searchResultsAtom) - const onSubmitEditing = () => { - console.log(text) - updateSearch(text) - } + // eslint-disable-next-line react-hooks/exhaustive-deps + const debouncedonUpdateSearch = useMemo(() => debounce(updateSearch, 400), []) + + const onChangeText = useCallback( + (value: string) => { + setText(value) + debouncedonUpdateSearch(value) + }, + [setText, debouncedonUpdateSearch], + ) + + const resultsCount = results.albums.length + results.artists.length + results.songs.length return ( - -
Artists
- {results.artists.map(a => ( - - ))} -
Albums
- {results.albums.map(a => ( - - ))} -
Songs
- {results.songs.map(a => ( - - ))} + + + + + {resultsCount > 0 ? : }
) @@ -56,14 +101,28 @@ const styles = StyleSheet.create({ }, content: { paddingHorizontal: 20, + alignItems: 'stretch', + }, + inputBar: { + justifyContent: 'center', + }, + activity: { + position: 'absolute', + right: 16, + bottom: 15, }, textInput: { + width: '100%', backgroundColor: '#515151', fontFamily: font.regular, fontSize: 18, color: colors.text.primary, marginTop: 20, paddingHorizontal: 12, + paddingRight: 46, + }, + noResults: { + width: '100%', }, itemText: { color: colors.text.primary, diff --git a/app/state/music.ts b/app/state/music.ts index 4bff476..47b4067 100644 --- a/app/state/music.ts +++ b/app/state/music.ts @@ -114,7 +114,7 @@ export const useUpdateSearchResults = () => { } return async (query: string) => { - if (updating) { + if (updating || query.length < 2) { return } setUpdating(true) diff --git a/package.json b/package.json index f1325ca..25761bd 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@react-navigation/native": "^5.9.4", "fast-deep-equal": "^3.1.3", "jotai": "^1.1.0", + "lodash.debounce": "^4.0.8", "md5": "^2.3.0", "react": "17.0.1", "react-native": "0.64.1", @@ -41,6 +42,7 @@ "@babel/runtime": "^7.12.5", "@react-native-community/eslint-config": "^2.0.0", "@types/jest": "^26.0.23", + "@types/lodash.debounce": "^4.0.6", "@types/md5": "^2.3.0", "@types/react-native": "^0.64.5", "@types/react-native-vector-icons": "^6.4.7", diff --git a/yarn.lock b/yarn.lock index 2020d76..ee506ba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1293,6 +1293,18 @@ resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad" integrity sha512-cxWFQVseBm6O9Gbw1IWb8r6OS4OhSt3hPZLkFApLjM8TEXROBuQGLAH2i2gZpcXdLBIrpXuTDhH7Vbm1iXmNGA== +"@types/lodash.debounce@^4.0.6": + version "4.0.6" + resolved "https://registry.yarnpkg.com/@types/lodash.debounce/-/lodash.debounce-4.0.6.tgz#c5a2326cd3efc46566c47e4c0aa248dc0ee57d60" + integrity sha512-4WTmnnhCfDvvuLMaF3KV4Qfki93KebocUF45msxhYyjMttZDQYzHkO639ohhk8+oco2cluAFL3t5+Jn4mleylQ== + dependencies: + "@types/lodash" "*" + +"@types/lodash@*": + version "4.14.171" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.171.tgz#f01b3a5fe3499e34b622c362a46a609fdb23573b" + integrity sha512-7eQ2xYLLI/LsicL2nejW9Wyko3lcpN6O/z0ZLHrEQsg280zIdCv1t/0m6UtBjUHokCGBQ3gYTbHzDkZ1xOBwwg== + "@types/md5@^2.3.0": version "2.3.0" resolved "https://registry.yarnpkg.com/@types/md5/-/md5-2.3.0.tgz#3b6a623091160f4dc75be3173e25f2110dc3fa1f"