Skip to content

Commit

Permalink
[FIX] Android stuck on splash screen after hardware back button is pr…
Browse files Browse the repository at this point in the history
…essed (#550)

* [FIX] Android stuck on splash screen after hardware button is pressed

* Fix empty user at asyncstorage

* Remove unused subscribe
  • Loading branch information
diegolmello committed Nov 27, 2018
1 parent 35fac39 commit e66dbd8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 20 deletions.
1 change: 0 additions & 1 deletion app/lib/rocketchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ const RocketChat = {

SDK.driver.on('connected', () => {
reduxStore.dispatch(connectSuccess());
SDK.driver.subscribe('meteor.loginServiceConfiguration');
SDK.driver.subscribe('activeUsers');
SDK.driver.subscribe('roles');
RocketChat.getSettings();
Expand Down
6 changes: 2 additions & 4 deletions app/sagas/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,12 @@ const handleForgotPasswordRequest = function* handleForgotPasswordRequest({ emai
const handleSetUser = function* handleSetUser() {
yield delay(2000);
const [server, user] = yield all([select(getServer), select(getUser)]);
if (user) {
// TODO: temporary... remove in future releases
// delete user.user;
if (user && user.id) {
if (user.language) {
I18n.locale = user.language;
}
yield AsyncStorage.setItem(`${ RocketChat.TOKEN_KEY }-${ server }`, JSON.stringify(user));
}
yield AsyncStorage.setItem(`${ RocketChat.TOKEN_KEY }-${ server }`, JSON.stringify(user));
};

const root = function* root() {
Expand Down
17 changes: 14 additions & 3 deletions app/views/OnboardingView/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import {
View, Text, Image, TouchableOpacity
View, Text, Image, TouchableOpacity, BackHandler
} from 'react-native';
import PropTypes from 'prop-types';
import Icon from 'react-native-vector-icons/MaterialIcons';
Expand All @@ -10,6 +10,7 @@ import SafeAreaView from 'react-native-safe-area-view';
import { gestureHandlerRootHOC } from 'react-native-gesture-handler';

import { selectServerRequest, serverInitAdd, serverFinishAdd } from '../../actions/server';
import { appStart as appStartAction } from '../../actions';
import I18n from '../../i18n';
import openLink from '../../utils/openLink';
import Button from './Button';
Expand All @@ -28,7 +29,8 @@ let NewServerView = null;
}), dispatch => ({
initAdd: () => dispatch(serverInitAdd()),
finishAdd: () => dispatch(serverFinishAdd()),
selectServer: server => dispatch(selectServerRequest(server))
selectServer: server => dispatch(selectServerRequest(server)),
appStart: () => dispatch(appStartAction())
}))
/** @extends React.Component */
export default class OnboardingView extends LoggedView {
Expand All @@ -49,11 +51,13 @@ export default class OnboardingView extends LoggedView {
selectServer: PropTypes.func.isRequired,
currentServer: PropTypes.string,
initAdd: PropTypes.func,
finishAdd: PropTypes.func
finishAdd: PropTypes.func,
appStart: PropTypes.func
}

constructor(props) {
super('OnboardingView', props);
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}

componentDidMount() {
Expand All @@ -75,6 +79,13 @@ export default class OnboardingView extends LoggedView {
finishAdd();
}
EventEmitter.removeListener('NewServer', this.handleNewServerEvent);
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}

handleBackPress = () => {
const { appStart } = this.props;
appStart('background');
return false;
}

close = () => {
Expand Down
19 changes: 17 additions & 2 deletions app/views/ProfileView/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import {
View, ScrollView, Keyboard, Dimensions
View, ScrollView, Keyboard, Dimensions, BackHandler
} from 'react-native';
import { connect } from 'react-redux';
import Dialog from 'react-native-dialog';
Expand All @@ -28,6 +28,7 @@ import Avatar from '../../containers/Avatar';
import Touch from '../../utils/touch';
import Drawer from '../../Drawer';
import { DEFAULT_HEADER } from '../../constants/headerOptions';
import { appStart as appStartAction } from '../../actions';

@connect(state => ({
user: {
Expand All @@ -38,6 +39,8 @@ import { DEFAULT_HEADER } from '../../constants/headerOptions';
},
Accounts_CustomFields: state.settings.Accounts_CustomFields,
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
}), dispatch => ({
appStart: () => dispatch(appStartAction())
}))
/** @extends React.Component */
export default class ProfileView extends LoggedView {
Expand Down Expand Up @@ -71,7 +74,8 @@ export default class ProfileView extends LoggedView {
baseUrl: PropTypes.string,
componentId: PropTypes.string,
user: PropTypes.object,
Accounts_CustomFields: PropTypes.string
Accounts_CustomFields: PropTypes.string,
appStart: PropTypes.func
}

constructor(props) {
Expand All @@ -90,6 +94,7 @@ export default class ProfileView extends LoggedView {
customFields: {}
};
Navigation.events().bindComponent(this);
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}

async componentDidMount() {
Expand All @@ -110,12 +115,22 @@ export default class ProfileView extends LoggedView {
}
}

componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}

navigationButtonPressed = ({ buttonId }) => {
if (buttonId === 'settings') {
Drawer.toggle();
}
}

handleBackPress = () => {
const { appStart } = this.props;
appStart('background');
return false;
}

setAvatar = (avatar) => {
this.setState({ avatar });
}
Expand Down
24 changes: 18 additions & 6 deletions app/views/RoomsListView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import SortDropdown from './SortDropdown';
import ServerDropdown from './ServerDropdown';
import Touch from '../../utils/touch';
import { toggleSortDropdown as toggleSortDropdownAction, openSearchHeader as openSearchHeaderAction, closeSearchHeader as closeSearchHeaderAction } from '../../actions/rooms';
import { appStart as appStartAction } from '../../actions';
import store from '../../lib/createStore';
import Drawer from '../../Drawer';
import { DEFAULT_HEADER } from '../../constants/headerOptions';
Expand Down Expand Up @@ -69,7 +70,8 @@ let NewMessageView = null;
}), dispatch => ({
toggleSortDropdown: () => dispatch(toggleSortDropdownAction()),
openSearchHeader: () => dispatch(openSearchHeaderAction()),
closeSearchHeader: () => dispatch(closeSearchHeaderAction())
closeSearchHeader: () => dispatch(closeSearchHeaderAction()),
appStart: () => dispatch(appStartAction())
}))
/** @extends React.Component */
export default class RoomsListView extends LoggedView {
Expand Down Expand Up @@ -114,14 +116,16 @@ export default class RoomsListView extends LoggedView {
useRealName: PropTypes.bool,
toggleSortDropdown: PropTypes.func,
openSearchHeader: PropTypes.func,
closeSearchHeader: PropTypes.func
closeSearchHeader: PropTypes.func,
appStart: PropTypes.func
}

constructor(props) {
super('RoomsListView', props);

this.data = [];
this.state = {
searching: false,
search: [],
loading: true,
chats: [],
Expand All @@ -133,6 +137,7 @@ export default class RoomsListView extends LoggedView {
livechat: []
};
Navigation.events().bindComponent(this);
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}

componentDidMount() {
Expand Down Expand Up @@ -180,6 +185,7 @@ export default class RoomsListView extends LoggedView {
this.removeListener(this.privateGroup);
this.removeListener(this.direct);
this.removeListener(this.livechat);
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);

if (this.timeout) {
clearTimeout(this.timeout);
Expand Down Expand Up @@ -336,6 +342,7 @@ export default class RoomsListView extends LoggedView {

initSearchingAndroid = () => {
const { openSearchHeader } = this.props;
this.setState({ searching: true });
openSearchHeader();
Navigation.mergeOptions('RoomsListView', {
topBar: {
Expand All @@ -347,12 +354,12 @@ export default class RoomsListView extends LoggedView {
rightButtons: []
}
});
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}

cancelSearchingAndroid = () => {
if (Platform.OS === 'android') {
const { closeSearchHeader } = this.props;
this.setState({ searching: false });
closeSearchHeader();
Navigation.mergeOptions('RoomsListView', {
topBar: {
Expand All @@ -362,16 +369,21 @@ export default class RoomsListView extends LoggedView {
});
this.internalSetState({ search: [] });
Keyboard.dismiss();
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}
}

// this is necessary during development (enables Cmd + r)
hasActiveDB = () => database && database.databases && database.databases.activeDB;

handleBackPress = () => {
this.cancelSearchingAndroid();
return true;
const { searching } = this.state;
const { appStart } = this.props;
if (searching) {
this.cancelSearchingAndroid();
return true;
}
appStart('background');
return false;
}

_isUnread = item => item.unread > 0 || item.alert
Expand Down
22 changes: 19 additions & 3 deletions app/views/SettingsView/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import PropTypes from 'prop-types';
import { View, ScrollView, Dimensions } from 'react-native';
import {
View, ScrollView, Dimensions, BackHandler
} from 'react-native';
import RNPickerSelect from 'react-native-picker-select';
import { connect } from 'react-redux';
import { Navigation } from 'react-native-navigation';
Expand All @@ -18,13 +20,15 @@ import Loading from '../../containers/Loading';
import { showErrorAlert, showToast } from '../../utils/info';
import log from '../../utils/log';
import { setUser as setUserAction } from '../../actions/login';
import { appStart as appStartAction } from '../../actions';
import Drawer from '../../Drawer';
import { DEFAULT_HEADER } from '../../constants/headerOptions';

@connect(state => ({
userLanguage: state.login.user && state.login.user.language
}), dispatch => ({
setUser: params => dispatch(setUserAction(params))
setUser: params => dispatch(setUserAction(params)),
appStart: () => dispatch(appStartAction())
}))
/** @extends React.Component */
export default class SettingsView extends LoggedView {
Expand Down Expand Up @@ -57,7 +61,8 @@ export default class SettingsView extends LoggedView {
static propTypes = {
componentId: PropTypes.string,
userLanguage: PropTypes.string,
setUser: PropTypes.func
setUser: PropTypes.func,
appStart: PropTypes.func
}

constructor(props) {
Expand All @@ -81,6 +86,11 @@ export default class SettingsView extends LoggedView {
saving: false
};
Navigation.events().bindComponent(this);
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}

componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}

navigationButtonPressed = ({ buttonId }) => {
Expand All @@ -89,6 +99,12 @@ export default class SettingsView extends LoggedView {
}
}

handleBackPress = () => {
const { appStart } = this.props;
appStart('background');
return false;
}

getLabel = (language) => {
const { languages } = this.state;
const l = languages.find(i => i.value === language);
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e66dbd8

Please sign in to comment.