MOBILE-3833 forum: Avoid assert declarations

main
Noel De Martin 2021-12-09 15:18:10 +01:00
parent 3cf8c1df74
commit 848b5e79b5
2 changed files with 39 additions and 23 deletions

View File

@ -69,8 +69,8 @@
</div> </div>
<ion-item *ngFor="let discussion of discussionsItems" class="addon-mod-forum-discussion" detail="true" <ion-item *ngFor="let discussion of discussionsItems" class="addon-mod-forum-discussion" detail="true"
[lines]="discussion.groupname && 'none'" [attr.aria-current]="discussions.getItemAriaCurrent(discussion)" [lines]="discussion.groupname && 'none'" [attr.aria-current]="discussions?.getItemAriaCurrent(discussion)"
(click)="discussions.select(discussion)" button> (click)="discussions?.select(discussion)" button>
<ion-label> <ion-label>
<div class="addon-mod-forum-discussion-title"> <div class="addon-mod-forum-discussion-title">
<p class="ion-text-wrap item-heading"> <p class="ion-text-wrap item-heading">

View File

@ -58,6 +58,7 @@ import { ContextLevel } from '@/core/constants';
import { AddonModForumDiscussionItem, AddonModForumDiscussionsSource } from '../../classes/forum-discussions-source'; import { AddonModForumDiscussionItem, AddonModForumDiscussionsSource } from '../../classes/forum-discussions-source';
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';
/** /**
* Component that displays a forum entry page. * Component that displays a forum entry page.
@ -74,7 +75,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
component = AddonModForumProvider.COMPONENT; component = AddonModForumProvider.COMPONENT;
moduleName = 'forum'; moduleName = 'forum';
descriptionNote?: string; descriptionNote?: string;
discussions!: AddonModForumDiscussionsManager; promisedDiscussions: CorePromisedValue<AddonModForumDiscussionsManager>;
discussionsItems: AddonModForumDiscussionItem[] = []; discussionsItems: AddonModForumDiscussionItem[] = [];
fetchFailed = false; fetchFailed = false;
canAddDiscussion = false; canAddDiscussion = false;
@ -104,6 +105,12 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
@Optional() courseContentsPage?: CoreCourseContentsPage, @Optional() courseContentsPage?: CoreCourseContentsPage,
) { ) {
super('AddonModForumIndexComponent', content, courseContentsPage); super('AddonModForumIndexComponent', content, courseContentsPage);
this.promisedDiscussions = new CorePromisedValue();
}
get discussions(): AddonModForumDiscussionsManager | null {
return this.promisedDiscussions.value;
} }
get forum(): AddonModForumData | undefined { get forum(): AddonModForumData | undefined {
@ -121,7 +128,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
* @return Whether the discussion is online. * @return Whether the discussion is online.
*/ */
isOnlineDiscussion(discussion: AddonModForumDiscussionItem): boolean { isOnlineDiscussion(discussion: AddonModForumDiscussionItem): boolean {
return this.discussions && this.discussions.getSource().isOnlineDiscussion(discussion); return !!this.discussions?.getSource().isOnlineDiscussion(discussion);
} }
/** /**
@ -131,7 +138,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
* @return Whether the discussion is offline. * @return Whether the discussion is offline.
*/ */
isOfflineDiscussion(discussion: AddonModForumDiscussionItem): boolean { isOfflineDiscussion(discussion: AddonModForumDiscussionItem): boolean {
return this.discussions && this.discussions.getSource().isOfflineDiscussion(discussion); return !!this.discussions?.getSource().isOfflineDiscussion(discussion);
} }
/** /**
@ -170,7 +177,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
if (hasOffline) { if (hasOffline) {
// Only update new fetched discussions. // Only update new fetched discussions.
const promises = discussions.map(async (discussion) => { const promises = discussions.map(async (discussion) => {
if (!this.discussions.getSource().isOnlineDiscussion(discussion)) { if (!this.discussions?.getSource().isOnlineDiscussion(discussion)) {
return; return;
} }
@ -188,7 +195,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
}, },
}); });
this.discussions = new AddonModForumDiscussionsManager(source, this); this.promisedDiscussions.resolve(new AddonModForumDiscussionsManager(source, this));
// Refresh data if this forum discussion is synchronized from discussions list. // Refresh data if this forum discussion is synchronized from discussions list.
this.syncManualObserver = CoreEvents.on(AddonModForumSyncProvider.MANUAL_SYNCED, (data) => { this.syncManualObserver = CoreEvents.on(AddonModForumSyncProvider.MANUAL_SYNCED, (data) => {
@ -213,8 +220,8 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
AddonModForum.invalidateDiscussionsList(this.forum.id).finally(() => { AddonModForum.invalidateDiscussionsList(this.forum.id).finally(() => {
if (data.discussionId) { if (data.discussionId) {
// Discussion changed, search it in the list of discussions. // Discussion changed, search it in the list of discussions.
const discussion = this.discussions.items.find( const discussion = this.discussions?.items.find(
(disc) => this.discussions.getSource().isOnlineDiscussion(disc) && data.discussionId == disc.discussion, disc => this.discussions?.getSource().isOnlineDiscussion(disc) && data.discussionId == disc.discussion,
) as AddonModForumDiscussion; ) as AddonModForumDiscussion;
if (discussion) { if (discussion) {
@ -233,7 +240,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
} }
if (data.deleted !== undefined && data.deleted) { if (data.deleted !== undefined && data.deleted) {
if (data.post?.parentid == 0 && CoreScreen.isTablet && !this.discussions.empty) { if (data.post?.parentid == 0 && CoreScreen.isTablet && this.discussions && !this.discussions.empty) {
// Discussion deleted, clear details page. // Discussion deleted, clear details page.
this.discussions.select(this.discussions[0]); this.discussions.select(this.discussions[0]);
} }
@ -264,7 +271,9 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
async ngAfterViewInit(): Promise<void> { async ngAfterViewInit(): Promise<void> {
await this.loadContent(false, true); await this.loadContent(false, true);
this.discussions.start(this.splitView); const discussions = await this.promisedDiscussions;
discussions.start(this.splitView);
} }
/** /**
@ -281,7 +290,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
this.ratingOfflineObserver && this.ratingOfflineObserver.off(); this.ratingOfflineObserver && this.ratingOfflineObserver.off();
this.ratingSyncObserver && this.ratingSyncObserver.off(); this.ratingSyncObserver && this.ratingSyncObserver.off();
this.sourceUnsubscribe && this.sourceUnsubscribe(); this.sourceUnsubscribe && this.sourceUnsubscribe();
this.discussions.destroy(); this.discussions?.destroy();
} }
/** /**
@ -304,8 +313,10 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
return; return;
} }
const discussions = await this.promisedDiscussions;
await Promise.all([ await Promise.all([
refresh ? this.discussions.reload() : this.discussions.load(), refresh ? discussions.reload() : discussions.load(),
CoreRatingOffline.hasRatings('mod_forum', 'post', ContextLevel.MODULE, this.forum.cmid).then((hasRatings) => { CoreRatingOffline.hasRatings('mod_forum', 'post', ContextLevel.MODULE, this.forum.cmid).then((hasRatings) => {
this.hasOfflineRatings = hasRatings; this.hasOfflineRatings = hasRatings;
@ -331,7 +342,9 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
return; return;
} }
await this.discussions.getSource().loadForum(); const discussions = await this.promisedDiscussions;
await discussions.getSource().loadForum();
if (!this.forum) { if (!this.forum) {
return; return;
@ -379,7 +392,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
CoreGroups.instance CoreGroups.instance
.getActivityGroupMode(forum.cmid) .getActivityGroupMode(forum.cmid)
.then(async mode => { .then(async mode => {
this.discussions.getSource().usesGroups = discussions.getSource().usesGroups =
mode === CoreGroupsProvider.SEPARATEGROUPS || mode === CoreGroupsProvider.VISIBLEGROUPS; mode === CoreGroupsProvider.SEPARATEGROUPS || mode === CoreGroupsProvider.VISIBLEGROUPS;
return; return;
@ -431,10 +444,12 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
async fetchMoreDiscussions(complete: () => void): Promise<void> { async fetchMoreDiscussions(complete: () => void): Promise<void> {
const discussions = await this.promisedDiscussions;
try { try {
this.fetchFailed = false; this.fetchFailed = false;
await this.discussions.load(); await discussions.load();
} catch (error) { } catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'addon.mod_forum.errorgetforum', true); CoreDomUtils.showErrorModalDefault(error, 'addon.mod_forum.errorgetforum', true);
@ -462,10 +477,11 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
return value ? parseInt(value, 10) : null; return value ? parseInt(value, 10) : null;
}; };
const discussions = await this.promisedDiscussions;
const value = await getSortOrder(); const value = await getSortOrder();
const selectedOrder = this.sortOrders.find(sortOrder => sortOrder.value === value) || this.sortOrders[0]; const selectedOrder = this.sortOrders.find(sortOrder => sortOrder.value === value) || this.sortOrders[0];
this.discussions.getSource().selectedSortOrder = selectedOrder; discussions.getSource().selectedSortOrder = selectedOrder;
if (this.sortOrderSelectorModalOptions.componentProps) { if (this.sortOrderSelectorModalOptions.componentProps) {
this.sortOrderSelectorModalOptions.componentProps.selected = selectedOrder.value; this.sortOrderSelectorModalOptions.componentProps.selected = selectedOrder.value;
@ -542,19 +558,19 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
// If it's a new discussion in tablet mode, try to open it. // If it's a new discussion in tablet mode, try to open it.
if (isNewDiscussion && CoreScreen.isTablet) { if (isNewDiscussion && CoreScreen.isTablet) {
const newDiscussionData = data as AddonModForumNewDiscussionData; const newDiscussionData = data as AddonModForumNewDiscussionData;
const discussion = this.discussions.items.find(disc => { const discussion = this.discussions?.items.find(disc => {
if (this.discussions.getSource().isOfflineDiscussion(disc)) { if (this.discussions?.getSource().isOfflineDiscussion(disc)) {
return disc.timecreated === newDiscussionData.discTimecreated; return disc.timecreated === newDiscussionData.discTimecreated;
} }
if (this.discussions.getSource().isOnlineDiscussion(disc)) { if (this.discussions?.getSource().isOnlineDiscussion(disc)) {
return CoreArray.contains(newDiscussionData.discussionIds ?? [], disc.discussion); return CoreArray.contains(newDiscussionData.discussionIds ?? [], disc.discussion);
} }
return false; return false;
}); });
if (discussion || !this.discussions.empty) { if (this.discussions && (discussion || !this.discussions.empty)) {
this.discussions.select(discussion ?? this.discussions.items[0]); this.discussions.select(discussion ?? this.discussions.items[0]);
} }
} }
@ -571,7 +587,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
* @param timeCreated Creation time of the offline discussion. * @param timeCreated Creation time of the offline discussion.
*/ */
openNewDiscussion(): void { openNewDiscussion(): void {
this.discussions.select(AddonModForumDiscussionsSource.NEW_DISCUSSION); this.discussions?.select(AddonModForumDiscussionsSource.NEW_DISCUSSION);
} }
/** /**
@ -580,7 +596,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
* @param sortOrder Sort order new data. * @param sortOrder Sort order new data.
*/ */
async setSortOrder(sortOrder: AddonModForumSortOrder): Promise<void> { async setSortOrder(sortOrder: AddonModForumSortOrder): Promise<void> {
if (sortOrder.value != this.discussions.getSource().selectedSortOrder?.value) { if (this.discussions && sortOrder.value != this.discussions.getSource().selectedSortOrder?.value) {
this.discussions.getSource().selectedSortOrder = sortOrder; this.discussions.getSource().selectedSortOrder = sortOrder;
this.discussions.getSource().setDirty(true); this.discussions.getSource().setDirty(true);