2019-03-12 16:23:06 +00:00
|
|
|
import React from 'react';
|
2023-05-18 21:09:33 +00:00
|
|
|
import { Dimensions, Linking, EmitterSubscription } from 'react-native';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { initialWindowMetrics, SafeAreaProvider } from 'react-native-safe-area-context';
|
2020-06-15 14:00:46 +00:00
|
|
|
import RNScreens from 'react-native-screens';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Provider } from 'react-redux';
|
2022-06-06 13:23:49 +00:00
|
|
|
import { GestureHandlerRootView } from 'react-native-gesture-handler';
|
2017-09-01 19:42:50 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
import { appInit, appInitLocalSettings, setMasterDetail as setMasterDetailAction } from './actions/app';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { deepLinkingOpen } from './actions/deepLinking';
|
2020-06-15 14:00:46 +00:00
|
|
|
import AppContainer from './AppContainer';
|
2020-06-16 20:32:30 +00:00
|
|
|
import { ActionSheetProvider } from './containers/ActionSheet';
|
2022-04-28 20:37:25 +00:00
|
|
|
import InAppNotification from './containers/InAppNotification';
|
|
|
|
import Toast from './containers/Toast';
|
|
|
|
import TwoFactor from './containers/TwoFactor';
|
2022-08-19 21:14:37 +00:00
|
|
|
import Loading from './containers/Loading';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { IThemePreference } from './definitions/ITheme';
|
|
|
|
import { DimensionsContext } from './dimensions';
|
|
|
|
import { colors, isFDroidBuild, MIN_WIDTH_MASTER_DETAIL_LAYOUT, themes } from './lib/constants';
|
|
|
|
import { getAllowAnalyticsEvents, getAllowCrashReport } from './lib/methods';
|
|
|
|
import parseQuery from './lib/methods/helpers/parseQuery';
|
|
|
|
import { initializePushNotifications, onNotification } from './lib/notifications';
|
2022-04-07 14:19:54 +00:00
|
|
|
import store from './lib/store';
|
|
|
|
import { initStore } from './lib/store/auxStore';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { ThemeContext, TSupportedThemes } from './theme';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { debounce, isTablet } from './lib/methods/helpers';
|
|
|
|
import { toggleAnalyticsEventsReport, toggleCrashErrorsReport } from './lib/methods/helpers/log';
|
2022-07-15 16:39:07 +00:00
|
|
|
import {
|
|
|
|
getTheme,
|
|
|
|
initialTheme,
|
|
|
|
newThemeState,
|
|
|
|
setNativeTheme,
|
|
|
|
subscribeTheme,
|
|
|
|
unsubscribeTheme
|
|
|
|
} from './lib/methods/helpers/theme';
|
2022-04-28 20:37:25 +00:00
|
|
|
import ChangePasscodeView from './views/ChangePasscodeView';
|
|
|
|
import ScreenLockedView from './views/ScreenLockedView';
|
2020-06-16 20:32:30 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
RNScreens.enableScreens();
|
2022-02-09 21:16:20 +00:00
|
|
|
initStore(store);
|
2018-11-14 21:42:03 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface IDimensions {
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
scale: number;
|
|
|
|
fontScale: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
2022-03-31 23:04:29 +00:00
|
|
|
theme: TSupportedThemes;
|
2022-01-12 12:54:04 +00:00
|
|
|
themePreferences: IThemePreference;
|
2021-09-13 20:41:05 +00:00
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
scale: number;
|
|
|
|
fontScale: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parseDeepLinking = (url: string) => {
|
2018-07-10 13:40:32 +00:00
|
|
|
if (url) {
|
|
|
|
url = url.replace(/rocketchat:\/\/|https:\/\/go.rocket.chat\//, '');
|
2020-01-28 13:22:35 +00:00
|
|
|
const regex = /^(room|auth|invite)\?/;
|
2018-07-10 13:40:32 +00:00
|
|
|
if (url.match(regex)) {
|
2019-03-18 18:52:38 +00:00
|
|
|
url = url.replace(regex, '').trim();
|
|
|
|
if (url) {
|
|
|
|
return parseQuery(url);
|
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2020-07-30 17:25:52 +00:00
|
|
|
const call = /^(https:\/\/)?jitsi.rocket.chat\//;
|
2021-11-16 16:04:33 +00:00
|
|
|
const fullURL = url;
|
|
|
|
|
2020-07-30 17:25:52 +00:00
|
|
|
if (url.match(call)) {
|
|
|
|
url = url.replace(call, '').trim();
|
|
|
|
if (url) {
|
2021-11-16 16:04:33 +00:00
|
|
|
return { path: url, isCall: true, fullURL };
|
2020-07-30 17:25:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2019-03-18 18:52:38 +00:00
|
|
|
return null;
|
2018-07-10 13:40:32 +00:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
export default class Root extends React.Component<{}, IState> {
|
|
|
|
private listenerTimeout!: any;
|
2023-05-18 21:09:33 +00:00
|
|
|
private dimensionsListener?: EmitterSubscription;
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
constructor(props: any) {
|
2019-03-18 18:52:38 +00:00
|
|
|
super(props);
|
|
|
|
this.init();
|
2020-08-24 12:24:10 +00:00
|
|
|
if (!isFDroidBuild) {
|
|
|
|
this.initCrashReport();
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
const { width, height, scale, fontScale } = Dimensions.get('window');
|
2022-03-09 19:41:26 +00:00
|
|
|
const theme = initialTheme();
|
2019-11-25 20:01:17 +00:00
|
|
|
this.state = {
|
2022-03-09 19:41:26 +00:00
|
|
|
theme: getTheme(theme),
|
|
|
|
themePreferences: theme,
|
2020-06-17 17:35:58 +00:00
|
|
|
width,
|
|
|
|
height,
|
2020-10-30 13:59:44 +00:00
|
|
|
scale,
|
|
|
|
fontScale
|
2019-11-25 20:01:17 +00:00
|
|
|
};
|
|
|
|
if (isTablet) {
|
|
|
|
this.initTablet();
|
|
|
|
}
|
2022-07-15 16:39:07 +00:00
|
|
|
setNativeTheme(theme);
|
2023-05-18 21:09:33 +00:00
|
|
|
subscribeTheme(theme, this.state.theme, this.setTheme);
|
2019-03-18 18:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.listenerTimeout = setTimeout(() => {
|
|
|
|
Linking.addEventListener('url', ({ url }) => {
|
|
|
|
const parsedDeepLinkingURL = parseDeepLinking(url);
|
|
|
|
if (parsedDeepLinkingURL) {
|
|
|
|
store.dispatch(deepLinkingOpen(parsedDeepLinkingURL));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 5000);
|
2023-05-18 21:09:33 +00:00
|
|
|
this.dimensionsListener = Dimensions.addEventListener('change', this.onDimensionsChange);
|
2019-11-25 20:01:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 18:52:38 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
clearTimeout(this.listenerTimeout);
|
2023-05-18 21:09:33 +00:00
|
|
|
if (this.dimensionsListener) {
|
|
|
|
this.dimensionsListener.remove();
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
unsubscribeTheme();
|
2019-03-18 18:52:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
init = async () => {
|
2019-12-04 16:50:22 +00:00
|
|
|
store.dispatch(appInitLocalSettings());
|
2021-03-05 16:10:21 +00:00
|
|
|
|
|
|
|
// Open app from push notification
|
|
|
|
const notification = await initializePushNotifications();
|
2019-03-18 18:52:38 +00:00
|
|
|
if (notification) {
|
|
|
|
onNotification(notification);
|
2021-03-05 16:10:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open app from deep linking
|
|
|
|
const deepLinking = await Linking.getInitialURL();
|
2021-09-13 20:41:05 +00:00
|
|
|
const parsedDeepLinkingURL = parseDeepLinking(deepLinking!);
|
2021-03-05 16:10:21 +00:00
|
|
|
if (parsedDeepLinkingURL) {
|
2019-03-18 18:52:38 +00:00
|
|
|
store.dispatch(deepLinkingOpen(parsedDeepLinkingURL));
|
2021-03-05 16:10:21 +00:00
|
|
|
return;
|
2019-03-18 18:52:38 +00:00
|
|
|
}
|
2021-03-05 16:10:21 +00:00
|
|
|
|
|
|
|
// Open app from app icon
|
|
|
|
store.dispatch(appInit());
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-03-18 18:52:38 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
getMasterDetail = (width: number) => {
|
2020-06-15 14:00:46 +00:00
|
|
|
if (!isTablet) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return width > MIN_WIDTH_MASTER_DETAIL_LAYOUT;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
setMasterDetail = (width: number) => {
|
2020-06-15 14:00:46 +00:00
|
|
|
const isMasterDetail = this.getMasterDetail(width);
|
|
|
|
store.dispatch(setMasterDetailAction(isMasterDetail));
|
|
|
|
};
|
|
|
|
|
2020-06-17 17:35:58 +00:00
|
|
|
// Dimensions update fires twice
|
2021-09-13 20:41:05 +00:00
|
|
|
onDimensionsChange = debounce(({ window: { width, height, scale, fontScale } }: { window: IDimensions }) => {
|
2020-10-30 13:59:44 +00:00
|
|
|
this.setDimensions({
|
2021-09-13 20:41:05 +00:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
scale,
|
|
|
|
fontScale
|
2020-10-30 13:59:44 +00:00
|
|
|
});
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setMasterDetail(width);
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2020-06-15 14:00:46 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
setTheme = (newTheme = {}) => {
|
|
|
|
// change theme state
|
2021-09-13 20:41:05 +00:00
|
|
|
this.setState(
|
2022-01-12 12:54:04 +00:00
|
|
|
prevState => newThemeState(prevState, newTheme as IThemePreference),
|
2021-09-13 20:41:05 +00:00
|
|
|
() => {
|
2023-05-18 21:09:33 +00:00
|
|
|
const { themePreferences, theme } = this.state;
|
2021-09-13 20:41:05 +00:00
|
|
|
// subscribe to Appearance changes
|
2023-05-18 21:09:33 +00:00
|
|
|
subscribeTheme(themePreferences, theme, this.setTheme);
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
setDimensions = ({ width, height, scale, fontScale }: IDimensions) => {
|
|
|
|
this.setState({ width, height, scale, fontScale });
|
|
|
|
};
|
2020-06-17 17:35:58 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
initTablet = () => {
|
2020-06-17 17:35:58 +00:00
|
|
|
const { width } = this.state;
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setMasterDetail(width);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-11-25 20:01:17 +00:00
|
|
|
|
2019-08-23 13:18:47 +00:00
|
|
|
initCrashReport = () => {
|
2022-04-28 20:37:25 +00:00
|
|
|
getAllowCrashReport().then(allowCrashReport => {
|
2021-09-13 20:41:05 +00:00
|
|
|
toggleCrashErrorsReport(allowCrashReport);
|
|
|
|
});
|
2022-04-28 20:37:25 +00:00
|
|
|
getAllowAnalyticsEvents().then(allowAnalyticsEvents => {
|
2021-09-13 20:41:05 +00:00
|
|
|
toggleAnalyticsEventsReport(allowAnalyticsEvents);
|
|
|
|
});
|
|
|
|
};
|
2019-08-23 13:18:47 +00:00
|
|
|
|
2019-03-18 18:52:38 +00:00
|
|
|
render() {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { themePreferences, theme, width, height, scale, fontScale } = this.state;
|
2019-03-18 18:52:38 +00:00
|
|
|
return (
|
2022-03-09 19:41:26 +00:00
|
|
|
<SafeAreaProvider
|
|
|
|
initialMetrics={initialWindowMetrics}
|
2022-08-08 21:02:08 +00:00
|
|
|
style={{ backgroundColor: themes[this.state.theme].backgroundColor }}
|
|
|
|
>
|
|
|
|
<Provider store={store}>
|
|
|
|
<ThemeContext.Provider
|
|
|
|
value={{
|
|
|
|
theme,
|
|
|
|
themePreferences,
|
|
|
|
setTheme: this.setTheme,
|
|
|
|
colors: colors[theme]
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<DimensionsContext.Provider
|
2020-06-16 20:32:30 +00:00
|
|
|
value={{
|
2022-08-08 21:02:08 +00:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
scale,
|
|
|
|
fontScale,
|
|
|
|
setDimensions: this.setDimensions
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<GestureHandlerRootView style={{ flex: 1 }}>
|
|
|
|
<ActionSheetProvider>
|
|
|
|
<AppContainer />
|
|
|
|
<TwoFactor />
|
|
|
|
<ScreenLockedView />
|
|
|
|
<ChangePasscodeView />
|
|
|
|
<InAppNotification />
|
|
|
|
<Toast />
|
2022-08-19 21:14:37 +00:00
|
|
|
<Loading />
|
2022-08-08 21:02:08 +00:00
|
|
|
</ActionSheetProvider>
|
|
|
|
</GestureHandlerRootView>
|
|
|
|
</DimensionsContext.Provider>
|
|
|
|
</ThemeContext.Provider>
|
|
|
|
</Provider>
|
2020-06-16 20:32:30 +00:00
|
|
|
</SafeAreaProvider>
|
2019-03-18 18:52:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|