Use a persistent notifications model to improve load times of the notifications view

This commit is contained in:
Paul Frazee 2022-11-28 14:19:49 -06:00
parent 1aa1f77049
commit 9051aecdcb
4 changed files with 49 additions and 41 deletions

View file

@ -32,7 +32,7 @@ export const Feed = observer(function Feed({
}
return (
<View style={{flex: 1}}>
{view.isLoading && !view.isRefreshing && !view.hasContent && (
{view.isLoading && !view.isRefreshing && (
<NotificationFeedLoadingPlaceholder />
)}
{view.hasError && (
@ -43,7 +43,7 @@ export const Feed = observer(function Feed({
onPressTryAgain={onPressTryAgain}
/>
)}
{view.hasContent && (
{view.hasLoaded && (
<FlatList
data={view.notifications}
keyExtractor={item => item._reactKey}
@ -53,7 +53,7 @@ export const Feed = observer(function Feed({
onEndReached={onEndReached}
/>
)}
{view.isEmpty && (
{view.hasLoaded && view.isEmpty && (
<EmptyState icon="bell" message="No notifications yet!" />
)}
</View>

View file

@ -7,43 +7,33 @@ import {NotificationsViewModel} from '../../state/models/notifications-view'
import {ScreenParams} from '../routes'
export const Notifications = ({navIdx, visible}: ScreenParams) => {
const [hasSetup, setHasSetup] = useState<boolean>(false)
const [notesView, setNotesView] = useState<
NotificationsViewModel | undefined
>()
const store = useStores()
useEffect(() => {
let aborted = false
if (!visible) {
return
}
console.log('Updating notifications feed')
store.me.refreshMemberships() // needed for the invite notifications
if (hasSetup) {
console.log('Updating notifications feed')
notesView?.update()
} else {
store.nav.setTitle(navIdx, 'Notifications')
const newNotesView = new NotificationsViewModel(store, {})
setNotesView(newNotesView)
newNotesView.setup().then(() => {
if (aborted) return
setHasSetup(true)
store.me.notifications
.update()
.catch(e => {
console.error('Error while updating notifications feed', e)
})
}
return () => {
aborted = true
}
.then(() => {
store.me.notifications.updateReadState()
})
store.nav.setTitle(navIdx, 'Notifications')
}, [visible, store])
const onPressTryAgain = () => {
notesView?.refresh()
store.me.notifications.refresh()
}
return (
<View style={{flex: 1}}>
<ViewHeader title="Notifications" />
{notesView && <Feed view={notesView} onPressTryAgain={onPressTryAgain} />}
<Feed view={store.me.notifications} onPressTryAgain={onPressTryAgain} />
</View>
)
}