Save last session date on background

This commit is contained in:
Diego Mello 2020-04-14 11:07:51 -03:00
parent dc59232305
commit ff64cbd5a1
4 changed files with 38 additions and 14 deletions

View File

@ -18,7 +18,7 @@ import {
import { isIOS } from '../utils/deviceInfo';
import database from '../lib/database';
import protectedFunction from '../lib/methods/helpers/protectedFunction';
import localAuthenticate from '../utils/localAuthentication';
import { localAuthenticate } from '../utils/localAuthentication';
export const initLocalSettings = function* initLocalSettings() {
const sortPreferences = yield RocketChat.getSortPreferences();

View File

@ -4,7 +4,7 @@ import { FOREGROUND, BACKGROUND } from 'redux-enhancer-react-native-appstate';
import RocketChat from '../lib/rocketchat';
import { setBadgeCount } from '../notifications/push';
import log from '../utils/log';
import localAuthenticate from '../utils/localAuthentication';
import { localAuthenticate, saveLastLocalAuthenticationSession } from '../utils/localAuthentication';
import * as actions from '../actions';
const appHasComeBackToForeground = function* appHasComeBackToForeground() {
@ -39,7 +39,10 @@ const appHasComeBackToBackground = function* appHasComeBackToBackground() {
return;
}
try {
return yield RocketChat.setUserPresenceAway();
yield RocketChat.setUserPresenceAway();
const server = yield select(state => state.server.server);
yield saveLastLocalAuthenticationSession(server);
} catch (e) {
log(e);
}

View File

@ -1,8 +1,26 @@
import * as LocalAuthentication from 'expo-local-authentication';
import moment from 'moment';
import database from '../lib/database';
const localAuthenticate = async(server) => {
export const saveLastLocalAuthenticationSession = async(server) => {
console.log('saveLastLocalAuthenticationSession -> server', server);
const serversDB = database.servers;
const serversCollection = serversDB.collections.get('servers');
await serversDB.action(async() => {
try {
const serverRecord = await serversCollection.find(server);
console.log('saveLastLocalAuthenticationSession -> serverRecord', serverRecord);
await serverRecord.update((record) => {
record.lastLocalAuthenticatedSession = new Date();
});
} catch (e) {
// Do nothing
}
});
};
export const localAuthenticate = async(server) => {
const serversDB = database.servers;
const serversCollection = serversDB.collections.get('servers');
@ -14,12 +32,16 @@ const localAuthenticate = async(server) => {
return Promise.reject();
}
// if (serverRecord?.lastLocalAuthenticatedSession) {
// }
const authResult = await LocalAuthentication.authenticateAsync();
return Promise.resolve(authResult?.success);
const diffToLastSession = moment().diff(serverRecord?.lastLocalAuthenticatedSession, 'seconds');
console.log('localAuthenticate -> diffToLastSession', diffToLastSession);
if (diffToLastSession >= 5) {
const authResult = await LocalAuthentication.authenticateAsync();
if (authResult?.success) {
await saveLastLocalAuthenticationSession(server);
}
return Promise.resolve(authResult?.success);
} else {
await saveLastLocalAuthenticationSession(server);
}
return Promise.resolve(true);
};
export default localAuthenticate;

View File

@ -7,7 +7,6 @@ import { connect } from 'react-redux';
import equal from 'deep-equal';
import { withNavigation } from 'react-navigation';
import RNUserDefaults from 'rn-user-defaults';
import * as LocalAuthentication from 'expo-local-authentication';
import { toggleServerDropdown as toggleServerDropdownAction } from '../../actions/rooms';
import { selectServerRequest as selectServerRequestAction } from '../../actions/server';
@ -23,7 +22,7 @@ import { withTheme } from '../../theme';
import { KEY_COMMAND, handleCommandSelectServer } from '../../commands';
import { isTablet } from '../../utils/deviceInfo';
import { withSplit } from '../../split';
import localAuthenticate from '../../utils/localAuthentication';
import { localAuthenticate } from '../../utils/localAuthentication';
const ROW_HEIGHT = 68;
const ANIMATION_DURATION = 200;