From 9baca2918b34bcf723f874c0044446ae2bf9ef50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 23 Jan 2019 11:56:35 +0100 Subject: [PATCH] MOBILE-2820 calendar: Manage reminder exact time --- src/addon/calendar/lang/en.json | 3 +- src/addon/calendar/pages/event/event.html | 36 +++++----- src/addon/calendar/pages/event/event.ts | 84 ++++++++++++++++------- src/addon/calendar/providers/calendar.ts | 12 +++- src/assets/lang/en.json | 3 +- 5 files changed, 92 insertions(+), 46 deletions(-) diff --git a/src/addon/calendar/lang/en.json b/src/addon/calendar/lang/en.json index 3342aaeeb..1fc6d2e34 100644 --- a/src/addon/calendar/lang/en.json +++ b/src/addon/calendar/lang/en.json @@ -8,7 +8,8 @@ "eventstarttime": "Start time", "gotoactivity": "Go to activity", "noevents": "There are no events", - "notifications": "Notifications", + "reminders": "Reminders", + "setnewreminder": "Set a new reminder", "typeclose": "Close event", "typecourse": "Course event", "typecategory": "Category event", diff --git a/src/addon/calendar/pages/event/event.html b/src/addon/calendar/pages/event/event.html index a9711f678..fa58fb49b 100644 --- a/src/addon/calendar/pages/event/event.html +++ b/src/addon/calendar/pages/event/event.html @@ -10,10 +10,10 @@ - + - - +

+

{{ 'addon.calendar.eventstarttime' | translate}}

{{ event.timestart * 1000 | coreFormatDate }}

@@ -52,20 +52,24 @@
- + - {{ 'addon.calendar.notifications' | translate }} - - {{ 'core.defaultvalue' | translate :{$a: defaultTimeReadable} }} - {{ 'core.settings.disabled' | translate }} - {{ 600 | coreDuration }} - {{ 1800 | coreDuration }} - {{ 3600 | coreDuration }} - {{ 7200 | coreDuration }} - {{ 21600 | coreDuration }} - {{ 43200 | coreDuration }} - {{ 86400 | coreDuration }} - +

{{ 'addon.calendar.reminders' | translate }}

+
+ +

{{ 'core.defaultvalue' | translate :{$a: ((event.timestart - defaultTime) * 1000) | coreFormatDate } }}

+

{{ notificationTime * 1000 | coreFormatDate }}

