2018-07-10 13:40:32 +00:00
|
|
|
import { Component } from 'react';
|
|
|
|
import { Linking } from 'react-native';
|
|
|
|
import { Navigation } from 'react-native-navigation';
|
2017-09-01 19:42:50 +00:00
|
|
|
|
2017-08-21 00:11:46 +00:00
|
|
|
import store from './lib/createStore';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { appInit } from './actions';
|
|
|
|
import database from './lib/realm';
|
|
|
|
import { iconsLoaded } from './Icons';
|
|
|
|
import { registerScreens } from './views';
|
|
|
|
import { deepLinkingOpen } from './actions/deepLinking';
|
|
|
|
import parseQuery from './lib/methods/helpers/parseQuery';
|
|
|
|
import I18n from './i18n';
|
|
|
|
import { initializePushNotifications } from './push';
|
2017-08-21 00:11:46 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
const startLogged = () => {
|
|
|
|
Navigation.startSingleScreenApp({
|
|
|
|
screen: {
|
|
|
|
screen: 'RoomsListView',
|
|
|
|
title: I18n.t('Messages')
|
|
|
|
},
|
|
|
|
drawer: {
|
|
|
|
left: {
|
|
|
|
screen: 'Sidebar'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
animationType: 'fade'
|
|
|
|
});
|
|
|
|
};
|
2017-08-21 00:11:46 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
const startNotLogged = (route) => {
|
|
|
|
Navigation.startSingleScreenApp({
|
|
|
|
screen: {
|
|
|
|
screen: route,
|
|
|
|
title: route === 'NewServerView' ? I18n.t('New_Server') : I18n.t('Servers')
|
|
|
|
},
|
|
|
|
animationType: 'fade'
|
|
|
|
});
|
|
|
|
};
|
2017-08-22 01:24:41 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
const hasServers = () => {
|
|
|
|
const db = database.databases.serversDB.objects('servers');
|
|
|
|
return db.length > 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleOpenURL = ({ url }) => {
|
|
|
|
if (url) {
|
|
|
|
url = url.replace(/rocketchat:\/\/|https:\/\/go.rocket.chat\//, '');
|
|
|
|
const regex = /^(room|auth)\?/;
|
|
|
|
if (url.match(regex)) {
|
|
|
|
url = url.replace(regex, '');
|
|
|
|
const params = parseQuery(url);
|
|
|
|
store.dispatch(deepLinkingOpen(params));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
registerScreens(store);
|
|
|
|
iconsLoaded();
|
|
|
|
|
|
|
|
export default class App extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
store.dispatch(appInit());
|
|
|
|
store.subscribe(this.onStoreUpdate.bind(this));
|
|
|
|
initializePushNotifications();
|
|
|
|
|
|
|
|
Linking
|
|
|
|
.getInitialURL()
|
|
|
|
.then(url => handleOpenURL({ url }))
|
|
|
|
.catch(e => console.warn(e));
|
|
|
|
Linking.addEventListener('url', handleOpenURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
onStoreUpdate = () => {
|
|
|
|
const { root } = store.getState().app;
|
|
|
|
|
|
|
|
if (this.currentRoot !== root) {
|
|
|
|
this.currentRoot = root;
|
|
|
|
if (root === 'outside') {
|
|
|
|
if (hasServers()) {
|
|
|
|
startNotLogged('ListServerView');
|
|
|
|
} else {
|
|
|
|
startNotLogged('NewServerView');
|
|
|
|
}
|
|
|
|
} else if (root === 'inside') {
|
|
|
|
startLogged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setDeviceToken(deviceToken) {
|
|
|
|
this.deviceToken = deviceToken;
|
|
|
|
}
|
|
|
|
}
|