From 43f1892570bea62029ffb446d3db61b785109236 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Tue, 28 Nov 2017 09:01:18 -0200 Subject: [PATCH] [FIX][ANDROID] Fix return from background (#115) * fix frozen screen when app return from background (Android) * Update state.js * Update state.js * Update RoomsListView.js --- app/lib/createStore.js | 7 ++++++- app/sagas/index.js | 4 +++- app/sagas/init.js | 9 ++++++--- app/sagas/state.js | 23 +++++++++++++++++++++++ app/views/RoomsListView.js | 2 +- package-lock.json | 5 +++++ package.json | 5 +++-- 7 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 app/sagas/state.js diff --git a/app/lib/createStore.js b/app/lib/createStore.js index 7a2f79a52..f894b5437 100644 --- a/app/lib/createStore.js +++ b/app/lib/createStore.js @@ -2,6 +2,7 @@ import { createStore, applyMiddleware } from 'redux'; import createSagaMiddleware from 'redux-saga'; import logger from 'redux-logger'; import { composeWithDevTools } from 'remote-redux-devtools'; +import applyAppStateListener from 'redux-enhancer-react-native-appstate'; import reducers from '../reducers'; import sagas from '../sagas'; @@ -13,12 +14,16 @@ if (__DEV__) { const reduxImmutableStateInvariant = require('redux-immutable-state-invariant').default(); enhacers = composeWithDevTools( + applyAppStateListener(), applyMiddleware(reduxImmutableStateInvariant), applyMiddleware(sagaMiddleware), applyMiddleware(logger) ); } else { - enhacers = composeWithDevTools(applyMiddleware(sagaMiddleware)); + enhacers = composeWithDevTools( + applyAppStateListener(), + applyMiddleware(sagaMiddleware) + ); } const store = enhacers(createStore)(reducers); diff --git a/app/sagas/index.js b/app/sagas/index.js index fcc8fd72f..df6f7d7ad 100644 --- a/app/sagas/index.js +++ b/app/sagas/index.js @@ -7,6 +7,7 @@ import messages from './messages'; import selectServer from './selectServer'; import createChannel from './createChannel'; import init from './init'; +import state from './state'; const root = function* root() { yield all([ @@ -17,7 +18,8 @@ const root = function* root() { login(), connect(), messages(), - selectServer() + selectServer(), + state() ]); }; // Consider using takeEvery diff --git a/app/sagas/init.js b/app/sagas/init.js index 024c26b45..5117c86ca 100644 --- a/app/sagas/init.js +++ b/app/sagas/init.js @@ -1,5 +1,5 @@ import { AsyncStorage } from 'react-native'; -import { call, put, take } from 'redux-saga/effects'; +import { call, put, takeLatest } from 'redux-saga/effects'; import * as actions from '../actions'; import { setServer } from '../actions/server'; import { restoreToken } from '../actions/login'; @@ -9,7 +9,6 @@ import RocketChat from '../lib/rocketchat'; const restore = function* restore() { try { - yield take(APP.INIT); const token = yield call([AsyncStorage, 'getItem'], 'reactnativemeteor_usertoken'); if (token) { yield put(restoreToken(token)); @@ -28,4 +27,8 @@ const restore = function* restore() { console.log(e); } }; -export default restore; + +const root = function* root() { + yield takeLatest(APP.INIT, restore); +}; +export default root; diff --git a/app/sagas/state.js b/app/sagas/state.js new file mode 100644 index 000000000..0f58ae7f9 --- /dev/null +++ b/app/sagas/state.js @@ -0,0 +1,23 @@ +import { takeLatest } from 'redux-saga/effects'; +import { FOREGROUND, BACKGROUND, INACTIVE } from 'redux-enhancer-react-native-appstate'; + +const appHasComeBackToForeground = function* appHasComeBackToForeground() { + yield console.log('appHasComeBackToForeground'); +}; + +const root = function* root() { + yield takeLatest( + FOREGROUND, + appHasComeBackToForeground + ); + yield takeLatest( + BACKGROUND, + appHasComeBackToForeground + ); + yield takeLatest( + INACTIVE, + appHasComeBackToForeground + ); +}; + +export default root; diff --git a/app/views/RoomsListView.js b/app/views/RoomsListView.js index b6ffcfe17..ebf130715 100644 --- a/app/views/RoomsListView.js +++ b/app/views/RoomsListView.js @@ -259,7 +259,7 @@ export default class RoomsListView extends React.Component { userMentions={item.userMentions} favorite={item.f} name={item.name} - _updatedAt={item._updatedAt} + _updatedAt={item.roomUpdatedAt} key={item._id} type={item.t} baseUrl={this.props.Site_Url} diff --git a/package-lock.json b/package-lock.json index ffe974829..79fa47d67 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12564,6 +12564,11 @@ "symbol-observable": "1.0.4" } }, + "redux-enhancer-react-native-appstate": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/redux-enhancer-react-native-appstate/-/redux-enhancer-react-native-appstate-0.3.0.tgz", + "integrity": "sha1-+5Fwk3WM4J9DLNCytWPRNh6PV28=" + }, "redux-immutable-state-invariant": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/redux-immutable-state-invariant/-/redux-immutable-state-invariant-2.1.0.tgz", diff --git a/package.json b/package.json index a18c15b59..efa0055e1 100644 --- a/package.json +++ b/package.json @@ -46,14 +46,15 @@ "react-redux": "^5.0.6", "realm": "^2.0.7", "redux": "^3.7.2", + "redux-enhancer-react-native-appstate": "^0.3.0", "redux-immutable-state-invariant": "^2.1.0", "redux-logger": "^3.0.6", "redux-saga": "^0.16.0", "regenerator-runtime": "^0.11.0", "remote-redux-devtools": "^0.5.12", - "strip-ansi": "^4.0.0", "@storybook/react-native": "^3.2.15", - "snyk": "^1.41.1" + "snyk": "^1.41.1", + "strip-ansi": "^4.0.0" }, "devDependencies": { "@storybook/addon-storyshots": "^3.2.15",