Fixes to the followers list view

zio/stable
Paul Frazee 2023-01-19 16:38:12 -06:00
parent 88ab1f171a
commit aec0f1c3ba
3 changed files with 74 additions and 61 deletions

View File

@ -5,9 +5,9 @@ import {
} from '@atproto/api' } from '@atproto/api'
import {RootStoreModel} from './root-store' import {RootStoreModel} from './root-store'
export type FollowerItem = GetFollowers.Follower & { const PAGE_SIZE = 30
_reactKey: string
} export type FollowerItem = GetFollowers.Follower
export class UserFollowersViewModel { export class UserFollowersViewModel {
// state // state
@ -16,6 +16,9 @@ export class UserFollowersViewModel {
hasLoaded = false hasLoaded = false
error = '' error = ''
params: GetFollowers.QueryParams params: GetFollowers.QueryParams
hasMore = true
loadMoreCursor?: string
private _loadMorePromise: Promise<void> | undefined
// data // data
subject: ActorRef.WithInfo = { subject: ActorRef.WithInfo = {
@ -55,16 +58,17 @@ export class UserFollowersViewModel {
// public api // public api
// = // =
async setup() {
await this._fetch()
}
async refresh() { async refresh() {
await this._fetch(true) return this.loadMore(true)
} }
async loadMore() { async loadMore(isRefreshing = false) {
// TODO if (this._loadMorePromise) {
return this._loadMorePromise
}
this._loadMorePromise = this._loadMore(isRefreshing)
await this._loadMorePromise
this._loadMorePromise = undefined
} }
// state transitions // state transitions
@ -89,32 +93,30 @@ export class UserFollowersViewModel {
// loader functions // loader functions
// = // =
private async _fetch(isRefreshing = false) { private async _loadMore(isRefreshing = false) {
if (!this.hasMore) {
return
}
this._xLoading(isRefreshing) this._xLoading(isRefreshing)
try { try {
const res = await this.rootStore.api.app.bsky.graph.getFollowers( const params = Object.assign({}, this.params, {
this.params, limit: PAGE_SIZE,
) before: this.loadMoreCursor,
this._replaceAll(res) })
if (this.isRefreshing) {
this.followers = []
}
const res = await this.rootStore.api.app.bsky.graph.getFollowers(params)
await this._appendAll(res)
this._xIdle() this._xIdle()
} catch (e: any) { } catch (e: any) {
this._xIdle(`Failed to load feed: ${e.toString()}`) this._xIdle(e)
} }
} }
private _replaceAll(res: GetFollowers.Response) { private async _appendAll(res: GetFollowers.Response) {
this.subject.did = res.data.subject.did this.loadMoreCursor = res.data.cursor
this.subject.handle = res.data.subject.handle this.hasMore = !!this.loadMoreCursor
this.subject.displayName = res.data.subject.displayName this.followers = this.followers.concat(res.data.followers)
this.subject.avatar = res.data.subject.avatar
this.followers.length = 0
let counter = 0
for (const item of res.data.followers) {
this._append({_reactKey: `item-${counter++}`, ...item})
}
}
private _append(item: FollowerItem) {
this.followers.push(item)
} }
} }

View File

@ -10,7 +10,7 @@ import {Text} from '../util/text/Text'
import {ErrorMessage} from '../util/error/ErrorMessage' import {ErrorMessage} from '../util/error/ErrorMessage'
import {UserAvatar} from '../util/UserAvatar' import {UserAvatar} from '../util/UserAvatar'
import {useStores} from '../../../state' import {useStores} from '../../../state'
import {s, colors} from '../../lib/styles' import {s} from '../../lib/styles'
import {usePalette} from '../../lib/hooks/usePalette' import {usePalette} from '../../lib/hooks/usePalette'
export const ProfileFollowers = observer(function ProfileFollowers({ export const ProfileFollowers = observer(function ProfileFollowers({
@ -19,30 +19,29 @@ export const ProfileFollowers = observer(function ProfileFollowers({
name: string name: string
}) { }) {
const store = useStores() const store = useStores()
const [view, setView] = React.useState<UserFollowersViewModel | undefined>() const view = React.useMemo(
() => new UserFollowersViewModel(store, {user: name}),
[store, name],
)
useEffect(() => { useEffect(() => {
if (view?.params.user === name) { view
return // no change needed? or trigger refresh? .loadMore()
}
const newView = new UserFollowersViewModel(store, {user: name})
setView(newView)
newView
.setup()
.catch(err => store.log.error('Failed to fetch user followers', err)) .catch(err => store.log.error('Failed to fetch user followers', err))
}, [name, view?.params.user, store]) }, [view, store.log])
const onRefresh = () => { const onRefresh = () => {
view?.refresh() view.refresh()
}
const onEndReached = () => {
view
.loadMore()
.catch(err =>
view?.rootStore.log.error('Failed to load more followers', err),
)
} }
// loading if (!view.hasLoaded) {
// =
if (
!view ||
(view.isLoading && !view.isRefreshing) ||
view.params.user !== name
) {
return ( return (
<View> <View>
<ActivityIndicator /> <ActivityIndicator />
@ -66,16 +65,25 @@ export const ProfileFollowers = observer(function ProfileFollowers({
// loaded // loaded
// = // =
const renderItem = ({item}: {item: FollowerItem}) => <User item={item} /> const renderItem = ({item}: {item: FollowerItem}) => (
<User key={item.did} item={item} />
)
return ( return (
<View> <FlatList
<FlatList data={view.followers}
data={view.followers} keyExtractor={item => item.did}
keyExtractor={item => item._reactKey} refreshing={view.isRefreshing}
renderItem={renderItem} onRefresh={onRefresh}
contentContainerStyle={{paddingBottom: 200}} onEndReached={onEndReached}
/> renderItem={renderItem}
</View> initialNumToRender={15}
ListFooterComponent={() => (
<View style={styles.footer}>
{view.isLoading && <ActivityIndicator />}
</View>
)}
extraData={view.isLoading}
/>
) )
}) })
@ -128,4 +136,8 @@ const styles = StyleSheet.create({
paddingTop: 10, paddingTop: 10,
paddingBottom: 10, paddingBottom: 10,
}, },
footer: {
height: 200,
paddingTop: 20,
},
}) })

View File

@ -396,7 +396,7 @@ export const MobileShell: React.FC = observer(() => {
/> />
<Animated.View <Animated.View
style={[ style={[
s.flex1, {height: '100%'},
screenBg, screenBg,
current current
? [ ? [
@ -543,13 +543,12 @@ function constructScreenRenderDesc(nav: NavigationModel): {
const styles = StyleSheet.create({ const styles = StyleSheet.create({
outerContainer: { outerContainer: {
height: '100%', height: '100%',
flex: 1,
}, },
innerContainer: { innerContainer: {
flex: 1, height: '100%',
}, },
screenContainer: { screenContainer: {
flex: 1, height: '100%',
}, },
screenMask: { screenMask: {
position: 'absolute', position: 'absolute',