Remove AlarmEntry

pull/2906/head
Alex Baker 1 month ago
parent a49c233584
commit 9ee739627e

@ -1,8 +1,8 @@
package com.todoroo.astrid.alarms
import org.tasks.data.entity.Task
import org.tasks.data.entity.Alarm
import org.tasks.jobs.AlarmEntry
import org.tasks.data.entity.Notification
import org.tasks.data.entity.Task
import org.tasks.preferences.Preferences
import org.tasks.reminders.Random
import org.tasks.time.DateTimeUtils.withMillisOfDay
@ -18,7 +18,7 @@ class AlarmCalculator(
preferences: Preferences
) : this(preferences.isDefaultDueTimeEnabled, Random(), preferences.defaultDueTime)
fun toAlarmEntry(task: Task, alarm: Alarm): AlarmEntry? {
fun toAlarmEntry(task: Task, alarm: Alarm): Notification? {
val trigger = when (alarm.type) {
Alarm.TYPE_SNOOZE,
Alarm.TYPE_DATE_TIME ->
@ -50,12 +50,12 @@ class AlarmCalculator(
trigger <= AlarmService.NO_ALARM ->
null
trigger > task.reminderLast || alarm.type == Alarm.TYPE_SNOOZE ->
AlarmEntry(alarm.id, alarm.task, trigger, alarm.type)
Notification(taskId = alarm.task, timestamp = trigger, type = alarm.type)
alarm.repeat > 0 -> {
val past = (task.reminderLast - trigger) / alarm.interval
val next = trigger + (past + 1) * alarm.interval
if (past < alarm.repeat && next > task.reminderLast) {
AlarmEntry(alarm.id, alarm.task, next, alarm.type)
Notification(taskId = alarm.task, timestamp = next, type = alarm.type)
} else {
null
}

@ -12,7 +12,6 @@ import org.tasks.data.db.DbUtils
import org.tasks.data.entity.Alarm
import org.tasks.data.entity.Alarm.Companion.TYPE_SNOOZE
import org.tasks.data.entity.Notification
import org.tasks.jobs.AlarmEntry
import org.tasks.jobs.WorkManager
import org.tasks.notifications.NotificationManager
import org.tasks.preferences.Preferences
@ -76,28 +75,28 @@ class AlarmService @Inject constructor(
}
val (overdue, _) = getAlarms()
overdue
.sortedBy { it.time }
.sortedBy { it.timestamp }
.also { alarms ->
alarms
.map { it.taskId }
.chunked(DbUtils.MAX_SQLITE_ARGS)
.onEach { alarmDao.deleteSnoozed(it) }
}
.map { it.toNotification() }
.map { it.copy(timestamp = currentTimeMillis()) }
.let { trigger(it) }
val alreadyTriggered = overdue.map { it.taskId }.toSet()
val (moreOverdue, future) = getAlarms()
return moreOverdue
.filterNot { it.type == Alarm.TYPE_RANDOM || alreadyTriggered.contains(it.taskId) }
.plus(future)
.minOfOrNull { it.time }
.minOfOrNull { it.timestamp }
?: 0
}
internal suspend fun getAlarms(): Pair<List<AlarmEntry>, List<AlarmEntry>> {
internal suspend fun getAlarms(): Pair<List<Notification>, List<Notification>> {
val start = currentTimeMillis()
val overdue = ArrayList<AlarmEntry>()
val future = ArrayList<AlarmEntry>()
val overdue = ArrayList<Notification>()
val future = ArrayList<Notification>()
alarmDao.getActiveAlarms()
.groupBy { it.task }
.forEach { (taskId, alarms) ->
@ -105,14 +104,14 @@ class AlarmService @Inject constructor(
val alarmEntries = alarms.mapNotNull {
alarmCalculator.toAlarmEntry(task, it)
}
val (now, later) = alarmEntries.partition { it.time <= DateTime().startOfMinute().plusMinutes(1).millis }
val (now, later) = alarmEntries.partition { it.timestamp <= DateTime().startOfMinute().plusMinutes(1).millis }
later
.filter { it.type == TYPE_SNOOZE }
.maxByOrNull { it.time }
.maxByOrNull { it.timestamp }
?.let { future.add(it) }
?: run {
now.firstOrNull()?.let { overdue.add(it) }
later.minByOrNull { it.time }?.let { future.add(it) }
later.minByOrNull { it.timestamp }?.let { future.add(it) }
}
}
Timber.d("took ${currentTimeMillis() - start}ms overdue=${overdue.size} future=${future.size}")

@ -1,22 +0,0 @@
package org.tasks.jobs
import org.tasks.data.entity.Notification
import org.tasks.time.DateTimeUtils2.currentTimeMillis
import org.tasks.time.printTimestamp
data class AlarmEntry(
val id: Long,
val taskId: Long,
val time: Long,
val type: Int
) {
fun toNotification(): Notification = Notification(
taskId = taskId,
type = type,
timestamp = currentTimeMillis(),
)
override fun toString(): String {
return "AlarmEntry(id=$id, taskId=$taskId, time=${printTimestamp(time)}, type=$type)"
}
}

@ -18,14 +18,12 @@ import org.tasks.data.entity.Alarm.Companion.TYPE_SNOOZE
import org.tasks.data.entity.Alarm.Companion.whenDue
import org.tasks.data.entity.Alarm.Companion.whenOverdue
import org.tasks.data.entity.Alarm.Companion.whenStarted
import org.tasks.data.entity.Notification
import org.tasks.data.entity.Task.Companion.HIDE_UNTIL_DUE
import org.tasks.data.entity.Task.Companion.HIDE_UNTIL_DUE_TIME
import org.tasks.data.entity.Task.Companion.URGENCY_SPECIFIC_DAY_TIME
import org.tasks.date.DateTimeUtils.newDateTime
import org.tasks.date.DateTimeUtils.toDateTime
import org.tasks.makers.AlarmEntryMaker.TIME
import org.tasks.makers.AlarmEntryMaker.TYPE
import org.tasks.makers.AlarmEntryMaker.newAlarmEntry
import org.tasks.makers.TaskMaker.CREATION_TIME
import org.tasks.makers.TaskMaker.DUE_DATE
import org.tasks.makers.TaskMaker.DUE_TIME
@ -70,7 +68,7 @@ class AlarmCalculatorTest {
Alarm(time = now.millis + 1, type = TYPE_DATE_TIME)
)
assertEquals(newAlarmEntry(with(TIME, now.plusMillis(1)), with(TYPE, TYPE_DATE_TIME)), alarm)
assertEquals(Notification(timestamp = now.millis + 1, type = TYPE_DATE_TIME), alarm)
}
@Test
@ -80,10 +78,7 @@ class AlarmCalculatorTest {
Alarm(time = now.millis, type = TYPE_SNOOZE)
)
assertEquals(
newAlarmEntry(with(TIME, now), with(TYPE, TYPE_SNOOZE)),
alarm
)
assertEquals(Notification(timestamp = now.millis, type = TYPE_SNOOZE), alarm)
}
@Test
@ -91,9 +86,9 @@ class AlarmCalculatorTest {
val alarm = alarmCalculator.toAlarmEntry(newTask(with(DUE_DATE, now)), whenDue(0L))
assertEquals(
newAlarmEntry(
with(TIME, now.startOfDay().withHourOfDay(13)),
with(TYPE, TYPE_REL_END)
Notification(
timestamp = now.startOfDay().withHourOfDay(13).millis,
type = TYPE_REL_END
),
alarm
)
@ -104,8 +99,9 @@ class AlarmCalculatorTest {
val alarm = alarmCalculator.toAlarmEntry(newTask(with(DUE_TIME, now)), whenDue(0L))
assertEquals(
newAlarmEntry(
with(TIME, now.startOfMinute().plusMillis(1000)), with(TYPE, TYPE_REL_END)
Notification(
timestamp = now.startOfMinute().plusMillis(1000).millis,
type = TYPE_REL_END
),
alarm
)
@ -119,9 +115,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(
with(TIME, now.startOfDay().withHourOfDay(13)),
with(TYPE, TYPE_REL_START)
Notification(
timestamp = now.startOfDay().withHourOfDay(13).millis,
type = TYPE_REL_START
),
alarm
)
@ -135,9 +131,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(
with(TIME, now.startOfMinute().plusMillis(1000)),
with(TYPE, TYPE_REL_START)
Notification(
timestamp = now.startOfMinute().plusMillis(1000).millis,
type = TYPE_REL_START
),
alarm
)
@ -152,9 +148,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(
with(TIME, DateTime(2023, 11, 4, 13, 0, 0)),
with(TYPE, TYPE_REL_END)
Notification(
timestamp = DateTime(2023, 11, 4, 13, 0, 0).millis,
type = TYPE_REL_END
),
alarm
)
@ -169,9 +165,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(
with(TIME, now.plusDays(1).startOfMinute().plusMillis(1000)),
with(TYPE, TYPE_REL_END)
Notification(
timestamp = now.plusDays(1).startOfMinute().plusMillis(1000).millis,
type = TYPE_REL_END
),
alarm
)
@ -186,9 +182,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(
with(TIME, DateTime(2023, 11, 4, 13, 0)),
with(TYPE, TYPE_REL_START)
Notification(
timestamp = DateTime(2023, 11, 4, 13, 0).millis,
type = TYPE_REL_START
),
alarm
)
@ -203,9 +199,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(
with(TIME, now.plusDays(1).startOfMinute().plusMillis(1000)),
with(TYPE, TYPE_REL_START)
Notification(
timestamp = now.plusDays(1).startOfMinute().plusMillis(1000).millis,
type = TYPE_REL_START
),
alarm
)
@ -219,9 +215,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(
with(TIME, now.plusMinutes(5).startOfMinute().plusMillis(1000)),
with(TYPE, TYPE_REL_END)
Notification(
timestamp = now.plusMinutes(5).startOfMinute().plusMillis(1000).millis,
type = TYPE_REL_END
),
alarm
)
@ -235,9 +231,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(
with(TIME, now.plusMinutes(10).startOfMinute().plusMillis(1000)),
with(TYPE, TYPE_REL_END)
Notification(
timestamp = now.plusMinutes(10).startOfMinute().plusMillis(1000).millis,
type = TYPE_REL_END
),
alarm
)
@ -279,7 +275,7 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(with(TIME, dueDate.plusDays(7)), with(TYPE, TYPE_REL_END)),
Notification(timestamp = dueDate.plusDays(7).millis, type = TYPE_REL_END),
alarm
)
}
@ -310,7 +306,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(with(TIME, now.minusDays(14).plusMillis(584206592)), with(TYPE, TYPE_RANDOM)),
Notification(
timestamp = now.minusDays(14).plusMillis(584206592).millis,
type = TYPE_RANDOM),
alarm
)
}
@ -331,10 +329,7 @@ class AlarmCalculatorTest {
Alarm(time = ONE_WEEK, type = TYPE_RANDOM)
)
assertEquals(
newAlarmEntry(with(TIME, task.dueDate.toDateTime()), with(TYPE, TYPE_RANDOM)),
alarm
)
assertEquals(Notification(timestamp = task.dueDate, type = TYPE_RANDOM), alarm)
}
}
@ -352,9 +347,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(
with(TIME, now.minusDays(1).plusMillis(584206592)),
with(TYPE, TYPE_RANDOM)
Notification(
timestamp = now.minusDays(1).plusMillis(584206592).millis,
type = TYPE_RANDOM
),
alarm
)
@ -375,9 +370,9 @@ class AlarmCalculatorTest {
)
assertEquals(
newAlarmEntry(
with(TIME, now.minusDays(1).plusMillis(584206592)),
with(TYPE, TYPE_RANDOM)
Notification(
timestamp = now.minusDays(1).plusMillis(584206592).millis,
type = TYPE_RANDOM
),
alarm
)

@ -1,32 +0,0 @@
package org.tasks.makers
import com.natpryce.makeiteasy.Instantiator
import com.natpryce.makeiteasy.Property
import com.natpryce.makeiteasy.Property.newProperty
import com.natpryce.makeiteasy.PropertyLookup
import com.natpryce.makeiteasy.PropertyValue
import org.tasks.data.entity.Alarm.Companion.TYPE_DATE_TIME
import org.tasks.date.DateTimeUtils.newDateTime
import org.tasks.jobs.AlarmEntry
import org.tasks.makers.Maker.make
import org.tasks.time.DateTime
object AlarmEntryMaker {
val ID: Property<AlarmEntry, Long> = newProperty()
val TASK: Property<AlarmEntry, Long> = newProperty()
val TIME: Property<AlarmEntry, DateTime> = newProperty()
val TYPE: Property<AlarmEntry, Int> = newProperty()
private val instantiator = Instantiator { lookup: PropertyLookup<AlarmEntry> ->
AlarmEntry(
lookup.valueOf(ID, 0L),
lookup.valueOf(TASK, 0L),
lookup.valueOf(TIME, newDateTime()).millis,
lookup.valueOf(TYPE, TYPE_DATE_TIME)
)
}
fun newAlarmEntry(vararg properties: PropertyValue<in AlarmEntry?, *>): AlarmEntry {
return make(instantiator, *properties)
}
}
Loading…
Cancel
Save