From 7f2d406c7e8dbcbd319d0ea40c6dc91a5dcd75cc Mon Sep 17 00:00:00 2001 From: Alfonso Salces Date: Wed, 1 Jun 2022 12:10:06 +0200 Subject: [PATCH] MOBILE-4085 course-storage: Improve performance with OnPush --- .../pages/course-storage/course-storage.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/addons/storagemanager/pages/course-storage/course-storage.ts b/src/addons/storagemanager/pages/course-storage/course-storage.ts index 6aa817913..8b22a4fb4 100644 --- a/src/addons/storagemanager/pages/course-storage/course-storage.ts +++ b/src/addons/storagemanager/pages/course-storage/course-storage.ts @@ -13,7 +13,7 @@ // limitations under the License. import { CoreConstants } from '@/core/constants'; -import { Component, ElementRef, OnDestroy, OnInit } from '@angular/core'; +import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, OnDestroy, OnInit } from '@angular/core'; import { CoreCourse, CoreCourseProvider } from '@features/course/services/course'; import { CoreCourseHelper, @@ -41,6 +41,7 @@ import { CoreEventObserver, CoreEvents } from '@singletons/events'; selector: 'page-addon-storagemanager-course-storage', templateUrl: 'course-storage.html', styleUrls: ['course-storage.scss'], + changeDetection: ChangeDetectionStrategy.OnPush, }) export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { @@ -71,7 +72,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { protected isDestroyed = false; protected isGuest = false; - constructor(protected elementRef: ElementRef) { + constructor(protected elementRef: ElementRef, protected changeDetectorRef: ChangeDetectorRef) { // Refresh the enabled flags if site is updated. this.siteUpdatedObserver = CoreEvents.on(CoreEvents.SITE_UPDATED, () => { this.downloadCourseEnabled = !CoreCourses.isDownloadCourseDisabledInSite(); @@ -79,6 +80,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { this.initCoursePrefetch(); this.initModulePrefetch(); + this.changeDetectorRef.markForCheck(); }, CoreSites.getCurrentSiteId()); } @@ -134,6 +136,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { this.initCoursePrefetch(), this.initModulePrefetch(), ]); + this.changeDetectorRef.markForCheck(); } /** @@ -311,6 +314,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { } module.calculatingSize = true; + this.changeDetectorRef.markForCheck(); if (!section) { section = this.sections.find((section) => section.modules.some((mod) => mod.id === module.id)); @@ -333,6 +337,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { // Ignore errors, it shouldn't happen. } finally { module.calculatingSize = false; + this.changeDetectorRef.markForCheck(); } })); @@ -340,6 +345,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { if (section) { section.calculatingSize = false; } + this.changeDetectorRef.markForCheck(); } /** @@ -468,6 +474,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { await this.updateModulesSizes(modules, section); CoreCourseHelper.calculateSectionsStatus(this.sections, this.courseId, false, false); + this.changeDetectorRef.markForCheck(); } } @@ -503,7 +510,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { */ async prefecthSection(section: AddonStorageManagerCourseSection): Promise { section.isCalculating = true; - + this.changeDetectorRef.markForCheck(); try { await CoreCourseHelper.confirmDownloadSizeSection(this.courseId, section, this.sections); @@ -516,16 +523,19 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { } } finally { await this.updateModulesSizes(section.modules, section); + this.changeDetectorRef.markForCheck(); } } catch (error) { // User cancelled or there was an error calculating the size. if (!this.isDestroyed && error) { CoreDomUtils.showErrorModal(error); + this.changeDetectorRef.markForCheck(); return; } } finally { section.isCalculating = false; + this.changeDetectorRef.markForCheck(); } } @@ -549,6 +559,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { try { // Get download size to ask for confirm if it's high. + this.changeDetectorRef.markForCheck(); const size = await module.prefetchHandler.getDownloadSize(module, module.course, true); await CoreCourseHelper.prefetchModule(module.prefetchHandler, module, size, module.course, refresh); @@ -562,6 +573,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { module.spinner = false; await this.updateModulesSizes([module]); + this.changeDetectorRef.markForCheck(); } } @@ -580,6 +592,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { module.downloadStatus = status; module.handlerData?.updateStatus?.(status); + this.changeDetectorRef.markForCheck(); } /** @@ -635,6 +648,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { } try { + this.changeDetectorRef.markForCheck(); await CoreCourseHelper.confirmAndPrefetchCourse( this.prefetchCourseData, course, @@ -643,6 +657,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { isGuest: this.isGuest, }, ); + this.changeDetectorRef.markForCheck(); } catch (error) { if (this.isDestroyed) { return;