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
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
import { defaultTheme, newThemeState, subscribeTheme, unsubscribeTheme } from './utils/theme';
|
2020-08-19 17:14:22 +00:00
|
|
|
import UserPreferences from './lib/userPreferences';
|
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';
|
2019-06-10 16:23:19 +00:00
|
|
|
import { initializePushNotifications, onNotification } from './notifications/push';
|
2019-03-12 16:23:06 +00:00
|
|
|
import store from './lib/createStore';
|
2021-08-23 14:15:01 +00:00
|
|
|
import { toggleAnalyticsEventsReport, toggleCrashErrorsReport } from './utils/log';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { ThemeContext } from './theme';
|
2020-06-17 17:35:58 +00:00
|
|
|
import { DimensionsContext } from './dimensions';
|
2019-12-04 16:39:53 +00:00
|
|
|
import RocketChat, { THEME_PREFERENCES_KEY } from './lib/rocketchat';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { MIN_WIDTH_MASTER_DETAIL_LAYOUT } from './constants/tablet';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { isTablet, supportSystemTheme } 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';
|
2020-08-24 12:24:10 +00:00
|
|
|
import { isFDroidBuild } from './constants/environment';
|
2020-06-16 20:32:30 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
RNScreens.enableScreens();
|
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 {
|
|
|
|
theme: string;
|
|
|
|
themePreferences: {
|
|
|
|
currentTheme: 'automatic' | 'light';
|
|
|
|
darkLevel: string;
|
|
|
|
};
|
|
|
|
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\//;
|
|
|
|
if (url.match(call)) {
|
|
|
|
url = url.replace(call, '').trim();
|
|
|
|
if (url) {
|
|
|
|
return { path: url, isCall: true };
|
|
|
|
}
|
|
|
|
}
|
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');
|
2019-11-25 20:01:17 +00:00
|
|
|
this.state = {
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: defaultTheme(),
|
|
|
|
themePreferences: {
|
|
|
|
currentTheme: supportSystemTheme() ? 'automatic' : 'light',
|
2021-07-12 18:05:09 +00:00
|
|
|
darkLevel: 'black'
|
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 () => {
|
|
|
|
UserPreferences.getMapAsync(THEME_PREFERENCES_KEY).then((theme: any) => this.setTheme(theme));
|
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(
|
|
|
|
prevState => newThemeState(prevState, newTheme),
|
|
|
|
() => {
|
|
|
|
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);
|
2021-09-13 20:41:05 +00:00
|
|
|
this.onKeyCommands = KeyCommandsEmitter.addListener('onKeyCommand', (command: unknown) => {
|
|
|
|
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 (
|
2020-06-16 20:32:30 +00:00
|
|
|
<SafeAreaProvider initialMetrics={initialWindowMetrics}>
|
|
|
|
<AppearanceProvider>
|
|
|
|
<Provider store={store}>
|
|
|
|
<ThemeContext.Provider
|
|
|
|
value={{
|
|
|
|
theme,
|
|
|
|
themePreferences,
|
|
|
|
setTheme: this.setTheme
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|