From 69d2530bb820eafff10d39e178b514504f01c178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Tue, 8 Nov 2022 17:33:50 +0100 Subject: [PATCH] MOBILE-3936 reminders: Avoid creating reminders in the past --- .../pages/edit-event/edit-event.page.ts | 7 ++++- src/addons/calendar/pages/event/event.page.ts | 4 ++- .../components/set-button/set-button.html | 8 +++--- .../components/set-button/set-button.ts | 16 +++++++++--- .../set-reminder-custom.html | 13 +++++----- .../set-reminder-menu/set-reminder-menu.html | 24 ++++++++++------- .../set-reminder-menu/set-reminder-menu.ts | 26 +++++++++++++++++++ 7 files changed, 74 insertions(+), 24 deletions(-) diff --git a/src/addons/calendar/pages/edit-event/edit-event.page.ts b/src/addons/calendar/pages/edit-event/edit-event.page.ts index ce5c0994c..79190818a 100644 --- a/src/addons/calendar/pages/edit-event/edit-event.page.ts +++ b/src/addons/calendar/pages/edit-event/edit-event.page.ts @@ -43,7 +43,6 @@ import { CoreError } from '@classes/errors/error'; import { CoreNavigator } from '@services/navigator'; import { CanLeave } from '@guards/can-leave'; import { CoreForms } from '@singletons/form'; -import { CoreLocalNotifications } from '@services/local-notifications'; import { CoreReminders, CoreRemindersService, CoreRemindersUnits } from '@features/reminders/services/reminders'; import { CoreRemindersSetReminderMenuComponent } from '@features/reminders/components/set-reminder-menu/set-reminder-menu'; @@ -671,8 +670,14 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy, CanLeave { * Add a reminder. */ async addReminder(): Promise { + const formData = this.form.value; + const eventTime = CoreTimeUtils.convertToTimestamp(formData.timestart, true); + const reminderTime = await CoreDomUtils.openPopover<{timeBefore: number}>({ component: CoreRemindersSetReminderMenuComponent, + componentProps: { + eventTime, + }, // TODO: Add event to open the popover in place. }); diff --git a/src/addons/calendar/pages/event/event.page.ts b/src/addons/calendar/pages/event/event.page.ts index 3d96ce88d..907114a1e 100644 --- a/src/addons/calendar/pages/event/event.page.ts +++ b/src/addons/calendar/pages/event/event.page.ts @@ -28,7 +28,6 @@ import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreDomUtils, ToastDuration } from '@services/utils/dom'; import { CoreTextUtils } from '@services/utils/text'; import { CoreSites } from '@services/sites'; -import { CoreLocalNotifications } from '@services/local-notifications'; import { CoreCourse } from '@features/course/services/course'; import { CoreTimeUtils } from '@services/utils/time'; import { CoreGroups } from '@services/groups'; @@ -389,6 +388,9 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy { const reminderTime = await CoreDomUtils.openPopover<{timeBefore: number}>({ component: CoreRemindersSetReminderMenuComponent, + componentProps: { + eventTime: this.event.timestart, + }, // TODO: Add event to open the popover in place. }); diff --git a/src/core/features/reminders/components/set-button/set-button.html b/src/core/features/reminders/components/set-button/set-button.html index fcc693d20..97b9f1b9f 100644 --- a/src/core/features/reminders/components/set-button/set-button.html +++ b/src/core/features/reminders/components/set-button/set-button.html @@ -1,4 +1,6 @@ - - - + + + diff --git a/src/core/features/reminders/components/set-button/set-button.ts b/src/core/features/reminders/components/set-button/set-button.ts index d9fa8f439..28477c9ca 100644 --- a/src/core/features/reminders/components/set-button/set-button.ts +++ b/src/core/features/reminders/components/set-button/set-button.ts @@ -13,7 +13,7 @@ // limitations under the License. import { CoreReminderData, CoreReminders, CoreRemindersService } from '@features/reminders/services/reminders'; -import { Component, Input } from '@angular/core'; +import { Component, Input, OnInit } from '@angular/core'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreRemindersSetReminderMenuComponent } from '../set-reminder-menu/set-reminder-menu'; @@ -24,7 +24,7 @@ import { CoreRemindersSetReminderMenuComponent } from '../set-reminder-menu/set- selector: 'core-reminders-set-button', templateUrl: 'set-button.html', }) -export class CoreRemindersSetButtonComponent { +export class CoreRemindersSetButtonComponent implements OnInit { @Input() component?: string; @Input() instanceId?: number; @@ -35,6 +35,15 @@ export class CoreRemindersSetButtonComponent { @Input() title = ''; @Input() url = ''; + labelClean = ''; + + /** + * @inheritdoc + */ + ngOnInit(): void { + this.labelClean = this.label.replace(':', ''); + } + /** * Set reminder. * @@ -60,6 +69,7 @@ export class CoreRemindersSetButtonComponent { component: CoreRemindersSetReminderMenuComponent, componentProps: { initialValue: this.timebefore, + eventTime: this.time, noReminderLabel: 'core.reminders.delete', }, event: ev, @@ -85,7 +95,7 @@ export class CoreRemindersSetButtonComponent { return; } - if (timebefore === CoreRemindersService.DISABLED) { + if (timebefore === undefined || timebefore === CoreRemindersService.DISABLED) { // Remove the reminder. await CoreReminders.removeReminders({ instanceId: this.instanceId, diff --git a/src/core/features/reminders/components/set-reminder-custom/set-reminder-custom.html b/src/core/features/reminders/components/set-reminder-custom/set-reminder-custom.html index 963338033..023ea6791 100644 --- a/src/core/features/reminders/components/set-reminder-custom/set-reminder-custom.html +++ b/src/core/features/reminders/components/set-reminder-custom/set-reminder-custom.html @@ -9,14 +9,15 @@
- + - - - + {{ 'core.reminders.units' | translate }} + + {{ option.label | translate }} @@ -29,7 +30,7 @@ - {{ 'core.done' | translate }} + {{ 'core.reminders.setreminder' | translate }} diff --git a/src/core/features/reminders/components/set-reminder-menu/set-reminder-menu.html b/src/core/features/reminders/components/set-reminder-menu/set-reminder-menu.html index 1ca9b3e2f..c7d1c24e8 100644 --- a/src/core/features/reminders/components/set-reminder-menu/set-reminder-menu.html +++ b/src/core/features/reminders/components/set-reminder-menu/set-reminder-menu.html @@ -8,27 +8,31 @@ - - -

{{ option.label }}

-
- -
+ + + +

{{ option.label }}

+
+ +
+
- +

{{ 'core.reminders.custom' | translate }}

{{ customLabel }}

- +
- +

{{ noReminderLabel | translate }}

- +
diff --git a/src/core/features/reminders/components/set-reminder-menu/set-reminder-menu.ts b/src/core/features/reminders/components/set-reminder-menu/set-reminder-menu.ts index f14167c38..4fac9cdb3 100644 --- a/src/core/features/reminders/components/set-reminder-menu/set-reminder-menu.ts +++ b/src/core/features/reminders/components/set-reminder-menu/set-reminder-menu.ts @@ -34,6 +34,7 @@ import { CoreRemindersSetReminderCustomComponent } from '../set-reminder-custom/ export class CoreRemindersSetReminderMenuComponent implements OnInit { @Input() initialValue?: number; + @Input() eventTime?: number; @Input() noReminderLabel = ''; currentValue = '0m'; @@ -48,24 +49,28 @@ export class CoreRemindersSetReminderMenuComponent implements OnInit { value: 0, unit: CoreRemindersUnits.MINUTE, label: '', + enabled: true, }, { radioValue: '1h', value: 1, unit: CoreRemindersUnits.HOUR, label: '', + enabled: true, }, { radioValue: '12h', value: 12, unit: CoreRemindersUnits.HOUR, label: '', + enabled: true, }, { radioValue: '1d', value: 1, unit: CoreRemindersUnits.DAY, label: '', + enabled: true, }, ]; @@ -75,6 +80,7 @@ export class CoreRemindersSetReminderMenuComponent implements OnInit { async ngOnInit(): Promise { this.presetOptions.forEach((option) => { option.label = CoreReminders.getUnitValueLabel(option.value, option.unit); + option.enabled = this.isValidTime(option.unit, option.value); }); const initialValue = CoreRemindersService.convertSecondsToValueAndUnit(this.initialValue); @@ -106,7 +112,10 @@ export class CoreRemindersSetReminderMenuComponent implements OnInit { */ setReminder(value?: string): void { const option = this.presetOptions.find(option => option.radioValue === value); + if (!option) { + PopoverController.dismiss(); + return; } @@ -120,6 +129,23 @@ export class CoreRemindersSetReminderMenuComponent implements OnInit { PopoverController.dismiss({ timeBefore: undefined }); } + /** + * Check the time is on the future. + * + * @param unit Time unit. + * @param value Time value. + * @return Wether is a valid time or not. + */ + protected isValidTime(unit: number, value: number): boolean { + if (!this.eventTime) { + return true; + } + + const timebefore = unit * value; + + return (this.eventTime - timebefore) * 1000 > Date.now(); + } + /** * Custom value input clicked. *