MOBILE-3784 calendar: Support opening any month with page params

main
Dani Palou 2022-11-15 16:07:57 +01:00
parent 1a1e03bdd4
commit ee4e8f6b05
3 changed files with 84 additions and 26 deletions

View File

@ -257,27 +257,9 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro
* Go to current month.
*/
async goToCurrentMonth(): Promise<void> {
const manager = this.manager;
const slides = this.slides;
if (!manager || !slides) {
return;
}
const currentMoment = moment();
const currentMonth = {
moment: moment(),
};
this.loaded = false;
try {
// Make sure the day is loaded.
await manager.getSource().loadItem(currentMonth);
slides.slideToItem(currentMonth);
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'addon.calendar.errorloadevents', true);
} finally {
this.loaded = true;
}
await this.viewMonth(currentMoment.month() + 1, currentMoment.year());
}
/**
@ -319,6 +301,39 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro
});
}
/**
* View a certain month and year.
*
* @param month Month.
* @param year Year.
*/
async viewMonth(month: number, year: number): Promise<void> {
const manager = this.manager;
const slides = this.slides;
if (!manager || !slides) {
return;
}
this.loaded = false;
const item = {
moment: moment({
year,
month: month - 1,
}),
};
try {
// Make sure the day is loaded.
await manager.getSource().loadItem(item);
slides.slideToItem(item);
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'addon.calendar.errorloadevents', true);
} finally {
this.loaded = true;
}
}
/**
* Component destroyed.
*/

View File

@ -173,6 +173,10 @@ export class AddonCalendarIndexPage implements OnInit, OnDestroy {
this.filter.filtered = !!this.filter.courseId;
this.fetchData(true, false);
if (this.year !== undefined && this.month !== undefined && this.calendarComponent) {
this.calendarComponent.viewMonth(this.month, this.year);
}
});
const deepLinkManager = new CoreMainMenuDeepLinkManager();

View File

@ -18,6 +18,7 @@ import {
import { CoreSwipeSlidesItemsManager } from '@classes/items-management/swipe-slides-items-manager';
import { IonContent, IonSlides } from '@ionic/angular';
import { CoreDomUtils, VerticalPoint } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils';
import { CoreDom } from '@singletons/dom';
import { CoreEventObserver } from '@singletons/events';
import { CoreMath } from '@singletons/math';
@ -43,6 +44,7 @@ export class CoreSwipeSlidesComponent<Item = unknown> implements OnChanges, OnDe
protected hostElement: HTMLElement;
protected unsubscribe?: () => void;
protected resizeListener: CoreEventObserver;
protected updateSlidesPromise?: Promise<void>;
constructor(
elementRef: ElementRef<HTMLElement>,
@ -51,7 +53,7 @@ export class CoreSwipeSlidesComponent<Item = unknown> implements OnChanges, OnDe
this.hostElement = elementRef.nativeElement;
this.resizeListener = CoreDom.onWindowResize(() => {
this.slides?.update();
this.updateSlidesComponent();
});
}
@ -118,7 +120,22 @@ export class CoreSwipeSlidesComponent<Item = unknown> implements OnChanges, OnDe
* @param speed Animation speed.
* @param runCallbacks Whether to run callbacks.
*/
slideToIndex(index: number, speed?: number, runCallbacks?: boolean): void {
async slideToIndex(index: number, speed?: number, runCallbacks?: boolean): Promise<void> {
// If slides are being updated, wait for the update to finish.
await this.updateSlidesPromise;
const slides = this.slides;
if (!slides) {
return;
}
// Verify that the number of slides matches the number of items.
const slidesLength = await slides.length();
if (slidesLength !== this.items.length) {
// Number doesn't match, do a new update to try to match them.
await this.updateSlidesComponent();
}
this.slides?.slideTo(index, speed, runCallbacks);
}
@ -132,7 +149,7 @@ export class CoreSwipeSlidesComponent<Item = unknown> implements OnChanges, OnDe
slideToItem(item: Item, speed?: number, runCallbacks?: boolean): void {
const index = this.manager?.getSource().getItemIndex(item) ?? -1;
if (index != -1) {
this.slides?.slideTo(index, speed, runCallbacks);
this.slideToIndex(index, speed, runCallbacks);
}
}
@ -158,10 +175,14 @@ export class CoreSwipeSlidesComponent<Item = unknown> implements OnChanges, OnDe
/**
* Called when items list has been updated.
*
* @param items New items.
*/
protected onItemsUpdated(): void {
protected async onItemsUpdated(): Promise<void> {
// Wait for slides to be added in DOM.
await CoreUtils.nextTick();
// Update the slides component so the slides list reflects the new items.
await this.updateSlidesComponent();
const currentItem = this.manager?.getSelectedItem();
if (!currentItem || !this.manager) {
@ -249,6 +270,24 @@ export class CoreSwipeSlidesComponent<Item = unknown> implements OnChanges, OnDe
};
}
/**
* Update slides component.
*/
protected async updateSlidesComponent(): Promise<void> {
if (!this.slides) {
return;
}
const promise = this.slides.update();
this.updateSlidesPromise = promise;
await promise;
if (this.updateSlidesPromise === promise) {
delete this.updateSlidesPromise;
}
}
/**
* @inheritdoc
*/