Skip to content

Commit

Permalink
If dump restore fails for some reason then prompt user for the messag…
Browse files Browse the repository at this point in the history
…e to retry or perform slow sync.
  • Loading branch information
petmongrels committed Apr 26, 2021
1 parent 39a56a5 commit 0728bfa
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 22 deletions.
34 changes: 25 additions & 9 deletions packages/openchs-android/src/action/LoginActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class LoginActions {

static onLoad(state, action, context) {
const userInfo = context.get(UserInfoService).getUserInfo();
return _.assignIn({}, state, userInfo ? {userId: userInfo.username, loggedInUser: userInfo.username, percentProgress: null} : {percentProgress: null, dumpRestoreMessage: null});
return _.assignIn({}, state, userInfo ? {userId: userInfo.username, loggedInUser: userInfo.username, percentProgress: null} : {
percentProgress: null,
dumpRestoreMessage: null
});
}

static onUserIdChange(state, action) {
Expand Down Expand Up @@ -70,21 +73,26 @@ class LoginActions {
return _.isEmpty(anEntity);
})
.then((doRestoreDump) => {
General.logInfo("LoginActions", `Dump restore required = ${doRestoreDump}`);
General.logInfo("LoginActions", `Dump restore can be done = ${doRestoreDump}`);
if (doRestoreDump) {
let restoreService = context.get(BackupRestoreRealmService);
restoreService.restore((percentProgress, message) => {
if (percentProgress === 100) action.successCb();
else action.onLoginProgress(percentProgress, message);
});
LoginActions.restoreDump(context, action);
} else {
action.successCb();
}
});
return newState;
}

static onStateChange(state, action, context) {
static restoreDump(context, action) {
let restoreService = context.get(BackupRestoreRealmService);
restoreService.restore((percentProgress, message, failed = false, failureMessage) => {
if (failed) action.checkForRetry(failureMessage);
else if (percentProgress === 100) action.successCb();
else action.onLoginProgress(percentProgress, message);
});
}

static onStateChange(state, action) {
return _.assignIn({}, state, action.newState);
}

Expand All @@ -101,6 +109,12 @@ class LoginActions {
newState.dumpRestoring = action.percentProgress !== 100;
return newState;
}

static onDumpRestoreRetry(state, action, context) {
let newState = _.assignIn({}, state, {percentProgress: 0});
LoginActions.restoreDump(context, action);
return newState;
}
}

const LoginActionsNames = {
Expand All @@ -112,7 +126,8 @@ const LoginActionsNames = {
ON_TOGGLE_SHOW_PASSWORD: "LA.ON_TOGGLE_SHOW_PASSWORD",
ON_EMPTY_LOGIN: "LA.ON_EMPTY_LOGIN",
ON_LOAD: 'LA.ON_LOAD',
ON_DUMP_RESTORING: 'LA.ON_DUMP_RESTORING'
ON_DUMP_RESTORING: 'LA.ON_DUMP_RESTORING',
ON_DUMP_RESTORE_RETRY: 'LA.ON_DUMP_RESTORE_RETRY'
};

const LoginActionsMap = new Map([
Expand All @@ -124,6 +139,7 @@ const LoginActionsMap = new Map([
[LoginActionsNames.ON_LOAD, LoginActions.onLoad],
[LoginActionsNames.ON_EMPTY_LOGIN, LoginActions.onEmptyLogin],
[LoginActionsNames.ON_DUMP_RESTORING, LoginActions.onDumpRestoring],
[LoginActionsNames.ON_DUMP_RESTORE_RETRY, LoginActions.onDumpRestoreRetry]
]);

export {LoginActionsNames, LoginActionsMap, LoginActions} ;
10 changes: 5 additions & 5 deletions packages/openchs-android/src/service/BackupRestoreRealm.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ export default class BackupRestoreRealmService extends BaseService {
General.logDebug("BackupRestoreRealmService", `Backup file exists:${exists}`);
if (exists === "true") {
authService.getAuthToken()
.then((authToken) => get(`${settingsService.getSettings().serverURL}/media/mobileDatabaseBackupUrl/download`, authToken)
.then((authToken) => get(`${settingsService.getSettings().serverURL}/media/mobileDatabaseBackupUrl/download`, authToken))
.then((url) => mediaService.downloadFromUrl(url, downloadedFile, (received, total) => {
cb(1 + (received * 85) / total, "restoreDownloadPreparedDb")
})))
cb(1 + (received * 85) / total, "restoreDownloadPreparedDb")
}))
.then(() => {
General.logDebug("BackupRestoreRealmService", "Decompressing downloaded database dump");
cb(87, "restoringDb");
Expand Down Expand Up @@ -127,15 +127,15 @@ export default class BackupRestoreRealmService extends BaseService {
})
.catch((error) => {
General.logErrorAsInfo("BackupRestoreRealm", error);
cb(100, "restoreFailed");
cb(100, "restoreFailed", true, error.message);
});
} else {
cb(100, "restoreNoDump");
}
})
.catch((error) => {
General.logErrorAsInfo("BackupRestoreRealm", error);
cb(100, "restoreFailed");
cb(100, "restoreFailed", true, error.message);
});
}

