Skip to content

Commit

Permalink
feat: Improved search on mode screens
Browse files Browse the repository at this point in the history
Searchbar now disapears when scrolling down and when searching scrolls to top of list
  • Loading branch information
TriPSs committed Apr 28, 2020
1 parent 5953cfa commit af0b971
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 37 deletions.
23 changes: 10 additions & 13 deletions app/mobile/screens/Mode/ModeScreen.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useEffect, useState } from 'react'
import React, { useEffect, useState, useRef } from 'react'
import PropTypes from 'prop-types'
import { StyleSheet, View, StatusBar, Animated, InteractionManager } from 'react-native'
import { useLazyQuery } from '@apollo/react-hooks'
import { useCollapsibleStack, CollapsibleStackSub } from 'react-navigation-collapsible'
import { useCollapsibleStack } from 'react-navigation-collapsible'
import { getDefaultHeaderHeight } from 'react-navigation-collapsible/lib/src/utils'

import i18n from 'modules/i18n'
Expand Down Expand Up @@ -37,8 +37,8 @@ const styles = StyleSheet.create({
},
})

//TODO:: The searchbar does not disapear correctly so that needs to be checked out
export const Mode = ({ mode, navigation }) => {
const flatListRef = useRef(null)
const [query, setQuery] = useState(null)
const [executeQuery, { loading, data, fetchMore }] = useLazyQuery(
mode === 'movies'
Expand All @@ -54,7 +54,7 @@ export const Mode = ({ mode, navigation }) => {
},
)

const { onScroll, containerPaddingTop, scrollIndicatorInsetTop } = useCollapsibleStack()
const { onScroll, scrollIndicatorInsetTop } = useCollapsibleStack()

useEffect(() => {
// Execute the query after the component is done navigation
Expand Down Expand Up @@ -102,6 +102,7 @@ export const Mode = ({ mode, navigation }) => {

<Animated.FlatList
removeClippedSubviews
ref={flatListRef}
data={items.length === 0 && !query
? Array(24).fill({ loading: true })
: items
Expand Down Expand Up @@ -130,18 +131,14 @@ export const Mode = ({ mode, navigation }) => {
}
onEndReachedThreshold={dimensions.CARD_HEIGHT_SMALL * 4}
onScroll={onScroll}
contentContainerStyle={{
...styles.container,
paddingTop: containerPaddingTop - getDefaultHeaderHeight(true),
}}
contentContainerStyle={styles.container}
scrollIndicatorInsets={{ top: scrollIndicatorInsetTop - getDefaultHeaderHeight(true) }}
/>

<CollapsibleStackSub>
<SearchBar
query={query}
setQuery={setQuery} />
</CollapsibleStackSub>
<SearchBar
flatListRef={flatListRef}
searchedQuery={query}
search={setQuery} />

</View>
)
Expand Down
74 changes: 51 additions & 23 deletions app/mobile/screens/Mode/SearchBar/SearchBar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { useState } from 'react'
import PropTypes from 'prop-types'
import { StyleSheet, StatusBar, View, TextInput } from 'react-native'
import { StyleSheet, StatusBar, TextInput, Animated } from 'react-native'
import * as Animatable from 'react-native-animatable'
import { useNavigation } from '@react-navigation/native'
import { useCollapsibleStack } from 'react-navigation-collapsible'

import colors from 'modules/colors'
import dimensions from 'modules/dimensions'
Expand Down Expand Up @@ -50,10 +52,13 @@ export const styles = StyleSheet.create({

})

export const SearchBar = ({ query: searchedQuery, setQuery: search }) => {
export const SearchBar = ({ searchedQuery, search, flatListRef }) => {
const navigation = useNavigation()
const [query, setQuery] = useState(searchedQuery)
const [timeout, setTheTimeout] = useState(null)

const { translateY } = useCollapsibleStack()

const cancelSearch = () => {
setQuery(null)
search(null)
Expand All @@ -63,8 +68,46 @@ export const SearchBar = ({ query: searchedQuery, setQuery: search }) => {
}
}

const handleSearch = (text) => {
if (text.trim().length === 0) {
cancelSearch()

} else {
setQuery(text)

if (timeout) {
clearTimeout(timeout)
}

setTheTimeout(setTimeout(() => {
search(text)

// Scroll to top
if (flatListRef && flatListRef.current) {
flatListRef.current.getNode().scrollToOffset({ offset: 0, animated: true })
}
}, 500))
}
}

const handleLayout = ({
nativeEvent: {
layout: { height = 0 },
},
}) => {
navigation.setParams({
collapsibleSubHeaderHeight: height + StatusBar.currentHeight,
isCollapsibleDirty: true,
})
}

return (
<View style={styles.root}>
<Animated.View
onLayout={handleLayout}
style={{
transform: [{ translateY }],
...styles.root,
}}>

<Container
elevation={1}
Expand Down Expand Up @@ -96,37 +139,22 @@ export const SearchBar = ({ query: searchedQuery, setQuery: search }) => {
style={styles.input}
selectionColor={'#FFF'}
underlineColorAndroid={'transparent'}
onChangeText={(text) => {
if (text.trim().length === 0) {
cancelSearch()

} else {
setQuery(text)

if (timeout) {
clearTimeout(timeout)
}

setTheTimeout(setTimeout(() => {
search(text)
}, 500))
}
}}
onChangeText={handleSearch}
value={query} />

</Container>

</View>
</Animated.View>
)
}

SearchBar.propTypes = {
query: PropTypes.string,
setQuery: PropTypes.func.isRequired,
searchedQuery: PropTypes.string,
search: PropTypes.func.isRequired,
}

SearchBar.defaultProps = {
query: null,
searchedQuery: null,
}

export default SearchBar
9 changes: 8 additions & 1 deletion app/mobile/screens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const Stack = createStackNavigator()

export default () => (
<Stack.Navigator
initialRouteName={'Home'}
screenOptions={{
headerShown: false
headerShown: false,
}}>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Item" component={ItemScreen} />
Expand All @@ -22,18 +23,24 @@ export default () => (
<Stack.Screen name="Movies">
{(props) => <ModeScreen mode={'movies'} {...props} />}
</Stack.Screen>,
{},
1
)}

{createCollapsibleStack(
<Stack.Screen name="Shows">
{(props) => <ModeScreen mode={'shows'} {...props} />}
</Stack.Screen>,
{},
1
)}

{createCollapsibleStack(
<Stack.Screen name="MyList">
{(props) => <ModeScreen mode={'bookmarks'} {...props} />}
</Stack.Screen>,
{},
1
)}
</Stack.Navigator>
)

0 comments on commit af0b971

Please sign in to comment.