Merge pull request #3034 from NoelDeMartin/MOBILE-3833

MOBILE-3833: Fix navigation & prefetch handlers
main
Pau Ferrer Ocaña 2021-12-21 16:37:59 +01:00 committed by GitHub
commit 5af14b7f2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 72 deletions

View File

@ -24,29 +24,8 @@ export class AddonModForumDiscussionsSwipeManager
/** /**
* @inheritdoc * @inheritdoc
*/ */
async navigateToNextItem(): Promise<void> { protected skipItemInSwipe(item: AddonModForumDiscussionItem): boolean {
let delta = -1; return this.getSource().isNewDiscussionForm(item);
const item = await this.getItemBy(-1);
if (item && this.getSource().isNewDiscussionForm(item)) {
delta--;
}
await this.navigateToItemBy(delta, 'back');
}
/**
* @inheritdoc
*/
async navigateToPreviousItem(): Promise<void> {
let delta = 1;
const item = await this.getItemBy(1);
if (item && this.getSource().isNewDiscussionForm(item)) {
delta++;
}
await this.navigateToItemBy(delta, 'forward');
} }
} }

View File

@ -131,6 +131,7 @@ export class AddonModGlossaryEntriesSource extends CoreRoutedItemsManagerSource<
*/ */
startSearch(): void { startSearch(): void {
this.isSearch = true; this.isSearch = true;
this.setDirty(true);
} }
/** /**
@ -148,6 +149,7 @@ export class AddonModGlossaryEntriesSource extends CoreRoutedItemsManagerSource<
this.hasSearched = false; this.hasSearched = false;
this.onlineEntries = cachedOnlineEntries; this.onlineEntries = cachedOnlineEntries;
this.hasMoreItems = hasMoreOnlineEntries; this.hasMoreItems = hasMoreOnlineEntries;
this.setDirty(true);
} }
/** /**
@ -177,6 +179,7 @@ export class AddonModGlossaryEntriesSource extends CoreRoutedItemsManagerSource<
'ASC', 'ASC',
); );
this.hasSearched = true; this.hasSearched = true;
this.setDirty(true);
} }
/** /**
@ -209,6 +212,7 @@ export class AddonModGlossaryEntriesSource extends CoreRoutedItemsManagerSource<
this.fetchMode = mode; this.fetchMode = mode;
this.isSearch = false; this.isSearch = false;
this.setDirty(true);
switch (mode) { switch (mode) {
case 'author_all': case 'author_all':

View File

@ -24,29 +24,8 @@ export abstract class AddonModGlossaryEntriesSwipeManager
/** /**
* @inheritdoc * @inheritdoc
*/ */
async navigateToNextItem(): Promise<void> { protected skipItemInSwipe(item: AddonModGlossaryEntryItem): boolean {
let delta = -1; return this.getSource().isNewEntryForm(item);
const item = await this.getItemBy(-1);
if (item && this.getSource().isNewEntryForm(item)) {
delta--;
}
await this.navigateToItemBy(delta, 'back');
}
/**
* @inheritdoc
*/
async navigateToPreviousItem(): Promise<void> {
let delta = 1;
const item = await this.getItemBy(1);
if (item && this.getSource().isNewEntryForm(item)) {
delta++;
}
await this.navigateToItemBy(delta, 'forward');
} }
} }

View File

