autobots-lib
Version:
汽车人基础库
84 lines (80 loc) • 2.77 kB
JavaScript
import React from 'react';
import { NativeModules, RCTDeviceEventEmitter } from 'react-native';
const { AppLoader } = NativeModules;
import { NavigationContainerRef } from '@react-navigation/native';
import config from "./config"
var nav = {
navigator: null,
initnavigator: function (ref) {
if (ref && ref != null) {
console.log('ref.getRootState() == ', ref, ref.getRootState())
// this.navigator = ref.getRootState().navigation
}
// this.navigator = ref.getRootState().navigation
},
pop: function () {
const state = (this.navigator && this.navigator != null) ? this.navigator.getState() : {}; // Get the navigation state
// Accessing the stack
const stack = state?.routes || [];
console.log('当前堆栈信息', stack)
if (stack && stack.length > 1) {
this.navigator.goBack();
}
},
push: function (option) {
console.log('this.navigator == ', this.navigator, option)
const Com = option.component
var BootList = config.get().BootList;
var comName = ""
Object.keys(BootList).forEach((key) => {
if (BootList[key] == Com) {
comName = key
}
})
if (comName && comName.length > 0) {
this.navigator.navigate(comName, option.params || {})
}
},
replace: function (option) {
console.log('this.navigator == ', this.navigator, option)
const Com = option.component
var BootList = config.get().BootList;
var comName = ""
Object.keys(BootList).forEach((key) => {
if (BootList[key] == Com) {
comName = key
}
})
if (comName && comName.length > 0) {
// this.navigator.replace(comName, option.params || {})
this.navigator?.dispatch({
type: 'REPLACE',
payload: { name: comName, params: option.params || {} },
});
}
},
getStack: function () {
console.log("Navigation State:", this.navigator);
const state = (this.navigator && this.navigator != null) ? this.navigator.getState() : {}; // Get the navigation state
// Accessing the stack
const stack = state.routes || [];
const currentRoute = state.index || 0;
console.log("Stack Length:", stack.length);
console.log("Current Route:", stack.length > 0 ? stack[currentRoute].name : "");
return { currentIndex: currentRoute, stack }
},
openGesturePop: function () {
if (AppLoader && AppLoader.startGesturePop) {
AppLoader.startGesturePop({})
}
},
addFocusListener: function () {
this.navigator.navigationContext.addListener('didfocus', () => {
const routeStack = this.navigator.getCurrentRoutes()
if (AppLoader && AppLoader.storeCurrentStackLength) {
AppLoader.storeCurrentStackLength(routeStack.length)
}
});
}
}
module.exports = nav;