MOBILE-3068 blogs: Fix infinite loading performance on my entries
parent
f3a51a3717
commit
fd6c700ff8
|
@ -4,9 +4,9 @@
|
||||||
</ion-refresher>
|
</ion-refresher>
|
||||||
<core-loading [hideUntil]="loaded" class="core-loading-center">
|
<core-loading [hideUntil]="loaded" class="core-loading-center">
|
||||||
<div class="safe-padding-horizontal">
|
<div class="safe-padding-horizontal">
|
||||||
<ion-item *ngIf="showMyIssuesToggle">
|
<ion-item *ngIf="showMyEntriesToggle">
|
||||||
<ion-label>{{ 'addon.blog.showonlyyourentries' | translate }}</ion-label>
|
<ion-label>{{ 'addon.blog.showonlyyourentries' | translate }}</ion-label>
|
||||||
<ion-toggle [(ngModel)]="onlyMyEntries"></ion-toggle>
|
<ion-toggle [(ngModel)]="onlyMyEntries" (ionChange)="onlyMyEntriesToggleChanged(onlyMyEntries)">></ion-toggle>
|
||||||
</ion-item>
|
</ion-item>
|
||||||
</div>
|
</div>
|
||||||
<core-empty-box *ngIf="entries && entries.length == 0" icon="fa-newspaper-o" [message]="'addon.blog.noentriesyet' | translate"></core-empty-box>
|
<core-empty-box *ngIf="entries && entries.length == 0" icon="fa-newspaper-o" [message]="'addon.blog.noentriesyet' | translate"></core-empty-box>
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
import { Component, Input, OnInit, ViewChild } from '@angular/core';
|
import { Component, Input, OnInit, ViewChild } from '@angular/core';
|
||||||
import { Content } from 'ionic-angular';
|
import { Content } from 'ionic-angular';
|
||||||
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
||||||
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
||||||
import { CoreSitesProvider } from '@providers/sites';
|
import { CoreSitesProvider } from '@providers/sites';
|
||||||
import { CoreUserProvider } from '@core/user/providers/user';
|
import { CoreUserProvider } from '@core/user/providers/user';
|
||||||
import { AddonBlogProvider } from '../../providers/blog';
|
import { AddonBlogProvider } from '../../providers/blog';
|
||||||
|
@ -38,6 +39,9 @@ export class AddonBlogEntriesComponent implements OnInit {
|
||||||
|
|
||||||
protected filter = {};
|
protected filter = {};
|
||||||
protected pageLoaded = 0;
|
protected pageLoaded = 0;
|
||||||
|
protected userPageLoaded = 0;
|
||||||
|
protected canLoadMoreEntries = false;
|
||||||
|
protected canLoadMoreUserEntries = true;
|
||||||
|
|
||||||
@ViewChild(Content) content: Content;
|
@ViewChild(Content) content: Content;
|
||||||
|
|
||||||
|
@ -46,14 +50,14 @@ export class AddonBlogEntriesComponent implements OnInit {
|
||||||
loadMoreError = false;
|
loadMoreError = false;
|
||||||
entries = [];
|
entries = [];
|
||||||
currentUserId: number;
|
currentUserId: number;
|
||||||
showMyIssuesToggle = false;
|
showMyEntriesToggle = false;
|
||||||
onlyMyEntries = false;
|
onlyMyEntries = false;
|
||||||
component = AddonBlogProvider.COMPONENT;
|
component = AddonBlogProvider.COMPONENT;
|
||||||
commentsEnabled: boolean;
|
commentsEnabled: boolean;
|
||||||
tagsEnabled: boolean;
|
tagsEnabled: boolean;
|
||||||
|
|
||||||
constructor(protected blogProvider: AddonBlogProvider, protected domUtils: CoreDomUtilsProvider,
|
constructor(protected blogProvider: AddonBlogProvider, protected domUtils: CoreDomUtilsProvider,
|
||||||
protected userProvider: CoreUserProvider, sitesProvider: CoreSitesProvider,
|
protected userProvider: CoreUserProvider, sitesProvider: CoreSitesProvider, protected utils: CoreUtilsProvider,
|
||||||
protected commentsProvider: CoreCommentsProvider, private tagProvider: CoreTagProvider) {
|
protected commentsProvider: CoreCommentsProvider, private tagProvider: CoreTagProvider) {
|
||||||
this.currentUserId = sitesProvider.getCurrentSiteUserId();
|
this.currentUserId = sitesProvider.getCurrentSiteUserId();
|
||||||
}
|
}
|
||||||
|
@ -65,6 +69,7 @@ export class AddonBlogEntriesComponent implements OnInit {
|
||||||
if (this.userId) {
|
if (this.userId) {
|
||||||
this.filter['userid'] = this.userId;
|
this.filter['userid'] = this.userId;
|
||||||
}
|
}
|
||||||
|
this.showMyEntriesToggle = !this.userId;
|
||||||
|
|
||||||
if (this.courseId) {
|
if (this.courseId) {
|
||||||
this.filter['courseid'] = this.courseId;
|
this.filter['courseid'] = this.courseId;
|
||||||
|
@ -107,9 +112,12 @@ export class AddonBlogEntriesComponent implements OnInit {
|
||||||
|
|
||||||
if (refresh) {
|
if (refresh) {
|
||||||
this.pageLoaded = 0;
|
this.pageLoaded = 0;
|
||||||
|
this.userPageLoaded = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.blogProvider.getEntries(this.filter, this.pageLoaded).then((result) => {
|
const loadPage = this.onlyMyEntries ? this.userPageLoaded : this.pageLoaded;
|
||||||
|
|
||||||
|
return this.blogProvider.getEntries(this.filter, loadPage).then((result) => {
|
||||||
const promises = result.entries.map((entry) => {
|
const promises = result.entries.map((entry) => {
|
||||||
switch (entry.publishstate) {
|
switch (entry.publishstate) {
|
||||||
case 'draft':
|
case 'draft':
|
||||||
|
@ -134,16 +142,25 @@ export class AddonBlogEntriesComponent implements OnInit {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (refresh) {
|
if (refresh) {
|
||||||
this.showMyIssuesToggle = false;
|
|
||||||
this.entries = result.entries;
|
this.entries = result.entries;
|
||||||
} else {
|
} else {
|
||||||
this.entries = this.entries.concat(result.entries);
|
this.entries = this.utils.uniqueArray(this.entries.concat(result.entries), 'id').sort((a, b) => {
|
||||||
|
return b.created - a.created;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.canLoadMore = result.totalentries > this.entries.length;
|
if (this.onlyMyEntries) {
|
||||||
|
const count = this.entries.filter((entry) => {
|
||||||
|
return entry.userid == this.currentUserId;
|
||||||
|
}).length;
|
||||||
|
this.canLoadMoreUserEntries = result.totalentries > count;
|
||||||
|
this.canLoadMore = this.canLoadMoreUserEntries;
|
||||||
|
this.userPageLoaded++;
|
||||||
|
} else {
|
||||||
|
this.canLoadMoreEntries = result.totalentries > this.entries.length;
|
||||||
|
this.canLoadMore = this.canLoadMoreEntries;
|
||||||
this.pageLoaded++;
|
this.pageLoaded++;
|
||||||
|
}
|
||||||
this.showMyIssuesToggle = !this.userId;
|
|
||||||
|
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
}).catch((message) => {
|
}).catch((message) => {
|
||||||
|
@ -154,6 +171,30 @@ export class AddonBlogEntriesComponent implements OnInit {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle between showing only my entries or not.
|
||||||
|
*
|
||||||
|
* @param {boolean} enabled If true, filter my entries. False otherwise.
|
||||||
|
*/
|
||||||
|
onlyMyEntriesToggleChanged(enabled: boolean): void {
|
||||||
|
if (enabled) {
|
||||||
|
const count = this.entries.filter((entry) => {
|
||||||
|
return entry.userid == this.currentUserId;
|
||||||
|
}).length;
|
||||||
|
this.userPageLoaded = Math.floor(count / AddonBlogProvider.ENTRIES_PER_PAGE);
|
||||||
|
this.filter['userid'] = this.currentUserId;
|
||||||
|
|
||||||
|
if (count == 0 && this.canLoadMoreUserEntries) {
|
||||||
|
// First time but no entry loaded. Try to load some.
|
||||||
|
this.loadMore();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
delete this.filter['userid'];
|
||||||
|
}
|
||||||
|
|
||||||
|
this.canLoadMore = enabled ? this.canLoadMoreUserEntries : this.canLoadMoreEntries;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to load more entries.
|
* Function to load more entries.
|
||||||
*
|
*
|
||||||
|
@ -178,7 +219,17 @@ export class AddonBlogEntriesComponent implements OnInit {
|
||||||
|
|
||||||
promises.push(this.blogProvider.invalidateEntries(this.filter));
|
promises.push(this.blogProvider.invalidateEntries(this.filter));
|
||||||
|
|
||||||
Promise.all(promises).finally(() => {
|
if (this.showMyEntriesToggle) {
|
||||||
|
this.filter['userid'] = this.currentUserId;
|
||||||
|
promises.push(this.blogProvider.invalidateEntries(this.filter));
|
||||||
|
|
||||||
|
if (!this.showMyEntriesToggle) {
|
||||||
|
delete this.filter['userid'];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
this.utils.allPromises(promises).finally(() => {
|
||||||
this.fetchEntries(true).finally(() => {
|
this.fetchEntries(true).finally(() => {
|
||||||
if (refresher) {
|
if (refresher) {
|
||||||
refresher.complete();
|
refresher.complete();
|
||||||
|
|
Loading…
Reference in New Issue