@ -82,7 +82,13 @@ export class CoreSwipeNavigationItemsManager<
* @param animationDirection Animation direction. * @param animationDirection Animation direction.
*/ */
protected async navigateToItemBy(delta: number, animationDirection: 'forward' | 'back'): Promise<void> { protected async navigateToItemBy(delta: number, animationDirection: 'forward' | 'back'): Promise<void> {
const item = await this.getItemBy(delta); let item: Item | null;
do {
item = await this.getItemBy(delta);
delta += delta > 0 ? 1 : -1;
} while (item && this.skipItemInSwipe(item));
if (!item) { if (!item) {
return; return;
@ -100,14 +106,15 @@ export class CoreSwipeNavigationItemsManager<
const items = this.getSource().getItems(); const items = this.getSource().getItems();
// Get selected item. // Get selected item.
const index = (this.selectedItem && items?.indexOf(this.selectedItem)) ?? -1; const selectedIndex = (this.selectedItem && items?.indexOf(this.selectedItem)) ?? -1;
const nextIndex = selectedIndex + delta;
if (index === -1) { if (selectedIndex === -1 || nextIndex < 0) {
return null; return null;
} }
// Get item by delta. // Get item by delta.
const item = items?.[index + delta] ?? null; const item = items?.[nextIndex] ?? null;
if (!item && !this.getSource().isCompleted()) { if (!item && !this.getSource().isCompleted()) {
await this.getSource().load(); await this.getSource().load();
@ -118,4 +125,15 @@ export class CoreSwipeNavigationItemsManager<
return item; return item;
} }
/**
* Check if an item should be skipped during swipe navigation.
*
* @param item Item.
* @returns Whether to skip this item during swipe navigation.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected skipItemInSwipe(item: Item): boolean {
return false;
}
} }

View File

@ -106,7 +106,7 @@ export class CoreCourseModuleComponent implements OnInit, OnDestroy {
if (this.module.handlerData.showDownloadButton) { if (this.module.handlerData.showDownloadButton) {
// Listen for changes on this module status, even if download isn't enabled. // Listen for changes on this module status, even if download isn't enabled.
this.prefetchHandler = CoreCourseModulePrefetchDelegate.getPrefetchHandlerFor(this.module.name); this.prefetchHandler = CoreCourseModulePrefetchDelegate.getPrefetchHandlerFor(this.module.modname);
this.statusObserver = CoreEvents.on(CoreEvents.PACKAGE_STATUS_CHANGED, (data) => { this.statusObserver = CoreEvents.on(CoreEvents.PACKAGE_STATUS_CHANGED, (data) => {
if (!this.module || data.componentId != this.module.id || !this.prefetchHandler || if (!this.module || data.componentId != this.module.id || !this.prefetchHandler ||

View File

@ -1013,7 +1013,7 @@ export class CoreCourseHelperProvider {
): Promise<void> { ): Promise<void> {
siteId = siteId || CoreSites.getCurrentSiteId(); siteId = siteId || CoreSites.getCurrentSiteId();
const prefetchHandler = CoreCourseModulePrefetchDelegate.getPrefetchHandlerFor(module.name); const prefetchHandler = CoreCourseModulePrefetchDelegate.getPrefetchHandlerFor(module.modname);
if (prefetchHandler) { if (prefetchHandler) {
// Use the prefetch handler to download the module. // Use the prefetch handler to download the module.
@ -2048,7 +2048,7 @@ export class CoreCourseHelperProvider {
promises.push(CoreCourseModulePrefetchDelegate.removeModuleFiles(module, courseId)); promises.push(CoreCourseModulePrefetchDelegate.removeModuleFiles(module, courseId));
const handler = CoreCourseModulePrefetchDelegate.getPrefetchHandlerFor(module.name); const handler = CoreCourseModulePrefetchDelegate.getPrefetchHandlerFor(module.modname);
const site = CoreSites.getCurrentSite(); const site = CoreSites.getCurrentSite();
if (handler && site) { if (handler && site) {
promises.push(site.deleteComponentFromCache(handler.component, module.id)); promises.push(site.deleteComponentFromCache(handler.component, module.id));

View File

@ -95,7 +95,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
* @return Promise resolved with boolean: whether the module can use check updates WS. * @return Promise resolved with boolean: whether the module can use check updates WS.
*/ */
async canModuleUseCheckUpdates(module: CoreCourseAnyModuleData, courseId: number): Promise<boolean> { async canModuleUseCheckUpdates(module: CoreCourseAnyModuleData, courseId: number): Promise<boolean> {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler) { if (!handler) {
// Module not supported, cannot use check updates. // Module not supported, cannot use check updates.
@ -171,7 +171,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
* @return Module status. * @return Module status.
*/ */
determineModuleStatus(module: CoreCourseAnyModuleData, status: string): string { determineModuleStatus(module: CoreCourseAnyModuleData, status: string): string {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
const siteId = CoreSites.getCurrentSiteId(); const siteId = CoreSites.getCurrentSiteId();
if (!handler) { if (!handler) {
@ -203,7 +203,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
*/ */
async downloadModule(module: CoreCourseAnyModuleData, courseId: number, dirPath?: string): Promise<void> { async downloadModule(module: CoreCourseAnyModuleData, courseId: number, dirPath?: string): Promise<void> {
// Check if the module has a prefetch handler. // Check if the module has a prefetch handler.
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler) { if (!handler) {
return; return;
@ -381,7 +381,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
* @return Promise resolved with the size. * @return Promise resolved with the size.
*/ */
async getModuleDownloadSize(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise<CoreFileSizeSum> { async getModuleDownloadSize(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise<CoreFileSizeSum> {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler) { if (!handler) {
return { size: 0, total: false }; return { size: 0, total: false };
@ -419,7 +419,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
* @return Promise resolved with the size. * @return Promise resolved with the size.
*/ */
async getModuleDownloadedSize(module: CoreCourseAnyModuleData, courseId: number): Promise<number> { async getModuleDownloadedSize(module: CoreCourseAnyModuleData, courseId: number): Promise<number> {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler) { if (!handler) {
return 0; return 0;
} }
@ -492,7 +492,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
} }
const site = CoreSites.getCurrentSite(); const site = CoreSites.getCurrentSite();
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler || !site) { if (!handler || !site) {
// If there is no handler then we can't find out the component name. // If there is no handler then we can't find out the component name.
// We can't work out the cached size, so just return downloaded size. // We can't work out the cached size, so just return downloaded size.
@ -515,7 +515,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
module: CoreCourseAnyModuleData, module: CoreCourseAnyModuleData,
courseId: number, courseId: number,
): Promise<(CoreWSFile | CoreCourseModuleContentFile)[]> { ): Promise<(CoreWSFile | CoreCourseModuleContentFile)[]> {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (handler?.getFiles) { if (handler?.getFiles) {
// The handler defines a function to get files, use it. // The handler defines a function to get files, use it.
@ -548,7 +548,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
refresh?: boolean, refresh?: boolean,
sectionId?: number, sectionId?: number,
): Promise<string> { ): Promise<string> {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler) { if (!handler) {
// No handler found, module not downloadable. // No handler found, module not downloadable.
@ -712,7 +712,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
} }
await Promise.all(modules.map(async (module) => { await Promise.all(modules.map(async (module) => {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler || (onlyToDisplay && handler.skipListStatus)) { if (!handler || (onlyToDisplay && handler.skipListStatus)) {
return; return;
} }
@ -758,7 +758,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
module: CoreCourseAnyModuleData, module: CoreCourseAnyModuleData,
courseId: number, courseId: number,
): Promise<{ status: string; downloadTime?: number }> { ): Promise<{ status: string; downloadTime?: number }> {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
const siteId = CoreSites.getCurrentSiteId(); const siteId = CoreSites.getCurrentSiteId();
if (!handler) { if (!handler) {
@ -895,7 +895,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
async invalidateModules(modules: CoreCourseModuleData[], courseId: number): Promise<void> { async invalidateModules(modules: CoreCourseModuleData[], courseId: number): Promise<void> {
const promises = modules.map(async (module) => { const promises = modules.map(async (module) => {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler) { if (!handler) {
return; return;
} }
@ -919,7 +919,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
* @param module Module to be invalidated. * @param module Module to be invalidated.
*/ */
invalidateModuleStatusCache(module: CoreCourseAnyModuleData): void { invalidateModuleStatusCache(module: CoreCourseAnyModuleData): void {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (handler) { if (handler) {
this.statusCache.invalidate(CoreFilepool.getPackageId(handler.component, module.id)); this.statusCache.invalidate(CoreFilepool.getPackageId(handler.component, module.id));
} }
@ -964,7 +964,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
return false; return false;
} }
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler) { if (!handler) {
return false; return false;
} }
@ -1000,7 +1000,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
* @return Promise resolved with boolean: whether the module has updates. * @return Promise resolved with boolean: whether the module has updates.
*/ */
async moduleHasUpdates(module: CoreCourseAnyModuleData, courseId: number, updates: CourseUpdates): Promise<boolean> { async moduleHasUpdates(module: CoreCourseAnyModuleData, courseId: number, updates: CourseUpdates): Promise<boolean> {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
const moduleUpdates = updates[module.id]; const moduleUpdates = updates[module.id];
if (handler?.hasUpdates) { if (handler?.hasUpdates) {
@ -1033,7 +1033,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
* @return Promise resolved when finished. * @return Promise resolved when finished.
*/ */
async prefetchModule(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise<void> { async prefetchModule(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise<void> {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler) { if (!handler) {
return; return;
} }
@ -1067,7 +1067,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
* @return Promise resolved when finished. * @return Promise resolved when finished.
*/ */
async syncModule<T = unknown>(module: CoreCourseAnyModuleData, courseId: number): Promise<T | undefined> { async syncModule<T = unknown>(module: CoreCourseAnyModuleData, courseId: number): Promise<T | undefined> {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler?.sync) { if (!handler?.sync) {
return; return;
} }
@ -1124,7 +1124,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
const promises = modules.map(async (module) => { const promises = modules.map(async (module) => {
// Check if the module has a prefetch handler. // Check if the module has a prefetch handler.
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler) { if (!handler) {
return; return;
} }
@ -1170,7 +1170,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
async removeModuleFiles(module: CoreCourseAnyModuleData, courseId: number): Promise<void> { async removeModuleFiles(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
const handler = this.getPrefetchHandlerFor(module.name); const handler = this.getPrefetchHandlerFor(module.modname);
const siteId = CoreSites.getCurrentSiteId(); const siteId = CoreSites.getCurrentSiteId();
if (handler?.removeFiles) { if (handler?.removeFiles) {

View File

@ -145,7 +145,7 @@ export class CoreSitePluginsModuleIndexComponent implements OnInit, OnDestroy, C
this.refreshIcon = CoreConstants.ICON_REFRESH; this.refreshIcon = CoreConstants.ICON_REFRESH;
// Check if there is a prefetch handler for this type of module. // Check if there is a prefetch handler for this type of module.
if (CoreCourseModulePrefetchDelegate.getPrefetchHandlerFor(this.module.name)) { if (CoreCourseModulePrefetchDelegate.getPrefetchHandlerFor(this.module.modname)) {
CoreCourseHelper.fillContextMenu(this, this.module, this.courseId, refresh, this.component); CoreCourseHelper.fillContextMenu(this, this.module, this.courseId, refresh, this.component);
} }
} }