swimtracker-app/SwimTracker/App.js

158 lines
4.8 KiB
JavaScript
Raw Permalink Normal View History

2019-09-17 20:24:01 +02:00
import React from 'react';
import * as SplashScreen from 'expo-splash-screen';
2019-09-17 20:24:01 +02:00
import { Ionicons } from '@expo/vector-icons';
2020-06-04 18:46:15 +02:00
import * as Font from 'expo-font';
2020-09-03 19:06:31 +02:00
// Redux + Storage
2020-06-04 18:46:15 +02:00
import swimtrackerReducer from './state/Reducer';
import { createStore } from 'redux';
2021-06-13 12:43:50 +02:00
import { ConnState, WifiState, DeviceReduxCoupling } from './state/DeviceReduxCoupling';
2020-06-04 18:46:15 +02:00
import { Provider } from 'react-redux';
import AsyncStorage from '@react-native-async-storage/async-storage';
2020-09-03 19:06:31 +02:00
import { persistStore, persistReducer } from 'redux-persist'
import hardSet from 'redux-persist/lib/stateReconciler/hardSet'
import { PersistGate } from 'redux-persist/integration/react'
2020-06-04 18:46:15 +02:00
2020-09-03 19:06:31 +02:00
// Navigation
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
2020-06-04 18:46:15 +02:00
2020-09-03 19:06:31 +02:00
// Own views
import MainMenuView from "./views/MainMenuView";
import SettingsView from "./views/SettingsView";
import TrainingView from "./views/TrainingView";
import LastSessionsView from "./views/LastSessionsView";
import ConnectingView from './views/ConnectingView';
import WifiSelectionView from './views/WifiSelectionView';
import WifiPasswordView from './views/WifiPasswordView';
2020-09-03 19:06:31 +02:00
const persistConfig = {
key: 'root',
storage: AsyncStorage,
// stateReconciler: hardSet,
2020-09-03 19:06:31 +02:00
};
const persistedReducer = persistReducer(persistConfig, swimtrackerReducer);
2020-09-03 19:06:31 +02:00
const store = createStore(persistedReducer);
const persistor = persistStore(store);
const Stack = createStackNavigator();
2019-09-17 20:24:01 +02:00
// Keep the splash screen visible while we fetch resources
SplashScreen.preventAutoHideAsync();
2019-09-17 20:24:01 +02:00
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
isReady: false,
2021-06-13 12:43:50 +02:00
disconnected: true,
isProvisioning: false,
2020-05-17 15:57:26 +02:00
};
2021-06-13 12:43:50 +02:00
this.unsubscribe = undefined;
2019-09-17 20:24:01 +02:00
}
2021-06-13 12:43:50 +02:00
componentDidMount() {
2019-09-17 20:24:01 +02:00
this.setState({ isReady: true });
2020-07-15 15:53:16 +02:00
this.device = new DeviceReduxCoupling(store);
2021-06-13 12:43:50 +02:00
let theApp = this;
this.unsubscribe = store.subscribe(() => {
const state = store.getState();
theApp.setState({
disconnected: state.deviceState.connState == ConnState.DISCONNECTED,
isProvisioning: state.deviceState.wifiState == WifiState.AP_PROVISIONING || state.deviceState.wifiState == WifiState.UNKNOWN,
});
});
}
componentWillUnmount() {
if (this.unsubscribe) {
2021-07-22 18:39:02 +02:00
console.log("Unsubscribe");
2021-06-13 12:43:50 +02:00
this.unsubscribe();
}
2019-09-17 20:24:01 +02:00
}
render() {
if(this.state.isReady) {
SplashScreen.hideAsync();
2019-09-17 20:24:01 +02:00
}
2020-09-03 19:06:31 +02:00
const screenOptions = {
headerShown: false,
};
2021-06-13 12:43:50 +02:00
let disconnectedView = (
<>
<Stack.Screen
name="ConnectingView"
options={screenOptions}
component={ConnectingView} />
</>
);
2021-06-13 12:43:50 +02:00
let provisioningView = (
<>
<Stack.Screen
name="WifiSelectionView"
options={screenOptions} >
{props => <WifiSelectionView {...props} device={this.device} />}
</Stack.Screen>
<Stack.Screen
name="WifiPasswordView"
options={screenOptions}
component={WifiPasswordView}
>
</Stack.Screen>
</>
);
2021-06-13 12:43:50 +02:00
let normalView = (
<>
<Stack.Screen
name="Home"
component={MainMenuView}
options={screenOptions}
/>
<Stack.Screen
name="Settings"
options={screenOptions}>
{props => <SettingsView {...props} device={this.device} />}
</Stack.Screen>
2021-06-13 12:43:50 +02:00
<Stack.Screen
name="Training"
component={TrainingView}
options={screenOptions}
/>
<Stack.Screen
name="LastSessions"
component={LastSessionsView}
options={screenOptions}
/>
</>
);
let activeView;
if (this.state.disconnected)
activeView = disconnectedView;
else if (this.state.isProvisioning)
activeView = provisioningView;
else
activeView = normalView;
2020-07-25 14:06:39 +02:00
return (
<Provider store={store}>
<PersistGate persistor={persistor}>
<NavigationContainer >
2021-06-13 12:43:50 +02:00
<Stack.Navigator >
{activeView}
2020-09-03 19:06:31 +02:00
</Stack.Navigator>
</NavigationContainer>
</PersistGate>
2020-07-25 14:06:39 +02:00
</Provider>
2019-09-17 20:24:01 +02:00
);
2021-06-13 12:43:50 +02:00
2019-09-17 20:24:01 +02:00
}
}