From ec3150f4555ab55415d7bd046f5f3373b472f32e Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sat, 19 Feb 2022 13:29:37 -0600 Subject: [PATCH] Fix alarm migration issues --- .../main/java/com/todoroo/astrid/service/Upgrade_12_4.kt | 9 +++++---- app/src/main/java/org/tasks/data/Alarm.kt | 6 ++++++ app/src/main/java/org/tasks/db/Migrations.kt | 6 +++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/service/Upgrade_12_4.kt b/app/src/main/java/com/todoroo/astrid/service/Upgrade_12_4.kt index 9312fbb66..b1e4902e7 100644 --- a/app/src/main/java/com/todoroo/astrid/service/Upgrade_12_4.kt +++ b/app/src/main/java/com/todoroo/astrid/service/Upgrade_12_4.kt @@ -15,15 +15,16 @@ class Upgrade_12_4 @Inject constructor( private val upgraderDao: UpgraderDao, ) { internal suspend fun syncExistingAlarms() { - val existingAlarms = alarmDao.getActiveAlarms().map { it.task } + val existingAlarms = alarmDao.getActiveAlarms() upgraderDao.tasksWithVtodos().forEach { caldav -> val remoteTask = caldav.vtodo?.let(iCalendar::fromVtodo) ?: return@forEach remoteTask .reminders - .onEach { alarm -> alarm.task = caldav.id } - .let { alarms -> alarmDao.insert(alarms) } + .filter { existingAlarms.find { e -> e.task == caldav.id && e.same(it) } == null } + .onEach { it.task = caldav.id } + .let { alarmDao.insert(it) } } - taskDao.touch(existingAlarms) + taskDao.touch(existingAlarms.map { it.task }.toSet().toList()) } companion object { diff --git a/app/src/main/java/org/tasks/data/Alarm.kt b/app/src/main/java/org/tasks/data/Alarm.kt index b88268b21..5b24dc9e0 100644 --- a/app/src/main/java/org/tasks/data/Alarm.kt +++ b/app/src/main/java/org/tasks/data/Alarm.kt @@ -70,6 +70,12 @@ class Alarm : Parcelable { return "Alarm(id=$id, task=$task, time=$timestamp, type=$type, repeat=$repeat, interval=$interval)" } + fun same(other: Alarm) = + type == other.type && + time == other.time && + repeat == other.repeat && + interval == other.interval + override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false diff --git a/app/src/main/java/org/tasks/db/Migrations.kt b/app/src/main/java/org/tasks/db/Migrations.kt index d2fec0fc6..0be765c2e 100644 --- a/app/src/main/java/org/tasks/db/Migrations.kt +++ b/app/src/main/java/org/tasks/db/Migrations.kt @@ -420,13 +420,13 @@ object Migrations { database.execSQL("ALTER TABLE `alarms` ADD COLUMN `repeat` INTEGER NOT NULL DEFAULT 0") database.execSQL("ALTER TABLE `alarms` ADD COLUMN `interval` INTEGER NOT NULL DEFAULT 0") database.execSQL( - "INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, 0, $TYPE_REL_START FROM `tasks` WHERE `hideUntil` > 0 AND `notificationFlags` | $NOTIFY_AT_START" + "INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, 0, $TYPE_REL_START FROM `tasks` WHERE `hideUntil` > 0 AND `notificationFlags` & $NOTIFY_AT_START = $NOTIFY_AT_START" ) database.execSQL( - "INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, 0, $TYPE_REL_END FROM `tasks` WHERE `dueDate` > 0 AND `notificationFlags` | $NOTIFY_AT_DEADLINE" + "INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, 0, $TYPE_REL_END FROM `tasks` WHERE `dueDate` > 0 AND `notificationFlags` & $NOTIFY_AT_DEADLINE = $NOTIFY_AT_DEADLINE" ) database.execSQL( - "INSERT INTO `alarms` (`task`, `time`, `type`, `repeat`, `interval`) SELECT `_id`, ${HOURS.toMillis(24)}, $TYPE_REL_END, 6, ${HOURS.toMillis(24)} FROM `tasks` WHERE `dueDate` > 0 AND `notificationFlags` | $NOTIFY_AFTER_DEADLINE" + "INSERT INTO `alarms` (`task`, `time`, `type`, `repeat`, `interval`) SELECT `_id`, ${HOURS.toMillis(24)}, $TYPE_REL_END, 6, ${HOURS.toMillis(24)} FROM `tasks` WHERE `dueDate` > 0 AND `notificationFlags` & $NOTIFY_AFTER_DEADLINE = $NOTIFY_AFTER_DEADLINE" ) database.execSQL( "INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, `notifications`, $TYPE_RANDOM FROM `tasks` WHERE `notifications` > 0"