+ +
+ + + {{ 'addon.calendar.setnewreminder' | translate }} + + + +
diff --git a/src/addon/calendar/pages/event/event.ts b/src/addon/calendar/pages/event/event.ts index e20a61877..6b7b1ee55 100644 --- a/src/addon/calendar/pages/event/event.ts +++ b/src/addon/calendar/pages/event/event.ts @@ -39,8 +39,12 @@ export class AddonCalendarEventPage { protected eventId; protected siteHomeId: number; eventLoaded: boolean; + notificationFormat: string; + notificationMin: string; + notificationMax: string; notificationTime: number; - defaultTimeReadable: string; + notificationTimeText: string; + timeToLoad: number; event: any = {}; title: string; courseName: string; @@ -58,24 +62,21 @@ export class AddonCalendarEventPage { private textUtils: CoreTextUtilsProvider, private timeUtils: CoreTimeUtilsProvider) { this.eventId = navParams.get('id'); - this.notificationsEnabled = localNotificationsProvider.isAvailable(); + this.notificationsEnabled = localNotificationsProvider.isAvailable() || true; this.siteHomeId = sitesProvider.getCurrentSite().getSiteHomeId(); if (this.notificationsEnabled) { this.calendarProvider.getEventNotificationTimeOption(this.eventId).then((notificationTime) => { - this.notificationTime = notificationTime; - this.loadNotificationTime(); + this.setNotificationTime(notificationTime); }); this.calendarProvider.getDefaultNotificationTime().then((defaultTime) => { this.defaultTime = defaultTime * 60; - this.loadNotificationTime(); - if (defaultTime === 0) { - // Disabled by default. - this.defaultTimeReadable = this.translate.instant('core.settings.disabled'); - } else { - this.defaultTimeReadable = timeUtils.formatTime(defaultTime * 60); - } + this.setNotificationTime(); }); + + // Calculate format to use. ion-datetime doesn't support escaping characters ([]), so we remove them. + this.notificationFormat = this.timeUtils.convertPHPToMoment(this.translate.instant('core.strftimedatetimeshort')) + .replace(/[\[\]]/g, ''); } } @@ -88,12 +89,6 @@ export class AddonCalendarEventPage { }); } - updateNotificationTime(): void { - if (!isNaN(this.notificationTime) && this.event && this.event.id) { - this.calendarProvider.updateNotificationTime(this.event, this.notificationTime); - } - } - /** * Fetches the event and updates the view. * @@ -117,7 +112,11 @@ export class AddonCalendarEventPage { this.event = event; this.currentTime = this.timeUtils.timestamp(); - this.loadNotificationTime(); + this.notificationMin = this.timeUtils.userDate(this.currentTime * 1000, 'YYYY-MM-DDTHH:mm:ss', false); + this.notificationMax = this.timeUtils.userDate((event.timestart + event.timeduration) * 1000, + 'YYYY-MM-DDTHH:mm:ss', false); + + this.setNotificationTime(); // Reset some of the calculated data. this.categoryPath = ''; @@ -187,15 +186,50 @@ export class AddonCalendarEventPage { } /** - * Loads notification time by discarding options not in the list. + * Add a notification time for this event. + * + * @param {Event} e Click event. */ - loadNotificationTime(): void { - if (typeof this.notificationTime != 'undefined') { - if (this.notificationTime > 0 && this.event.timestart - this.notificationTime * 60 < this.currentTime) { - this.notificationTime = 0; - } else if (this.notificationTime < 0 && this.event.timestart - this.defaultTime < this.currentTime) { - this.notificationTime = 0; + addNotificationTime(e: Event): void { + e.preventDefault(); + e.stopPropagation(); + + if (this.notificationTimeText && this.event && this.event.id) { + this.setNotificationTime(new Date(this.notificationTimeText).getTime() / 1000); + this.calendarProvider.updateNotificationTime(this.event, this.notificationTime); + } + } + + /** + * Cancel the current notification. + * + * @param {Event} e Click event. + */ + cancelNotification(e: Event): void { + e.preventDefault(); + e.stopPropagation(); + + this.calendarProvider.updateNotificationTime(this.event, 0); + this.notificationTime = 0; + } + + /** + * Loads notification time. + * + * @param {number} [timeToLoad] Time to load. If not set, just recalculate. + */ + setNotificationTime(timeToLoad?: number): void { + this.timeToLoad = typeof timeToLoad == 'undefined' ? this.timeToLoad : timeToLoad; + + if (typeof this.timeToLoad != 'undefined') { + if (this.timeToLoad < 0) { + this.notificationTime = this.event.timestart - this.defaultTime * 60; + } else if (this.timeToLoad == 0 || this.timeToLoad > 1440) { + this.notificationTime = this.timeToLoad; + } else { + this.notificationTime = this.event.timestart - this.timeToLoad * 60; } + this.notificationTimeText = new Date(this.notificationTime * 1000).toString(); } } diff --git a/src/addon/calendar/providers/calendar.ts b/src/addon/calendar/providers/calendar.ts index ab5b3e301..9bfd2b4f0 100644 --- a/src/addon/calendar/providers/calendar.ts +++ b/src/addon/calendar/providers/calendar.ts @@ -305,11 +305,12 @@ export class AddonCalendarProvider { * * @param {number} id Event ID. * @param {string} [siteId] ID of the site the event belongs to. If not defined, use current site. - * @return {Promise} Promise with wvent notification time in minutes. 0 if disabled, -1 if default time. + * @return {Promise} Promise with event notification time in minutes. 0 if disabled, -1 if default time. */ getEventNotificationTimeOption(id: number, siteId?: string): Promise { return this.getEventFromLocalDb(id, siteId).then((e) => { - return e.notificationtime || -1; + console.error(e.notificationtime); + return e.notificationtime || 0; }).catch(() => { return -1; }); @@ -670,7 +671,12 @@ export class AddonCalendarProvider { return site.getDb().updateRecords(AddonCalendarProvider.EVENTS_TABLE, {notificationtime: time}, {id: event.id}) .then(() => { - return this.scheduleEventNotification(event, time); + if (time == 0) { + // No notification, cancel it. + return this.localNotificationsProvider.cancel(event.id, AddonCalendarProvider.COMPONENT, site.getId()); + } else { + return this.scheduleEventNotification(event, time); + } }); }); } diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index 048521fcd..f440f6e3a 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -65,7 +65,8 @@ "addon.calendar.eventstarttime": "Start time", "addon.calendar.gotoactivity": "Go to activity", "addon.calendar.noevents": "There are no events", - "addon.calendar.notifications": "Notifications", + "addon.calendar.reminders": "Reminders", + "addon.calendar.setnewreminder": "Set a new reminder", "addon.calendar.typecategory": "Category event", "addon.calendar.typeclose": "Close event", "addon.calendar.typecourse": "Course event",