From a4225b8c02dd676a7114825467100d0d964ee3b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 10 Feb 2021 11:36:54 +0100 Subject: [PATCH] MOBILE-3636 sync: Small improvements on previous sync services --- .../pages/edit-event/edit-event.module.ts | 2 ++ src/addons/calendar/services/calendar-sync.ts | 33 +++++++++---------- src/addons/mod/book/book-lazy.module.ts | 2 +- .../index/addon-mod-lesson-index.html | 2 +- src/addons/mod/lesson/lesson-lazy.module.ts | 2 +- .../mod/lesson/services/handlers/prefetch.ts | 2 +- src/addons/mod/lesson/services/lesson-sync.ts | 22 ++++++------- src/addons/mod/page/page-lazy.module.ts | 2 +- src/core/classes/base-sync.ts | 4 +-- .../features/course/classes/activity-sync.ts | 2 +- src/core/services/sync.ts | 9 +++-- src/core/services/utils/utils.ts | 2 +- 12 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/addons/calendar/pages/edit-event/edit-event.module.ts b/src/addons/calendar/pages/edit-event/edit-event.module.ts index c9c865932..0acf07633 100644 --- a/src/addons/calendar/pages/edit-event/edit-event.module.ts +++ b/src/addons/calendar/pages/edit-event/edit-event.module.ts @@ -19,11 +19,13 @@ import { CoreSharedModule } from '@/core/shared.module'; import { CoreEditorComponentsModule } from '@features/editor/components/components.module'; import { AddonCalendarEditEventPage } from './edit-event.page'; +import { CanLeaveGuard } from '@guards/can-leave'; const routes: Routes = [ { path: '', component: AddonCalendarEditEventPage, + canDeactivate: [CanLeaveGuard], }, ]; diff --git a/src/addons/calendar/services/calendar-sync.ts b/src/addons/calendar/services/calendar-sync.ts index d897a3d19..eee65e310 100644 --- a/src/addons/calendar/services/calendar-sync.ts +++ b/src/addons/calendar/services/calendar-sync.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { CoreSyncBaseProvider } from '@classes/base-sync'; +import { CoreSyncBaseProvider, CoreSyncBlockedError } from '@classes/base-sync'; import { CoreApp } from '@services/app'; import { CoreEvents } from '@singletons/events'; import { CoreSites } from '@services/sites'; @@ -27,9 +27,9 @@ import { import { AddonCalendarOffline } from './calendar-offline'; import { AddonCalendarHelper } from './calendar-helper'; import { makeSingleton, Translate } from '@singletons'; -import { CoreError } from '@classes/errors/error'; import { CoreSync } from '@services/sync'; import { CoreTextUtils } from '@services/utils/text'; +import { CoreNetworkError } from '@classes/errors/network-error'; /** * Service to sync calendar. @@ -52,21 +52,23 @@ export class AddonCalendarSyncProvider extends CoreSyncBaseProvider { + async syncAllEvents(siteId?: string, force = false): Promise { await this.syncOnSites('all calendar events', this.syncAllEventsFunc.bind(this, [force]), siteId); } /** * Sync all events on a site. * - * @param siteId Site ID to sync. * @param force Wether to force sync not depending on last execution. + * @param siteId Site ID to sync. * @return Promise resolved if sync is successful, rejected if sync fails. */ - protected async syncAllEventsFunc(siteId: string, force?: boolean): Promise { - const result = await (force ? this.syncEvents(siteId) : this.syncEventsIfNeeded(siteId)); + protected async syncAllEventsFunc(force = false, siteId?: string): Promise { + const result = force + ? await this.syncEvents(siteId) + : await this.syncEventsIfNeeded(siteId); - if (result && result.updated) { + if (result?.updated) { // Sync successful, send event. CoreEvents.trigger(AddonCalendarSyncProvider.AUTO_SYNCED, result, siteId); } @@ -78,13 +80,13 @@ export class AddonCalendarSyncProvider extends CoreSyncBaseProvider { + async syncEventsIfNeeded(siteId?: string): Promise { siteId = siteId || CoreSites.instance.getCurrentSiteId(); const needed = await this.isSyncNeeded(AddonCalendarSyncProvider.SYNC_ID, siteId); if (needed) { - await this.syncEvents(siteId); + return this.syncEvents(siteId); } } @@ -125,17 +127,12 @@ export class AddonCalendarSyncProvider extends CoreSyncBaseProvider 0) { if (!CoreApp.instance.isOnline()) { // Cannot sync in offline. - throw new CoreError('Cannot sync while offline'); + throw new CoreNetworkError(); } const promises = eventIds.map((eventId) => this.syncOfflineEvent(eventId, result, siteId)); @@ -175,10 +172,10 @@ export class AddonCalendarSyncProvider extends CoreSyncBaseProvider import('./pages/index/index.module').then( m => m.AddonModBookIndexPageModule), }, ]; diff --git a/src/addons/mod/lesson/components/index/addon-mod-lesson-index.html b/src/addons/mod/lesson/components/index/addon-mod-lesson-index.html index ab0d8373c..4f4f7b741 100644 --- a/src/addons/mod/lesson/components/index/addon-mod-lesson-index.html +++ b/src/addons/mod/lesson/components/index/addon-mod-lesson-index.html @@ -8,7 +8,7 @@ (action)="expandDescription()" iconAction="fas-arrow-right"> + iconAction="far-newspaper" (action)="gotoBlog()"> diff --git a/src/addons/mod/lesson/lesson-lazy.module.ts b/src/addons/mod/lesson/lesson-lazy.module.ts index 41c85cdd7..9670c4132 100644 --- a/src/addons/mod/lesson/lesson-lazy.module.ts +++ b/src/addons/mod/lesson/lesson-lazy.module.ts @@ -17,7 +17,7 @@ import { RouterModule, Routes } from '@angular/router'; const routes: Routes = [ { - path: ':courseId/:cmdId', + path: ':courseId/:cmId', loadChildren: () => import('./pages/index/index.module').then( m => m.AddonModLessonIndexPageModule), }, { diff --git a/src/addons/mod/lesson/services/handlers/prefetch.ts b/src/addons/mod/lesson/services/handlers/prefetch.ts index f81535a66..943a3ab32 100644 --- a/src/addons/mod/lesson/services/handlers/prefetch.ts +++ b/src/addons/mod/lesson/services/handlers/prefetch.ts @@ -257,7 +257,7 @@ export class AddonModLessonPrefetchHandlerService extends CoreCourseActivityPref */ protected async prefetchLesson(module: CoreCourseAnyModuleData, courseId?: number, single?: boolean): Promise { const siteId = CoreSites.instance.getCurrentSiteId(); - courseId = courseId || module.course || 1; + courseId = courseId || module.course || CoreSites.instance.getCurrentSiteHomeId(); const commonOptions = { readingStrategy: CoreSitesReadingStrategy.OnlyNetwork, diff --git a/src/addons/mod/lesson/services/lesson-sync.ts b/src/addons/mod/lesson/services/lesson-sync.ts index 48034d781..ea7c0a544 100644 --- a/src/addons/mod/lesson/services/lesson-sync.ts +++ b/src/addons/mod/lesson/services/lesson-sync.ts @@ -14,7 +14,7 @@ import { Injectable } from '@angular/core'; -import { CoreError } from '@classes/errors/error'; +import { CoreSyncBlockedError } from '@classes/base-sync'; import { CoreNetworkError } from '@classes/errors/network-error'; import { CoreCourseActivitySyncBaseProvider } from '@features/course/classes/activity-sync'; import { CoreCourse } from '@features/course/services/course'; @@ -122,7 +122,7 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid * @param force Wether to force sync not depending on last execution. * @return Promise resolved if sync is successful, rejected if sync fails. */ - syncAllLessons(siteId?: string, force?: boolean): Promise { + syncAllLessons(siteId?: string, force = false): Promise { return this.syncOnSites('all lessons', this.syncAllLessonsFunc.bind(this, !!force), siteId); } @@ -163,7 +163,7 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid */ async syncLessonIfNeeded( lessonId: number, - askPassword?: boolean, + askPassword = false, siteId?: string, ): Promise { const needed = await this.isSyncNeeded(lessonId, siteId); @@ -184,8 +184,8 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid */ async syncLesson( lessonId: number, - askPassword?: boolean, - ignoreBlock?: boolean, + askPassword = false, + ignoreBlock = false, siteId?: string, ): Promise { siteId = siteId || CoreSites.instance.getCurrentSiteId(); @@ -201,7 +201,7 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid if (!ignoreBlock && CoreSync.instance.isBlocked(AddonModLessonProvider.COMPONENT, lessonId, siteId)) { this.logger.debug('Cannot sync lesson ' + lessonId + ' because it is blocked.'); - throw new CoreError(Translate.instance.instant('core.errorsyncblocked', { $a: this.componentTranslate })); + throw new CoreSyncBlockedError(Translate.instance.instant('core.errorsyncblocked', { $a: this.componentTranslate })); } this.logger.debug('Try to sync lesson ' + lessonId + ' in site ' + siteId); @@ -222,8 +222,8 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid */ protected async performSyncLesson( lessonId: number, - askPassword?: boolean, - ignoreBlock?: boolean, + askPassword = false, + ignoreBlock = false, siteId?: string, ): Promise { // Sync offline logs. @@ -270,7 +270,7 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid protected async syncAttempts( lessonId: number, result: AddonModLessonSyncResult, - askPassword?: boolean, + askPassword = false, siteId?: string, ): Promise { let attempts = await AddonModLessonOffline.instance.getLessonAttempts(lessonId, siteId); @@ -408,8 +408,8 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid lessonId: number, result: AddonModLessonSyncResult, passwordData?: AddonModLessonGetPasswordResult, - askPassword?: boolean, - ignoreBlock?: boolean, + askPassword = false, + ignoreBlock = false, siteId?: string, ): Promise { // Attempts sent or there was none. If there is a finished retake, send it. diff --git a/src/addons/mod/page/page-lazy.module.ts b/src/addons/mod/page/page-lazy.module.ts index 759035838..a52103771 100644 --- a/src/addons/mod/page/page-lazy.module.ts +++ b/src/addons/mod/page/page-lazy.module.ts @@ -17,7 +17,7 @@ import { RouterModule, Routes } from '@angular/router'; const routes: Routes = [ { - path: ':courseId/:cmdId', + path: ':courseId/:cmId', loadChildren: () => import('./pages/index/index.module').then( m => m.AddonModPageIndexPageModule), }, ]; diff --git a/src/core/classes/base-sync.ts b/src/core/classes/base-sync.ts index 4f7c1b63f..1eef32e85 100644 --- a/src/core/classes/base-sync.ts +++ b/src/core/classes/base-sync.ts @@ -231,7 +231,7 @@ export class CoreSyncBaseProvider { * @param time Time to set. If not defined, current time. * @return Promise resolved when the time is set. */ - async setSyncTime(id: string, siteId?: string, time?: number): Promise { + async setSyncTime(id: string | number, siteId?: string, time?: number): Promise { time = typeof time != 'undefined' ? time : Date.now(); await CoreSync.instance.insertOrUpdateSyncRecord(this.component, id, { time: time }, siteId); @@ -245,7 +245,7 @@ export class CoreSyncBaseProvider { * @param siteId Site ID. If not defined, current site. * @return Promise resolved when done. */ - async setSyncWarnings(id: string, warnings: string[], siteId?: string): Promise { + async setSyncWarnings(id: string | number, warnings: string[], siteId?: string): Promise { const warningsText = JSON.stringify(warnings || []); await CoreSync.instance.insertOrUpdateSyncRecord(this.component, id, { warnings: warningsText }, siteId); diff --git a/src/core/features/course/classes/activity-sync.ts b/src/core/features/course/classes/activity-sync.ts index f7e97c136..ee5bb8983 100644 --- a/src/core/features/course/classes/activity-sync.ts +++ b/src/core/features/course/classes/activity-sync.ts @@ -20,7 +20,7 @@ import { CoreCourseModulePrefetchHandlerBase } from './module-prefetch-handler'; /** * Base class to create activity sync providers. It provides some common functions. */ -export class CoreCourseActivitySyncBaseProvider extends CoreSyncBaseProvider { +export class CoreCourseActivitySyncBaseProvider extends CoreSyncBaseProvider { /** * Conveniece function to prefetch data after an update. diff --git a/src/core/services/sync.ts b/src/core/services/sync.ts index 84a22e8ef..92b32718b 100644 --- a/src/core/services/sync.ts +++ b/src/core/services/sync.ts @@ -112,11 +112,16 @@ export class CoreSyncProvider { * @param siteId Site ID. If not defined, current site. * @return Promise resolved with done. */ - async insertOrUpdateSyncRecord(component: string, id: string, data: Partial, siteId?: string): Promise { + async insertOrUpdateSyncRecord( + component: string, + id: string | number, + data: Partial, + siteId?: string, + ): Promise { const db = await CoreSites.instance.getSiteDb(siteId); data.component = component; - data.id = id; + data.id = id + ''; await db.insertRecord(SYNC_TABLE_NAME, data); } diff --git a/src/core/services/utils/utils.ts b/src/core/services/utils/utils.ts index d4ccd5df4..bb8ea56a1 100644 --- a/src/core/services/utils/utils.ts +++ b/src/core/services/utils/utils.ts @@ -1607,7 +1607,7 @@ export class CoreUtilsProvider { const result = await promise; return result; - } catch (error) { + } catch { // Ignore errors. return fallback; }