From f8028e0c16180b8d8d984e91c7d2fe60dfd1ef7c Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 12 Mar 2019 11:55:11 +0100 Subject: [PATCH] use the Reminder model class at fetching event reminders --- .../calendar/pro/activities/EventActivity.kt | 2 +- .../simplemobiletools/calendar/pro/extensions/Context.kt | 7 ++++--- .../com/simplemobiletools/calendar/pro/models/Event.kt | 6 +++++- .../calendar/pro/receivers/NotificationReceiver.kt | 3 ++- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index 55bef8d86..18ad13563 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -879,7 +879,7 @@ class EventActivity : SimpleActivity() { if (mEvent.id == null || mEvent.id == null) { eventsHelper.insertEvent(mEvent, true, true) { if (DateTime.now().isAfter(mEventStartDateTime.millis)) { - if (mEvent.repeatInterval == 0 && mEvent.getReminders().isNotEmpty()) { + if (mEvent.repeatInterval == 0 && mEvent.getReminders().any { it.type == REMINDER_NOTIFICATION }) { notifyEvent(mEvent) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index 0d5d36f5c..3813e37bc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -80,7 +80,8 @@ fun Context.scheduleAllEvents() { } fun Context.scheduleNextEventReminder(event: Event, showToasts: Boolean) { - if (event.getReminders().isEmpty()) { + val validReminders = event.getReminders().filter { it.type == REMINDER_NOTIFICATION } + if (validReminders.isEmpty()) { if (showToasts) { toast(R.string.saving) } @@ -88,7 +89,7 @@ fun Context.scheduleNextEventReminder(event: Event, showToasts: Boolean) { } val now = getNowSeconds() - val reminderSeconds = event.getReminders().reversed().map { it * 60 } + val reminderSeconds = validReminders.reversed().map { it.minutes * 60 } eventsHelper.getEvents(now, now + YEAR, event.id!!, false) { if (it.isNotEmpty()) { for (curEvent in it) { @@ -159,7 +160,7 @@ fun Context.getRepetitionText(seconds: Int) = when (seconds) { } fun Context.notifyRunningEvents() { - eventsHelper.getRunningEvents().filter { it.getReminders().isNotEmpty() }.forEach { + eventsHelper.getRunningEvents().filter { it.getReminders().any { it.type == REMINDER_NOTIFICATION } }.forEach { notifyEvent(it) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt index b71be3d41..842eb6d46 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt @@ -120,7 +120,11 @@ data class Event( fun getIsAllDay() = flags and FLAG_ALL_DAY != 0 - fun getReminders() = setOf(reminder1Minutes, reminder2Minutes, reminder3Minutes).filter { it != REMINDER_OFF } + fun getReminders() = setOf( + Reminder(reminder1Minutes, reminder1Type), + Reminder(reminder2Minutes, reminder2Type), + Reminder(reminder3Minutes, reminder3Type) + ).filter { it.minutes != REMINDER_OFF } // properly return the start time of all-day events as midnight fun getEventStartTS(): Long { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt index 37996827d..58e905633 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt @@ -10,6 +10,7 @@ import com.simplemobiletools.calendar.pro.extensions.scheduleNextEventReminder import com.simplemobiletools.calendar.pro.extensions.updateListWidget import com.simplemobiletools.calendar.pro.helpers.EVENT_ID import com.simplemobiletools.calendar.pro.helpers.Formatter +import com.simplemobiletools.calendar.pro.helpers.REMINDER_NOTIFICATION class NotificationReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { @@ -30,7 +31,7 @@ class NotificationReceiver : BroadcastReceiver() { context.updateListWidget() val event = context.eventsDB.getEventWithId(id) - if (event == null || event.getReminders().isEmpty()) { + if (event == null || event.getReminders().none { it.type == REMINDER_NOTIFICATION }) { return }