2019-03-12 16:23:06 +00:00
|
|
|
import React from 'react';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { Linking, Dimensions } 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
|
|
|
|
2019-12-04 16:39:53 +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';
|
2019-08-23 13:18:47 +00:00
|
|
|
import { loggerConfig, analytics } 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';
|
2019-11-25 20:01:17 +00:00
|
|
|
import {
|
2020-06-15 14:00:46 +00:00
|
|
|
isTablet, supportSystemTheme
|
2019-11-25 20:01:17 +00:00
|
|
|
} from './utils/deviceInfo';
|
|
|
|
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
|
|
|
|
2019-03-18 18:52:38 +00:00
|
|
|
const parseDeepLinking = (url) => {
|
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
|
|
|
};
|
|
|
|
|
2019-03-18 18:52:38 +00:00
|
|
|
export default class Root extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.init();
|
2020-08-24 12:24:10 +00:00
|
|
|
if (!isFDroidBuild) {
|
|
|
|
this.initCrashReport();
|
|
|
|
}
|
2020-10-30 13:59:44 +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',
|
|
|
|
darkLevel: 'dark'
|
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
|
|
|
}
|
|
|
|
|
|
|
|
init = async() => {
|
2020-08-19 17:14:22 +00:00
|
|
|
UserPreferences.getMapAsync(THEME_PREFERENCES_KEY).then(this.setTheme);
|
2019-12-04 16:50:22 +00:00
|
|
|
store.dispatch(appInitLocalSettings());
|
2021-03-15 20:16:34 +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-15 20:16:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open app from deep linking
|
|
|
|
const deepLinking = await Linking.getInitialURL();
|
|
|
|
const parsedDeepLinkingURL = parseDeepLinking(deepLinking);
|
|
|
|
if (parsedDeepLinkingURL) {
|
2019-03-18 18:52:38 +00:00
|
|
|
store.dispatch(deepLinkingOpen(parsedDeepLinkingURL));
|
2021-03-15 20:16:34 +00:00
|
|
|
return;
|
2019-03-18 18:52:38 +00:00
|
|
|
}
|
2021-03-15 20:16:34 +00:00
|
|
|
|
|
|
|
// Open app from app icon
|
|
|
|
store.dispatch(appInit());
|
2019-03-18 18:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
getMasterDetail = (width) => {
|
|
|
|
if (!isTablet) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return width > MIN_WIDTH_MASTER_DETAIL_LAYOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
setMasterDetail = (width) => {
|
|
|
|
const isMasterDetail = this.getMasterDetail(width);
|
|
|
|
store.dispatch(setMasterDetailAction(isMasterDetail));
|
|
|
|
};
|
|
|
|
|
2020-06-17 17:35:58 +00:00
|
|
|
// Dimensions update fires twice
|
2020-10-30 13:59:44 +00:00
|
|
|
onDimensionsChange = debounce(({
|
|
|
|
window: {
|
|
|
|
width, height, scale, fontScale
|
|
|
|
}
|
|
|
|
}) => {
|
|
|
|
this.setDimensions({
|
|
|
|
width, height, scale, fontScale
|
|
|
|
});
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setMasterDetail(width);
|
2020-06-17 17:35:58 +00:00
|
|
|
})
|
2020-06-15 14:00:46 +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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:59:44 +00:00
|
|
|
setDimensions = ({
|
|
|
|
width, height, scale, fontScale
|
|
|
|
}) => {
|
|
|
|
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);
|
2019-11-25 20:01:17 +00:00
|
|
|
this.onKeyCommands = KeyCommandsEmitter.addListener(
|
|
|
|
'onKeyCommand',
|
2020-06-15 14:00:46 +00:00
|
|
|
(command) => {
|
|
|
|
EventEmitter.emit(KEY_COMMAND, { event: command });
|
|
|
|
}
|
2019-11-25 20:01:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:18:47 +00:00
|
|
|
initCrashReport = () => {
|
|
|
|
RocketChat.getAllowCrashReport()
|
|
|
|
.then((allowCrashReport) => {
|
|
|
|
if (!allowCrashReport) {
|
|
|
|
loggerConfig.autoNotify = false;
|
|
|
|
loggerConfig.registerBeforeSendCallback(() => false);
|
|
|
|
analytics().setAnalyticsCollectionEnabled(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-18 18:52:38 +00:00
|
|
|
render() {
|
2020-06-17 17:35:58 +00:00
|
|
|
const {
|
2020-10-30 13:59:44 +00:00
|
|
|
themePreferences, theme, width, height, scale, fontScale
|
2020-06-17 17:35:58 +00:00
|
|
|
} = 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
|
|
|
|
}}
|
|
|
|
>
|
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
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|