Chore: Hooks App/share (#4417)

* Chore: Hooks App/share

* getTheme to outside

* update types

Co-authored-by: Gleidson Daniel <gleidson10daniel@hotmail.com>
This commit is contained in:
Reinaldo Neto 2022-08-18 20:25:39 -03:00 committed by GitHub
parent 3ac10ee860
commit f0ed501450
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 56 additions and 125 deletions

View File

@ -1,26 +1,18 @@
import React, { useContext } from 'react'; import React, { useContext, useEffect, useLayoutEffect, useState } from 'react';
import { Dimensions } from 'react-native'; import { Dimensions } from 'react-native';
import { NavigationContainer } from '@react-navigation/native'; import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack'; import { createStackNavigator } from '@react-navigation/stack';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import { import { getTheme, setNativeTheme, initialTheme as initialThemeFunction, unsubscribeTheme } from './lib/methods/helpers/theme';
getTheme,
initialTheme,
newThemeState,
setNativeTheme,
subscribeTheme,
unsubscribeTheme
} from './lib/methods/helpers/theme';
import UserPreferences from './lib/methods/userPreferences'; import UserPreferences from './lib/methods/userPreferences';
import Navigation from './lib/navigation/shareNavigation'; import Navigation from './lib/navigation/shareNavigation';
import store from './lib/store'; import store from './lib/store';
import { initStore } from './lib/store/auxStore'; import { initStore } from './lib/store/auxStore';
import { closeShareExtension, shareExtensionInit } from './lib/methods/shareExtension'; import { closeShareExtension, shareExtensionInit } from './lib/methods/shareExtension';
import { defaultHeader, getActiveRouteName, navigationTheme, themedHeader } from './lib/methods/helpers/navigation'; import { defaultHeader, getActiveRouteName, navigationTheme, themedHeader } from './lib/methods/helpers/navigation';
import { ThemeContext, TSupportedThemes } from './theme'; import { ThemeContext } from './theme';
import { localAuthenticate } from './lib/methods/helpers/localAuthentication'; import { localAuthenticate } from './lib/methods/helpers/localAuthentication';
import { IThemePreference } from './definitions/ITheme';
import ScreenLockedView from './views/ScreenLockedView'; import ScreenLockedView from './views/ScreenLockedView';
// Outside Stack // Outside Stack
import WithoutServersView from './views/WithoutServersView'; import WithoutServersView from './views/WithoutServersView';
@ -31,29 +23,11 @@ import SelectServerView from './views/SelectServerView';
import { setCurrentScreen } from './lib/methods/helpers/log'; import { setCurrentScreen } from './lib/methods/helpers/log';
import AuthLoadingView from './views/AuthLoadingView'; import AuthLoadingView from './views/AuthLoadingView';
import { DimensionsContext } from './dimensions'; import { DimensionsContext } from './dimensions';
import { debounce } from './lib/methods/helpers';
import { ShareInsideStackParamList, ShareOutsideStackParamList, ShareAppStackParamList } from './definitions/navigationTypes'; import { ShareInsideStackParamList, ShareOutsideStackParamList, ShareAppStackParamList } from './definitions/navigationTypes';
import { colors, CURRENT_SERVER } from './lib/constants'; import { colors, CURRENT_SERVER } from './lib/constants';
initStore(store); initStore(store);
interface IDimensions {
width: number;
height: number;
scale: number;
fontScale: number;
}
interface IState {
theme: TSupportedThemes;
themePreferences: IThemePreference;
root: any;
width: number;
height: number;
scale: number;
fontScale: number;
}
const Inside = createStackNavigator<ShareInsideStackParamList>(); const Inside = createStackNavigator<ShareInsideStackParamList>();
const InsideStack = () => { const InsideStack = () => {
const { theme } = useContext(ThemeContext); const { theme } = useContext(ThemeContext);
@ -86,7 +60,7 @@ const OutsideStack = () => {
// App // App
const Stack = createStackNavigator<ShareAppStackParamList>(); const Stack = createStackNavigator<ShareAppStackParamList>();
export const App = ({ root }: any) => ( export const App = ({ root }: { root: string }): React.ReactElement => (
<Stack.Navigator screenOptions={{ headerShown: false, animationEnabled: false }}> <Stack.Navigator screenOptions={{ headerShown: false, animationEnabled: false }}>
<> <>
{!root ? <Stack.Screen name='AuthLoading' component={AuthLoadingView} /> : null} {!root ? <Stack.Screen name='AuthLoading' component={AuthLoadingView} /> : null}
@ -96,84 +70,43 @@ export const App = ({ root }: any) => (
</Stack.Navigator> </Stack.Navigator>
); );
class Root extends React.Component<{}, IState> { const { width, height, scale, fontScale } = Dimensions.get('screen');
private mounted = false; const initialTheme = initialThemeFunction();
const theme = getTheme(initialTheme);
constructor(props: any) { const Root = (): React.ReactElement => {
super(props); const [root, setRoot] = useState('');
const { width, height, scale, fontScale } = Dimensions.get('screen'); const navTheme = navigationTheme(theme);
const theme = initialTheme();
this.state = {
theme: getTheme(theme),
themePreferences: theme,
root: '',
width,
height,
scale,
fontScale
};
this.init();
setNativeTheme(theme);
}
componentDidMount() { useLayoutEffect(() => {
this.mounted = true; setNativeTheme(initialTheme);
} }, []);
componentWillUnmount(): void { useEffect(() => {
closeShareExtension(); const authenticateShare = async (currentServer: string) => {
unsubscribeTheme();
}
init = async () => {
const currentServer = UserPreferences.getString(CURRENT_SERVER);
if (currentServer) {
await localAuthenticate(currentServer); await localAuthenticate(currentServer);
this.setState({ root: 'inside' }); setRoot('inside');
await shareExtensionInit(currentServer); await shareExtensionInit(currentServer);
} else if (this.mounted) { };
this.setState({ root: 'outside' });
const currentServer = UserPreferences.getString(CURRENT_SERVER);
if (currentServer) {
authenticateShare(currentServer);
} else { } else {
// @ts-ignore setRoot('outside');
this.state.root = 'outside';
} }
const state = Navigation.navigationRef.current?.getRootState(); const state = Navigation.navigationRef.current?.getRootState();
const currentRouteName = getActiveRouteName(state); const currentRouteName = getActiveRouteName(state);
Navigation.routeNameRef.current = currentRouteName; Navigation.routeNameRef.current = currentRouteName;
setCurrentScreen(currentRouteName); setCurrentScreen(currentRouteName);
return () => {
closeShareExtension();
unsubscribeTheme();
}; };
}, []);
setTheme = (newTheme = {}) => {
// change theme state
this.setState(
prevState => newThemeState(prevState, newTheme as IThemePreference),
() => {
const { themePreferences } = this.state;
// subscribe to Appearance changes
subscribeTheme(themePreferences, this.setTheme);
}
);
};
// Dimensions update fires twice
onDimensionsChange = debounce(({ window: { width, height, scale, fontScale } }: { window: IDimensions }) => {
this.setDimensions({ width, height, scale, fontScale });
});
setDimensions = ({ width, height, scale, fontScale }: IDimensions) => {
this.setState({
width,
height,
scale,
fontScale
});
};
render() {
const { theme, root, width, height, scale, fontScale } = this.state;
const navTheme = navigationTheme(theme);
return ( return (
<Provider store={store}> <Provider store={store}>
<ThemeContext.Provider value={{ theme, colors: colors[theme] }}> <ThemeContext.Provider value={{ theme, colors: colors[theme] }}>
@ -182,8 +115,7 @@ class Root extends React.Component<{}, IState> {
width, width,
height, height,
scale, scale,
fontScale, fontScale
setDimensions: this.setDimensions
}} }}
> >
<NavigationContainer <NavigationContainer
@ -205,7 +137,6 @@ class Root extends React.Component<{}, IState> {
</ThemeContext.Provider> </ThemeContext.Provider>
</Provider> </Provider>
); );
} };
}
export default Root; export default Root;