Use new task dao in notification manager

pull/1055/head
Alex Baker 4 years ago
parent 234af09ad5
commit 1e5036a335

@ -5,7 +5,6 @@
*/ */
package com.todoroo.astrid.dao package com.todoroo.astrid.dao
import com.todoroo.andlib.utility.DateUtilities
import com.todoroo.astrid.api.Filter import com.todoroo.astrid.api.Filter
import com.todoroo.astrid.data.Task import com.todoroo.astrid.data.Task
import org.tasks.data.SubtaskInfo import org.tasks.data.SubtaskInfo
@ -26,8 +25,6 @@ class TaskDao @Inject constructor(
suspend fun activeTimers(): Int = taskDao.activeTimers() suspend fun activeTimers(): Int = taskDao.activeTimers()
suspend fun activeNotifications(): List<Task> = taskDao.activeNotifications()
suspend fun fetch(remoteId: String): Task? = taskDao.fetch(remoteId) suspend fun fetch(remoteId: String): Task? = taskDao.fetch(remoteId)
suspend fun getRecurringTasks(remoteIds: List<String>): List<Task> = suspend fun getRecurringTasks(remoteIds: List<String>): List<Task> =

@ -30,26 +30,18 @@ class ReminderService internal constructor(
taskDao: TaskDao taskDao: TaskDao
) : this(preferences, notificationQueue, Random(), taskDao) ) : this(preferences, notificationQueue, Random(), taskDao)
suspend fun scheduleAllAlarms(taskIds: List<Long>) { suspend fun scheduleAlarm(id: Long) = scheduleAllAlarms(listOf(id))
taskDao
.fetch(taskIds)
.map { getReminderEntry(it) }
.let { jobs.add(it) }
}
suspend fun scheduleAllAlarms() { suspend fun scheduleAllAlarms(taskIds: List<Long>) = scheduleAlarms(taskDao.fetch(taskIds))
taskDao
.getTasksWithReminders()
.map { getReminderEntry(it) }
.let { jobs.add(it) }
}
fun scheduleAlarm(task: Task?) { suspend fun scheduleAllAlarms() = scheduleAlarms(taskDao.getTasksWithReminders())
val reminder = getReminderEntry(task)
if (reminder != null) { fun scheduleAlarm(task: Task) = scheduleAlarms(listOf(task))
jobs.add(reminder)
} private fun scheduleAlarms(tasks: List<Task>) =
} tasks
.mapNotNull { getReminderEntry(it) }
.let { jobs.add(it) }
fun cancelReminder(taskId: Long) { fun cancelReminder(taskId: Long) {
jobs.cancelReminder(taskId) jobs.cancelReminder(taskId)

@ -18,7 +18,6 @@ import org.tasks.preferences.Preferences
import org.tasks.themes.ColorProvider import org.tasks.themes.ColorProvider
import org.tasks.time.DateTimeUtils import org.tasks.time.DateTimeUtils
import timber.log.Timber import timber.log.Timber
import java.util.*
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.min import kotlin.math.min
@ -67,20 +66,22 @@ class Notifier @Inject constructor(
} }
suspend fun triggerNotifications(entries: List<Notification>) { suspend fun triggerNotifications(entries: List<Notification>) {
val notifications: MutableList<Notification> = ArrayList()
var ringFiveTimes = false var ringFiveTimes = false
var ringNonstop = false var ringNonstop = false
for (entry in entries.takeLast(NotificationManager.MAX_NOTIFICATIONS)) { val notifications = entries
val task = taskDao.fetch(entry.taskId) ?: continue .filter {
if (entry.type != ReminderService.TYPE_RANDOM) { taskDao.fetch(it.taskId)
?.let { task ->
if (it.type != ReminderService.TYPE_RANDOM) {
ringFiveTimes = ringFiveTimes or task.isNotifyModeFive ringFiveTimes = ringFiveTimes or task.isNotifyModeFive
ringNonstop = ringNonstop or task.isNotifyModeNonstop ringNonstop = ringNonstop or task.isNotifyModeNonstop
} }
val notification = notificationManager.getTaskNotification(entry) notificationManager.getTaskNotification(it) != null
if (notification != null) {
notifications.add(entry)
} }
?: false
} }
.takeLast(NotificationManager.MAX_NOTIFICATIONS)
if (notifications.isEmpty()) { if (notifications.isEmpty()) {
return return
} }

@ -147,6 +147,9 @@ SELECT EXISTS(SELECT 1 FROM tasks WHERE parent > 0 AND deleted = 0) AS hasSubtas
@Query("UPDATE tasks SET parent = :parent WHERE _id IN (:children)") @Query("UPDATE tasks SET parent = :parent WHERE _id IN (:children)")
internal abstract suspend fun setParentInternal(parent: Long, children: List<Long>) internal abstract suspend fun setParentInternal(parent: Long, children: List<Long>)
@Query("UPDATE tasks SET lastNotified = :timestamp WHERE _id = :id AND lastNotified != :timestamp")
abstract suspend fun setLastNotified(id: Long, timestamp: Long): Int
@Transaction @Transaction
open suspend fun fetchChildren(id: Long): List<Task> { open suspend fun fetchChildren(id: Long): List<Task> {
return fetch(getChildren(id)) return fetch(getChildren(id))

@ -18,9 +18,9 @@ class NotificationQueue @Inject constructor(private val preferences: Preferences
fun <T : NotificationQueueEntry> add(entry: T) = add(listOf(entry)) fun <T : NotificationQueueEntry> add(entry: T) = add(listOf(entry))
@Synchronized @Synchronized
fun <T : NotificationQueueEntry> add(entries: Iterable<T?>) { fun <T : NotificationQueueEntry> add(entries: Iterable<T>) {
val originalFirstTime = firstTime() val originalFirstTime = firstTime()
entries.filterNotNull().forEach { jobs.put(it.time, it) } entries.forEach { jobs.put(it.time, it) }
if (originalFirstTime != firstTime()) { if (originalFirstTime != firstTime()) {
scheduleNext(true) scheduleNext(true)
} }

@ -11,7 +11,6 @@ import com.todoroo.andlib.sql.QueryTemplate
import com.todoroo.andlib.utility.AndroidUtilities import com.todoroo.andlib.utility.AndroidUtilities
import com.todoroo.andlib.utility.DateUtilities import com.todoroo.andlib.utility.DateUtilities
import com.todoroo.astrid.api.Filter import com.todoroo.astrid.api.Filter
import com.todoroo.astrid.dao.TaskDao
import com.todoroo.astrid.data.Task import com.todoroo.astrid.data.Task
import com.todoroo.astrid.reminders.ReminderService import com.todoroo.astrid.reminders.ReminderService
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
@ -19,6 +18,7 @@ import org.tasks.LocalBroadcastManager
import org.tasks.R import org.tasks.R
import org.tasks.Strings.isNullOrEmpty import org.tasks.Strings.isNullOrEmpty
import org.tasks.data.LocationDao import org.tasks.data.LocationDao
import org.tasks.data.TaskDao
import org.tasks.intents.TaskIntents import org.tasks.intents.TaskIntents
import org.tasks.preferences.Preferences import org.tasks.preferences.Preferences
import org.tasks.receivers.CompleteTaskReceiver import org.tasks.receivers.CompleteTaskReceiver
@ -40,7 +40,8 @@ class NotificationManager @Inject constructor(
private val notificationDao: NotificationDao, private val notificationDao: NotificationDao,
private val taskDao: TaskDao, private val taskDao: TaskDao,
private val locationDao: LocationDao, private val locationDao: LocationDao,
private val localBroadcastManager: LocalBroadcastManager) { private val localBroadcastManager: LocalBroadcastManager,
private val reminderService: ReminderService) {
private val notificationManagerCompat = NotificationManagerCompat.from(context) private val notificationManagerCompat = NotificationManagerCompat.from(context)
private val colorProvider = ColorProvider(context, preferences) private val colorProvider = ColorProvider(context, preferences)
private val throttle = Throttle(NOTIFICATIONS_PER_SECOND) private val throttle = Throttle(NOTIFICATIONS_PER_SECOND)
@ -159,6 +160,10 @@ class NotificationManager @Inject constructor(
.setGroupAlertBehavior( .setGroupAlertBehavior(
if (alert) NotificationCompat.GROUP_ALERT_CHILDREN else NotificationCompat.GROUP_ALERT_SUMMARY) if (alert) NotificationCompat.GROUP_ALERT_CHILDREN else NotificationCompat.GROUP_ALERT_SUMMARY)
notify(notification.taskId, builder, alert, nonstop, fiveTimes) notify(notification.taskId, builder, alert, nonstop, fiveTimes)
val reminderTime = DateTime(notification.timestamp).endOfMinute().millis
if (taskDao.setLastNotified(notification.taskId, reminderTime) == 1) {
reminderService.scheduleAlarm(notification.taskId)
}
alert = false alert = false
} }
} }
@ -303,12 +308,6 @@ class NotificationManager @Inject constructor(
val taskTitle = task.title val taskTitle = task.title
val taskDescription = task.notes val taskDescription = task.notes
// update last reminder time
val reminderTime = DateTime(`when`).endOfMinute().millis
if (reminderTime != task.reminderLast) {
task.reminderLast = reminderTime
taskDao.save(task)
}
val builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_DEFAULT) val builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_REMINDER) .setCategory(NotificationCompat.CATEGORY_REMINDER)
.setContentTitle(taskTitle) .setContentTitle(taskTitle)

Loading…
Cancel
Save