From 7be22369ac79177b8b03180029dc59445a1e3b6c Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Thu, 20 Jul 2017 22:20:56 +0100 Subject: [PATCH] Fixed RN history --- ReactNativeClient/root.js | 72 ++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/ReactNativeClient/root.js b/ReactNativeClient/root.js index 4ec18481c..ca53576ef 100644 --- a/ReactNativeClient/root.js +++ b/ReactNativeClient/root.js @@ -62,11 +62,10 @@ const initialRoute = { defaultState.route = initialRoute; let navHistory = []; -navHistory.push(initialRoute); function historyCanGoBackTo(route) { - if (route.routeName == 'Note' && !route.noteId) return false; - if (route.routeName == 'Folder' && !route.folderId) return false; + if (route.routeName == 'Note') return false; + if (route.routeName == 'Folder') return false; return true; } @@ -86,26 +85,25 @@ const reducer = (state = defaultState, action) => { reg.logger().info('Reducer action', action.type); let newState = state; + let historyGoingBack = false; try { switch (action.type) { + case 'Navigation/BACK': - if (navHistory.length < 2) break; + if (!navHistory.length) break; - action = navHistory.pop(); // Current page - action = navHistory.pop(); // Previous page - - while (!historyCanGoBackTo(action)) { - if (!navHistory.length) { - action = null; - break; - } - action = navHistory.pop(); + let newAction = null; + while (navHistory.length) { + newAction = navHistory.pop(); + if (newAction.routeName != state.route.routeName) break; } - if (!action) action = Object.assign({}, initialRoute); + action = newAction ? newAction : navHistory.pop(); + + historyGoingBack = true; // Fall throught @@ -114,6 +112,32 @@ const reducer = (state = defaultState, action) => { const currentRoute = state.route; const currentRouteName = currentRoute ? currentRoute.routeName : ''; + if (!historyGoingBack && historyCanGoBackTo(currentRoute)) { + // If the route *name* is the same (even if the other parameters are different), we + // overwrite the last route in the history with the current one. If the route name + // is different, we push a new history entry. + if (currentRoute.routeName == action.routeName) { + // nothing + } else { + navHistory.push(currentRoute); + } + } + + // HACK: whenever a new screen is loaded, all the previous screens of that type + // are overwritten with the new screen parameters. This is because the way notes + // are currently loaded is not optimal (doesn't retain history properly) so + // this is a simple fix without doing a big refactoring to change the way notes + // are loaded. Might be good enough since going back to different folders + // is probably not a common workflow. + for (let i = 0; i < navHistory.length; i++) { + let n = navHistory[i]; + if (n.routeName == action.routeName) { + navHistory[i] = Object.assign({}, action); + } + } + + if (action.routeName == 'Welcome') navHistory = []; + reg.logger().info('Route: ' + currentRouteName + ' => ' + action.routeName); newState = Object.assign({}, state); @@ -132,19 +156,7 @@ const reducer = (state = defaultState, action) => { newState.route = action; - // If the route *name* is the same (even if the other parameters are different), we - // overwrite the last route in the history with the current one. If the route name - // is different, we push a new history entry. - - if (currentRouteName == action.routeName) { - if (navHistory.length) navHistory[navHistory.length - 1] = action; - // If the current screen is already the requested screen, don't do anything - } else { - if (action.routeName == 'Welcome') navHistory = []; - navHistory.push(action); - } - - newState.historyCanGoBack = navHistory.length >= 2; + newState.historyCanGoBack = !!navHistory.length; if (newState.route.routeName == 'Notes') { Setting.setValue('activeFolderId', newState.selectedFolderId); @@ -412,7 +424,11 @@ class AppComponent extends React.Component { async componentDidMount() { await initialize(this.props.dispatch, this.backButtonHandler.bind(this)); - reg.scheduleSync(); + if (Setting.value('env') == 'dev') { + + } else { + reg.scheduleSync(); + } } backButtonHandler() {