import {CommonActions, StackActions} from '@react-navigation/native'; import {BottomSheetParams, ModalParams, ScreenParams} from './types'; class Navigator { ref?: any; isReady?: any; isWidget?: any; maxApi?: any; dismissData?: any; constructor(navigation: any, isReady: any, isWidget = false) { this.ref = navigation; this.isReady = isReady; this.isWidget = isWidget; } /** * push new stack screen * @param params */ push = (params: ScreenParams) => { if (this.isReady.current) { this.ref.current?.dispatch?.(StackActions.push('Stack', params)); } }; /** * replace current screen with new screen * @param params */ replace = (params: ScreenParams) => { if (this.isReady.current) { this.ref.current?.dispatch?.(StackActions.replace('Stack', params)); } }; /** * pop to dismiss a screen * @param count */ pop = (count?: number) => { if (this.isReady.current) { this.ref.current?.dispatch?.(StackActions.pop(count ?? 1)); } }; /** * present a new screen, show from bottom iOS * @param params */ present = (params: ScreenParams) => { if (this.isReady.current) { this.ref.current?.dispatch?.(StackActions.push('Dialog', params)); } }; /** * show a modal popup * @param params * @param onError */ showModal = (params: ModalParams, onError?: (error: string) => void) => { try { if (this.isReady.current) { this.ref.current?.dispatch?.( StackActions.push('Modal', { ...params, useNativeModal: this.isWidget ? true : params?.useNativeModal, }) ); } else { onError?.('NavigationContainer not ready'); } } catch (error) { onError?.(`${error}`); } }; /** * show a bottom sheet * @param params * @param onError */ showBottomSheet = ( params: BottomSheetParams, onError?: (error: string) => void ) => { try { if (this.isReady.current) { this.ref.current?.dispatch?.( StackActions.push('Modal', { ...params, useNativeModal: this.isWidget ? true : params?.useNativeModal, isBottomSheet: true, }) ); } else { onError?.('NavigationContainer not ready'); } } catch (error) { onError?.(`${error}`); } }; /** * pop all screen route */ popToTop = () => { if (this.isReady.current) { this.ref.current?.dispatch?.(StackActions.popToTop()); } }; /** * navigate route name bottom tab only * @param name */ navigate = (name: string) => { if (this.isReady.current) { this.ref.current?.dispatch?.( CommonActions.navigate({ name, }) ); } }; /** * reset a navigation flow with new screen * @param params */ reset = (params: ScreenParams) => { if (this.isReady.current) { this.ref.current?.dispatch?.( CommonActions.reset({ index: 0, routes: [ { name: 'Stack', key: `Stack_${new Date().getTime()}`, params, }, ], }) ); } }; /** * dismiss a feature data * @param data */ setDismissData = (data: any) => { this.dismissData = data; }; /** * set app context using for features of kit * @param context */ setCurrentContext = (context: { code?: string; name?: {vi: string; en: string}; description?: {vi: string; en: string}; icon?: string; }) => { console.log(context); }; } export default Navigator;