Skip to content

Commit

Permalink
#1149 | Disable manual sync-button when background auto-sync is in pr…
Browse files Browse the repository at this point in the history
…ogress
  • Loading branch information
himeshr committed Oct 25, 2023
1 parent 6365afd commit 5dda36a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
14 changes: 12 additions & 2 deletions packages/openchs-android/src/action/SyncActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class SyncActions {
progress: 0,
message: '',
syncMessage: '',
startSync: false
startSync: false,
backgroundSyncInProgress: false
};
}

Expand Down Expand Up @@ -50,6 +51,13 @@ class SyncActions {
}
}

static onBackgroundSyncStatusChange(state, action, context) {
return {
...state,
backgroundSyncInProgress: action.backgroundSyncInProgress
}
}

static onMessageCallback(state, action, context) {
return {
...state,
Expand All @@ -66,7 +74,8 @@ const SyncActionNames = {
ON_ERROR: `${ActionPrefix}.ON_ERROR`,
ON_CONNECTION_CHANGE: `${ActionPrefix}.ON_CONNECTION_CHANGE`,
ON_UPDATE: `${ActionPrefix}.ON_UPDATE`,
ON_MESSAGE_CALLBACK: `${ActionPrefix}.ON_MESSAGE_CALLBACK`
ON_MESSAGE_CALLBACK: `${ActionPrefix}.ON_MESSAGE_CALLBACK`,
ON_BACKGROUND_SYNC_STATUS_CHANGE: `${ActionPrefix}.ON_BACKGROUND_SYNC_STATUS_CHANGE`
};

const SyncActionMap = new Map([
Expand All @@ -76,6 +85,7 @@ const SyncActionMap = new Map([
[SyncActionNames.ON_CONNECTION_CHANGE, SyncActions.onConnectionChange],
[SyncActionNames.ON_UPDATE, SyncActions.onUpdate],
[SyncActionNames.ON_MESSAGE_CALLBACK, SyncActions.onMessageCallback],
[SyncActionNames.ON_BACKGROUND_SYNC_STATUS_CHANGE, SyncActions.onBackgroundSyncStatusChange],
]);

export {
Expand Down
5 changes: 4 additions & 1 deletion packages/openchs-android/src/service/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ class SyncService extends BaseService {
.then(() => Promise.resolve(progressBarStatus.onSyncComplete()))
.then(() => Promise.resolve(this.logSyncCompleteEvent(syncStartTime)))
.then(() => this.clearDataIn([RuleFailureTelemetry]))
.then(() => this.downloadNewsImages());
.then(() => this.downloadNewsImages())
.then(() => {
return isOnlyUploadRequired;
});

// Even blank dataServerSync with no data in or out takes quite a while.
// Don't do it twice if no image sync required
Expand Down
56 changes: 50 additions & 6 deletions packages/openchs-android/src/task/Sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,26 @@ import UserInfoService from "../service/UserInfoService";
import _ from "lodash";
import SettingsService from '../service/SettingsService';
import EnvironmentConfig from "../framework/EnvironmentConfig";
import {SyncActionNames as SyncActions} from '../action/SyncActions';
import RuleEvaluationService from '../service/RuleEvaluationService';
import ProgramConfigService from '../service/ProgramConfigService';
import MessageService from '../service/MessageService';
import RuleService from '../service/RuleService';
import PrivilegeService from '../service/PrivilegeService';
import {IndividualSearchActionNames as IndividualSearchActions} from '../action/individual/IndividualSearchActions';
import {LandingViewActions} from '../action/LandingViewActions';

class Sync extends BaseTask {
async execute() {
const dispatchAction = (action, params) => {
const type = action instanceof Function ? action.Id : action;
if (General.canLog(General.LogLevel.Debug))
General.logDebug('BaseService', `Dispatching action: ${JSON.stringify(type)}`);
return GlobalContext.getInstance().reduxStore.dispatch({type, ...params});
}

try {
General.logInfo("Sync", "Starting background sync");
const globalContext = GlobalContext.getInstance();
let settings = globalContext.beanRegistry.getService(SettingsService).getSettings();
if (_.isNil(settings.userId)) {
Expand All @@ -29,24 +45,52 @@ class Sync extends BaseTask {
General.logInfo("Sync", "Starting SyncService");
General.logInfo("Sync", "Getting SyncService");
const syncService = globalContext.beanRegistry.getService("syncService");

dispatchAction(SyncActions.ON_BACKGROUND_SYNC_STATUS_CHANGE, {backgroundSyncInProgress: true});
General.logInfo("Sync", "Getting connection info");
let connectionInfo;
await NetInfo.fetch().then((state) => connectionInfo = state);
General.logInfo("Sync", "Calling syncService.sync");
return syncService.sync(EntityMetaData.model(), (progress) => {
General.logInfo("Sync", progress);
},
(message) => {
}, connectionInfo, Date.now(), SyncService.syncSources.ONLY_UPLOAD_BACKGROUND_JOB, null).then(() => General.logInfo("Sync", "Sync completed")).catch((e) => {
ErrorHandler.postScheduledJobError(e);
});
General.logInfo("Sync", progress);
},
(message) => {
}, connectionInfo, Date.now(), SyncService.syncSources.ONLY_UPLOAD_BACKGROUND_JOB, null)
.then(this.performPostBackgroundSyncActions(dispatchAction, globalContext))
.catch((e) => {
ErrorHandler.postScheduledJobError(e);
});
} catch (e) {
if (e instanceof AuthenticationError && e.code === NO_USER) {
return;
}
ErrorHandler.postScheduledJobError(e);
}
}

performPostBackgroundSyncActions(dispatchAction, globalContext) {
return (updatedSyncSource) => {
General.logInfo("Sync", "Sync completed")
dispatchAction(SyncActions.ON_BACKGROUND_SYNC_STATUS_CHANGE, {backgroundSyncInProgress: false});
if (updatedSyncSource === SyncService.syncSources.BACKGROUND_JOB) {
General.logInfo("Background Sync", "Full Background Sync completed, performing reset")
setTimeout(() => {
globalContext.beanRegistry.getService(RuleEvaluationService).init();
globalContext.beanRegistry.getService(ProgramConfigService).init();
globalContext.beanRegistry.getService(MessageService).init();
globalContext.beanRegistry.getService(RuleService).init();
globalContext.beanRegistry.getService(PrivilegeService).deleteRevokedEntities();
//To load subjectType after sync
globalContext.beanRegistry.getService(IndividualSearchActions.ON_LOAD);

//To re-render LandingView after sync
dispatchAction(LandingViewActions.ON_LOAD, {syncRequired: false});
}, 1);
globalContext.beanRegistry.getService(SettingsService).initLanguages();
General.logInfo("Background Sync", 'Background Sync completed, reset completed');
}
};
}
}

export default new Sync();
14 changes: 12 additions & 2 deletions packages/openchs-android/src/views/SyncComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,27 @@ class SyncComponent extends AbstractComponent {
alignSelf: 'center',
fontSize: 30
});
const syncDisabledIcon = this.props.icon("sync-circle", {
color: Colors.DisabledButtonColor,
alignSelf: 'center',
fontSize: 30
});
const entitySyncStatusService = this.context.getService(EntitySyncStatusService);
const totalPending = entitySyncStatusService.getTotalEntitiesPending();
return !this.state.syncing && totalPending > 0 ? <Badge icon={icon} number={totalPending}/> : icon;
if(this.state.backgroundSyncInProgress) {
return syncDisabledIcon;
} else {
return !this.state.syncing && totalPending > 0 ? <Badge icon={icon} number={totalPending}/> : icon;
}
}

render() {
return (
<View>
{this.renderSyncModal()}
<TouchableNativeFeedback
onPress={() => this.sync()}>
onPress={() => this.sync()}
disabled={this.state.backgroundSyncInProgress}>
<View style={{
flexDirection: 'column',
justifyContent: 'center',
Expand Down

0 comments on commit 5dda36a

Please sign in to comment.