Merge pull request #2013 from dpalou/MOBILE-3061

MOBILE-3061 dashboard: Pass download enabled as input
main
Juan Leyva 2019-07-29 11:56:36 +02:00 committed by GitHub
commit 83d2f455ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 81 additions and 73 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -2,9 +2,11 @@
<h2>{{ 'addon.block_sitemainmenu.pluginname' | translate }}</h2>
</ion-item-divider>
<core-loading [hideUntil]="loaded" class="core-loading-center">
<ion-item text-wrap *ngIf="block.summary">
<core-format-text [text]="block.summary"></core-format-text>
<ng-container *ngIf="mainMenuBlock">
<ion-item text-wrap *ngIf="mainMenuBlock.summary">
<core-format-text [text]="mainMenuBlock.summary"></core-format-text>
</ion-item>
<core-course-module *ngFor="let module of block.modules" [module]="module" [courseId]="siteHomeId" [downloadEnabled]="true" [section]="block"></core-course-module>
<core-course-module *ngFor="let module of mainMenuBlock.modules" [module]="module" [courseId]="siteHomeId" [downloadEnabled]="downloadEnabled" [section]="mainMenuBlock"></core-course-module>
</ng-container>
</core-loading>

View File

@ -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<any> {
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;
}
}

View File

@ -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();
}
}

View File

@ -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 cant or wont 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

View File

@ -26,7 +26,7 @@
<ion-refresher [enabled]="!!siteHomeComponent && siteHomeComponent.dataLoaded" (ionRefresh)="siteHomeComponent.doRefresh($event)">
<ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
</ion-refresher>
<core-sitehome-index></core-sitehome-index>
<core-sitehome-index [downloadEnabled]="downloadEnabled"></core-sitehome-index>
</ion-content>
</ng-template>
</core-tab>

View File

@ -24,7 +24,7 @@
<!-- Site home blocks. -->
<ng-container *ngFor="let block of blocks">
<core-block [block]="block" contextLevel="course" [instanceId]="siteHomeId"></core-block>
<core-block [block]="block" contextLevel="course" [instanceId]="siteHomeId" [extraData]="{'downloadEnabled': downloadEnabled}"></core-block>
</ng-container>
</ion-list>

View File

@ -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<CoreBlockComponent>;
@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;
});