import React from 'react';
import * as SplashScreen from 'expo-splash-screen';
import { Ionicons } from '@expo/vector-icons';
import * as Font from 'expo-font';
// Redux + Storage
import swimtrackerReducer from './state/Reducer';
import { createStore } from 'redux';
import { ConnState, WifiState, DeviceReduxCoupling } from './state/DeviceReduxCoupling';
import { Provider } from 'react-redux';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { persistStore, persistReducer } from 'redux-persist'
import hardSet from 'redux-persist/lib/stateReconciler/hardSet'
import { PersistGate } from 'redux-persist/integration/react'
// Navigation
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
// 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';
const persistConfig = {
key: 'root',
storage: AsyncStorage,
// stateReconciler: hardSet,
};
const persistedReducer = persistReducer(persistConfig, swimtrackerReducer);
const store = createStore(persistedReducer);
const persistor = persistStore(store);
const Stack = createStackNavigator();
// Keep the splash screen visible while we fetch resources
SplashScreen.preventAutoHideAsync();
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
isReady: false,
disconnected: true,
isProvisioning: false,
};
this.unsubscribe = undefined;
}
componentDidMount() {
this.setState({ isReady: true });
this.device = new DeviceReduxCoupling(store);
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) {
console.log("Unsubscribe");
this.unsubscribe();
}
}
render() {
if(this.state.isReady) {
SplashScreen.hideAsync();
}
const screenOptions = {
headerShown: false,
};
let disconnectedView = (
<>
>
);
let provisioningView = (
<>
{props => }
>
);
let normalView = (
<>
{props => }
>
);
let activeView;
if (this.state.disconnected)
activeView = disconnectedView;
else if (this.state.isProvisioning)
activeView = provisioningView;
else
activeView = normalView;
return (
{activeView}
);
}
}