Home button scrolls to top when on home page

zio/stable
Paul Frazee 2022-10-11 09:37:01 -05:00
parent 8dc8200f07
commit d7a75a2062
5 changed files with 29 additions and 16 deletions

View File

@ -1,4 +1,4 @@
import React from 'react'
import React, {MutableRefObject} from 'react'
import {observer} from 'mobx-react-lite'
import {Text, View, FlatList, StyleProp, ViewStyle} from 'react-native'
import {FeedModel, FeedItemModel} from '../../../state/models/feed-view'
@ -7,9 +7,11 @@ import {FeedItem} from './FeedItem'
export const Feed = observer(function Feed({
feed,
style,
scrollElRef,
}: {
feed: FeedModel
style?: StyleProp<ViewStyle>
scrollElRef?: MutableRefObject<FlatList<any> | null>
}) {
// TODO optimize renderItem or FeedItem, we're getting this notice from RN: -prf
// VirtualizedList: You have a large list that is slow to update - make sure your
@ -30,6 +32,7 @@ export const Feed = observer(function Feed({
{feed.hasError && <Text>{feed.error}</Text>}
{feed.hasContent && (
<FlatList
ref={scrollElRef}
data={feed.feed.slice()}
keyExtractor={item => item._reactKey}
renderItem={renderItem}

View File

@ -1,4 +1,5 @@
import React from 'react'
import React, {MutableRefObject} from 'react'
import {FlatList} from 'react-native'
import {IconProp} from '@fortawesome/fontawesome-svg-core'
import {Home} from './screens/Home'
import {Search} from './screens/Search'
@ -15,6 +16,7 @@ import {Settings} from './screens/Settings'
export type ScreenParams = {
params: Record<string, any>
visible: boolean
scrollElRef: MutableRefObject<FlatList<any> | undefined>
}
export type Route = [React.FC<ScreenParams>, IconProp, RegExp]
export type MatchResult = {

View File

@ -1,20 +1,20 @@
import React, {useState, useEffect, useMemo} from 'react'
import {useSharedValue} from 'react-native-reanimated'
import {View} from 'react-native'
import {observer} from 'mobx-react-lite'
import {Feed} from '../com/posts/Feed'
import {FAB} from '../com/util/FloatingActionButton'
import {Selector} from '../com/util/Selector'
import {useStores} from '../../state'
import {FeedModel} from '../../state/models/feed-view'
import {ComposePostModel} from '../../state/models/shell'
import {ScreenParams} from '../routes'
import {s} from '../lib/styles'
export const Home = observer(function Home({visible}: ScreenParams) {
export const Home = observer(function Home({
visible,
scrollElRef,
}: ScreenParams) {
const store = useStores()
const [hasSetup, setHasSetup] = useState<boolean>(false)
const [selectedViewIndex, setSelectedViewIndex] = useState<number>(0)
const defaultFeedView = useMemo<FeedModel>(
() =>
new FeedModel(store, 'home', {
@ -43,13 +43,10 @@ export const Home = observer(function Home({visible}: ScreenParams) {
const onCreatePost = () => {
defaultFeedView.loadLatest()
}
const onSelectView = (viewIndex: number) => {
setSelectedViewIndex(viewIndex)
}
return (
<View style={s.flex1}>
<Feed key="default" feed={defaultFeedView} />
<Feed key="default" feed={defaultFeedView} scrollElRef={scrollElRef} />
<FAB icon="pen-nib" onPress={onComposePress} />
</View>
)

View File

@ -1,9 +1,9 @@
import React, {useState, useEffect} from 'react'
import React, {useState, useEffect, useRef} from 'react'
import {observer} from 'mobx-react-lite'
import {
useWindowDimensions,
FlatList,
GestureResponderEvent,
Image,
SafeAreaView,
StyleSheet,
Text,
@ -97,6 +97,7 @@ export const MobileShell: React.FC = observer(() => {
const [isLocationMenuActive, setLocationMenuActive] = useState(false)
const [isMainMenuActive, setMainMenuActive] = useState(false)
const [isTabsSelectorActive, setTabsSelectorActive] = useState(false)
const scrollElRef = useRef<FlatList | undefined>()
const winDim = useWindowDimensions()
const swipeGestureInterp = useSharedValue<number>(0)
const screenRenderDesc = constructScreenRenderDesc(store.nav)
@ -109,7 +110,13 @@ export const MobileShell: React.FC = observer(() => {
const onPressBack = () => store.nav.tab.goBack()
const onPressForward = () => store.nav.tab.goForward()
const onPressHome = () => store.nav.navigate('/')
const onPressHome = () => {
if (store.nav.tab.current.url === '/') {
scrollElRef.current?.scrollToOffset({offset: 0})
} else {
store.nav.navigate('/')
}
}
const onPressMenu = () => setMainMenuActive(true)
const onPressTabs = () => setTabsSelectorActive(true)
@ -184,7 +191,11 @@ export const MobileShell: React.FC = observer(() => {
styles.screen,
current ? swipeTransform : undefined,
]}>
<Com params={params} visible={true} />
<Com
params={params}
visible={true}
scrollElRef={current ? scrollElRef : undefined}
/>
</Animated.View>
</Screen>
)

View File

@ -4,11 +4,11 @@ Paul's todo list
- Update to RN 0.70
- Cache some profile/userinfo lookups
- Cursor behaviors on all views
- Home button should scroll to top
- Onboarding flow
- *
- Private beta
- Users list
- Firehose
- Linking
- Web linking
- App linking
@ -20,4 +20,4 @@ Paul's todo list
- Bugs
- Check that sub components arent reloading too much
- Titles are getting screwed up (possibly swipe related)
- Home feed not showing own posts
- Home feed sizing is off now