MOBILE-3656 wiki: Add go to wiki first page feature

main
Dani Palou 2021-04-07 10:12:27 +02:00
parent a881722a50
commit 2f0eb0088b
5 changed files with 88 additions and 37 deletions

View File

@ -101,6 +101,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
protected ignoreManualSyncEvent = false; // Whether manual sync event should be ignored. protected ignoreManualSyncEvent = false; // Whether manual sync event should be ignored.
protected currentUserId?: number; // Current user ID. protected currentUserId?: number; // Current user ID.
protected hasEdited = false; // Whether the user has opened the edit page. protected hasEdited = false; // Whether the user has opened the edit page.
protected currentPath!: string;
constructor( constructor(
protected content?: IonContent, protected content?: IonContent,
@ -120,6 +121,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
this.currentUserId = CoreSites.getCurrentSiteUserId(); this.currentUserId = CoreSites.getCurrentSiteUserId();
this.isMainPage = !this.pageId && !this.pageTitle; this.isMainPage = !this.pageId && !this.pageTitle;
this.currentPage = this.pageId; this.currentPage = this.pageId;
this.currentPath = CoreNavigator.getCurrentPath();
this.listenEvents(); this.listenEvents();
try { try {
@ -212,6 +214,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
this.wiki = await AddonModWiki.getWiki(this.courseId, this.module.id); this.wiki = await AddonModWiki.getWiki(this.courseId, this.module.id);
this.dataRetrieved.emit(this.wiki); this.dataRetrieved.emit(this.wiki);
AddonModWiki.wikiPageOpened(this.wiki.id, this.currentPath);
if (sync) { if (sync) {
// Try to synchronize the wiki. // Try to synchronize the wiki.
@ -421,40 +424,16 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
} }
/** /**
* Get the wiki home view. If cannot determine or it's current view, return undefined. * Get path to the wiki home view. If cannot determine or it's current view, return undefined.
* *
* @return The view controller of the home view * @return The path of the home view
*/ */
protected getWikiHomeView(): unknown { protected getWikiHomeView(): string | undefined {
if (!this.wiki?.id) { if (!this.wiki) {
return; return;
} }
// @todo return AddonModWiki.getFirstWikiPageOpened(this.wiki.id, this.currentPath);
// const views = this.navCtrl.getViews();
// // Go back in history until we find a page that doesn't belong to current wiki.
// for (let i = views.length - 2; i >= 0; i--) {
// const view = views[i];
// if (view.component.name != 'AddonModWikiIndexPage') {
// if (i == views.length - 2) {
// // Next view is current view, return undefined.
// return;
// }
// // This view is no longer from wiki, return the next view.
// return views[i + 1];
// }
// // Check that the view belongs to the same wiki as current view.
// const wikiId = view.data.wikiId ? view.data.wikiId : view.data.module.instance;
// if (!wikiId || wikiId != this.wiki.id) {
// // Wiki has changed, return the next view.
// return views[i + 1];
// }
// }
} }
/** /**
@ -591,7 +570,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
componentProps: { componentProps: {
pages: this.subwikiPages, pages: this.subwikiPages,
selected: this.currentPageObj && 'id' in this.currentPageObj && this.currentPageObj.id, selected: this.currentPageObj && 'id' in this.currentPageObj && this.currentPageObj.id,
// homeView: @todo this.getWikiHomeView(), homeView: this.getWikiHomeView(),
moduleId: this.module.id, moduleId: this.module.id,
courseId: this.courseId, courseId: this.courseId,
}, },
@ -609,7 +588,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
if (result.data) { if (result.data) {
if (result.data.type == 'home') { if (result.data.type == 'home') {
// Go back to the initial page of the wiki. // Go back to the initial page of the wiki.
// @todo this.navCtrl.popTo(page.goto); CoreNavigator.navigateToSitePath(result.data.goto);
} else { } else {
this.goToPage(result.data.goto); this.goToPage(result.data.goto);
} }
@ -840,6 +819,9 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
this.manualSyncObserver?.off(); this.manualSyncObserver?.off();
this.newPageObserver?.off(); this.newPageObserver?.off();
if (this.wiki) {
AddonModWiki.wikiPageClosed(this.wiki.id, this.currentPath);
}
} }
/** /**

View File

@ -12,7 +12,7 @@
<nav> <nav>
<ion-list> <ion-list>
<!-- Go to "home". --> <!-- Go to "home". -->
<ion-item class="ion-text-wrap" *ngIf="homeView" (click)="goToWikiHome()"> <ion-item class="ion-text-wrap" *ngIf="homeView" (click)="goToWikiHome()" tappable>
<ion-icon name="fas-home" slot="start"></ion-icon> <ion-icon name="fas-home" slot="start"></ion-icon>
<ion-label>{{ 'addon.mod_wiki.gowikihome' | translate }}</ion-label> <ion-label>{{ 'addon.mod_wiki.gowikihome' | translate }}</ion-label>
</ion-item> </ion-item>
@ -21,7 +21,7 @@
<ion-label>{{ letter.label }}</ion-label> <ion-label>{{ letter.label }}</ion-label>
</ion-item-divider> </ion-item-divider>
<ion-item class="ion-text-wrap" *ngFor="let page of letter.pages" (click)="goToPage(page)" <ion-item class="ion-text-wrap" *ngFor="let page of letter.pages" (click)="goToPage(page)"
[class.core-selected-item]="selected == page.id"> [class.core-selected-item]="selected == page.id" tappable>
<ion-icon name="fas-home" slot="start" *ngIf="page.firstpage"></ion-icon> <ion-icon name="fas-home" slot="start" *ngIf="page.firstpage"></ion-icon>
<ion-label> <ion-label>
<core-format-text [text]="page.title" contextLevel="module" [contextInstanceId]="moduleId" <core-format-text [text]="page.title" contextLevel="module" [contextInstanceId]="moduleId"

View File

@ -30,7 +30,7 @@ export class AddonModWikiMapModalComponent implements OnInit {
@Input() selected?: number; @Input() selected?: number;
@Input() moduleId?: number; @Input() moduleId?: number;
@Input() courseId?: number; @Input() courseId?: number;
@Input() homeView: unknown; // @todo @Input() homeView?: string;
map: AddonModWikiPagesMapLetter[] = []; // Map of pages, categorized by letter. map: AddonModWikiPagesMapLetter[] = []; // Map of pages, categorized by letter.
@ -54,7 +54,7 @@ export class AddonModWikiMapModalComponent implements OnInit {
* Go back to the initial page of the wiki. * Go back to the initial page of the wiki.
*/ */
goToWikiHome(): void { goToWikiHome(): void {
// @todo ModalController.dismiss({ type: 'home', goto: this.homeView }); ModalController.dismiss({ type: 'home', goto: this.homeView });
} }
/** /**

View File

@ -19,6 +19,7 @@ import { CoreCourseCommonModWSOptions } from '@features/course/services/course';
import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreCourseLogHelper } from '@features/course/services/log-helper';
import { CoreTagItem } from '@features/tag/services/tag'; import { CoreTagItem } from '@features/tag/services/tag';
import { CoreApp } from '@services/app'; import { CoreApp } from '@services/app';
import { CoreNavigator } from '@services/navigator';
import { CoreSites, CoreSitesCommonWSOptions, CoreSitesReadingStrategy } from '@services/sites'; import { CoreSites, CoreSitesCommonWSOptions, CoreSitesReadingStrategy } from '@services/sites';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
import { CoreWSExternalFile, CoreWSExternalWarning } from '@services/ws'; import { CoreWSExternalFile, CoreWSExternalWarning } from '@services/ws';
@ -41,6 +42,7 @@ export class AddonModWikiProvider {
static readonly RENEW_LOCK_TIME = 30000; // Milliseconds. static readonly RENEW_LOCK_TIME = 30000; // Milliseconds.
protected subwikiListsCache: {[wikiId: number]: AddonModWikiSubwikiListData} = {}; protected subwikiListsCache: {[wikiId: number]: AddonModWikiSubwikiListData} = {};
protected wikiFirstViewedPage: Record<string, Record<number, string>> = {};
constructor() { constructor() {
// Clear subwiki lists cache on logout. // Clear subwiki lists cache on logout.
@ -87,6 +89,23 @@ export class AddonModWikiProvider {
return response.pageid; return response.pageid;
} }
/**
* Get the first page opened for a wiki in the app if it isn't the current one.
*
* @param wikiId Wiki ID.
* @param path Path.
*/
getFirstWikiPageOpened(wikiId: number, path: string): string | undefined {
const tab = CoreNavigator.getMainMenuTabFromPath(path);
if (!tab) {
return;
}
if (this.wikiFirstViewedPage[tab] && this.wikiFirstViewedPage[tab][wikiId] !== path) {
return this.wikiFirstViewedPage[tab][wikiId];
}
}
/** /**
* Get a wiki page contents. * Get a wiki page contents.
* *
@ -806,6 +825,47 @@ export class AddonModWikiProvider {
} }
} }
/**
* If this page is the first opened page for a wiki, remove the stored path so it's no longer the first viewed page.
*
* @param wikiId Wiki ID.
* @param path Path.
*/
wikiPageClosed(wikiId: number, path: string): void {
const tab = CoreNavigator.getMainMenuTabFromPath(path);
if (!tab) {
return;
}
this.wikiFirstViewedPage[tab] = this.wikiFirstViewedPage[tab] || {};
if (this.wikiFirstViewedPage[tab][wikiId] === path) {
delete this.wikiFirstViewedPage[tab][wikiId];
}
}
/**
* If this page is the first opened page for a wiki, save its path so we can go back to it.
*
* @param wikiId Wiki ID.
* @param path Path.
*/
wikiPageOpened(wikiId: number, path: string): void {
const tab = CoreNavigator.getMainMenuTabFromPath(path);
if (!tab) {
return;
}
this.wikiFirstViewedPage[tab] = this.wikiFirstViewedPage[tab] || {};
if (this.wikiFirstViewedPage[tab][wikiId]) {
// There's already an opened page for this wiki.
return;
}
this.wikiFirstViewedPage[tab][wikiId] = path;
}
} }
export const AddonModWiki = makeSingleton(AddonModWikiProvider); export const AddonModWiki = makeSingleton(AddonModWikiProvider);

View File

@ -96,8 +96,17 @@ export class CoreNavigatorService {
* @return Current main menu tab or null if the current route is not using the main menu. * @return Current main menu tab or null if the current route is not using the main menu.
*/ */
getCurrentMainMenuTab(): string | null { getCurrentMainMenuTab(): string | null {
const currentPath = this.getCurrentPath(); return this.getMainMenuTabFromPath(this.getCurrentPath());
const matches = /^\/main\/([^/]+).*$/.exec(currentPath); }
/**
* Get main menu tab from a path.
*
* @param path The path to check.
* @return Path's main menu tab or null if the path is not using the main menu.
*/
getMainMenuTabFromPath(path: string): string | null {
const matches = /^\/main\/([^/]+).*$/.exec(path);
return matches?.[1] ?? null; return matches?.[1] ?? null;
} }