From 8e9b550254f70805e0140a6baf953990d588a6b3 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 14 Apr 2021 11:24:08 +0200 Subject: [PATCH] MOBILE-3644 sync: Always use new offline warning functions --- src/addons/mod/choice/services/choice-sync.ts | 9 ++------ src/addons/mod/data/services/data-sync.ts | 20 ++++++------------ .../mod/glossary/services/glossary-sync.ts | 13 ++---------- src/addons/mod/wiki/services/wiki-sync.ts | 7 +------ src/core/classes/base-sync.ts | 21 ++++++++++++++----- 5 files changed, 27 insertions(+), 43 deletions(-) diff --git a/src/addons/mod/choice/services/choice-sync.ts b/src/addons/mod/choice/services/choice-sync.ts index d933c29c2..a27a01f05 100644 --- a/src/addons/mod/choice/services/choice-sync.ts +++ b/src/addons/mod/choice/services/choice-sync.ts @@ -19,9 +19,8 @@ import { CoreCourse } from '@features/course/services/course'; import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreApp } from '@services/app'; import { CoreSites } from '@services/sites'; -import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; -import { makeSingleton, Translate } from '@singletons'; +import { makeSingleton } from '@singletons'; import { CoreEvents } from '@singletons/events'; import { AddonModChoice, AddonModChoiceProvider } from './choice'; import { AddonModChoiceOffline } from './choice-offline'; @@ -192,11 +191,7 @@ export class AddonModChoiceSyncProvider extends CoreCourseActivitySyncBaseProvid await AddonModChoiceOffline.deleteResponse(choiceId, siteId, userId); // Responses deleted, add a warning. - result.warnings.push(Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: data.name, - error: CoreTextUtils.getErrorMessageFromError(error), - })); + this.addOfflineDataDeletedWarning(result.warnings, data.name, error); } // Data has been sent to server, prefetch choice if needed. diff --git a/src/addons/mod/data/services/data-sync.ts b/src/addons/mod/data/services/data-sync.ts index 92d0d94c9..b89e4b2a6 100644 --- a/src/addons/mod/data/services/data-sync.ts +++ b/src/addons/mod/data/services/data-sync.ts @@ -235,28 +235,20 @@ export class AddonModDataSyncProvider extends CoreCourseActivitySyncBaseProvider result: AddonModDataSyncResult, siteId: string, ): Promise { - const synEntryResult = await this.performSyncEntry(database, entryActions, result, siteId); + const syncEntryResult = await this.performSyncEntry(database, entryActions, result, siteId); - if (synEntryResult.discardError) { + if (syncEntryResult.discardError) { // Submission was discarded, add a warning. - const message = Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: database.name, - error: synEntryResult.discardError, - }); - - if (result.warnings.indexOf(message) == -1) { - result.warnings.push(message); - } + this.addOfflineDataDeletedWarning(result.warnings, database.name, syncEntryResult.discardError); } // Sync done. Send event. CoreEvents.trigger(AddonModDataSyncProvider.AUTO_SYNCED, { dataId: database.id, - entryId: synEntryResult.entryId, - offlineEntryId: synEntryResult.offlineId, + entryId: syncEntryResult.entryId, + offlineEntryId: syncEntryResult.offlineId, warnings: result.warnings, - deleted: synEntryResult.deleted, + deleted: syncEntryResult.deleted, }, siteId); } diff --git a/src/addons/mod/glossary/services/glossary-sync.ts b/src/addons/mod/glossary/services/glossary-sync.ts index ca697c46d..3502a82a5 100644 --- a/src/addons/mod/glossary/services/glossary-sync.ts +++ b/src/addons/mod/glossary/services/glossary-sync.ts @@ -23,7 +23,6 @@ import { CoreRatingSync } from '@features/rating/services/rating-sync'; import { CoreApp } from '@services/app'; import { CoreSites } from '@services/sites'; import { CoreSync } from '@services/sync'; -import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; import { CoreWSExternalFile } from '@services/ws'; import { makeSingleton, Translate } from '@singletons'; @@ -222,11 +221,7 @@ export class AddonModGlossarySyncProvider extends CoreCourseActivitySyncBaseProv await this.deleteAddEntry(glossaryId, data.concept, data.timecreated, siteId); // Responses deleted, add a warning. - result.warnings.push(Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: data.concept, - error: CoreTextUtils.getErrorMessageFromError(error), - })); + this.addOfflineDataDeletedWarning(result.warnings, data.concept, error); } })); @@ -275,11 +270,7 @@ export class AddonModGlossarySyncProvider extends CoreCourseActivitySyncBaseProv const glossary = await AddonModGlossary.getGlossary(result.itemSet.courseId, result.itemSet.instanceId, { siteId }); result.warnings.forEach((warning) => { - warnings.push(Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: glossary.name, - error: warning, - })); + this.addOfflineDataDeletedWarning(warnings, glossary.name, warning); }); } })); diff --git a/src/addons/mod/wiki/services/wiki-sync.ts b/src/addons/mod/wiki/services/wiki-sync.ts index 0f9375231..691a08e9b 100644 --- a/src/addons/mod/wiki/services/wiki-sync.ts +++ b/src/addons/mod/wiki/services/wiki-sync.ts @@ -20,7 +20,6 @@ import { CoreApp } from '@services/app'; import { CoreGroups } from '@services/groups'; import { CoreSites } from '@services/sites'; import { CoreSync } from '@services/sync'; -import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton, Translate } from '@singletons'; import { CoreEvents } from '@singletons/events'; @@ -260,11 +259,7 @@ export class AddonModWikiSyncProvider extends CoreSyncBaseProvider { * @param error Specific error message. */ protected addOfflineDataDeletedWarning(warnings: string[], name: string, error: CoreAnyError): void { - const warning = Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: name, - error: CoreTextUtils.getErrorMessageFromError(error), - }); + const warning = this.getOfflineDataDeletedWarning(name, error); if (warnings.indexOf(warning) == -1) { warnings.push(warning); @@ -113,6 +109,21 @@ export class CoreSyncBaseProvider { } } + /** + * Add an offline data deleted warning to a list of warnings. + * + * @param name Instance name. + * @param error Specific error message. + * @return Warning message. + */ + protected getOfflineDataDeletedWarning(name: string, error: CoreAnyError): string { + return Translate.instant('core.warningofflinedatadeleted', { + component: this.componentTranslate, + name: name, + error: CoreTextUtils.getErrorMessageFromError(error), + }); + } + /** * If there's an ongoing sync for a certain identifier return it. *