UNPKG

autobots-lib

Version:

汽车人基础库

113 lines (96 loc) 2.86 kB
'use strict'; import React, { Component } from 'react'; import ReactNative from 'react-native'; import config from './config'; import na from './native'; import Nav from "./nav" // import BackAndroid from './BackAndroid'; const { View, StyleSheet, InteractionManager, TouchableOpacity, Image, Text, ScrollView, DeviceEventEmitter, Platform, NativeModules } = ReactNative; import { enableScreens } from 'react-native-screens'; // 在初始化React Navigation之前调用enableScreens(false) enableScreens(false); import { NavigationContainer } from '@react-navigation/native'; import { createStackNavigator } from '@react-navigation/stack'; const Stack = createStackNavigator(); class RootController extends Component { constructor(props) { super(props); this.navigationRef = React.createRef(); // 创建 ref 以访问 NavigationContainer this.state = { canRender: false }; // this.renderScene = this.renderScene.bind(this); } handleStateChange = (state) => { if (state) { const currentRoute = state.routes[state.index]; console.log('Current route name:', currentRoute.name); // 更新全局的 navigation 对象 Nav.navigator = this.navigationRef.current; } }; componentDidMount() { var that = this; na.initNative(function (result, error) { if (result) { that.setState({ canRender: true }); } else { if (__DEV__) { that.setState({ canRender: true }); } else { alert("获取接口数据失败"); } } }); DeviceEventEmitter.addListener("qcr_event_back", () => { console.log('DeviceEventEmitter 收到 qcr_event_back 事件') Nav.pop() }) Nav.navigator = this.navigationRef.current; // BackAndroid.addEventListener('hardwareBackPress', this.handleBackButton); } componentWillUnmount() { // BackAndroid.removeEventListener('hardwareBackPress', this.handleBackButton); } render() { var BootList = config.get().BootList; var initialRouteName = Object.keys(BootList)[0]; if (this.props.BootName && Object.keys(BootList).indexOf(this.props.BootName) >= 0) { initialRouteName = this.props.BootName; } return ( <NavigationContainer ref={this.navigationRef}> <Stack.Navigator initialRouteName={initialRouteName}> {Object.keys(BootList).map(key => { return ( <Stack.Screen key={key} name={key} component={BootList[key]} options={{ headerShown: false, }} /> ) })} </Stack.Navigator> </NavigationContainer> ); } } let styles = StyleSheet.create({ container: { flex: 1, }, }); export default RootController;