2020-06-15 14:00:46 +00:00
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { NavigationContainer } from '@react-navigation/native';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { AppearanceProvider } from 'react-native-appearance';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { createStackNavigator } from '@react-navigation/stack';
|
2019-07-18 17:44:02 +00:00
|
|
|
import { Provider } from 'react-redux';
|
2019-07-29 16:33:28 +00:00
|
|
|
import RNUserDefaults from 'rn-user-defaults';
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
import {
|
|
|
|
defaultTheme,
|
|
|
|
newThemeState,
|
|
|
|
subscribeTheme,
|
|
|
|
unsubscribeTheme
|
|
|
|
} from './utils/theme';
|
2019-07-29 16:33:28 +00:00
|
|
|
import Navigation from './lib/ShareNavigation';
|
2019-07-18 17:44:02 +00:00
|
|
|
import store from './lib/createStore';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { supportSystemTheme } from './utils/deviceInfo';
|
|
|
|
import {
|
|
|
|
defaultHeader, themedHeader, getActiveRouteName, navigationTheme
|
|
|
|
} from './utils/navigation';
|
2019-12-04 16:39:53 +00:00
|
|
|
import RocketChat, { THEME_PREFERENCES_KEY } from './lib/rocketchat';
|
|
|
|
import { ThemeContext } from './theme';
|
2020-05-08 17:04:37 +00:00
|
|
|
import { localAuthenticate } from './utils/localAuthentication';
|
|
|
|
import ScreenLockedView from './views/ScreenLockedView';
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
// Outside Stack
|
|
|
|
import WithoutServersView from './views/WithoutServersView';
|
|
|
|
|
|
|
|
// Inside Stack
|
|
|
|
import ShareListView from './views/ShareListView';
|
|
|
|
import ShareView from './views/ShareView';
|
|
|
|
import SelectServerView from './views/SelectServerView';
|
|
|
|
import { setCurrentScreen } from './utils/log';
|
|
|
|
import AuthLoadingView from './views/AuthLoadingView';
|
|
|
|
|
|
|
|
const Inside = createStackNavigator();
|
|
|
|
const InsideStack = () => {
|
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
|
|
|
|
const screenOptions = {
|
|
|
|
...defaultHeader,
|
|
|
|
...themedHeader(theme)
|
|
|
|
};
|
|
|
|
screenOptions.headerStyle = {
|
|
|
|
...screenOptions.headerStyle,
|
|
|
|
// TODO: fix on multiple files PR :)
|
|
|
|
height: 57
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Inside.Navigator screenOptions={screenOptions}>
|
|
|
|
<Inside.Screen
|
|
|
|
name='ShareListView'
|
|
|
|
component={ShareListView}
|
|
|
|
/>
|
|
|
|
<Inside.Screen
|
|
|
|
name='ShareView'
|
|
|
|
component={ShareView}
|
|
|
|
/>
|
|
|
|
<Inside.Screen
|
|
|
|
name='SelectServerView'
|
|
|
|
component={SelectServerView}
|
|
|
|
options={SelectServerView.navigationOptions}
|
|
|
|
/>
|
|
|
|
</Inside.Navigator>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const Outside = createStackNavigator();
|
|
|
|
const OutsideStack = () => {
|
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Outside.Navigator screenOptions={{ ...defaultHeader, ...themedHeader(theme) }}>
|
|
|
|
<Outside.Screen
|
|
|
|
name='WithoutServersView'
|
|
|
|
component={WithoutServersView}
|
|
|
|
options={WithoutServersView.navigationOptions}
|
|
|
|
/>
|
|
|
|
</Outside.Navigator>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// App
|
|
|
|
const Stack = createStackNavigator();
|
|
|
|
export const App = ({ root }) => (
|
|
|
|
<Stack.Navigator screenOptions={{ headerShown: false }}>
|
|
|
|
<>
|
|
|
|
{!root ? (
|
|
|
|
<Stack.Screen
|
|
|
|
name='AuthLoading'
|
|
|
|
component={AuthLoadingView}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
{root === 'outside' ? (
|
|
|
|
<Stack.Screen
|
|
|
|
name='OutsideStack'
|
|
|
|
component={OutsideStack}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
{root === 'inside' ? (
|
|
|
|
<Stack.Screen
|
|
|
|
name='InsideStack'
|
|
|
|
component={InsideStack}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
</Stack.Navigator>
|
|
|
|
);
|
|
|
|
|
|
|
|
App.propTypes = {
|
|
|
|
root: PropTypes.string
|
|
|
|
};
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
class Root extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-10-02 12:18:08 +00:00
|
|
|
this.state = {
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: defaultTheme(),
|
|
|
|
themePreferences: {
|
|
|
|
currentTheme: supportSystemTheme() ? 'automatic' : 'light',
|
|
|
|
darkLevel: 'dark'
|
2020-06-15 14:00:46 +00:00
|
|
|
},
|
|
|
|
root: ''
|
2019-10-02 12:18:08 +00:00
|
|
|
};
|
2019-07-29 16:33:28 +00:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
componentWillUnmount() {
|
2019-12-04 16:41:37 +00:00
|
|
|
RocketChat.closeShareExtension();
|
2019-12-04 16:39:53 +00:00
|
|
|
unsubscribeTheme();
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
init = async() => {
|
2019-12-04 16:39:53 +00:00
|
|
|
RNUserDefaults.objectForKey(THEME_PREFERENCES_KEY).then(this.setTheme);
|
2019-07-29 16:33:28 +00:00
|
|
|
const currentServer = await RNUserDefaults.get('currentServer');
|
|
|
|
const token = await RNUserDefaults.get(RocketChat.TOKEN_KEY);
|
|
|
|
|
|
|
|
if (currentServer && token) {
|
2020-05-08 17:04:37 +00:00
|
|
|
await localAuthenticate(currentServer);
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setState({ root: 'inside' });
|
2019-07-29 16:33:28 +00:00
|
|
|
await RocketChat.shareExtensionInit(currentServer);
|
|
|
|
} else {
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setState({ root: 'outside' });
|
2019-07-29 16:33:28 +00:00
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
|
|
|
|
const state = Navigation.navigationRef.current.getRootState();
|
|
|
|
const currentRouteName = getActiveRouteName(state);
|
|
|
|
Navigation.routeNameRef.current = currentRouteName;
|
|
|
|
setCurrentScreen(currentRouteName);
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
setTheme = (newTheme = {}) => {
|
|
|
|
// change theme state
|
|
|
|
this.setState(prevState => newThemeState(prevState, newTheme), () => {
|
|
|
|
const { themePreferences } = this.state;
|
|
|
|
// subscribe to Appearance changes
|
|
|
|
subscribeTheme(themePreferences, this.setTheme);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-18 17:44:02 +00:00
|
|
|
render() {
|
2020-06-15 14:00:46 +00:00
|
|
|
const { theme, root } = this.state;
|
|
|
|
const navTheme = navigationTheme(theme);
|
2019-07-18 17:44:02 +00:00
|
|
|
return (
|
2019-12-04 16:39:53 +00:00
|
|
|
<AppearanceProvider>
|
2020-06-15 14:00:46 +00:00
|
|
|
<Provider store={store}>
|
|
|
|
<ThemeContext.Provider value={{ theme }}>
|
|
|
|
<NavigationContainer
|
|
|
|
theme={navTheme}
|
|
|
|
ref={Navigation.navigationRef}
|
|
|
|
onStateChange={(state) => {
|
|
|
|
const previousRouteName = Navigation.routeNameRef.current;
|
|
|
|
const currentRouteName = getActiveRouteName(state);
|
|
|
|
if (previousRouteName !== currentRouteName) {
|
|
|
|
setCurrentScreen(currentRouteName);
|
|
|
|
}
|
|
|
|
Navigation.routeNameRef.current = currentRouteName;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<App root={root} />
|
|
|
|
</NavigationContainer>
|
|
|
|
<ScreenLockedView />
|
|
|
|
</ThemeContext.Provider>
|
|
|
|
</Provider>
|
2019-12-04 16:39:53 +00:00
|
|
|
</AppearanceProvider>
|
2019-07-18 17:44:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Root;
|