Handle all navigations from the state layer

This commit is contained in:
Paul Frazee
2023-03-10 15:25:01 -06:00
parent 330a1a0be4
commit d4d1fb820b
6 changed files with 28 additions and 24 deletions
+2 -4
View File
@@ -1,10 +1,9 @@
import notifee, {EventType} from '@notifee/react-native'
import {StackActions} from '@react-navigation/native'
import {AppBskyEmbedImages} from '@atproto/api'
import {RootStoreModel} from 'state/models/root-store'
import {NotificationsViewItemModel} from 'state/models/notifications-view'
import {enforceLen} from 'lib/strings/helpers'
import {navigation} from '../Routes'
import {resetToTab} from '../Routes'
export function init(store: RootStoreModel) {
store.onUnreadNotifications(count => notifee.setBadgeCount(count))
@@ -17,8 +16,7 @@ export function init(store: RootStoreModel) {
store.log.debug('Notifee foreground event', {type})
if (type === EventType.PRESS) {
store.log.debug('User pressed a notifee, opening notifications')
navigation.navigate('NotificationsTab')
navigation.dispatch(StackActions.popToTop())
resetToTab('NotificationsTab')
}
})
notifee.onBackgroundEvent(async _e => {}) // notifee requires this but we handle it with onForegroundEvent
+1 -15
View File
@@ -1,18 +1,4 @@
import {State, NavigationProp} from './types'
// TODO needed?
// export function getCurrentTabName(
// navigator: NavigationProp | undefined,
// ): string {
// if (!navigator) {
// throw new Error('Failed to get current tab')
// }
// const state = navigator.getState()
// if (state.type !== 'tab') {
// return getCurrentTabName(navigator.getParent())
// }
// return state.routes[state.index].name
// }
import {State} from './types'
export function getCurrentRoute(state: State) {
let node = state.routes[state.index || 0]
+1 -1
View File
@@ -43,7 +43,7 @@ export type NotificationsDrawerNavigatorParams = {
export type AllNavigatorParams = CommonNavigatorParams & {
HomeTab: undefined
Home: undefined
SeachTab: undefined
SearchTab: undefined
Search: undefined
NotificationsTab: undefined
Notifications: undefined
+19 -1
View File
@@ -2,6 +2,7 @@ import * as React from 'react'
import {
NavigationContainer,
createNavigationContainerRef,
StackActions,
} from '@react-navigation/native'
import {createNativeStackNavigator} from '@react-navigation/native-stack'
import {createDrawerNavigator} from '@react-navigation/drawer'
@@ -277,4 +278,21 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) {
)
}
export {navigationRef as navigation, matchPath, TabsNavigator, RoutesContainer}
function navigate<K extends keyof AllNavigatorParams>(
name: K,
params?: AllNavigatorParams[K],
) {
if (navigationRef.isReady()) {
// @ts-ignore I dont know what would make typescript happy but I have a life -prf
navigationRef.navigate(name, params)
}
}
function resetToTab(tabName: 'HomeTab' | 'SearchTab' | 'NotificationsTab') {
if (navigationRef.isReady()) {
navigate(tabName)
navigationRef.dispatch(StackActions.popToTop())
}
}
export {navigate, resetToTab, matchPath, TabsNavigator, RoutesContainer}
+3 -2
View File
@@ -16,6 +16,7 @@ import {ProfilesViewModel} from './profiles-view'
import {LinkMetasViewModel} from './link-metas-view'
import {NotificationsViewItemModel} from './notifications-view'
import {MeModel} from './me'
import {resetToTab} from '../../Routes'
export const appInfo = z.object({
build: z.string(),
@@ -138,7 +139,7 @@ export class RootStoreModel {
*/
async handleSessionDrop() {
this.log.debug('RootStoreModel:handleSessionDrop')
// this.nav.clear() TODO
resetToTab('HomeTab')
this.me.clear()
this.emitSessionDropped()
}
@@ -149,7 +150,7 @@ export class RootStoreModel {
clearAllSessionState() {
this.log.debug('RootStoreModel:clearAllSessionState')
this.session.clear()
// this.nav.clear() TODO
resetToTab('HomeTab')
this.me.clear()
}
+2 -1
View File
@@ -14,6 +14,7 @@ import {s, colors, gradients} from 'lib/styles'
import {usePalette} from 'lib/hooks/usePalette'
import {ErrorMessage} from '../util/error/ErrorMessage'
import {cleanError} from 'lib/strings/errors'
import {resetToTab} from '../../../Routes'
export const snapPoints = ['60%']
@@ -46,7 +47,7 @@ export function Component({}: {}) {
token: confirmCode,
})
Toast.show('Your account has been deleted')
// store.nav.tab.fixedTabReset() TODO
resetToTab('HomeTab')
store.session.clear()
store.shell.closeModal()
} catch (e: any) {