MOBILE-3833 glossary: Fix index page

main
Noel De Martin 2021-12-09 14:25:36 +01:00
parent e628e039fc
commit 3cf8c1df74
1 changed files with 34 additions and 22 deletions

View File

@ -17,6 +17,7 @@ import { AfterViewInit, Component, OnDestroy, OnInit, Optional, ViewChild } from
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { CoreListItemsManager } from '@classes/items-management/list-items-manager'; import { CoreListItemsManager } from '@classes/items-management/list-items-manager';
import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker'; import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker';
import { CorePromisedValue } from '@classes/promised-value';
import { CoreSplitViewComponent } from '@components/split-view/split-view'; import { CoreSplitViewComponent } from '@components/split-view/split-view';
import { CoreCourseModuleMainActivityComponent } from '@features/course/classes/main-activity-component'; import { CoreCourseModuleMainActivityComponent } from '@features/course/classes/main-activity-component';
import { CoreCourseContentsPage } from '@features/course/pages/contents/contents'; import { CoreCourseContentsPage } from '@features/course/pages/contents/contents';
@ -70,7 +71,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
canAdd = false; canAdd = false;
loadMoreError = false; loadMoreError = false;
loadingMessage: string; loadingMessage: string;
entries!: AddonModGlossaryEntriesManager; promisedEntries: CorePromisedValue<AddonModGlossaryEntriesManager>;
hasOfflineRatings = false; hasOfflineRatings = false;
protected syncEventName = AddonModGlossarySyncProvider.AUTO_SYNCED; protected syncEventName = AddonModGlossarySyncProvider.AUTO_SYNCED;
@ -92,18 +93,23 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
super('AddonModGlossaryIndexComponent', content, courseContentsPage); super('AddonModGlossaryIndexComponent', content, courseContentsPage);
this.loadingMessage = Translate.instant('core.loading'); this.loadingMessage = Translate.instant('core.loading');
this.promisedEntries = new CorePromisedValue();
}
get entries(): AddonModGlossaryEntriesManager | null {
return this.promisedEntries.value;
} }
get glossary(): AddonModGlossaryGlossary | undefined { get glossary(): AddonModGlossaryGlossary | undefined {
return this.entries.getSource().glossary; return this.entries?.getSource().glossary;
} }
get isSearch(): boolean { get isSearch(): boolean {
return this.entries.getSource().isSearch; return this.entries?.getSource().isSearch ?? false;
} }
get hasSearched(): boolean { get hasSearched(): boolean {
return this.entries.getSource().hasSearched; return this.entries?.getSource().hasSearched ?? false;
} }
/** /**
@ -118,10 +124,10 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
[this.courseId, this.module.id, this.courseContentsPage ? `${AddonModGlossaryModuleHandlerService.PAGE_NAME}/` : ''], [this.courseId, this.module.id, this.courseContentsPage ? `${AddonModGlossaryModuleHandlerService.PAGE_NAME}/` : ''],
); );
this.entries = new AddonModGlossaryEntriesManager( this.promisedEntries.resolve(new AddonModGlossaryEntriesManager(
source, source,
this.route.component, this.route.component,
); ));
this.sourceUnsubscribe = source.addListener({ this.sourceUnsubscribe = source.addListener({
onItemsUpdated: items => this.hasOffline = !!items.find(item => source.isOfflineEntry(item)), onItemsUpdated: items => this.hasOffline = !!items.find(item => source.isOfflineEntry(item)),
@ -156,13 +162,10 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
* @inheritdoc * @inheritdoc
*/ */
async ngAfterViewInit(): Promise<void> { async ngAfterViewInit(): Promise<void> {
const entries = await this.promisedEntries;
await this.loadContent(false, true); await this.loadContent(false, true);
await entries.start(this.splitView);
if (!this.glossary) {
return;
}
await this.entries.start(this.splitView);
try { try {
CoreCourse.checkModuleCompletion(this.courseId, this.module.completiondata); CoreCourse.checkModuleCompletion(this.courseId, this.module.completiondata);
@ -175,8 +178,10 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
* @inheritdoc * @inheritdoc
*/ */
protected async fetchContent(refresh: boolean = false, sync: boolean = false, showErrors: boolean = false): Promise<void> { protected async fetchContent(refresh: boolean = false, sync: boolean = false, showErrors: boolean = false): Promise<void> {
const entries = await this.promisedEntries;
try { try {
await this.entries.getSource().loadGlossary(); await entries.getSource().loadGlossary();
if (!this.glossary) { if (!this.glossary) {
return; return;
@ -187,7 +192,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
this.dataRetrieved.emit(this.glossary); this.dataRetrieved.emit(this.glossary);
if (!this.entries.getSource().fetchMode) { if (!entries.getSource().fetchMode) {
this.switchMode('letter_all'); this.switchMode('letter_all');
} }
@ -198,7 +203,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
const [hasOfflineRatings] = await Promise.all([ const [hasOfflineRatings] = await Promise.all([
CoreRatingOffline.hasRatings('mod_glossary', 'entry', ContextLevel.MODULE, this.glossary.coursemodule), CoreRatingOffline.hasRatings('mod_glossary', 'entry', ContextLevel.MODULE, this.glossary.coursemodule),
refresh ? this.entries.reload() : this.entries.load(), refresh ? entries.reload() : entries.load(),
]); ]);
this.hasOfflineRatings = hasOfflineRatings; this.hasOfflineRatings = hasOfflineRatings;
@ -211,7 +216,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
* @inheritdoc * @inheritdoc
*/ */
protected async invalidateContent(): Promise<void> { protected async invalidateContent(): Promise<void> {
await this.entries.getSource().invalidateCache(); await this.entries?.getSource().invalidateCache();
} }
/** /**
@ -250,7 +255,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
* @param mode New mode. * @param mode New mode.
*/ */
protected switchMode(mode: AddonModGlossaryFetchMode): void { protected switchMode(mode: AddonModGlossaryFetchMode): void {
this.entries.getSource().switchMode(mode); this.entries?.getSource().switchMode(mode);
switch (mode) { switch (mode) {
case 'author_all': case 'author_all':
@ -304,10 +309,12 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
async loadMoreEntries(infiniteComplete?: () => void): Promise<void> { async loadMoreEntries(infiniteComplete?: () => void): Promise<void> {
const entries = await this.promisedEntries;
try { try {
this.loadMoreError = false; this.loadMoreError = false;
await this.entries.load(); await entries.load();
} catch (error) { } catch (error) {
this.loadMoreError = true; this.loadMoreError = true;
CoreDomUtils.showErrorModalDefault(error, 'addon.mod_glossary.errorloadingentries', true); CoreDomUtils.showErrorModalDefault(error, 'addon.mod_glossary.errorloadingentries', true);
@ -326,7 +333,8 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
return; return;
} }
const previousMode = this.entries.getSource().fetchMode; const entries = await this.promisedEntries;
const previousMode = entries.getSource().fetchMode;
const newMode = await CoreDomUtils.openPopover<AddonModGlossaryFetchMode>({ const newMode = await CoreDomUtils.openPopover<AddonModGlossaryFetchMode>({
component: AddonModGlossaryModePickerPopoverComponent, component: AddonModGlossaryModePickerPopoverComponent,
componentProps: { componentProps: {
@ -357,6 +365,10 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
* Toggles between search and fetch mode. * Toggles between search and fetch mode.
*/ */
toggleSearch(): void { toggleSearch(): void {
if (!this.entries) {
return;
}
if (this.isSearch) { if (this.isSearch) {
const fetchMode = this.entries.getSource().fetchMode; const fetchMode = this.entries.getSource().fetchMode;
@ -393,7 +405,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
* Opens new entry editor. * Opens new entry editor.
*/ */
openNewEntry(): void { openNewEntry(): void {
this.entries.select(AddonModGlossaryEntriesSource.NEW_ENTRY); this.entries?.select(AddonModGlossaryEntriesSource.NEW_ENTRY);
} }
/** /**
@ -405,7 +417,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
this.loadingMessage = Translate.instant('core.searching'); this.loadingMessage = Translate.instant('core.searching');
this.loaded = false; this.loaded = false;
this.entries.getSource().search(query); this.entries?.getSource().search(query);
this.loadContent(); this.loadContent();
} }
@ -419,7 +431,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
this.ratingOfflineObserver?.off(); this.ratingOfflineObserver?.off();
this.ratingSyncObserver?.off(); this.ratingSyncObserver?.off();
this.sourceUnsubscribe?.call(null); this.sourceUnsubscribe?.call(null);
this.entries.destroy(); this.entries?.destroy();
} }
} }