diff --git a/src/addons/calendar/components/calendar/calendar.ts b/src/addons/calendar/components/calendar/calendar.ts index 0c8ef0178..81b65c10e 100644 --- a/src/addons/calendar/components/calendar/calendar.ts +++ b/src/addons/calendar/components/calendar/calendar.ts @@ -257,27 +257,9 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro * Go to current month. */ async goToCurrentMonth(): Promise { - 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 { + 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. */ diff --git a/src/addons/calendar/pages/index/index.page.ts b/src/addons/calendar/pages/index/index.page.ts index fa59a8f87..cba2727e8 100644 --- a/src/addons/calendar/pages/index/index.page.ts +++ b/src/addons/calendar/pages/index/index.page.ts @@ -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(); diff --git a/src/core/components/swipe-slides/swipe-slides.ts b/src/core/components/swipe-slides/swipe-slides.ts index 27e6587b3..bb25cfe22 100644 --- a/src/core/components/swipe-slides/swipe-slides.ts +++ b/src/core/components/swipe-slides/swipe-slides.ts @@ -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 implements OnChanges, OnDe protected hostElement: HTMLElement; protected unsubscribe?: () => void; protected resizeListener: CoreEventObserver; + protected updateSlidesPromise?: Promise; constructor( elementRef: ElementRef, @@ -51,7 +53,7 @@ export class CoreSwipeSlidesComponent implements OnChanges, OnDe this.hostElement = elementRef.nativeElement; this.resizeListener = CoreDom.onWindowResize(() => { - this.slides?.update(); + this.updateSlidesComponent(); }); } @@ -118,7 +120,22 @@ export class CoreSwipeSlidesComponent 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 { + // 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 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 implements OnChanges, OnDe /** * Called when items list has been updated. - * - * @param items New items. */ - protected onItemsUpdated(): void { + protected async onItemsUpdated(): Promise { + // 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 implements OnChanges, OnDe }; } + /** + * Update slides component. + */ + protected async updateSlidesComponent(): Promise { + if (!this.slides) { + return; + } + + const promise = this.slides.update(); + this.updateSlidesPromise = promise; + + await promise; + + if (this.updateSlidesPromise === promise) { + delete this.updateSlidesPromise; + } + } + /** * @inheritdoc */