Files
frontend/src/infrastructure/navigation/navigationService.ts

111 lines
2.4 KiB
TypeScript
Raw Normal View History

/**
*
*
*/
import { NavigationContainerRef, Route } from '@react-navigation/native';
class NavigationService {
private navigationRef: NavigationContainerRef<any> | null = null;
private isReady: boolean = false;
/**
*
*/
setNavigationRef(ref: NavigationContainerRef<any> | null): void {
this.navigationRef = ref;
this.isReady = ref !== null;
}
/**
*
*/
isNavigationReady(): boolean {
return this.isReady && this.navigationRef !== null;
}
/**
*
*/
navigate(routeName: string, params?: any): void {
if (!this.isNavigationReady()) {
console.warn('[NavigationService] Navigation not ready');
return;
}
this.navigationRef!.navigate(routeName as any, params);
}
/**
*
*/
goBack(): void {
if (!this.isNavigationReady()) {
console.warn('[NavigationService] Navigation not ready');
return;
}
this.navigationRef!.goBack();
}
/**
*
*/
getCurrentRoute(): Route<string> | null {
if (!this.isNavigationReady()) {
return null;
}
return this.navigationRef!.getCurrentRoute();
}
/**
*
*/
getCurrentRouteName(): string | null {
const route = this.getCurrentRoute();
return route?.name ?? null;
}
/**
*
*/
reset(routes: { name: string; params?: any }[], index: number = 0): void {
if (!this.isNavigationReady()) {
console.warn('[NavigationService] Navigation not ready');
return;
}
this.navigationRef!.reset({
index,
routes: routes.map(r => ({ name: r.name, params: r.params })),
});
}
/**
*
*/
replace(routeName: string, params?: any): void {
if (!this.isNavigationReady()) {
console.warn('[NavigationService] Navigation not ready');
return;
}
this.navigationRef!.dispatch({
type: 'REPLACE',
payload: { name: routeName, params },
});
}
/**
*
*/
navigateToRoot(): void {
this.reset([{ name: 'Main' }]);
}
/**
*
*/
navigateToAuth(): void {
this.reset([{ name: 'Auth' }]);
}
}
export const navigationService = new NavigationService();