From 966edb8beb4a21dcf2abba6035ce5d2a98cde95e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 15 Nov 2024 12:54:50 +0100 Subject: [PATCH] MOBILE-4653 reminders: Move reminders constants --- .../calendar/pages/edit-event/edit-event.ts | 5 +- src/addons/calendar/pages/event/event.ts | 5 +- .../calendar/pages/settings/settings.ts | 3 +- .../calendar/services/calendar-helper.ts | 5 +- src/addons/calendar/services/calendar.ts | 4 +- .../calendar/services/database/calendar.ts | 5 +- .../components/set-button/set-button.ts | 5 +- .../set-reminder-custom.ts | 2 +- .../set-reminder-menu/set-reminder-menu.ts | 4 +- src/core/features/reminders/constants.ts | 46 ++++++++++++ .../reminders/services/database/reminders.ts | 7 +- .../features/reminders/services/reminders.ts | 75 +++++++++---------- 12 files changed, 107 insertions(+), 59 deletions(-) create mode 100644 src/core/features/reminders/constants.ts diff --git a/src/addons/calendar/pages/edit-event/edit-event.ts b/src/addons/calendar/pages/edit-event/edit-event.ts index ecaf421c3..f3d02d4ce 100644 --- a/src/addons/calendar/pages/edit-event/edit-event.ts +++ b/src/addons/calendar/pages/edit-event/edit-event.ts @@ -40,7 +40,7 @@ import { CoreError } from '@classes/errors/error'; import { CoreNavigator } from '@services/navigator'; import { CanLeave } from '@guards/can-leave'; import { CoreForms } from '@singletons/form'; -import { CoreReminders, CoreRemindersService, CoreRemindersUnits } from '@features/reminders/services/reminders'; +import { CoreReminders, CoreRemindersService } from '@features/reminders/services/reminders'; import moment from 'moment-timezone'; import { ADDON_CALENDAR_COMPONENT, @@ -52,6 +52,7 @@ import { import { ContextLevel } from '@/core/constants'; import { CorePopovers } from '@services/popovers'; import { CoreLoadings } from '@services/loadings'; +import { REMINDERS_DISABLED, CoreRemindersUnits } from '@features/reminders/constants'; /** * Page that displays a form to create/edit an event. @@ -621,7 +622,7 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy, CanLeave { // Check if default reminders are enabled. const defaultTime = await CoreReminders.getDefaultNotificationTime(this.currentSite.getId()); - if (defaultTime === CoreRemindersService.DISABLED) { + if (defaultTime === REMINDERS_DISABLED) { return; } diff --git a/src/addons/calendar/pages/event/event.ts b/src/addons/calendar/pages/event/event.ts index 826133092..469e32a0a 100644 --- a/src/addons/calendar/pages/event/event.ts +++ b/src/addons/calendar/pages/event/event.ts @@ -37,7 +37,7 @@ import { CoreConstants } from '@/core/constants'; import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker'; import { AddonCalendarEventsSource } from '@addons/calendar/classes/events-source'; import { CoreSwipeNavigationItemsManager } from '@classes/items-management/swipe-navigation-items-manager'; -import { CoreReminders, CoreRemindersService } from '@features/reminders/services/reminders'; +import { CoreReminders } from '@features/reminders/services/reminders'; import { CoreLocalNotifications } from '@services/local-notifications'; import { CorePlatform } from '@services/platform'; import { CoreConfig } from '@services/config'; @@ -54,6 +54,7 @@ import { ADDON_CALENDAR_NEW_EVENT_EVENT, ADDON_CALENDAR_UNDELETED_EVENT_EVENT, } from '@addons/calendar/constants'; +import { REMINDERS_DEFAULT_NOTIFICATION_TIME_CHANGED } from '@features/reminders/constants'; /** * Page that displays a single calendar event. @@ -146,7 +147,7 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy { }); // Reload reminders if default notification time changes. - this.defaultTimeChangedObserver = CoreEvents.on(CoreRemindersService.DEFAULT_NOTIFICATION_TIME_CHANGED, () => { + this.defaultTimeChangedObserver = CoreEvents.on(REMINDERS_DEFAULT_NOTIFICATION_TIME_CHANGED, () => { this.loadReminders(); }, this.currentSiteId); diff --git a/src/addons/calendar/pages/settings/settings.ts b/src/addons/calendar/pages/settings/settings.ts index 1e7cb5e82..87237d62e 100644 --- a/src/addons/calendar/pages/settings/settings.ts +++ b/src/addons/calendar/pages/settings/settings.ts @@ -18,6 +18,7 @@ import { CoreReminders, CoreRemindersService, } from '@features/reminders/services/reminders'; +import { REMINDERS_DISABLED } from '@features/reminders/constants'; /** * Page that displays the calendar settings. @@ -67,7 +68,7 @@ export class AddonCalendarSettingsPage implements OnInit { return; } - await CoreReminders.setDefaultNotificationTime(reminderTime.timeBefore ?? CoreRemindersService.DISABLED); + await CoreReminders.setDefaultNotificationTime(reminderTime.timeBefore ?? REMINDERS_DISABLED); this.updateDefaultTimeLabel(); } diff --git a/src/addons/calendar/services/calendar-helper.ts b/src/addons/calendar/services/calendar-helper.ts index 295dc92c9..3a22698cf 100644 --- a/src/addons/calendar/services/calendar-helper.ts +++ b/src/addons/calendar/services/calendar-helper.ts @@ -42,6 +42,7 @@ import { AddonCalendarEventType, } from '../constants'; import { AddonCalendarSyncInvalidateEvent } from './calendar-sync'; +import { REMINDERS_DISABLED, REMINDERS_DEFAULT_REMINDER_TIMEBEFORE } from '@features/reminders/constants'; /** * Service that provides some features regarding lists of courses and categories. @@ -314,7 +315,7 @@ export class AddonCalendarHelperProvider { const defaultTime = await CoreReminders.getDefaultNotificationTime(siteId); let defaultLabel: string | undefined; - if (defaultTime > CoreRemindersService.DISABLED) { + if (defaultTime > REMINDERS_DISABLED) { const data = CoreRemindersService.convertSecondsToValueAndUnit(defaultTime); defaultLabel = CoreReminders.getUnitValueLabel(data.value, data.unit, true); } @@ -324,7 +325,7 @@ export class AddonCalendarHelperProvider { id: reminder.id, }; - if (reminder.timebefore === CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE) { + if (reminder.timebefore === REMINDERS_DEFAULT_REMINDER_TIMEBEFORE) { // Default time. Check if default notifications are disabled. if (defaultLabel !== undefined) { formatted.label = defaultLabel; diff --git a/src/addons/calendar/services/calendar.ts b/src/addons/calendar/services/calendar.ts index 2d532a2ff..072f629d1 100644 --- a/src/addons/calendar/services/calendar.ts +++ b/src/addons/calendar/services/calendar.ts @@ -41,7 +41,6 @@ import { CoreReminderData, CoreReminders, CoreRemindersPushNotificationData, - CoreRemindersService, } from '@features/reminders/services/reminders'; import { CoreEvents } from '@singletons/events'; import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site'; @@ -60,6 +59,7 @@ import { ADDON_CALENDAR_UNDELETED_EVENT_EVENT, AddonCalendarEventType, } from '../constants'; +import { REMINDERS_DEFAULT_REMINDER_TIMEBEFORE } from '@features/reminders/constants'; import { AddonCalendarFilter } from './calendar-helper'; declare module '@singletons/events' { @@ -685,7 +685,7 @@ export class AddonCalendarProvider { siteId?: string, ): Promise { - timebefore = timebefore ?? CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE; + timebefore = timebefore ?? REMINDERS_DEFAULT_REMINDER_TIMEBEFORE; const previousReminders = await CoreReminders.getReminders({ instanceId: event.id, diff --git a/src/addons/calendar/services/database/calendar.ts b/src/addons/calendar/services/database/calendar.ts index 1320c0cdb..4614e1792 100644 --- a/src/addons/calendar/services/database/calendar.ts +++ b/src/addons/calendar/services/database/calendar.ts @@ -14,7 +14,8 @@ import { ADDON_CALENDAR_EVENTS_TABLE, AddonCalendarEventType } from '@addons/calendar/constants'; import { SQLiteDB } from '@classes/sqlitedb'; -import { CoreRemindersService, CoreReminders } from '@features/reminders/services/reminders'; +import { REMINDERS_DISABLED } from '@features/reminders/constants'; +import { CoreReminders } from '@features/reminders/services/reminders'; import { CoreConfig } from '@services/config'; import { CoreSiteSchema } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; @@ -201,7 +202,7 @@ const migrateDefaultTime = async (siteId: string, convertToSeconds = false): Pro await CoreUtils.ignoreErrors(CoreConfig.delete(key)); if (defaultTime <= 0) { - defaultTime = CoreRemindersService.DISABLED; + defaultTime = REMINDERS_DISABLED; } else if (convertToSeconds) { // Convert from minutes to seconds. defaultTime = defaultTime * 60; 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 988b6ed40..f5cedf7dd 100644 --- a/src/core/features/reminders/components/set-button/set-button.ts +++ b/src/core/features/reminders/components/set-button/set-button.ts @@ -12,12 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { CoreReminderData, CoreReminders, CoreRemindersService } from '@features/reminders/services/reminders'; +import { CoreReminderData, CoreReminders } from '@features/reminders/services/reminders'; import { Component, Input, OnInit } from '@angular/core'; import { CorePopovers } from '@services/popovers'; import { Translate } from '@singletons'; import { CoreTimeUtils } from '@services/utils/time'; import { CoreToasts } from '@services/toasts'; +import { REMINDERS_DISABLED } from '@features/reminders/constants'; /** * Component that displays a button to set a reminder. @@ -128,7 +129,7 @@ export class CoreRemindersSetButtonComponent implements OnInit { type: this.type, }); - if (timebefore === undefined || timebefore === CoreRemindersService.DISABLED) { + if (timebefore === undefined || timebefore === REMINDERS_DISABLED) { this.setTimebefore(undefined); CoreToasts.show({ message: 'core.reminders.reminderunset', diff --git a/src/core/features/reminders/components/set-reminder-custom/set-reminder-custom.ts b/src/core/features/reminders/components/set-reminder-custom/set-reminder-custom.ts index 7f5373ffa..7d2958499 100644 --- a/src/core/features/reminders/components/set-reminder-custom/set-reminder-custom.ts +++ b/src/core/features/reminders/components/set-reminder-custom/set-reminder-custom.ts @@ -14,7 +14,7 @@ import { CoreSharedModule } from '@/core/shared.module'; import { Component, Input } from '@angular/core'; -import { CoreRemindersUnits } from '@features/reminders/services/reminders'; +import { CoreRemindersUnits } from '@features/reminders/constants'; import { PopoverController } from '@singletons'; /** 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 8fe54609a..e43530cb0 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 @@ -16,13 +16,13 @@ import { Component, Input, OnInit } from '@angular/core'; import { CoreReminders, CoreRemindersService, - CoreRemindersUnits, CoreReminderValueAndUnit, } from '@features/reminders/services/reminders'; import { CorePopovers } from '@services/popovers'; import { CoreWait } from '@singletons/wait'; import { PopoverController } from '@singletons'; import { CoreSharedModule } from '@/core/shared.module'; +import { CoreRemindersUnits, REMINDERS_DISABLED } from '@features/reminders/constants'; /** * This component is meant to display a popover with the reminder options. @@ -88,7 +88,7 @@ export class CoreRemindersSetReminderMenuComponent implements OnInit { }); const initialValue = CoreRemindersService.convertSecondsToValueAndUnit(this.initialValue); - if (initialValue.value === CoreRemindersService.DISABLED) { + if (initialValue.value === REMINDERS_DISABLED) { this.currentValue = 'disabled'; return; diff --git a/src/core/features/reminders/constants.ts b/src/core/features/reminders/constants.ts new file mode 100644 index 000000000..9cfa53273 --- /dev/null +++ b/src/core/features/reminders/constants.ts @@ -0,0 +1,46 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { CoreConstants } from '@/core/constants'; + +/** + * Units to set a reminder. + */ +export enum CoreRemindersUnits { + MINUTE = CoreConstants.SECONDS_MINUTE, + HOUR = CoreConstants.SECONDS_HOUR, + DAY = CoreConstants.SECONDS_DAY, + WEEK = CoreConstants.SECONDS_WEEK, +} + +export const REMINDERS_UNITS_LABELS = { + single: { + [CoreRemindersUnits.MINUTE]: 'core.minute', + [CoreRemindersUnits.HOUR]: 'core.hour', + [CoreRemindersUnits.DAY]: 'core.day', + [CoreRemindersUnits.WEEK]: 'core.week', + }, + multi: { + [CoreRemindersUnits.MINUTE]: 'core.minutes', + [CoreRemindersUnits.HOUR]: 'core.hours', + [CoreRemindersUnits.DAY]: 'core.days', + [CoreRemindersUnits.WEEK]: 'core.weeks', + }, +}; + +export const REMINDERS_DEFAULT_REMINDER_TIMEBEFORE = -1; +export const REMINDERS_DISABLED = -1; + +export const REMINDERS_DEFAULT_NOTIFICATION_TIME_SETTING = 'CoreRemindersDefaultNotification'; +export const REMINDERS_DEFAULT_NOTIFICATION_TIME_CHANGED = 'CoreRemindersDefaultNotificationChangedEvent'; diff --git a/src/core/features/reminders/services/database/reminders.ts b/src/core/features/reminders/services/database/reminders.ts index c4c2c8c8a..026d9fd7a 100644 --- a/src/core/features/reminders/services/database/reminders.ts +++ b/src/core/features/reminders/services/database/reminders.ts @@ -16,8 +16,9 @@ import { ADDON_CALENDAR_COMPONENT, ADDON_CALENDAR_EVENTS_TABLE } from '@addons/c import { SQLiteDB } from '@classes/sqlitedb'; import { CoreSiteSchema } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; -import { CoreReminderData, CoreRemindersService } from '../reminders'; +import { CoreReminderData } from '../reminders'; import { AddonCalendarEventDBRecord } from '@addons/calendar/services/database/calendar'; +import { REMINDERS_DEFAULT_REMINDER_TIMEBEFORE } from '@features/reminders/constants'; /** * Database variables for CoreRemindersService service. @@ -113,7 +114,7 @@ const migrateFromCalendarRemindersV1 = async (db: SQLiteDB): Promise => { if (!reminderTime || reminderTime === -1) { // Default reminder. - reminderTime = CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE; + reminderTime = REMINDERS_DEFAULT_REMINDER_TIMEBEFORE; } else if (reminderTime > event.timestart) { // Reminder is after the event, ignore it. return; @@ -165,7 +166,7 @@ const migrateFromCalendarRemindersV2 = async (db: SQLiteDB): Promise => { } const event = events[record.eventid]; - const reminderTime = record.time || CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE; + const reminderTime = record.time || REMINDERS_DEFAULT_REMINDER_TIMEBEFORE; if (uniqueReminder[record.eventid] === undefined) { uniqueReminder[record.eventid] = []; diff --git a/src/core/features/reminders/services/reminders.ts b/src/core/features/reminders/services/reminders.ts index d9e995f3b..fc0741de1 100644 --- a/src/core/features/reminders/services/reminders.ts +++ b/src/core/features/reminders/services/reminders.ts @@ -27,31 +27,14 @@ import { lazyMap, LazyMap } from '@/core/utils/lazy-map'; import { CoreDatabaseTable } from '@classes/database/database-table'; import { asyncInstance, AsyncInstance } from '@/core/utils/async-instance'; import { CoreDatabaseCachingStrategy } from '@classes/database/database-table-proxy'; - -/** - * Units to set a reminder. - */ -export enum CoreRemindersUnits { - MINUTE = CoreConstants.SECONDS_MINUTE, - HOUR = CoreConstants.SECONDS_HOUR, - DAY = CoreConstants.SECONDS_DAY, - WEEK = CoreConstants.SECONDS_WEEK, -} - -const REMINDER_UNITS_LABELS = { - single: { - [CoreRemindersUnits.MINUTE]: 'core.minute', - [CoreRemindersUnits.HOUR]: 'core.hour', - [CoreRemindersUnits.DAY]: 'core.day', - [CoreRemindersUnits.WEEK]: 'core.week', - }, - multi: { - [CoreRemindersUnits.MINUTE]: 'core.minutes', - [CoreRemindersUnits.HOUR]: 'core.hours', - [CoreRemindersUnits.DAY]: 'core.days', - [CoreRemindersUnits.WEEK]: 'core.weeks', - }, -}; +import { + CoreRemindersUnits, + REMINDERS_DEFAULT_NOTIFICATION_TIME_CHANGED, + REMINDERS_DEFAULT_NOTIFICATION_TIME_SETTING, + REMINDERS_DEFAULT_REMINDER_TIMEBEFORE, + REMINDERS_DISABLED, + REMINDERS_UNITS_LABELS, +} from '../constants'; /** * Service to handle reminders. @@ -59,11 +42,23 @@ const REMINDER_UNITS_LABELS = { @Injectable({ providedIn: 'root' }) export class CoreRemindersService { - static readonly DEFAULT_REMINDER_TIMEBEFORE = -1; - static readonly DISABLED = -1; + /** + * @deprecated since 5.0. Use REMINDERS_DEFAULT_REMINDER_TIMEBEFORE instead. + */ + static readonly DEFAULT_REMINDER_TIMEBEFORE = REMINDERS_DEFAULT_REMINDER_TIMEBEFORE; + /** + * @deprecated since 5.0. Use REMINDERS_DISABLED instead. + */ + static readonly DISABLED = REMINDERS_DISABLED; - static readonly DEFAULT_NOTIFICATION_TIME_SETTING = 'CoreRemindersDefaultNotification'; - static readonly DEFAULT_NOTIFICATION_TIME_CHANGED = 'CoreRemindersDefaultNotificationChangedEvent'; + /** + * @deprecated since 5.0. Use REMINDERS_DEFAULT_NOTIFICATION_TIME_SETTING instead. + */ + static readonly DEFAULT_NOTIFICATION_TIME_SETTING = REMINDERS_DEFAULT_NOTIFICATION_TIME_SETTING; + /** + * @deprecated since 5.0. Use REMINDERS_DEFAULT_NOTIFICATION_TIME_CHANGED instead. + */ + static readonly DEFAULT_NOTIFICATION_TIME_CHANGED = REMINDERS_DEFAULT_NOTIFICATION_TIME_CHANGED; protected remindersTables: LazyMap>>; @@ -91,7 +86,7 @@ export class CoreRemindersService { this.scheduleAllNotifications(); - CoreEvents.on(CoreRemindersService.DEFAULT_NOTIFICATION_TIME_CHANGED, async (data) => { + CoreEvents.on(REMINDERS_DEFAULT_NOTIFICATION_TIME_CHANGED, async (data) => { const site = await CoreSites.getSite(data.siteId); const siteId = site.getId(); @@ -215,7 +210,7 @@ export class CoreRemindersService { protected async getRemindersWithDefaultTime(siteId?: string): Promise { siteId ??= CoreSites.getCurrentSiteId(); - return this.remindersTables[siteId].getMany({ timebefore: CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE }, { + return this.remindersTables[siteId].getMany({ timebefore: REMINDERS_DEFAULT_REMINDER_TIMEBEFORE }, { sorting: [ { time: 'asc' }, ], @@ -294,11 +289,11 @@ export class CoreRemindersService { siteId = siteId || CoreSites.getCurrentSiteId(); - const timebefore = reminder.timebefore === CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE + const timebefore = reminder.timebefore === REMINDERS_DEFAULT_REMINDER_TIMEBEFORE ? await this.getDefaultNotificationTime(siteId) : reminder.timebefore; - if (timebefore === CoreRemindersService.DISABLED) { + if (timebefore === REMINDERS_DISABLED) { // Notification disabled. Cancel. return this.cancelReminder(reminder.id, reminder.component, siteId); } @@ -364,7 +359,7 @@ export class CoreRemindersService { * @returns Translated label. */ getUnitValueLabel(value: number, unit: CoreRemindersUnits, addDefaultLabel = false): string { - if (value === CoreRemindersService.DISABLED) { + if (value === REMINDERS_DISABLED) { return Translate.instant('core.settings.disabled'); } @@ -373,8 +368,8 @@ export class CoreRemindersService { } const unitsLabel = value === 1 ? - REMINDER_UNITS_LABELS.single[unit] : - REMINDER_UNITS_LABELS.multi[unit]; + REMINDERS_UNITS_LABELS.single[unit] : + REMINDERS_UNITS_LABELS.multi[unit]; const label = Translate.instant('core.reminders.timebefore', { units: Translate.instant(unitsLabel), @@ -397,7 +392,7 @@ export class CoreRemindersService { static convertSecondsToValueAndUnit(seconds?: number): CoreReminderValueAndUnit { if (seconds === undefined || seconds < 0) { return { - value: CoreRemindersService.DISABLED, + value: REMINDERS_DISABLED, unit: CoreRemindersUnits.MINUTE, }; } else if (seconds === 0) { @@ -437,7 +432,7 @@ export class CoreRemindersService { async getDefaultNotificationTime(siteId?: string): Promise { siteId = siteId || CoreSites.getCurrentSiteId(); - const key = CoreRemindersService.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId; + const key = REMINDERS_DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId; return CoreConfig.get(key, CoreConstants.CONFIG.calendarreminderdefaultvalue || 3600); } @@ -452,11 +447,11 @@ export class CoreRemindersService { async setDefaultNotificationTime(time: number, siteId?: string): Promise { siteId = siteId || CoreSites.getCurrentSiteId(); - const key = CoreRemindersService.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId; + const key = REMINDERS_DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId; await CoreConfig.set(key, time); - CoreEvents.trigger(CoreRemindersService.DEFAULT_NOTIFICATION_TIME_CHANGED, { time }, siteId); + CoreEvents.trigger(REMINDERS_DEFAULT_NOTIFICATION_TIME_CHANGED, { time }, siteId); } }