MOBILE-3021 calendar: Schedule event notifications

main
Dani Palou 2019-07-18 17:22:51 +02:00
parent 6d94c961f1
commit 31c92398b3
3 changed files with 44 additions and 0 deletions

View File

@ -14,6 +14,7 @@
import { Component, OnDestroy, OnInit, Input, OnChanges, SimpleChange, Output, EventEmitter } from '@angular/core';
import { CoreEventsProvider } from '@providers/events';
import { CoreLocalNotificationsProvider } from '@providers/local-notifications';
import { CoreSitesProvider } from '@providers/sites';
import { CoreDomUtilsProvider } from '@providers/utils/dom';
import { CoreTimeUtilsProvider } from '@providers/utils/time';
@ -56,9 +57,11 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest
// Observers.
protected undeleteEventObserver: any;
protected obsDefaultTimeChange: any;
constructor(eventsProvider: CoreEventsProvider,
sitesProvider: CoreSitesProvider,
localNotificationsProvider: CoreLocalNotificationsProvider,
private calendarProvider: AddonCalendarProvider,
private calendarHelper: AddonCalendarHelperProvider,
private calendarOffline: AddonCalendarOfflineProvider,
@ -69,6 +72,17 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest
this.currentSiteId = sitesProvider.getCurrentSiteId();
if (localNotificationsProvider.isAvailable()) {
// Re-schedule events if default time changes.
this.obsDefaultTimeChange = eventsProvider.on(AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_CHANGED, () => {
this.weeks.forEach((week) => {
week.days.forEach((day) => {
calendarProvider.scheduleEventsNotifications(day.events);
});
});
}, this.currentSiteId);
}
// Listen for events "undeleted" (offline).
this.undeleteEventObserver = eventsProvider.on(AddonCalendarProvider.UNDELETED_EVENT_EVENT, (data) => {
if (data && data.eventId) {
@ -334,6 +348,9 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest
// Format online events.
day.events.forEach(this.calendarHelper.formatEventData.bind(this.calendarHelper));
// Schedule notifications for the events retrieved (only future events will be scheduled).
this.calendarProvider.scheduleEventsNotifications(day.events);
if (monthOfflineEvents || this.deletedEvents.length) {
// There is offline data, merge it.
@ -403,5 +420,6 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest
*/
ngOnDestroy(): void {
this.undeleteEventObserver && this.undeleteEventObserver.off();
this.obsDefaultTimeChange && this.obsDefaultTimeChange.off();
}
}

View File

@ -14,6 +14,7 @@
import { Component, OnDestroy, OnInit, Input, OnChanges, SimpleChange, Output, EventEmitter } from '@angular/core';
import { CoreEventsProvider } from '@providers/events';
import { CoreLocalNotificationsProvider } from '@providers/local-notifications';
import { CoreSitesProvider } from '@providers/sites';
import { CoreDomUtilsProvider } from '@providers/utils/dom';
import { AddonCalendarProvider } from '../../providers/calendar';
@ -51,9 +52,11 @@ export class AddonCalendarUpcomingEventsComponent implements OnInit, OnChanges,
// Observers.
protected undeleteEventObserver: any;
protected obsDefaultTimeChange: any;
constructor(eventsProvider: CoreEventsProvider,
sitesProvider: CoreSitesProvider,
localNotificationsProvider: CoreLocalNotificationsProvider,
private calendarProvider: AddonCalendarProvider,
private calendarHelper: AddonCalendarHelperProvider,
private calendarOffline: AddonCalendarOfflineProvider,
@ -62,6 +65,13 @@ export class AddonCalendarUpcomingEventsComponent implements OnInit, OnChanges,
this.currentSiteId = sitesProvider.getCurrentSiteId();
if (localNotificationsProvider.isAvailable()) {
// Re-schedule events if default time changes.
this.obsDefaultTimeChange = eventsProvider.on(AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_CHANGED, () => {
calendarProvider.scheduleEventsNotifications(this.onlineEvents);
}, this.currentSiteId);
}
// Listen for events "undeleted" (offline).
this.undeleteEventObserver = eventsProvider.on(AddonCalendarProvider.UNDELETED_EVENT_EVENT, (data) => {
if (data && data.eventId) {
@ -152,6 +162,9 @@ export class AddonCalendarUpcomingEventsComponent implements OnInit, OnChanges,
this.onlineEvents.forEach(this.calendarHelper.formatEventData.bind(this.calendarHelper));
// Schedule notifications for the events retrieved.
this.calendarProvider.scheduleEventsNotifications(this.onlineEvents);
// Merge the online events with offline data.
this.events = this.mergeEvents();
@ -319,5 +332,6 @@ export class AddonCalendarUpcomingEventsComponent implements OnInit, OnChanges,
*/
ngOnDestroy(): void {
this.undeleteEventObserver && this.undeleteEventObserver.off();
this.obsDefaultTimeChange && this.obsDefaultTimeChange.off();
}
}

View File

@ -61,6 +61,7 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy {
protected syncObserver: any;
protected manualSyncObserver: any;
protected onlineObserver: any;
protected obsDefaultTimeChange: any;
periodName: string;
filteredEvents = [];
@ -98,6 +99,13 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy {
this.courseId = navParams.get('courseId');
this.currentSiteId = sitesProvider.getCurrentSiteId();
if (localNotificationsProvider.isAvailable()) {
// Re-schedule events if default time changes.
this.obsDefaultTimeChange = eventsProvider.on(AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_CHANGED, () => {
calendarProvider.scheduleEventsNotifications(this.onlineEvents);
}, this.currentSiteId);
}
// Listen for events added. When an event is added, reload the data.
this.newEventObserver = eventsProvider.on(AddonCalendarProvider.NEW_EVENT_EVENT, (data) => {
if (data && data.event) {
@ -282,6 +290,9 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy {
this.onlineEvents = result.events;
this.onlineEvents.forEach(this.calendarHelper.formatEventData.bind(this.calendarHelper));
// Schedule notifications for the events retrieved (only future events will be scheduled).
this.calendarProvider.scheduleEventsNotifications(this.onlineEvents);
// Merge the online events with offline data.
this.events = this.mergeEvents();
@ -609,5 +620,6 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy {
this.syncObserver && this.syncObserver.off();
this.manualSyncObserver && this.manualSyncObserver.off();
this.onlineObserver && this.onlineObserver.unsubscribe();
this.obsDefaultTimeChange && this.obsDefaultTimeChange.off();
}
}