Move some AfterSaveWork to TaskDao

pull/1055/head
Alex Baker 4 years ago
parent 0eb5d65166
commit 91827654d0

@ -7,17 +7,31 @@ package com.todoroo.astrid.dao
import com.todoroo.astrid.api.Filter
import com.todoroo.astrid.data.Task
import com.todoroo.astrid.reminders.ReminderService
import com.todoroo.astrid.timers.TimerPlugin
import org.tasks.LocalBroadcastManager
import org.tasks.data.SubtaskInfo
import org.tasks.data.TaskContainer
import org.tasks.data.TaskDao
import org.tasks.db.SuspendDbUtils.eachChunk
import org.tasks.jobs.WorkManager
import org.tasks.location.GeofenceApi
import org.tasks.notifications.NotificationManager
import org.tasks.preferences.Preferences
import org.tasks.scheduling.RefreshScheduler
import org.tasks.sync.SyncAdapters
import javax.inject.Inject
class TaskDao @Inject constructor(
private val workManager: WorkManager,
private val taskDao: TaskDao) {
private val taskDao: TaskDao,
private val reminderService: ReminderService,
private val refreshScheduler: RefreshScheduler,
private val localBroadcastManager: LocalBroadcastManager,
private val notificationManager: NotificationManager,
private val geofenceApi: GeofenceApi,
private val timerPlugin: TimerPlugin,
private val syncAdapters: SyncAdapters) {
suspend fun fetch(id: Long): Task? = taskDao.fetch(id)
@ -69,7 +83,29 @@ class TaskDao @Inject constructor(
suspend fun save(task: Task, original: Task?) {
if (taskDao.update(task, original)) {
workManager.afterSave(task, original)
val completionDateModified = task.completionDate != original?.completionDate ?: 0
val deletionDateModified = task.deletionDate != original?.deletionDate ?: 0
val justCompleted = completionDateModified && task.isCompleted
val justDeleted = deletionDateModified && task.isDeleted
if (justCompleted) {
workManager.afterComplete(task, original)
}
if (justCompleted || justDeleted) {
notificationManager.cancel(task.id)
if (task.timerStart > 0) {
timerPlugin.stopTimer(task)
}
}
if (completionDateModified || deletionDateModified) {
geofenceApi.update(task.id)
}
reminderService.scheduleAlarm(task)
refreshScheduler.scheduleRefresh(task)
if (!task.checkTransitory(Task.TRANS_SUPPRESS_REFRESH)) {
localBroadcastManager.broadcastRefresh()
}
syncAdapters.sync(task, original)
}
}

@ -6,24 +6,15 @@ import android.net.Uri
import android.provider.CalendarContract
import androidx.hilt.Assisted
import androidx.hilt.work.WorkerInject
import androidx.work.Data
import androidx.work.WorkerParameters
import com.todoroo.astrid.dao.TaskDao
import com.todoroo.astrid.data.SyncFlags
import com.todoroo.astrid.data.Task
import com.todoroo.astrid.reminders.ReminderService
import com.todoroo.astrid.repeats.RepeatTaskHelper
import com.todoroo.astrid.timers.TimerPlugin
import org.tasks.LocalBroadcastManager
import org.tasks.R
import org.tasks.Strings.isNullOrEmpty
import org.tasks.analytics.Firebase
import org.tasks.data.CaldavDao
import org.tasks.data.TaskDao
import org.tasks.injection.BaseWorker
import org.tasks.location.GeofenceApi
import org.tasks.notifications.NotificationManager
import org.tasks.scheduling.RefreshScheduler
import org.tasks.sync.SyncAdapters
import timber.log.Timber
class AfterSaveWork @WorkerInject constructor(
@ -31,53 +22,22 @@ class AfterSaveWork @WorkerInject constructor(
@Assisted workerParams: WorkerParameters,
firebase: Firebase,
private val repeatTaskHelper: RepeatTaskHelper,
private val notificationManager: NotificationManager,
private val geofenceApi: GeofenceApi,
private val timerPlugin: TimerPlugin,
private val reminderService: ReminderService,
private val refreshScheduler: RefreshScheduler,
private val localBroadcastManager: LocalBroadcastManager,
private val taskDao: TaskDao,
private val syncAdapters: SyncAdapters,
private val workManager: WorkManager,
private val caldavDao: CaldavDao) : BaseWorker(context, workerParams, firebase) {
private val caldavDao: CaldavDao
) : BaseWorker(context, workerParams, firebase) {
override suspend fun run(): Result {
val data = inputData
val taskId = data.getLong(EXTRA_ID, -1)
val taskId = inputData.getLong(EXTRA_ID, -1)
val task = taskDao.fetch(taskId)
if (task == null) {
Timber.e("Missing saved task")
return Result.failure()
}
reminderService.scheduleAlarm(task)
val completionDateModified = task.completionDate != data.getLong(EXTRA_ORIG_COMPLETED, 0)
val deletionDateModified = task.deletionDate != data.getLong(EXTRA_ORIG_DELETED, 0)
val justCompleted = completionDateModified && task.isCompleted
val justDeleted = deletionDateModified && task.isDeleted
if (justCompleted || justDeleted) {
notificationManager.cancel(taskId)
}
if (completionDateModified || deletionDateModified) {
geofenceApi.update(taskId)
}
if (justCompleted) {
updateCalendarTitle(task)
val account = caldavDao.getAccountForTask(taskId)
if (account == null || !account.isSuppressRepeatingTasks) {
repeatTaskHelper.handleRepeat(task)
}
if (task.timerStart > 0) {
timerPlugin.stopTimer(task)
}
}
if (data.getBoolean(EXTRA_PUSH_GTASKS, false) && syncAdapters.isGoogleTaskSyncEnabled()
|| data.getBoolean(EXTRA_PUSH_CALDAV, false) && syncAdapters.isCaldavSyncEnabled()) {
workManager.sync(false)
}
refreshScheduler.scheduleRefresh(task)
if (!data.getBoolean(EXTRA_SUPPRESS_REFRESH, false)) {
localBroadcastManager.broadcastRefresh()
updateCalendarTitle(task)
val account = caldavDao.getAccountForTask(taskId)
if (account == null || !account.isSuppressRepeatingTasks) {
repeatTaskHelper.handleRepeat(task)
}
return Result.success()
}
@ -100,27 +60,6 @@ class AfterSaveWork @WorkerInject constructor(
}
companion object {
private const val EXTRA_ID = "extra_id"
private const val EXTRA_ORIG_COMPLETED = "extra_was_completed"
private const val EXTRA_ORIG_DELETED = "extra_was_deleted"
private const val EXTRA_PUSH_GTASKS = "extra_push_gtasks"
private const val EXTRA_PUSH_CALDAV = "extra_push_caldav"
private const val EXTRA_SUPPRESS_REFRESH = "extra_suppress_refresh"
fun getInputData(current: Task, original: Task?): Data {
val suppress = current.checkTransitory(SyncFlags.SUPPRESS_SYNC)
val forceCaldav = current.checkTransitory(SyncFlags.FORCE_CALDAV_SYNC)
val builder = Data.Builder()
.putLong(EXTRA_ID, current.id)
.putBoolean(EXTRA_PUSH_GTASKS, !suppress && !current.googleTaskUpToDate(original))
.putBoolean(
EXTRA_PUSH_CALDAV, !suppress && (!current.caldavUpToDate(original) || forceCaldav))
.putBoolean(EXTRA_SUPPRESS_REFRESH, current.checkTransitory(Task.TRANS_SUPPRESS_REFRESH))
if (original != null) {
builder
.putLong(EXTRA_ORIG_COMPLETED, original.completionDate)
.putLong(EXTRA_ORIG_DELETED, original.deletionDate)
}
return builder.build()
}
const val EXTRA_ID = "extra_id"
}
}

@ -7,7 +7,7 @@ import org.tasks.data.Place
interface WorkManager {
fun afterSave(current: Task, original: Task?)
fun afterComplete(current: Task, original: Task?)
fun cleanup(ids: Iterable<Long>)

@ -42,10 +42,12 @@ class WorkManagerImpl constructor(
private val alarmManager: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
private val workManager = androidx.work.WorkManager.getInstance(context)
override fun afterSave(current: Task, original: Task?) {
override fun afterComplete(current: Task, original: Task?) {
workManager.enqueue(
OneTimeWorkRequest.Builder(AfterSaveWork::class.java)
.setInputData(AfterSaveWork.getInputData(current, original))
.setInputData(Data.Builder()
.putLong(AfterSaveWork.EXTRA_ID, current.id)
.build())
.build())
}

@ -1,5 +1,7 @@
package org.tasks.sync
import com.todoroo.astrid.data.SyncFlags
import com.todoroo.astrid.data.Task
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import org.tasks.data.CaldavDao
@ -12,6 +14,20 @@ class SyncAdapters @Inject constructor(
private val caldavDao: CaldavDao,
private val googleTaskListDao: GoogleTaskListDao) {
suspend fun sync(task: Task, original: Task?) {
val suppress = task.checkTransitory(SyncFlags.SUPPRESS_SYNC)
val forceCaldav = task.checkTransitory(SyncFlags.FORCE_CALDAV_SYNC)
val pushGtasks = !suppress
&& !task.googleTaskUpToDate(original)
&& isGoogleTaskSyncEnabled()
val pushCaldav = !suppress
&& (forceCaldav || !task.caldavUpToDate(original))
&& isCaldavSyncEnabled()
if (pushGtasks || pushCaldav) {
sync()
}
}
suspend fun sync() {
sync(false)
}

Loading…
Cancel
Save