mirror of https://github.com/tasks/tasks
Convert TaskCompleter to Kotlin
parent
bd903357ae
commit
22b809a420
@ -1,58 +0,0 @@
|
||||
package com.todoroo.astrid.service;
|
||||
|
||||
import static com.google.common.collect.Lists.newArrayList;
|
||||
import static com.todoroo.andlib.utility.DateUtilities.now;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.todoroo.astrid.dao.TaskDao;
|
||||
import com.todoroo.astrid.data.Task;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import org.tasks.data.GoogleTaskDao;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class TaskCompleter {
|
||||
|
||||
private final TaskDao taskDao;
|
||||
private final GoogleTaskDao googleTaskDao;
|
||||
|
||||
@Inject
|
||||
TaskCompleter(TaskDao taskDao, GoogleTaskDao googleTaskDao) {
|
||||
this.taskDao = taskDao;
|
||||
this.googleTaskDao = googleTaskDao;
|
||||
}
|
||||
|
||||
public void setComplete(long taskId) {
|
||||
Task task = taskDao.fetch(taskId);
|
||||
if (task != null) {
|
||||
setComplete(task, true);
|
||||
} else {
|
||||
Timber.e("Could not find task with id %s", taskId);
|
||||
}
|
||||
}
|
||||
|
||||
public void setComplete(Task item, boolean completed) {
|
||||
long completionDate = completed ? now() : 0L;
|
||||
setComplete(Collections.singletonList(item), completionDate);
|
||||
List<Task> tasks = new ArrayList<>(googleTaskDao.getChildTasks(item.getId()));
|
||||
List<Long> caldavChildren = taskDao.getChildren(item.getId());
|
||||
if (!caldavChildren.isEmpty()) {
|
||||
tasks.addAll(taskDao.fetch(caldavChildren));
|
||||
}
|
||||
setComplete(
|
||||
newArrayList(Iterables.filter(tasks, t -> t.isCompleted() != completed)), completionDate);
|
||||
}
|
||||
|
||||
private void setComplete(List<Task> tasks, long completionDate) {
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
Task task = tasks.get(i);
|
||||
task.setCompletionDate(completionDate);
|
||||
if (i < tasks.size() - 1) {
|
||||
task.suppressRefresh();
|
||||
}
|
||||
taskDao.save(task);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
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) {
|
||||
|
||||
fun setComplete(taskId: Long) =
|
||||
taskDao.fetch(taskId)?.let { setComplete(it, true) }
|
||||
?: Timber.e("Could not find task $taskId")
|
||||
|
||||
fun setComplete(item: Task, completed: Boolean) {
|
||||
val completionDate = if (completed) DateUtilities.now() else 0L
|
||||
setComplete(listOf(item), completionDate)
|
||||
val tasks = googleTaskDao.getChildTasks(item.id)
|
||||
.plus(taskDao.getChildren(item.id).takeIf { it.isNotEmpty() }?.let(taskDao::fetch)
|
||||
?: emptyList())
|
||||
.filter { it.isCompleted != completed }
|
||||
setComplete(tasks, completionDate)
|
||||
}
|
||||
|
||||
private fun setComplete(tasks: List<Task>, completionDate: Long) {
|
||||
tasks.forEachIndexed { i, task ->
|
||||
task.completionDate = completionDate
|
||||
if (i < tasks.size - 1) {
|
||||
task.suppressRefresh()
|
||||
}
|
||||
taskDao.save(task)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue