From 41f7d1da6b50bd928717422ccae16053071ed0b8 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 28 Jul 2020 12:22:03 -0500 Subject: [PATCH] Convert TaskDuplicator to Kotlin --- .../astrid/activity/TaskListFragment.kt | 6 +- .../astrid/service/TaskDuplicator.java | 117 ------------------ .../todoroo/astrid/service/TaskDuplicator.kt | 70 +++++++++++ 3 files changed, 74 insertions(+), 119 deletions(-) delete mode 100644 app/src/main/java/com/todoroo/astrid/service/TaskDuplicator.java create mode 100644 app/src/main/java/com/todoroo/astrid/service/TaskDuplicator.kt diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt index 43752b622..d7bc9b77b 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt @@ -748,9 +748,11 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL makeSnackbar(R.string.delete_multiple_tasks_confirmation, result.size.toString()).show() } - private fun copySelectedItems(tasks: List) { + private fun copySelectedItems(tasks: List) = lifecycleScope.launch { finishActionMode() - val duplicates = taskDuplicator.duplicate(tasks) + val duplicates = withContext(NonCancellable) { + taskDuplicator.duplicate(tasks) + } onTaskCreated(duplicates) makeSnackbar(R.string.copy_multiple_tasks_confirmation, duplicates.size.toString()).show() } diff --git a/app/src/main/java/com/todoroo/astrid/service/TaskDuplicator.java b/app/src/main/java/com/todoroo/astrid/service/TaskDuplicator.java deleted file mode 100644 index d71eafcc4..000000000 --- a/app/src/main/java/com/todoroo/astrid/service/TaskDuplicator.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.todoroo.astrid.service; - -import static com.google.common.collect.Lists.transform; -import static com.todoroo.andlib.utility.DateUtilities.now; -import static com.todoroo.astrid.data.Task.NO_UUID; - -import com.todoroo.astrid.dao.TaskDaoBlocking; -import com.todoroo.astrid.data.Task; -import com.todoroo.astrid.gcal.GCalHelper; -import java.util.ArrayList; -import java.util.List; -import javax.inject.Inject; -import org.tasks.LocalBroadcastManager; -import org.tasks.data.Alarm; -import org.tasks.data.AlarmDaoBlocking; -import org.tasks.data.CaldavDaoBlocking; -import org.tasks.data.CaldavTask; -import org.tasks.data.Geofence; -import org.tasks.data.GoogleTask; -import org.tasks.data.GoogleTaskDaoBlocking; -import org.tasks.data.LocationDaoBlocking; -import org.tasks.data.Tag; -import org.tasks.data.TagDaoBlocking; -import org.tasks.data.TagData; -import org.tasks.data.TagDataDaoBlocking; -import org.tasks.preferences.Preferences; - -public class TaskDuplicator { - - private final GCalHelper gcalHelper; - private final TaskDaoBlocking taskDao; - private final TagDaoBlocking tagDao; - private final TagDataDaoBlocking tagDataDao; - private final GoogleTaskDaoBlocking googleTaskDao; - private final CaldavDaoBlocking caldavDao; - private final LocationDaoBlocking locationDao; - private final AlarmDaoBlocking alarmDao; - private final Preferences preferences; - private final LocalBroadcastManager localBroadcastManager; - - @Inject - public TaskDuplicator( - GCalHelper gcalHelper, - TaskDaoBlocking taskDao, - LocalBroadcastManager localBroadcastManager, - TagDaoBlocking tagDao, - TagDataDaoBlocking tagDataDao, - GoogleTaskDaoBlocking googleTaskDao, - CaldavDaoBlocking caldavDao, - LocationDaoBlocking locationDao, - AlarmDaoBlocking alarmDao, - Preferences preferences) { - this.gcalHelper = gcalHelper; - this.taskDao = taskDao; - this.localBroadcastManager = localBroadcastManager; - this.tagDao = tagDao; - this.tagDataDao = tagDataDao; - this.googleTaskDao = googleTaskDao; - this.caldavDao = caldavDao; - this.locationDao = locationDao; - this.alarmDao = alarmDao; - this.preferences = preferences; - } - - public List duplicate(List taskIds) { - List result = new ArrayList<>(); - for (Task task : taskDao.fetch(taskIds)) { - result.add(clone(task)); - } - localBroadcastManager.broadcastRefresh(); - return result; - } - - private Task clone(Task clone) { - long originalId = clone.getId(); - clone.setCreationDate(now()); - clone.setModificationDate(now()); - clone.setCompletionDate(0L); - clone.setCalendarURI(""); - clone.setUuid(NO_UUID); - clone.suppressSync(); - clone.suppressRefresh(); - taskDao.createNew(clone); - - List tags = tagDataDao.getTagDataForTask(originalId); - if (!tags.isEmpty()) { - tagDao.insert(transform(tags, td -> new Tag(clone, td))); - } - - GoogleTask googleTask = googleTaskDao.getByTaskId(originalId); - boolean addToTop = preferences.addTasksToTop(); - if (googleTask != null) { - googleTaskDao.insertAndShift(new GoogleTask(clone.getId(), googleTask.getListId()), addToTop); - } - - CaldavTask caldavTask = caldavDao.getTask(originalId); - if (caldavTask != null) { - caldavDao.insert(clone, new CaldavTask(clone.getId(), caldavTask.getCalendar()), addToTop); - } - - for (Geofence g : locationDao.getGeofencesForTask(originalId)) { - locationDao.insert( - new Geofence(clone.getId(), g.getPlace(), g.isArrival(), g.isDeparture(), g.getRadius())); - } - - List alarms = alarmDao.getAlarms(originalId); - if (!alarms.isEmpty()) { - alarmDao.insert(transform(alarms, a -> new Alarm(clone.getId(), a.getTime()))); - } - - gcalHelper.createTaskEventIfEnabled(clone); - - taskDao.save(clone, null); // TODO: delete me - - return clone; - } -} diff --git a/app/src/main/java/com/todoroo/astrid/service/TaskDuplicator.kt b/app/src/main/java/com/todoroo/astrid/service/TaskDuplicator.kt new file mode 100644 index 000000000..1ef3d21f8 --- /dev/null +++ b/app/src/main/java/com/todoroo/astrid/service/TaskDuplicator.kt @@ -0,0 +1,70 @@ +package com.todoroo.astrid.service + +import com.google.common.collect.Lists +import com.todoroo.andlib.utility.DateUtilities +import com.todoroo.astrid.dao.TaskDao +import com.todoroo.astrid.data.Task +import com.todoroo.astrid.gcal.GCalHelper +import org.tasks.LocalBroadcastManager +import org.tasks.data.* +import org.tasks.preferences.Preferences +import java.util.* +import javax.inject.Inject + +class TaskDuplicator @Inject constructor( + private val gcalHelper: GCalHelper, + private val taskDao: TaskDao, + private val localBroadcastManager: LocalBroadcastManager, + private val tagDao: TagDao, + private val tagDataDao: TagDataDao, + private val googleTaskDao: GoogleTaskDao, + private val caldavDao: CaldavDao, + private val locationDao: LocationDao, + private val alarmDao: AlarmDao, + private val preferences: Preferences) { + + suspend fun duplicate(taskIds: List): List { + val result: MutableList = ArrayList() + for (task in taskDao.fetch(taskIds)) { + result.add(clone(task)) + } + localBroadcastManager.broadcastRefresh() + return result + } + + private suspend fun clone(clone: Task): Task { + val originalId = clone.id + clone.creationDate = DateUtilities.now() + clone.modificationDate = DateUtilities.now() + clone.completionDate = 0L + clone.calendarURI = "" + clone.uuid = Task.NO_UUID + clone.suppressSync() + clone.suppressRefresh() + taskDao.createNew(clone) + val tags = tagDataDao.getTagDataForTask(originalId) + if (tags.isNotEmpty()) { + tagDao.insert(Lists.transform(tags) { td: TagData? -> Tag(clone, td!!) }) + } + val googleTask = googleTaskDao.getByTaskId(originalId) + val addToTop = preferences.addTasksToTop() + if (googleTask != null) { + googleTaskDao.insertAndShift(GoogleTask(clone.id, googleTask.listId!!), addToTop) + } + val caldavTask = caldavDao.getTask(originalId) + if (caldavTask != null) { + caldavDao.insert(clone, CaldavTask(clone.id, caldavTask.calendar), addToTop) + } + for (g in locationDao.getGeofencesForTask(originalId)) { + locationDao.insert( + Geofence(clone.id, g.place, g.isArrival, g.isDeparture, g.radius)) + } + val alarms = alarmDao.getAlarms(originalId) + if (alarms.isNotEmpty()) { + alarmDao.insert(Lists.transform(alarms) { a: Alarm? -> Alarm(clone.id, a!!.time) }) + } + gcalHelper.createTaskEventIfEnabled(clone) + taskDao.save(clone, null) // TODO: delete me + return clone + } +} \ No newline at end of file