Navigate to the last visited route (#112)

* navigate to the last visited route on app startup if there is no launch url
* update eslint no-console rule
This commit is contained in:
Akinwale Ariwodola 2020-01-20 21:40:17 +01:00 committed by GitHub
parent 0361b10575
commit 46bfbd242a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 26 deletions

View file

@ -10,6 +10,7 @@
"__": true
},
"rules": {
"no-console": 2,
"no-multi-spaces": 0,
"new-cap": 0,
"prefer-promise-reject-errors": 0,

View file

@ -95,6 +95,7 @@ const compressor = createCompressor();
const authFilter = createFilter('auth', ['authToken']);
const blockedFilter = createFilter('blocked', ['blockedChannels']);
const contentFilter = createFilter('content', ['positions']);
const drawerFilter = createFilter('drawer', ['lastRouteInStack']);
const saveClaimsFilter = createFilter('claims', ['claimsByUri']);
const subscriptionsFilter = createFilter('subscriptions', ['enabledChannelNotifications', 'subscriptions', 'latest']);
const settingsFilter = createFilter('settings', ['clientSettings']);
@ -102,12 +103,13 @@ const tagsFilter = createFilter('tags', ['followedTags']);
const walletFilter = createFilter('wallet', ['receiveAddress']);
const v4PersistOptions = {
whitelist: ['auth', 'blocked', 'claims', 'content', 'subscriptions', 'settings', 'tags', 'wallet'],
whitelist: ['auth', 'blocked', 'claims', 'drawer', 'content', 'subscriptions', 'settings', 'tags', 'wallet'],
// Order is important. Needs to be compressed last or other transforms can't
// read the data
transforms: [
authFilter,
blockedFilter,
drawerFilter,
saveClaimsFilter,
subscriptionsFilter,
settingsFilter,

View file

@ -14,11 +14,13 @@ import {
selectEmailToVerify,
} from 'lbryinc';
import { doSetClientSetting } from 'redux/actions/settings';
import { selectLastRouteInStack } from 'redux/selectors/drawer';
import SplashScreen from './view';
const select = state => ({
user: selectUser(state),
emailToVerify: selectEmailToVerify(state),
lastRouteInStack: selectLastRouteInStack(state),
});
const perform = dispatch => ({

View file

@ -1,5 +1,5 @@
import React from 'react';
import { Lbry, doPreferenceGet } from 'lbry-redux';
import { Lbry, doPreferenceGet, isURIValid } from 'lbry-redux';
import { Lbryio } from 'lbryinc';
import { ActivityIndicator, DeviceEventEmitter, Linking, NativeModules, Platform, Text, View } from 'react-native';
import { NavigationActions, StackActions } from 'react-navigation';
@ -11,7 +11,7 @@ import Button from 'component/button';
import ProgressBar from 'component/progressBar';
import PropTypes from 'prop-types';
import Colors from 'styles/colors';
import Constants from 'constants'; // eslint-disable-line node/no-deprecated-api
import Constants, { DrawerRoutes } from 'constants'; // eslint-disable-line node/no-deprecated-api
import splashStyle from 'styles/splash';
import RNFS from 'react-native-fs';
@ -43,7 +43,7 @@ class SplashScreen extends React.PureComponent {
}
navigateToMain = () => {
const { navigation, notify, verifyUserEmail, verifyUserEmailFailure } = this.props;
const { lastRouteInStack, navigation, notify, verifyUserEmail, verifyUserEmailFailure } = this.props;
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Main' })],
@ -55,29 +55,14 @@ class SplashScreen extends React.PureComponent {
? navigation.state.params.launchUrl
: this.state.launchUrl;
if (launchUrl) {
if (launchUrl.startsWith('lbry://?verify=')) {
let verification = {};
try {
verification = JSON.parse(atob(launchUrl.substring(15)));
} catch (error) {
console.log(error);
}
if (verification.token && verification.recaptcha) {
AsyncStorage.setItem(Constants.KEY_SHOULD_VERIFY_EMAIL, 'true');
try {
verifyUserEmail(verification.token, verification.recaptcha);
} catch (error) {
const message = __('Invalid Verification Token');
verifyUserEmailFailure(message);
notify({ message });
}
} else {
notify({
message: __('Invalid Verification URI'),
});
}
navigateToUri(navigation, transformUrl(launchUrl));
} else if (lastRouteInStack) {
// no launch url, check if there's a last route in stack to navigate to.
const { route, params } = lastRouteInStack;
if (!DrawerRoutes.includes(route) && isURIValid(route)) {
navigateToUri(navigation, route);
} else {
navigateToUri(navigation, transformUrl(launchUrl));
navigation.navigate({ routeName: route, params });
}
}
};

View file

@ -3,6 +3,7 @@ import Constants from 'constants'; // eslint-disable-line node/no-deprecated-api
const reducers = {};
const defaultState = {
stack: [{ route: Constants.DRAWER_ROUTE_DISCOVER, params: {} }], // Discover is always the first drawer route
lastRouteInStack: {},
playerVisible: false,
playerVisibleByUri: {},
currentRoute: null,
@ -42,23 +43,30 @@ reducers[Constants.ACTION_PUSH_DRAWER_STACK] = (state, action) => {
canPushStack = false;
}
let lastRouteInStack;
if (canPushStack) {
newStack.push({ route: routeName, params });
// save the route
lastRouteInStack = { route: routeName, params };
}
return {
...state,
stack: newStack,
lastRouteInStack,
};
};
reducers[Constants.ACTION_POP_DRAWER_STACK] = (state, action) => {
// We don't want to pop the Discover route, since it's always expected to be the first
const newStack = state.stack.length === 1 ? state.stack.slice() : state.stack.slice(0, state.stack.length - 1);
const lastRouteInStack = newStack && newStack.length > 0 ? newStack[newStack.length - 1] : null;
return {
...state,
stack: newStack,
lastRouteInStack,
};
};

View file

@ -34,6 +34,11 @@ export const selectLastDrawerRoute = createSelector(
},
);
export const selectLastRouteInStack = createSelector(
selectState,
state => state.lastRouteInStack,
);
export const selectCurrentRoute = createSelector(
selectState,
state => state.currentRoute,