mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-11 18:24:43 +02:00
Fixed RN history
This commit is contained in:
parent
aee3e9e4bf
commit
7be22369ac
@ -62,11 +62,10 @@ const initialRoute = {
|
|||||||
defaultState.route = initialRoute;
|
defaultState.route = initialRoute;
|
||||||
|
|
||||||
let navHistory = [];
|
let navHistory = [];
|
||||||
navHistory.push(initialRoute);
|
|
||||||
|
|
||||||
function historyCanGoBackTo(route) {
|
function historyCanGoBackTo(route) {
|
||||||
if (route.routeName == 'Note' && !route.noteId) return false;
|
if (route.routeName == 'Note') return false;
|
||||||
if (route.routeName == 'Folder' && !route.folderId) return false;
|
if (route.routeName == 'Folder') return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -86,26 +85,25 @@ const reducer = (state = defaultState, action) => {
|
|||||||
reg.logger().info('Reducer action', action.type);
|
reg.logger().info('Reducer action', action.type);
|
||||||
|
|
||||||
let newState = state;
|
let newState = state;
|
||||||
|
let historyGoingBack = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
|
||||||
|
|
||||||
case 'Navigation/BACK':
|
case 'Navigation/BACK':
|
||||||
|
|
||||||
if (navHistory.length < 2) break;
|
if (!navHistory.length) break;
|
||||||
|
|
||||||
action = navHistory.pop(); // Current page
|
let newAction = null;
|
||||||
action = navHistory.pop(); // Previous page
|
while (navHistory.length) {
|
||||||
|
newAction = navHistory.pop();
|
||||||
while (!historyCanGoBackTo(action)) {
|
if (newAction.routeName != state.route.routeName) break;
|
||||||
if (!navHistory.length) {
|
|
||||||
action = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
action = navHistory.pop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!action) action = Object.assign({}, initialRoute);
|
action = newAction ? newAction : navHistory.pop();
|
||||||
|
|
||||||
|
historyGoingBack = true;
|
||||||
|
|
||||||
// Fall throught
|
// Fall throught
|
||||||
|
|
||||||
@ -114,6 +112,32 @@ const reducer = (state = defaultState, action) => {
|
|||||||
const currentRoute = state.route;
|
const currentRoute = state.route;
|
||||||
const currentRouteName = currentRoute ? currentRoute.routeName : '';
|
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);
|
reg.logger().info('Route: ' + currentRouteName + ' => ' + action.routeName);
|
||||||
|
|
||||||
newState = Object.assign({}, state);
|
newState = Object.assign({}, state);
|
||||||
@ -132,19 +156,7 @@ const reducer = (state = defaultState, action) => {
|
|||||||
|
|
||||||
newState.route = action;
|
newState.route = action;
|
||||||
|
|
||||||
// If the route *name* is the same (even if the other parameters are different), we
|
newState.historyCanGoBack = !!navHistory.length;
|
||||||
// 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;
|
|
||||||
|
|
||||||
if (newState.route.routeName == 'Notes') {
|
if (newState.route.routeName == 'Notes') {
|
||||||
Setting.setValue('activeFolderId', newState.selectedFolderId);
|
Setting.setValue('activeFolderId', newState.selectedFolderId);
|
||||||
@ -412,7 +424,11 @@ class AppComponent extends React.Component {
|
|||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
await initialize(this.props.dispatch, this.backButtonHandler.bind(this));
|
await initialize(this.props.dispatch, this.backButtonHandler.bind(this));
|
||||||
reg.scheduleSync();
|
if (Setting.value('env') == 'dev') {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
reg.scheduleSync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backButtonHandler() {
|
backButtonHandler() {
|
||||||
|
Loading…
Reference in New Issue
Block a user