Expand Down
29 changes: 23 additions & 6 deletions packages/openchs-android/src/views/LoginView.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import AbstractComponent from "../framework/view/AbstractComponent";
import Path from "../framework/routing/Path";
import {
Alert,
Dimensions,
Modal,
StatusBar,
Text,
TouchableNativeFeedback,
TouchableWithoutFeedback,
View,
} from "react-native";
import TextFormElement from "./form/formElement/TextFormElement";
Expand All @@ -24,7 +21,7 @@ import CHSContent from "./common/CHSContent";
import Styles from "./primitives/Styles";
import Colors from "./primitives/Colors";
import _ from "lodash";
import {Button, CheckBox, Spinner} from "native-base";
import {CheckBox, Spinner} from "native-base";
import General from "../utility/General";
import AuthService from "../service/AuthService";
import {ConfirmDialog} from 'react-native-simple-dialogs';
Expand Down Expand Up @@ -148,11 +145,22 @@ class LoginView extends AbstractComponent {
</ConfirmDialog>);
}

restoreFailureAlert(errorMessage) {
Alert.alert(this.I18n.t("restoreFailedTitle"), errorMessage, [{
text: this.I18n.t('tryAgain'),
onPress: () => this.dispatchAction(Actions.ON_DUMP_RESTORE_RETRY, this.dumpRestoreAction())
},
{text: this.I18n.t('performNormalSync'), onPress: () => this.loginComplete(), style: 'cancel'}
]
);
}

render() {
General.logDebug("LoginView", 'render');
return (
<CHSContainer>
<ProgressBarView progress={this.state.percentProgress / 100} message={this.state.dumpRestoreMessage} syncing={this.state.dumpRestoring} onPress={_.noop} notifyUserOnCompletion={false}/>
<ProgressBarView progress={this.state.percentProgress / 100} message={this.state.dumpRestoreMessage} syncing={this.state.dumpRestoring} onPress={_.noop}
notifyUserOnCompletion={false}/>
<CHSContent>
{this.renderMultiUserLoginFailure()}
<StatusBar backgroundColor={Styles.blackColor} barStyle="light-content"/>
Expand Down Expand Up @@ -282,12 +290,21 @@ class LoginView extends AbstractComponent {
this.dispatchAction(Actions.ON_LOGIN, {
failure: this.loginFailure.bind(this),
newPasswordRequired: this.newPasswordRequired.bind(this),
...this.dumpRestoreAction()
});
}

dumpRestoreAction() {
return {
onLoginProgress: (percentProgress, message) => {
General.logDebug("LoginView", message);
this.dispatchAction(Actions.ON_DUMP_RESTORING, {percentProgress: percentProgress, message: message});
},
checkForRetry: (errorMessage) => {
this.restoreFailureAlert(errorMessage);
},
successCb: this.loginComplete.bind(this)
});
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/openchs-android/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,11 @@
"restoringDb": "Restoring database",
"restoreComplete": "Restore complete",
"restoreFailed": "Restore failed, will sync from server",
"restoreFailedTitle": "Fast sync failed",
"restoreNoDump": "No dump present, will sync fully from server",
"performNormalSync": "Perform slow sync",
"audioFileTitle": "Only audio file supported",
"audioFileDescription": "Please select an audio file.",
"recordingStartedMessage": "Recording started, use stop button to stop it."
}
}
}
10 changes: 9 additions & 1 deletion packages/openchs-android/translations/hi_IN.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@
"backPressMessage": "क्या आप वाकई वापस जाना चाहते हैं? आप सभी भरे हुए डेटा को बरबाद कर देंगे।",
"subjectSummary": "सारांश",
"homePressTitle": "होम स्क्रीन पर जाएं",
"homePressMessage": "क्या आप वाकई होम स्क्रीन पर वापस जाना चाहते हैं? आप सभी प्रपत्र परिवर्तनों को खो देंगे।"
"homePressMessage": "क्या आप वाकई होम स्क्रीन पर वापस जाना चाहते हैं? आप सभी प्रपत्र परिवर्तनों को खो देंगे।",
"restoreCheckDb": "देटाबसे चेक जारी",
"restoreDownloadPreparedDb": "देटाबसे डाउनलोड चालू",
"restoringDb": "देटाबसे लोड चालू",
"restoreComplete": "देटाबसे लोड ख़त्म",
"restoreFailedTitle": "देटाबसे रेस्टॉर फ़ेल",
"restoreFailedMessage": "फ़ास्ट सिंक फ़ेल",
"restoreNoDump": "कोई डम्प मौजूद नहीं, फ़्रेश सिंक चालू",
"performNormalSync": "स्लो सिंक करो"
}
}

0 comments on commit 0728bfa

Please sign in to comment.