diff --git a/packages/openchs-android/src/service/SyncService.js b/packages/openchs-android/src/service/SyncService.js index edcd6445f..f8a337a59 100644 --- a/packages/openchs-android/src/service/SyncService.js +++ b/packages/openchs-android/src/service/SyncService.js @@ -28,6 +28,9 @@ import TaskUnAssignmentService from "./task/TaskUnAssignmentService"; import UserSubjectAssignmentService from "./UserSubjectAssignmentService"; import moment from "moment"; import AllSyncableEntityMetaData from "../model/AllSyncableEntityMetaData"; +import ProgramConfigService from './ProgramConfigService'; +import {IndividualSearchActionNames as IndividualSearchActions} from '../action/individual/IndividualSearchActions'; +import {LandingViewActionsNames as LandingViewActions} from '../action/LandingViewActions'; @Service("syncService") class SyncService extends BaseService { @@ -352,6 +355,29 @@ class SyncService extends BaseService { this.messageService.init(); this.ruleService.init(); } + + resetServicesAfterFullSyncCompletion(updatedSyncSource) { + if (updatedSyncSource !== SyncService.syncSources.ONLY_UPLOAD_BACKGROUND_JOB) { + General.logInfo("Sync", "Full Sync completed, performing reset") + setTimeout(() => this.reset(), 1); + this.getService(SettingsService).initLanguages(); + General.logInfo("Sync", 'Full Sync completed, reset completed'); + } + } + + reset() { + this.context.getService(RuleEvaluationService).init(); + this.context.getService(ProgramConfigService).init(); + this.context.getService(MessageService).init(); + this.context.getService(RuleService).init(); + this.dispatchAction('RESET'); + this.context.getService(PrivilegeService).deleteRevokedEntities(); + //To load subjectType after sync + this.dispatchAction(IndividualSearchActions.ON_LOAD); + + //To re-render LandingView after sync + this.dispatchAction(LandingViewActions.ON_LOAD, {syncRequired: false}); + } } export default SyncService; diff --git a/packages/openchs-android/src/task/Sync.js b/packages/openchs-android/src/task/Sync.js index 8e3264795..5f6e482fc 100644 --- a/packages/openchs-android/src/task/Sync.js +++ b/packages/openchs-android/src/task/Sync.js @@ -11,23 +11,17 @@ 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'; import moment from 'moment'; +function 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}); +} + 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"); @@ -63,7 +57,7 @@ class Sync extends BaseTask { }, (message) => { }, connectionInfo, Date.now(), SyncService.syncSources.ONLY_UPLOAD_BACKGROUND_JOB, null) - .then(this.performPostBackgroundSyncActions(dispatchAction, globalContext)) + .then(this.performPostBackgroundSyncActions(globalContext)) .catch((e) => { ErrorHandler.postScheduledJobError(e); }); @@ -81,27 +75,11 @@ class Sync extends BaseTask { return _.isEmpty(lastSynced) || moment(lastSynced[0].syncEndTime).add(30, 'minutes').isBefore(moment()); } - performPostBackgroundSyncActions(dispatchAction, globalContext) { + performPostBackgroundSyncActions(globalContext) { return (updatedSyncSource) => { - General.logInfo("Sync", "Sync completed") + General.logInfo("Sync", "Background 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'); - } + globalContext.beanRegistry.getService(SyncService).resetServicesAfterFullSyncCompletion(updatedSyncSource); }; } } diff --git a/packages/openchs-android/src/views/SyncComponent.js b/packages/openchs-android/src/views/SyncComponent.js index fad3aaa65..c519eedf4 100644 --- a/packages/openchs-android/src/views/SyncComponent.js +++ b/packages/openchs-android/src/views/SyncComponent.js @@ -1,11 +1,5 @@ import AbstractComponent from "../framework/view/AbstractComponent"; import Colors from "./primitives/Colors"; -import RuleEvaluationService from "../service/RuleEvaluationService"; -import ProgramConfigService from "../service/ProgramConfigService"; -import MessageService from "../service/MessageService"; -import RuleService from "../service/RuleService"; -import {IndividualSearchActionNames as IndividualSearchActions} from "../action/individual/IndividualSearchActions"; -import {LandingViewActionsNames as LandingViewActions} from "../action/LandingViewActions"; import {SyncActionNames as SyncActions} from "../action/SyncActions"; import General from "../utility/General"; import {SyncTelemetryActionNames as SyncTelemetryActions} from "../action/SyncTelemetryActions"; @@ -22,8 +16,6 @@ import EntitySyncStatusService from "../service/EntitySyncStatusService"; import React from "react"; import ProgressBarView from "./ProgressBarView"; import Reducers from "../reducer"; -import SettingsService from "../service/SettingsService"; -import PrivilegeService from "../service/PrivilegeService"; import AsyncAlert from "./common/AsyncAlert"; import {ScheduleDummySyncJob, ScheduleSyncJob} from "../AvniBackgroundJob"; @@ -43,20 +35,6 @@ class SyncComponent extends AbstractComponent { this.dispatchAction(SyncActions.PRE_SYNC); } - reset() { - this.context.getService(RuleEvaluationService).init(); - this.context.getService(ProgramConfigService).init(); - this.context.getService(MessageService).init(); - this.context.getService(RuleService).init(); - this.dispatchAction('RESET'); - this.context.getService(PrivilegeService).deleteRevokedEntities(); - //To load subjectType after sync - this.dispatchAction(IndividualSearchActions.ON_LOAD); - - //To re-render LandingView after sync - this.dispatchAction(LandingViewActions.ON_LOAD, {syncRequired: false}); - } - progressBarUpdate(progress, totalNumberOfPagesForCurrentEntity, numberOfPagesProcessedForCurrentEntity) { this.dispatchAction(SyncActions.ON_UPDATE, {progress, numberOfPagesProcessedForCurrentEntity, totalNumberOfPagesForCurrentEntity}) } @@ -68,9 +46,7 @@ class SyncComponent extends AbstractComponent { _postSync() { this.setState({syncStarted: false}); this.dispatchAction(SyncActions.POST_SYNC); - setTimeout(() => this.reset(), 1); - - this.context.getService(SettingsService).initLanguages(); + this.context.getService(SyncService).resetServicesAfterFullSyncCompletion(SyncService.syncSources.BACKGROUND_JOB); General.logInfo(this.viewName(), 'Sync completed dispatching reset'); }