You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tasks/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.kt

47 lines
1.6 KiB
Kotlin

package com.todoroo.astrid.service
import com.todoroo.andlib.utility.DateUtilities
import com.todoroo.astrid.dao.TaskDao
import com.todoroo.astrid.data.Task
import org.tasks.data.GoogleTaskDao
import timber.log.Timber
import javax.inject.Inject
class TaskCompleter @Inject internal constructor(
private val taskDao: TaskDao,
private val googleTaskDao: GoogleTaskDao) {
suspend fun setComplete(taskId: Long) =
taskDao
.fetch(taskId)
?.let { setComplete(it, true) }
?: Timber.e("Could not find task $taskId")
suspend fun setComplete(item: Task, completed: Boolean) {
val completionDate = if (completed) DateUtilities.now() else 0L
completeChildren(item.id, completionDate)
setComplete(listOf(item), completionDate)
}
suspend fun completeChildren(id: Long, completionDate: Long) {
googleTaskDao
.getChildTasks(id)
.plus(taskDao.getChildren(id)
.takeIf { it.isNotEmpty() }
?.let { taskDao.fetch(it) }
?: emptyList()
)
.filter { it.isCompleted != completionDate > 0 }
.let { setComplete(it, completionDate)}
}
private suspend fun setComplete(tasks: List<Task>, completionDate: Long) {
tasks.forEachIndexed { i, task ->
task.completionDate = completionDate
if (i < tasks.size - 1) {
task.suppressRefresh()
}
taskDao.save(task)
}
}
}