2019-03-12 16:23:06 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { Dimensions, Linking } from 'react-native';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { AppearanceProvider } from 'react-native-appearance';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { Provider } from 'react-redux';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { KeyCommandsEmitter } from 'react-native-keycommands';
|
|
|
|
import RNScreens from 'react-native-screens';
|
2020-06-16 20:32:30 +00:00
|
|
|
import { SafeAreaProvider, initialWindowMetrics } from 'react-native-safe-area-context';
|
2017-09-01 19:42:50 +00:00
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
import { getTheme, initialTheme, newThemeState, subscribeTheme, unsubscribeTheme } from './utils/theme';
|
2019-11-25 20:01:17 +00:00
|
|
|
import EventEmitter from './utils/events';
|
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';
|
|
|
|
import parseQuery from './lib/methods/helpers/parseQuery';
|
2022-04-12 16:37:28 +00:00
|
|
|
import { initializePushNotifications, onNotification } from './lib/notifications';
|
2021-08-23 14:15:01 +00:00
|
|
|
import { toggleAnalyticsEventsReport, toggleCrashErrorsReport } from './utils/log';
|
2022-03-31 23:04:29 +00:00
|
|
|
import { ThemeContext, TSupportedThemes } from './theme';
|
2020-06-17 17:35:58 +00:00
|
|
|
import { DimensionsContext } from './dimensions';
|
2022-03-09 19:41:26 +00:00
|
|
|
import RocketChat from './lib/rocketchat';
|
|
|
|
import { isTablet } from './utils/deviceInfo';
|
2019-11-25 20:01:17 +00:00
|
|
|
import { KEY_COMMAND } from './commands';
|
2020-06-15 14:00:46 +00:00
|
|
|
import AppContainer from './AppContainer';
|
2020-04-01 20:32:24 +00:00
|
|
|
import TwoFactor from './containers/TwoFactor';
|
2020-05-08 17:04:37 +00:00
|
|
|
import ScreenLockedView from './views/ScreenLockedView';
|
|
|
|
import ChangePasscodeView from './views/ChangePasscodeView';
|
2020-06-16 20:32:30 +00:00
|
|
|
import Toast from './containers/Toast';
|
|
|
|
import InAppNotification from './containers/InAppNotification';
|
|
|
|
import { ActionSheetProvider } from './containers/ActionSheet';
|
2020-06-17 17:35:58 +00:00
|
|
|
import debounce from './utils/debounce';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { isFDroidBuild, MIN_WIDTH_MASTER_DETAIL_LAYOUT, colors, themes } from './lib/constants';
|
2022-01-12 12:54:04 +00:00
|
|
|
import { IThemePreference } from './definitions/ITheme';
|
|
|
|
import { ICommand } from './definitions/ICommand';
|
2022-04-07 14:19:54 +00:00
|
|
|
import store from './lib/store';
|
|
|
|
import { initStore } from './lib/store/auxStore';
|
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;
|
|
|
|
|
|
|
|
private onKeyCommands: any;
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
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);
|
2020-06-15 14:00:46 +00:00
|
|
|
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);
|
2020-06-15 14:00:46 +00:00
|
|
|
Dimensions.removeEventListener('change', this.onDimensionsChange);
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
unsubscribeTheme();
|
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
if (this.onKeyCommands && this.onKeyCommands.remove) {
|
|
|
|
this.onKeyCommands.remove();
|
|
|
|
}
|
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
|
|
|
() => {
|
|
|
|
const { themePreferences } = this.state;
|
|
|
|
// subscribe to Appearance changes
|
|
|
|
subscribeTheme(themePreferences, this.setTheme);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
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);
|
2022-01-12 12:54:04 +00:00
|
|
|
this.onKeyCommands = KeyCommandsEmitter.addListener('onKeyCommand', (command: ICommand) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
EventEmitter.emit(KEY_COMMAND, { event: command });
|
|
|
|
});
|
|
|
|
};
|
2019-11-25 20:01:17 +00:00
|
|
|
|
2019-08-23 13:18:47 +00:00
|
|
|
initCrashReport = () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
RocketChat.getAllowCrashReport().then(allowCrashReport => {
|
|
|
|
toggleCrashErrorsReport(allowCrashReport);
|
|
|
|
});
|
|
|
|
RocketChat.getAllowAnalyticsEvents().then(allowAnalyticsEvents => {
|
|
|
|
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}
|
|
|
|
style={{ backgroundColor: themes[this.state.theme].backgroundColor }}>
|
2020-06-16 20:32:30 +00:00
|
|
|
<AppearanceProvider>
|
|
|
|
<Provider store={store}>
|
|
|
|
<ThemeContext.Provider
|
|
|
|
value={{
|
|
|
|
theme,
|
|
|
|
themePreferences,
|
2022-03-31 23:04:29 +00:00
|
|
|
setTheme: this.setTheme,
|
|
|
|
colors: colors[theme]
|
2021-09-13 20:41:05 +00:00
|
|
|
}}>
|
2020-06-17 17:35:58 +00:00
|
|
|
<DimensionsContext.Provider
|
|
|
|
value={{
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
scale,
|
2020-10-30 13:59:44 +00:00
|
|
|
fontScale,
|
2020-06-17 17:35:58 +00:00
|
|
|
setDimensions: this.setDimensions
|
2021-09-13 20:41:05 +00:00
|
|
|
}}>
|
2020-06-17 17:35:58 +00:00
|
|
|
<ActionSheetProvider>
|
|
|
|
<AppContainer />
|
|
|
|
<TwoFactor />
|
|
|
|
<ScreenLockedView />
|
|
|
|
<ChangePasscodeView />
|
|
|
|
<InAppNotification />
|
|
|
|
<Toast />
|
|
|
|
</ActionSheetProvider>
|
|
|
|
</DimensionsContext.Provider>
|
2020-06-16 20:32:30 +00:00
|
|
|
</ThemeContext.Provider>
|
|
|
|
</Provider>
|
|
|
|
</AppearanceProvider>
|
|
|
|
</SafeAreaProvider>
|
2019-03-18 18:52:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|