diff --git a/scripts/langindex.json b/scripts/langindex.json index a022f6a59..5e5c11a5d 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -147,7 +147,6 @@ "addon.calendar.sunday": "calendar", "addon.calendar.thu": "calendar", "addon.calendar.thursday": "calendar", - "addon.calendar.timebefore": "local_moodlemobileapp", "addon.calendar.today": "calendar", "addon.calendar.tomorrow": "calendar", "addon.calendar.tue": "calendar", @@ -161,7 +160,6 @@ "addon.calendar.typeopen": "calendar", "addon.calendar.typesite": "calendar", "addon.calendar.typeuser": "calendar", - "addon.calendar.units": "qtype_numerical", "addon.calendar.upcomingevents": "calendar", "addon.calendar.userevents": "calendar", "addon.calendar.wed": "calendar", @@ -2150,6 +2148,15 @@ "core.rating.ratings": "rating", "core.redirectingtosite": "local_moodlemobileapp", "core.refresh": "moodle", + "core.reminders.atthetime": "local_moodlemobileapp", + "core.reminders.custom": "local_moodlemobileapp", + "core.reminders.customreminder": "local_moodlemobileapp", + "core.reminders.daybefore": "local_moodlemobileapp", + "core.reminders.daysbefore": "local_moodlemobileapp", + "core.reminders.delete": "moodle", + "core.reminders.setareminder": "local_moodlemobileapp", + "core.reminders.timebefore": "local_moodlemobileapp", + "core.reminders.units": "qtype_numerical", "core.remove": "moodle", "core.removefiles": "local_moodlemobileapp", "core.required": "moodle", diff --git a/src/addons/calendar/pages/edit-event/edit-event.html b/src/addons/calendar/pages/edit-event/edit-event.html index fe0a30baa..912194c76 100644 --- a/src/addons/calendar/pages/edit-event/edit-event.html +++ b/src/addons/calendar/pages/edit-event/edit-event.html @@ -113,7 +113,7 @@ - +

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

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 e01ca933d..ce5c0994c 100644 --- a/src/addons/calendar/pages/edit-event/edit-event.page.ts +++ b/src/addons/calendar/pages/edit-event/edit-event.page.ts @@ -86,7 +86,7 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy, CanLeave { descriptionControl: FormControl; // Reminders. - notificationsEnabled = false; + remindersEnabled = false; reminders: AddonCalendarEventCandidateReminder[] = []; protected courseId!: number; @@ -101,7 +101,7 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy, CanLeave { protected fb: FormBuilder, ) { this.currentSite = CoreSites.getRequiredCurrentSite(); - this.notificationsEnabled = CoreLocalNotifications.isAvailable(); + this.remindersEnabled = CoreReminders.isEnabled(); this.errors = { required: Translate.instant('core.required'), }; @@ -647,13 +647,13 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy, CanLeave { */ protected async initReminders(): Promise { // Don't init reminders when editing an event. Right now, only allow adding reminders for new events. - if (!this.notificationsEnabled || this.eventId) { + if (!this.remindersEnabled || this.eventId) { return; } // Check if default reminders are enabled. - const defaultTime = await AddonCalendar.getDefaultNotificationTime(this.currentSite.getId()); - if (defaultTime === AddonCalendarProvider.DEFAULT_NOTIFICATION_DISABLED) { + const defaultTime = await CoreReminders.getDefaultNotificationTime(this.currentSite.getId()); + if (defaultTime === CoreRemindersService.DISABLED) { return; } diff --git a/src/addons/calendar/pages/event/event.html b/src/addons/calendar/pages/event/event.html index 2fe34311c..add0b9a28 100644 --- a/src/addons/calendar/pages/event/event.html +++ b/src/addons/calendar/pages/event/event.html @@ -127,7 +127,7 @@ - +

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

diff --git a/src/addons/calendar/pages/event/event.page.ts b/src/addons/calendar/pages/event/event.page.ts index 6f0e3bd32..3d96ce88d 100644 --- a/src/addons/calendar/pages/event/event.page.ts +++ b/src/addons/calendar/pages/event/event.page.ts @@ -41,7 +41,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 } from '@features/reminders/services/reminders'; +import { CoreReminders, CoreRemindersService } from '@features/reminders/services/reminders'; import { CoreRemindersSetReminderMenuComponent } from '@features/reminders/components/set-reminder-menu/set-reminder-menu'; /** @@ -72,7 +72,7 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy { courseName = ''; groupName?: string; courseUrl = ''; - notificationsEnabled = false; + remindersEnabled = false; moduleUrl = ''; categoryPath = ''; currentTime = -1; @@ -85,7 +85,7 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy { constructor( protected route: ActivatedRoute, ) { - this.notificationsEnabled = CoreLocalNotifications.isAvailable(); + this.remindersEnabled = CoreReminders.isEnabled(); this.siteHomeId = CoreSites.getCurrentSiteHomeId(); this.currentSiteId = CoreSites.getCurrentSiteId(); @@ -132,7 +132,7 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy { }); // Reload reminders if default notification time changes. - this.defaultTimeChangedObserver = CoreEvents.on(AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_CHANGED, () => { + this.defaultTimeChangedObserver = CoreEvents.on(CoreRemindersService.DEFAULT_NOTIFICATION_TIME_CHANGED, () => { this.loadReminders(); }, this.currentSiteId); @@ -149,7 +149,7 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy { * @return Promise resolved when done. */ protected async loadReminders(): Promise { - if (!this.notificationsEnabled || !this.event) { + if (!this.remindersEnabled || !this.event) { return; } diff --git a/src/addons/calendar/pages/settings/settings.ts b/src/addons/calendar/pages/settings/settings.ts index 743a54c7e..dab3d7bab 100644 --- a/src/addons/calendar/pages/settings/settings.ts +++ b/src/addons/calendar/pages/settings/settings.ts @@ -13,18 +13,10 @@ // limitations under the License. import { Component, OnInit } from '@angular/core'; -import { - AddonCalendar, - AddonCalendarProvider, -} from '../../services/calendar'; -import { CoreEvents } from '@singletons/events'; -import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreReminders, CoreRemindersService, - CoreRemindersUnits, - CoreReminderValueAndUnit, } from '@features/reminders/services/reminders'; import { CoreRemindersSetReminderMenuComponent } from '@features/reminders/components/set-reminder-menu/set-reminder-menu'; @@ -39,10 +31,7 @@ export class AddonCalendarSettingsPage implements OnInit { defaultTimeLabel = ''; - protected defaultTime: CoreReminderValueAndUnit = { - value: 0, - unit: CoreRemindersUnits.MINUTE, - }; + protected defaultTime?: number; /** * @inheritdoc @@ -76,24 +65,18 @@ export class AddonCalendarSettingsPage implements OnInit { return; } - await AddonCalendar.setDefaultNotificationTime(reminderTime.timeBefore); + await CoreReminders.setDefaultNotificationTime(reminderTime.timeBefore ?? CoreRemindersService.DISABLED); this.updateDefaultTimeLabel(); - - CoreEvents.trigger( - AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_CHANGED, - { time: reminderTime.timeBefore }, - CoreSites.getCurrentSiteId(), - ); } /** * Update default time label. */ async updateDefaultTimeLabel(): Promise { - const defaultTime = await AddonCalendar.getDefaultNotificationTime(); + this.defaultTime = await CoreReminders.getDefaultNotificationTime(); - this.defaultTime = CoreRemindersService.convertSecondsToValueAndUnit(defaultTime); - this.defaultTimeLabel = CoreReminders.getUnitValueLabel(this.defaultTime.value, this.defaultTime.unit); + const defaultTime = CoreRemindersService.convertSecondsToValueAndUnit(this.defaultTime); + this.defaultTimeLabel = CoreReminders.getUnitValueLabel(defaultTime.value, defaultTime.unit); } } diff --git a/src/addons/calendar/services/calendar-helper.ts b/src/addons/calendar/services/calendar-helper.ts index dfccc8588..6ac5d6334 100644 --- a/src/addons/calendar/services/calendar-helper.ts +++ b/src/addons/calendar/services/calendar-helper.ts @@ -333,10 +333,10 @@ export class AddonCalendarHelperProvider { return []; } - const defaultTime = await AddonCalendar.getDefaultNotificationTime(siteId); + const defaultTime = await CoreReminders.getDefaultNotificationTime(siteId); let defaultLabel: string | undefined; - if (defaultTime > AddonCalendarProvider.DEFAULT_NOTIFICATION_DISABLED) { + if (defaultTime > CoreRemindersService.DISABLED) { const data = CoreRemindersService.convertSecondsToValueAndUnit(defaultTime); defaultLabel = CoreReminders.getUnitValueLabel(data.value, data.unit, true); } diff --git a/src/addons/calendar/services/calendar.ts b/src/addons/calendar/services/calendar.ts index 373d054b4..81d8cab75 100644 --- a/src/addons/calendar/services/calendar.ts +++ b/src/addons/calendar/services/calendar.ts @@ -38,7 +38,6 @@ import { SafeUrl } from '@angular/platform-browser'; import { CoreNavigator } from '@services/navigator'; import { AddonCalendarFilter } from './calendar-helper'; import { AddonCalendarSyncEvents, AddonCalendarSyncProvider } from './calendar-sync'; -import { CoreEvents } from '@singletons/events'; import { CoreText } from '@singletons/text'; import { CorePlatform } from '@services/platform'; import { @@ -103,8 +102,7 @@ export class AddonCalendarProvider { static readonly DAYS_INTERVAL = 30; static readonly COMPONENT = 'AddonCalendarEvents'; - static readonly DEFAULT_NOTIFICATION_TIME_CHANGED = 'AddonCalendarDefaultNotificationTimeChangedEvent'; - static readonly DEFAULT_NOTIFICATION_TIME_SETTING = 'mmaCalendarDefaultNotifTime'; + static readonly STARTING_WEEK_DAY = 'addon_calendar_starting_week_day'; static readonly NEW_EVENT_EVENT = 'addon_calendar_new_event'; static readonly NEW_EVENT_DISCARDED_EVENT = 'addon_calendar_new_event_discarded'; @@ -116,8 +114,6 @@ export class AddonCalendarProvider { static readonly CALENDAR_TF_24 = '%H:%M'; // Calendar time in 24 hours format. static readonly CALENDAR_TF_12 = '%I:%M %p'; // Calendar time in 12 hours format. - static readonly DEFAULT_NOTIFICATION_DISABLED = -1; - protected weekDays: AddonCalendarWeekDaysTranslationKeys[] = [ { shortname: 'addon.calendar.sun', @@ -293,7 +289,6 @@ export class AddonCalendarProvider { * @return Promise resolved when done. */ async initialize(): Promise { - CoreLocalNotifications.registerClick( AddonCalendarProvider.COMPONENT, async (notification) => { @@ -302,22 +297,6 @@ export class AddonCalendarProvider { this.notificationClicked(notification); }, ); - - if (!CoreLocalNotifications.isAvailable()) { - return; - } - - CoreEvents.on(AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_CHANGED, async (data) => { - const site = await CoreSites.getSite(data.siteId); - const siteId = site.getId(); - - // Get all the events that have a default reminder. - const reminders = await CoreReminders.getRemindersWithDefaultTime(AddonCalendarProvider.COMPONENT, siteId); - - // Reschedule all the default reminders. - reminders.forEach((reminder) => - CoreReminders.scheduleNotification(reminder, siteId)); - }); } /** @@ -602,13 +581,10 @@ export class AddonCalendarProvider { * * @param siteId ID of the site. If not defined, use current site. * @return Promise resolved with the default time (in seconds). + * @deprecated since 4.1 Use CoreReminders.getDefaultNotificationTime instead. */ async getDefaultNotificationTime(siteId?: string): Promise { - siteId = siteId || CoreSites.getCurrentSiteId(); - - const key = AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId; - - return CoreConfig.get(key, CoreConstants.CONFIG.calendarreminderdefaultvalue || 3600); + return CoreReminders.getDefaultNotificationTime(siteId); } /** @@ -1492,13 +1468,10 @@ export class AddonCalendarProvider { * @param time New default time. * @param siteId ID of the site. If not defined, use current site. * @return Promise resolved when stored. + * @deprecated since 4.1 Use CoreReminders.setDefaultNotificationTime. */ async setDefaultNotificationTime(time: number, siteId?: string): Promise { - siteId = siteId || CoreSites.getCurrentSiteId(); - - const key = AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId; - - await CoreConfig.set(key, time); + await CoreReminders.setDefaultNotificationTime(time, siteId); } /** diff --git a/src/addons/calendar/services/database/calendar.ts b/src/addons/calendar/services/database/calendar.ts index 43d626a07..1b87cb281 100644 --- a/src/addons/calendar/services/database/calendar.ts +++ b/src/addons/calendar/services/database/calendar.ts @@ -13,10 +13,11 @@ // limitations under the License. import { SQLiteDB } from '@classes/sqlitedb'; +import { CoreRemindersService, CoreReminders } from '@features/reminders/services/reminders'; import { CoreConfig } from '@services/config'; import { CoreSiteSchema } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; -import { AddonCalendar, AddonCalendarEventType, AddonCalendarProvider } from '../calendar'; +import { AddonCalendarEventType } from '../calendar'; /** * Database variables for AddonCalendarProvider service. @@ -180,20 +181,39 @@ export const CALENDAR_SITE_SCHEMA: CoreSiteSchema = { }, ], async migrate(db: SQLiteDB, oldVersion: number, siteId: string): Promise { - if (oldVersion < 4) { - // Migrate default notification time if it was changed. - // Don't use getDefaultNotificationTime to be able to detect if the value was changed or not. - const key = AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId; - const defaultTime = await CoreUtils.ignoreErrors(CoreConfig.get(key, null)); - - if (defaultTime) { - // Convert from minutes to seconds. - AddonCalendar.setDefaultNotificationTime(defaultTime * 60, siteId); - } + if (oldVersion < 5) { + await migrateDefaultTime(siteId, oldVersion < 4); } }, }; +/** + * Migrate default notification time if it was changed. + * Don't use getDefaultNotificationTime to be able to detect if the value was changed or not. + * + * @param siteId Site ID to migrate. + * @param convertToSeconds If true, time will be converted to seconds. + */ +const migrateDefaultTime = async (siteId: string, convertToSeconds = false): Promise => { + + const key = 'mmaCalendarDefaultNotifTime#' + siteId; + try { + let defaultTime = await CoreConfig.get(key); + await CoreUtils.ignoreErrors(CoreConfig.delete(key)); + + if (defaultTime <= 0) { + defaultTime = CoreRemindersService.DISABLED; + } else if (convertToSeconds) { + // Convert from minutes to seconds. + defaultTime = defaultTime * 60; + } + + CoreReminders.setDefaultNotificationTime(defaultTime, siteId); + } catch { + // Ignore errors, already migrated. + } +}; + export type AddonCalendarEventDBRecord = { id: number; name: string; 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 85c09e03e..1ca9b3e2f 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 @@ -24,8 +24,7 @@
- +

{{ 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 951886057..f14167c38 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 @@ -12,9 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { AddonCalendarProvider } from '@addons/calendar/services/calendar'; import { Component, Input, OnInit } from '@angular/core'; -import { CoreReminders, CoreRemindersUnits, CoreReminderValueAndUnit } from '@features/reminders/services/reminders'; +import { + CoreReminders, + CoreRemindersService, + CoreRemindersUnits, + CoreReminderValueAndUnit, +} from '@features/reminders/services/reminders'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreUtils } from '@services/utils/utils'; import { PopoverController } from '@singletons'; @@ -29,7 +33,7 @@ import { CoreRemindersSetReminderCustomComponent } from '../set-reminder-custom/ }) export class CoreRemindersSetReminderMenuComponent implements OnInit { - @Input() initialValue?: CoreReminderValueAndUnit; + @Input() initialValue?: number; @Input() noReminderLabel = ''; currentValue = '0m'; @@ -73,26 +77,25 @@ export class CoreRemindersSetReminderMenuComponent implements OnInit { option.label = CoreReminders.getUnitValueLabel(option.value, option.unit); }); - if (!this.initialValue) { + const initialValue = CoreRemindersService.convertSecondsToValueAndUnit(this.initialValue); + if (initialValue.value === CoreRemindersService.DISABLED) { + this.currentValue = 'disabled'; + return; } - if (this.initialValue.value === AddonCalendarProvider.DEFAULT_NOTIFICATION_DISABLED) { - this.currentValue = 'disabled'; - } else { - // Search if it's one of the preset options. - const option = this.presetOptions.find(option => - option.value === this.initialValue?.value && option.unit === this.initialValue.unit); + // Search if it's one of the preset options. + const option = this.presetOptions.find(option => + option.value === initialValue?.value && option.unit === initialValue.unit); - if (option) { - this.currentValue = option.radioValue; - } else { - // It's a custom value. - this.currentValue = 'custom'; - this.customValue = this.initialValue.value; - this.customUnits = this.initialValue.unit; - this.customLabel = CoreReminders.getUnitValueLabel(this.customValue, this.customUnits); - } + if (option) { + this.currentValue = option.radioValue; + } else { + // It's a custom value. + this.currentValue = 'custom'; + this.customValue = initialValue.value; + this.customUnits = initialValue.unit; + this.customLabel = CoreReminders.getUnitValueLabel(this.customValue, this.customUnits); } } @@ -101,14 +104,7 @@ export class CoreRemindersSetReminderMenuComponent implements OnInit { * * @param value Value to set. */ - setReminder(value: string): void { - // Return it as an object because 0 means undefined if not. - if (value === 'disabled') { - PopoverController.dismiss({ timeBefore: AddonCalendarProvider.DEFAULT_NOTIFICATION_DISABLED }); - - return; - } - + setReminder(value?: string): void { const option = this.presetOptions.find(option => option.radioValue === value); if (!option) { return; @@ -117,6 +113,13 @@ export class CoreRemindersSetReminderMenuComponent implements OnInit { PopoverController.dismiss({ timeBefore: option.unit * option.value }); } + /** + * Disable the reminder. + */ + disableReminder(): void { + PopoverController.dismiss({ timeBefore: undefined }); + } + /** * Custom value input clicked. * diff --git a/src/core/features/reminders/reminders.module.ts b/src/core/features/reminders/reminders.module.ts index 46ddaeee9..1155eac0e 100644 --- a/src/core/features/reminders/reminders.module.ts +++ b/src/core/features/reminders/reminders.module.ts @@ -36,7 +36,7 @@ export const CORE_REMINDERS_SERVICES: Type[] = [ provide: APP_INITIALIZER, multi: true, useValue: async () => { - CoreReminders.scheduleAllNotifications(); + await CoreReminders.initialize(); }, }, ], diff --git a/src/core/features/reminders/services/reminders.ts b/src/core/features/reminders/services/reminders.ts index 297178a8a..655d9f42a 100644 --- a/src/core/features/reminders/services/reminders.ts +++ b/src/core/features/reminders/services/reminders.ts @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { AddonCalendar, AddonCalendarProvider } from '@addons/calendar/services/calendar'; import { Injectable } from '@angular/core'; import { CoreLocalNotifications } from '@services/local-notifications'; import { CoreSites } from '@services/sites'; @@ -22,6 +21,8 @@ import { CoreReminderDBRecord, REMINDERS_TABLE } from './database/reminders'; import { ILocalNotification } from '@ionic-native/local-notifications'; import { CorePlatform } from '@services/platform'; import { CoreConstants } from '@/core/constants'; +import { CoreConfig } from '@services/config'; +import { CoreEvents } from '@singletons/events'; /** * Units to set a reminder. @@ -55,6 +56,35 @@ const REMINDER_UNITS_LABELS = { export class CoreRemindersService { static readonly DEFAULT_REMINDER_TIMEBEFORE = -1; + static readonly DISABLED = -1; + + static readonly DEFAULT_NOTIFICATION_TIME_SETTING = 'CoreRemindersDefaultNotification'; + static readonly DEFAULT_NOTIFICATION_TIME_CHANGED = 'CoreRemindersDefaultNotificationChangedEvent'; + + /** + * Initialize the service. + * + * @return Promise resolved when done. + */ + async initialize(): Promise { + if (!CoreLocalNotifications.isAvailable()) { + return; + } + + this.scheduleAllNotifications(); + + CoreEvents.on(CoreRemindersService.DEFAULT_NOTIFICATION_TIME_CHANGED, async (data) => { + const site = await CoreSites.getSite(data.siteId); + const siteId = site.getId(); + + // Get all the events that have a default reminder. + const reminders = await this.getRemindersWithDefaultTime(siteId); + + // Reschedule all the default reminders. + reminders.forEach((reminder) => + this.scheduleNotification(reminder, siteId)); + }); + } /** * Returns if Reminders are enabled. @@ -131,7 +161,7 @@ export class CoreRemindersService { * @param siteId ID of the site the reminder belongs to. If not defined, use current site. * @return Promise resolved when the reminder data is retrieved. */ - async getAllReminders( siteId?: string): Promise { + async getAllReminders(siteId?: string): Promise { const site = await CoreSites.getSite(siteId); return site.getDb().getRecords(REMINDERS_TABLE, undefined, 'time ASC'); @@ -153,16 +183,15 @@ export class CoreRemindersService { /** * Get all reminders of a component with default time. * - * @param component Component. * @param siteId ID of the site the reminder belongs to. If not defined, use current site. * @return Promise resolved when the reminder data is retrieved. */ - async getRemindersWithDefaultTime(component: string, siteId?: string): Promise { + protected async getRemindersWithDefaultTime(siteId?: string): Promise { const site = await CoreSites.getSite(siteId); return site.getDb().getRecords( REMINDERS_TABLE, - { component, timebefore: CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE }, + { timebefore: CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE }, 'time ASC', ); } @@ -241,10 +270,10 @@ export class CoreRemindersService { siteId = siteId || CoreSites.getCurrentSiteId(); const timebefore = reminder.timebefore === CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE - ? await AddonCalendar.getDefaultNotificationTime(siteId) + ? await this.getDefaultNotificationTime(siteId) : reminder.timebefore; - if (timebefore === AddonCalendarProvider.DEFAULT_NOTIFICATION_DISABLED) { + if (timebefore === CoreRemindersService.DISABLED) { // Notification disabled. Cancel. return this.cancelReminder(reminder.id, reminder.component, siteId); } @@ -309,8 +338,7 @@ export class CoreRemindersService { * @return Translated label. */ getUnitValueLabel(value: number, unit: CoreRemindersUnits, addDefaultLabel = false): string { - if (value === AddonCalendarProvider.DEFAULT_NOTIFICATION_DISABLED) { - // TODO: It will need a migration of date to set 0 to -1 when needed. + if (value === CoreRemindersService.DISABLED) { return Translate.instant('core.settings.disabled'); } @@ -343,7 +371,7 @@ export class CoreRemindersService { static convertSecondsToValueAndUnit(seconds?: number): CoreReminderValueAndUnit { if (seconds === undefined || seconds < 0) { return { - value: AddonCalendarProvider.DEFAULT_NOTIFICATION_DISABLED, + value: CoreRemindersService.DISABLED, unit: CoreRemindersUnits.MINUTE, }; } else if (seconds === 0) { @@ -374,6 +402,37 @@ export class CoreRemindersService { } } + /** + * Get the configured default notification time. + * + * @param siteId ID of the site. If not defined, use current site. + * @return Promise resolved with the default time (in seconds). + */ + async getDefaultNotificationTime(siteId?: string): Promise { + siteId = siteId || CoreSites.getCurrentSiteId(); + + const key = CoreRemindersService.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId; + + return CoreConfig.get(key, CoreConstants.CONFIG.calendarreminderdefaultvalue || 3600); + } + + /** + * Set the default notification time. + * + * @param time New default time. + * @param siteId ID of the site. If not defined, use current site. + * @return Promise resolved when stored. + */ + async setDefaultNotificationTime(time: number, siteId?: string): Promise { + siteId = siteId || CoreSites.getCurrentSiteId(); + + const key = CoreRemindersService.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId; + + await CoreConfig.set(key, time); + + CoreEvents.trigger(CoreRemindersService.DEFAULT_NOTIFICATION_TIME_CHANGED, { time }, siteId); + } + } export const CoreReminders = makeSingleton(CoreRemindersService);