Move posts feed to root store (me) and ensure new posts always update the feed

zio/stable
Paul Frazee 2022-12-13 15:52:18 -06:00
parent ebfa6e5581
commit 345ec83f26
3 changed files with 27 additions and 31 deletions

View File

@ -1,5 +1,6 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {RootStoreModel} from './root-store'
import {FeedModel} from './feed-view'
import {MembershipsViewModel} from './memberships-view'
import {NotificationsViewModel} from './notifications-view'
import {isObj, hasProp} from '../lib/type-guards'
@ -12,6 +13,7 @@ export class MeModel {
avatar: string = ''
notificationCount: number = 0
memberships?: MembershipsViewModel
mainFeed: FeedModel
notifications: NotificationsViewModel
constructor(public rootStore: RootStoreModel) {
@ -20,6 +22,9 @@ export class MeModel {
{rootStore: false, serialize: false, hydrate: false},
{autoBind: true},
)
this.mainFeed = new FeedModel(this.rootStore, 'home', {
algorithm: 'reverse-chronological',
})
this.notifications = new NotificationsViewModel(this.rootStore, {})
}
@ -93,12 +98,17 @@ export class MeModel {
this.memberships = new MembershipsViewModel(this.rootStore, {
actor: this.did,
})
await this.memberships?.setup().catch(e => {
console.error('Failed to setup memberships model', e)
})
await this.notifications.setup().catch(e => {
console.error('Failed to setup notifications model', e)
})
await Promise.all([
this.memberships?.setup().catch(e => {
console.error('Failed to setup memberships model', e)
}),
this.mainFeed.setup().catch(e => {
console.error('Failed to setup main feed model', e)
}),
this.notifications.setup().catch(e => {
console.error('Failed to setup notifications model', e)
}),
])
} else {
this.clear()
}

View File

@ -121,6 +121,7 @@ export const ComposePost = observer(function ComposePost({
setIsProcessing(false)
return
}
store.me.mainFeed.loadLatest()
onPost?.()
onClose()
Toast.show(`Your ${replyTo ? 'reply' : 'post'} has been published`)

View File

@ -27,23 +27,16 @@ export const Home = observer(function Home({
const {appState} = useAppState({
onForeground: () => doPoll(true),
})
const defaultFeedView = useMemo<FeedModel>(
() =>
new FeedModel(store, 'home', {
algorithm: 'reverse-chronological',
}),
[store],
)
const doPoll = (knownActive = false) => {
if ((!knownActive && appState !== 'active') || !visible) {
return
}
if (defaultFeedView.isLoading) {
if (store.me.mainFeed.isLoading) {
return
}
console.log('Polling home feed')
defaultFeedView.checkForLatest().catch(e => {
store.me.mainFeed.checkForLatest().catch(e => {
console.error('Failed to poll feed', e)
})
}
@ -57,11 +50,11 @@ export const Home = observer(function Home({
if (hasSetup) {
console.log('Updating home feed')
defaultFeedView.update()
store.me.mainFeed.update()
} else {
store.nav.setTitle(navIdx, 'Home')
console.log('Fetching home feed')
defaultFeedView.setup().then(() => {
store.me.mainFeed.setup().then(() => {
if (aborted) return
setHasSetup(true)
})
@ -73,37 +66,29 @@ export const Home = observer(function Home({
}, [visible, store])
const onPressCompose = () => {
store.shell.openComposer({onPost: onCreatePost})
}
const onCreatePost = () => {
defaultFeedView.loadLatest()
store.shell.openComposer({})
}
const onPressTryAgain = () => {
defaultFeedView.refresh()
store.me.mainFeed.refresh()
}
const onPressLoadLatest = () => {
defaultFeedView.refresh()
store.me.mainFeed.refresh()
scrollElRef?.current?.scrollToOffset({offset: 0})
}
return (
<View style={s.flex1}>
<ViewHeader
title="Bluesky"
subtitle="Private Beta"
canGoBack={false}
onPost={onCreatePost}
/>
<ViewHeader title="Bluesky" subtitle="Private Beta" canGoBack={false} />
<Feed
key="default"
feed={defaultFeedView}
feed={store.me.mainFeed}
scrollElRef={scrollElRef}
style={{flex: 1}}
onPressCompose={onPressCompose}
onPressTryAgain={onPressTryAgain}
onScroll={onMainScroll}
/>
{defaultFeedView.hasNewLatest ? (
{store.me.mainFeed.hasNewLatest ? (
<TouchableOpacity
style={[
styles.loadLatest,