From 007804494d838166f7be2fae2c114e4e201416c0 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 22 Jul 2019 15:52:47 +0200 Subject: [PATCH] MOBILE-3061 dashboard: Pass download enabled as input --- .../components/myoverview/myoverview.ts | 28 ++++++++----------- .../recentlyaccessedcourses.ts | 28 ++++++++----------- .../addon-block-sitemainmenu.html | 10 ++++--- .../components/sitemainmenu/sitemainmenu.ts | 26 +++++++++-------- .../starredcourses/starredcourses.ts | 28 ++++++++----------- src/core/block/components/block/block.ts | 25 ++++++++++++++--- .../courses/pages/dashboard/dashboard.html | 2 +- .../components/index/core-sitehome-index.html | 2 +- src/core/sitehome/components/index/index.ts | 5 ++-- 9 files changed, 81 insertions(+), 73 deletions(-) diff --git a/src/addon/block/myoverview/components/myoverview/myoverview.ts b/src/addon/block/myoverview/components/myoverview/myoverview.ts index b36202f7e..d257764c2 100644 --- a/src/addon/block/myoverview/components/myoverview/myoverview.ts +++ b/src/addon/block/myoverview/components/myoverview/myoverview.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, OnInit, Input, OnDestroy, ViewChild, Injector } from '@angular/core'; +import { Component, OnInit, Input, OnDestroy, ViewChild, Injector, OnChanges, SimpleChange } from '@angular/core'; import { Searchbar } from 'ionic-angular'; import { CoreEventsProvider } from '@providers/events'; import { CoreUtilsProvider } from '@providers/utils/utils'; @@ -32,7 +32,7 @@ import { CoreBlockBaseComponent } from '@core/block/classes/base-block-component selector: 'addon-block-myoverview', templateUrl: 'addon-block-myoverview.html' }) -export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implements OnInit, OnDestroy { +export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implements OnInit, OnChanges, OnDestroy { @ViewChild('searchbar') searchbar: Searchbar; @Input() downloadEnabled: boolean; @@ -67,7 +67,6 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem protected prefetchIconsInitialized = false; protected isDestroyed; - protected downloadButtonObserver; protected coursesObserver; protected updateSiteObserver; protected courseIds = []; @@ -87,18 +86,6 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem */ ngOnInit(): void { // Refresh the enabled flags if enabled. - this.downloadButtonObserver = this.eventsProvider.on(CoreCoursesProvider.EVENT_DASHBOARD_DOWNLOAD_ENABLED_CHANGED, - (data) => { - const wasEnabled = this.downloadEnabled; - - this.downloadEnabled = data.enabled; - - if (!wasEnabled && this.downloadEnabled && this.loaded) { - // Download all courses is enabled now, initialize it. - this.initPrefetchCoursesIcons(); - } - }); - this.downloadCourseEnabled = !this.coursesProvider.isDownloadCourseDisabledInSite(); this.downloadCoursesEnabled = !this.coursesProvider.isDownloadCoursesDisabledInSite(); @@ -128,6 +115,16 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem }); } + /** + * Detect changes on input properties. + */ + ngOnChanges(changes: {[name: string]: SimpleChange}): void { + if (changes.downloadEnabled && !changes.downloadEnabled.previousValue && this.downloadEnabled && this.loaded) { + // Download all courses is enabled now, initialize it. + this.initPrefetchCoursesIcons(); + } + } + /** * Perform the invalidate content function. * @@ -350,6 +347,5 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem this.isDestroyed = true; this.coursesObserver && this.coursesObserver.off(); this.updateSiteObserver && this.updateSiteObserver.off(); - this.downloadButtonObserver && this.downloadButtonObserver.off(); } } diff --git a/src/addon/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts b/src/addon/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts index 2d962d17c..92697e4f0 100644 --- a/src/addon/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts +++ b/src/addon/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, OnInit, OnDestroy, Injector, Input } from '@angular/core'; +import { Component, OnInit, OnDestroy, Injector, Input, OnChanges, SimpleChange } from '@angular/core'; import { CoreEventsProvider } from '@providers/events'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitesProvider } from '@providers/sites'; @@ -30,7 +30,7 @@ import { CoreBlockBaseComponent } from '@core/block/classes/base-block-component selector: 'addon-block-recentlyaccessedcourses', templateUrl: 'addon-block-recentlyaccessedcourses.html' }) -export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseComponent implements OnInit, OnDestroy { +export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseComponent implements OnInit, OnChanges, OnDestroy { @Input() downloadEnabled: boolean; courses = []; @@ -41,7 +41,6 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom protected prefetchIconsInitialized = false; protected isDestroyed; - protected downloadButtonObserver; protected coursesObserver; protected courseIds = []; protected fetchContentDefaultError = 'Error getting recent courses data.'; @@ -59,18 +58,6 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom * Component being initialized. */ ngOnInit(): void { - // Refresh the enabled flags if enabled. - this.downloadButtonObserver = this.eventsProvider.on(CoreCoursesProvider.EVENT_DASHBOARD_DOWNLOAD_ENABLED_CHANGED, - (data) => { - const wasEnabled = this.downloadEnabled; - - this.downloadEnabled = data.enabled; - - if (!wasEnabled && this.downloadEnabled && this.loaded) { - // Download all courses is enabled now, initialize it. - this.initPrefetchCoursesIcons(); - } - }); this.coursesObserver = this.eventsProvider.on(CoreCoursesProvider.EVENT_MY_COURSES_UPDATED, () => { this.refreshContent(); @@ -79,6 +66,16 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom super.ngOnInit(); } + /** + * Detect changes on input properties. + */ + ngOnChanges(changes: {[name: string]: SimpleChange}): void { + if (changes.downloadEnabled && !changes.downloadEnabled.previousValue && this.downloadEnabled && this.loaded) { + // Download all courses is enabled now, initialize it. + this.initPrefetchCoursesIcons(); + } + } + /** * Perform the invalidate content function. * @@ -155,6 +152,5 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom ngOnDestroy(): void { this.isDestroyed = true; this.coursesObserver && this.coursesObserver.off(); - this.downloadButtonObserver && this.downloadButtonObserver.off(); } } diff --git a/src/addon/block/sitemainmenu/components/sitemainmenu/addon-block-sitemainmenu.html b/src/addon/block/sitemainmenu/components/sitemainmenu/addon-block-sitemainmenu.html index e1a3c10a5..f9a5b7f82 100644 --- a/src/addon/block/sitemainmenu/components/sitemainmenu/addon-block-sitemainmenu.html +++ b/src/addon/block/sitemainmenu/components/sitemainmenu/addon-block-sitemainmenu.html @@ -2,9 +2,11 @@

{{ 'addon.block_sitemainmenu.pluginname' | translate }}

- - - + + + + - + + diff --git a/src/addon/block/sitemainmenu/components/sitemainmenu/sitemainmenu.ts b/src/addon/block/sitemainmenu/components/sitemainmenu/sitemainmenu.ts index 0f3355219..23bfb74ca 100644 --- a/src/addon/block/sitemainmenu/components/sitemainmenu/sitemainmenu.ts +++ b/src/addon/block/sitemainmenu/components/sitemainmenu/sitemainmenu.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, OnInit, Injector } from '@angular/core'; +import { Component, OnInit, Injector, Input } from '@angular/core'; import { CoreSitesProvider } from '@providers/sites'; import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreCourseHelperProvider } from '@core/course/providers/helper'; @@ -28,7 +28,9 @@ import { CoreBlockBaseComponent } from '@core/block/classes/base-block-component templateUrl: 'addon-block-sitemainmenu.html' }) export class AddonBlockSiteMainMenuComponent extends CoreBlockBaseComponent implements OnInit { - block: any; + @Input() downloadEnabled: boolean; + + mainMenuBlock: any; siteHomeId: number; protected fetchContentDefaultError = 'Error getting main menu data.'; @@ -60,9 +62,9 @@ export class AddonBlockSiteMainMenuComponent extends CoreBlockBaseComponent impl promises.push(this.courseProvider.invalidateSections(this.siteHomeId)); promises.push(this.siteHomeProvider.invalidateNewsForum(this.siteHomeId)); - if (this.block && this.block.modules) { + if (this.mainMenuBlock && this.mainMenuBlock.modules) { // Invalidate modules prefetch data. - promises.push(this.prefetchDelegate.invalidateModules(this.block.modules, this.siteHomeId)); + promises.push(this.prefetchDelegate.invalidateModules(this.mainMenuBlock.modules, this.siteHomeId)); } return Promise.all(promises); @@ -75,11 +77,11 @@ export class AddonBlockSiteMainMenuComponent extends CoreBlockBaseComponent impl */ protected fetchContent(): Promise { return this.courseProvider.getSections(this.siteHomeId, false, true).then((sections) => { - this.block = sections.find((section) => section.section == 0); + this.mainMenuBlock = sections.find((section) => section.section == 0); - if (this.block) { - this.block.hasContent = this.courseHelper.sectionHasContent(this.block); - this.courseHelper.addHandlerDataForModules([this.block], this.siteHomeId); + if (this.mainMenuBlock) { + this.mainMenuBlock.hasContent = this.courseHelper.sectionHasContent(this.mainMenuBlock); + this.courseHelper.addHandlerDataForModules([this.mainMenuBlock], this.siteHomeId); // Check if Site Home displays announcements. If so, remove it from the main menu block. const currentSite = this.sitesProvider.getCurrentSite(), @@ -92,15 +94,15 @@ export class AddonBlockSiteMainMenuComponent extends CoreBlockBaseComponent impl hasNewsItem = items.find((item) => { return item == '0'; }); } - if (hasNewsItem && this.block.modules) { + if (hasNewsItem && this.mainMenuBlock.modules) { // Remove forum activity (news one only) from the main menu block to prevent duplicates. return this.siteHomeProvider.getNewsForum(this.siteHomeId).then((forum) => { // Search the module that belongs to site news. - for (let i = 0; i < this.block.modules.length; i++) { - const module = this.block.modules[i]; + for (let i = 0; i < this.mainMenuBlock.modules.length; i++) { + const module = this.mainMenuBlock.modules[i]; if (module.modname == 'forum' && module.instance == forum.id) { - this.block.modules.splice(i, 1); + this.mainMenuBlock.modules.splice(i, 1); break; } } diff --git a/src/addon/block/starredcourses/components/starredcourses/starredcourses.ts b/src/addon/block/starredcourses/components/starredcourses/starredcourses.ts index ffa235134..ca25f3354 100644 --- a/src/addon/block/starredcourses/components/starredcourses/starredcourses.ts +++ b/src/addon/block/starredcourses/components/starredcourses/starredcourses.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, OnInit, OnDestroy, Injector, Input } from '@angular/core'; +import { Component, OnInit, OnDestroy, Injector, Input, OnChanges, SimpleChange } from '@angular/core'; import { CoreEventsProvider } from '@providers/events'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitesProvider } from '@providers/sites'; @@ -30,7 +30,7 @@ import { CoreBlockBaseComponent } from '@core/block/classes/base-block-component selector: 'addon-block-starredcourses', templateUrl: 'addon-block-starredcourses.html' }) -export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent implements OnInit, OnDestroy { +export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent implements OnInit, OnChanges, OnDestroy { @Input() downloadEnabled: boolean; courses = []; @@ -41,7 +41,6 @@ export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent im protected prefetchIconsInitialized = false; protected isDestroyed; - protected downloadButtonObserver; protected coursesObserver; protected courseIds = []; protected fetchContentDefaultError = 'Error getting starred courses data.'; @@ -59,18 +58,6 @@ export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent im * Component being initialized. */ ngOnInit(): void { - // Refresh the enabled flags if enabled. - this.downloadButtonObserver = this.eventsProvider.on(CoreCoursesProvider.EVENT_DASHBOARD_DOWNLOAD_ENABLED_CHANGED, - (data) => { - const wasEnabled = this.downloadEnabled; - - this.downloadEnabled = data.enabled; - - if (!wasEnabled && this.downloadEnabled && this.loaded) { - // Download all courses is enabled now, initialize it. - this.initPrefetchCoursesIcons(); - } - }); this.coursesObserver = this.eventsProvider.on(CoreCoursesProvider.EVENT_MY_COURSES_UPDATED, () => { this.refreshContent(); @@ -79,6 +66,16 @@ export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent im super.ngOnInit(); } + /** + * Detect changes on input properties. + */ + ngOnChanges(changes: {[name: string]: SimpleChange}): void { + if (changes.downloadEnabled && !changes.downloadEnabled.previousValue && this.downloadEnabled && this.loaded) { + // Download all courses is enabled now, initialize it. + this.initPrefetchCoursesIcons(); + } + } + /** * Perform the invalidate content function. * @@ -155,6 +152,5 @@ export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent im ngOnDestroy(): void { this.isDestroyed = true; this.coursesObserver && this.coursesObserver.off(); - this.downloadButtonObserver && this.downloadButtonObserver.off(); } } diff --git a/src/core/block/components/block/block.ts b/src/core/block/components/block/block.ts index faf8aa3be..eebf4fd4d 100644 --- a/src/core/block/components/block/block.ts +++ b/src/core/block/components/block/block.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, Input, OnInit, Injector, ViewChild, OnDestroy } from '@angular/core'; +import { Component, Input, OnInit, Injector, ViewChild, OnDestroy, DoCheck, KeyValueDiffers } from '@angular/core'; import { CoreBlockDelegate } from '../../providers/delegate'; import { CoreDynamicComponent } from '@components/dynamic-component/dynamic-component'; import { Subscription } from 'rxjs'; @@ -25,7 +25,7 @@ import { CoreEventsProvider } from '@providers/events'; selector: 'core-block', templateUrl: 'core-block.html' }) -export class CoreBlockComponent implements OnInit, OnDestroy { +export class CoreBlockComponent implements OnInit, OnDestroy, DoCheck { @ViewChild(CoreDynamicComponent) dynamicComponent: CoreDynamicComponent; @Input() block: any; // The block to render. @@ -40,8 +40,12 @@ export class CoreBlockComponent implements OnInit, OnDestroy { blockSubscription: Subscription; - constructor(protected injector: Injector, protected blockDelegate: CoreBlockDelegate, - protected eventsProvider: CoreEventsProvider) { } + protected differ: any; // To detect changes in the data input. + + constructor(protected injector: Injector, protected blockDelegate: CoreBlockDelegate, differs: KeyValueDiffers, + protected eventsProvider: CoreEventsProvider) { + this.differ = differs.find([]).create(); + } /** * Component being initialized. @@ -57,6 +61,19 @@ export class CoreBlockComponent implements OnInit, OnDestroy { this.initBlock(); } + /** + * Detect and act upon changes that Angular can’t or won’t detect on its own (objects and arrays). + */ + ngDoCheck(): void { + if (this.data) { + // Check if there's any change in the extraData object. + const changes = this.differ.diff(this.extraData); + if (changes) { + this.data = Object.assign(this.data, this.extraData || {}); + } + } + } + /** * Get block display data and initialises the block once this is available. If the block is not * supported at the moment, try again if the available blocks are updated (because it comes diff --git a/src/core/courses/pages/dashboard/dashboard.html b/src/core/courses/pages/dashboard/dashboard.html index f9eff8228..d94fcdd0a 100644 --- a/src/core/courses/pages/dashboard/dashboard.html +++ b/src/core/courses/pages/dashboard/dashboard.html @@ -26,7 +26,7 @@ - + diff --git a/src/core/sitehome/components/index/core-sitehome-index.html b/src/core/sitehome/components/index/core-sitehome-index.html index a8a1ed1bb..17a52f8b0 100644 --- a/src/core/sitehome/components/index/core-sitehome-index.html +++ b/src/core/sitehome/components/index/core-sitehome-index.html @@ -24,7 +24,7 @@ - + diff --git a/src/core/sitehome/components/index/index.ts b/src/core/sitehome/components/index/index.ts index 34aa44057..a629f76c4 100644 --- a/src/core/sitehome/components/index/index.ts +++ b/src/core/sitehome/components/index/index.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, OnInit, ViewChildren, QueryList } from '@angular/core'; +import { Component, OnInit, ViewChildren, QueryList, Input } from '@angular/core'; import { CoreSitesProvider } from '@providers/sites'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreCourseProvider } from '@core/course/providers/course'; @@ -31,6 +31,7 @@ import { CoreSite } from '@classes/site'; }) export class CoreSiteHomeIndexComponent implements OnInit { @ViewChildren(CoreBlockComponent) blocksComponents: QueryList; + @Input() downloadEnabled: boolean; dataLoaded = false; section: any; @@ -40,7 +41,6 @@ export class CoreSiteHomeIndexComponent implements OnInit { siteHomeId: number; currentSite: CoreSite; blocks = []; - downloadEnabled: boolean; constructor(private domUtils: CoreDomUtilsProvider, sitesProvider: CoreSitesProvider, private courseProvider: CoreCourseProvider, private courseHelper: CoreCourseHelperProvider, @@ -53,7 +53,6 @@ export class CoreSiteHomeIndexComponent implements OnInit { * Component being initialized. */ ngOnInit(): void { - this.downloadEnabled = !this.currentSite.isOfflineDisabled(); this.loadContent().finally(() => { this.dataLoaded = true; });