From 968ec62acb1744d7945b8b29f25637f33f605b9f Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 29 Jun 2020 17:17:24 -0500 Subject: [PATCH] Make hasChanges and apply suspending --- .../todoroo/astrid/activity/TaskEditFragment.java | 6 +++--- .../com/todoroo/astrid/files/FilesControlSet.kt | 2 +- .../todoroo/astrid/repeats/RepeatControlSet.kt | 4 ++-- .../com/todoroo/astrid/tags/TagsControlSet.kt | 4 ++-- .../com/todoroo/astrid/timers/TimerControlSet.kt | 4 ++-- .../com/todoroo/astrid/ui/HideUntilControlSet.kt | 4 ++-- .../com/todoroo/astrid/ui/ReminderControlSet.kt | 4 ++-- .../org/tasks/fragments/CommentBarFragment.kt | 2 +- .../main/java/org/tasks/ui/CalendarControlSet.kt | 4 ++-- .../main/java/org/tasks/ui/DeadlineControlSet.kt | 4 ++-- .../java/org/tasks/ui/DescriptionControlSet.kt | 4 ++-- app/src/main/java/org/tasks/ui/ListFragment.kt | 4 ++-- .../main/java/org/tasks/ui/LocationControlSet.kt | 4 ++-- .../main/java/org/tasks/ui/PriorityControlSet.kt | 4 ++-- .../main/java/org/tasks/ui/SubtaskControlSet.kt | 4 ++-- .../java/org/tasks/ui/TaskEditControlFragment.kt | 15 +++++++++++++-- 16 files changed, 42 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java b/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java index c1729a5c7..a6041d566 100755 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java @@ -302,7 +302,7 @@ public final class TaskEditFragment extends Fragment } for (TaskEditControlFragment fragment : filter(fragments, not(TaskEditControlFragment::requiresId))) { - fragment.apply(model); + fragment.applyBlocking(model); } Completable.fromAction( @@ -315,7 +315,7 @@ public final class TaskEditFragment extends Fragment for (TaskEditControlFragment fragment : filter(fragments, TaskEditControlFragment::requiresId)) { - fragment.apply(model); + fragment.applyBlocking(model); } taskDao.save(model, null); @@ -375,7 +375,7 @@ public final class TaskEditFragment extends Fragment try { for (TaskEditControlFragment fragment : fragments) { - if (fragment.hasChanges(model)) { + if (fragment.hasChangesBlocking(model)) { return true; } } diff --git a/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.kt b/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.kt index b199da153..5be318117 100644 --- a/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/files/FilesControlSet.kt @@ -75,7 +75,7 @@ class FilesControlSet : TaskEditControlFragment() { override fun controlId() = TAG - override fun apply(task: Task) {} + override suspend fun apply(task: Task) {} override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (requestCode == AddAttachmentDialog.REQUEST_CAMERA || requestCode == AddAttachmentDialog.REQUEST_AUDIO) { diff --git a/app/src/main/java/com/todoroo/astrid/repeats/RepeatControlSet.kt b/app/src/main/java/com/todoroo/astrid/repeats/RepeatControlSet.kt index 54cd44f7a..6b1c55802 100644 --- a/app/src/main/java/com/todoroo/astrid/repeats/RepeatControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/repeats/RepeatControlSet.kt @@ -174,13 +174,13 @@ class RepeatControlSet : TaskEditControlFragment() { override fun controlId() = TAG - override fun hasChanges(original: Task): Boolean { + override suspend fun hasChanges(original: Task): Boolean { val repeatUntil = rrule?.let { DateTime.from(it.until).millis } ?: 0 return recurrenceValue != original.recurrence.orEmpty() || original.repeatUntil != repeatUntil } - override fun apply(task: Task) { + override suspend fun apply(task: Task) { task.repeatUntil = if (rrule == null) 0 else DateTime.from(rrule!!.until).millis task.recurrence = recurrenceValue } diff --git a/app/src/main/java/com/todoroo/astrid/tags/TagsControlSet.kt b/app/src/main/java/com/todoroo/astrid/tags/TagsControlSet.kt index 42902a53d..2a0b85c3a 100644 --- a/app/src/main/java/com/todoroo/astrid/tags/TagsControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/tags/TagsControlSet.kt @@ -74,7 +74,7 @@ class TagsControlSet : TaskEditControlFragment() { override val layout: Int get() = R.layout.control_set_tags - override fun apply(task: Task) { + override suspend fun apply(task: Task) { if (tagDao.applyTags(task, tagDataDao, selectedTags)) { task.modificationDate = DateUtilities.now() } @@ -94,7 +94,7 @@ class TagsControlSet : TaskEditControlFragment() { override fun controlId() = TAG - override fun hasChanges(original: Task): Boolean { + override suspend fun hasChanges(original: Task): Boolean { return HashSet(originalTags) != HashSet(selectedTags) } diff --git a/app/src/main/java/com/todoroo/astrid/timers/TimerControlSet.kt b/app/src/main/java/com/todoroo/astrid/timers/TimerControlSet.kt index 7120f2f6e..087b4c17b 100644 --- a/app/src/main/java/com/todoroo/astrid/timers/TimerControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/timers/TimerControlSet.kt @@ -136,12 +136,12 @@ class TimerControlSet : TaskEditControlFragment() { override fun controlId() = TAG - override fun hasChanges(original: Task): Boolean { + override suspend fun hasChanges(original: Task): Boolean { return (elapsed.timeDurationInSeconds != original.elapsedSeconds || estimated.timeDurationInSeconds != original.estimatedSeconds) } - override fun apply(task: Task) { + override suspend fun apply(task: Task) { task.elapsedSeconds = elapsed.timeDurationInSeconds task.estimatedSeconds = estimated.timeDurationInSeconds } diff --git a/app/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.kt b/app/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.kt index d0074f680..eeb6290b1 100644 --- a/app/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.kt @@ -162,11 +162,11 @@ class HideUntilControlSet : TaskEditControlFragment(), OnItemSelectedListener { } } - override fun apply(task: Task) { + override suspend fun apply(task: Task) { task.hideUntil = getHideUntil(task) } - override fun hasChanges(original: Task): Boolean { + override suspend fun hasChanges(original: Task): Boolean { return original.hideUntil != getHideUntil(original) } diff --git a/app/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.kt b/app/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.kt index 7e880f6de..20c086fa9 100644 --- a/app/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.kt @@ -167,13 +167,13 @@ class ReminderControlSet : TaskEditControlFragment() { } } - override fun hasChanges(original: Task): Boolean { + override suspend fun hasChanges(original: Task): Boolean { return getFlags() != original.reminderFlags || randomReminderPeriod != original.reminderPeriod || HashSet(currentAlarms()) != alarms } override fun requiresId() = true - override fun apply(task: Task) { + override suspend fun apply(task: Task) { task.reminderFlags = getFlags() task.reminderPeriod = randomReminderPeriod if (alarmService.synchronizeAlarms(task.id, alarms)) { diff --git a/app/src/main/java/org/tasks/fragments/CommentBarFragment.kt b/app/src/main/java/org/tasks/fragments/CommentBarFragment.kt index 17c6f981d..d9d2c7fab 100644 --- a/app/src/main/java/org/tasks/fragments/CommentBarFragment.kt +++ b/app/src/main/java/org/tasks/fragments/CommentBarFragment.kt @@ -88,7 +88,7 @@ class CommentBarFragment : TaskEditControlFragment() { override fun controlId() = TAG - override fun apply(task: Task) {} + override suspend fun apply(task: Task) {} @OnTextChanged(R.id.commentField) fun onTextChanged(s: CharSequence) { diff --git a/app/src/main/java/org/tasks/ui/CalendarControlSet.kt b/app/src/main/java/org/tasks/ui/CalendarControlSet.kt index 71561e020..9427b2d25 100644 --- a/app/src/main/java/org/tasks/ui/CalendarControlSet.kt +++ b/app/src/main/java/org/tasks/ui/CalendarControlSet.kt @@ -92,7 +92,7 @@ class CalendarControlSet : TaskEditControlFragment() { override fun controlId() = TAG - override fun hasChanges(original: Task): Boolean { + override suspend fun hasChanges(original: Task): Boolean { if (!permissionChecker.canAccessCalendars()) { return false } @@ -105,7 +105,7 @@ class CalendarControlSet : TaskEditControlFragment() { } else originalUri != eventUri } - override fun apply(task: Task) { + override suspend fun apply(task: Task) { if (!permissionChecker.canAccessCalendars()) { return } diff --git a/app/src/main/java/org/tasks/ui/DeadlineControlSet.kt b/app/src/main/java/org/tasks/ui/DeadlineControlSet.kt index 849889926..ec5612862 100644 --- a/app/src/main/java/org/tasks/ui/DeadlineControlSet.kt +++ b/app/src/main/java/org/tasks/ui/DeadlineControlSet.kt @@ -71,11 +71,11 @@ class DeadlineControlSet : TaskEditControlFragment() { override fun controlId() = TAG - override fun hasChanges(original: Task): Boolean { + override suspend fun hasChanges(original: Task): Boolean { return original.dueDate != dueDateTime } - override fun apply(task: Task) { + override suspend fun apply(task: Task) { val dueDate = dueDateTime if (dueDate != task.dueDate) { task.reminderSnooze = 0L diff --git a/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt b/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt index 7014ca068..8d7837af4 100644 --- a/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt +++ b/app/src/main/java/org/tasks/ui/DescriptionControlSet.kt @@ -60,11 +60,11 @@ class DescriptionControlSet : TaskEditControlFragment() { description = text.toString().trim { it <= ' ' } } - override fun apply(task: Task) { + override suspend fun apply(task: Task) { task.notes = description } - override fun hasChanges(original: Task): Boolean { + override suspend fun hasChanges(original: Task): Boolean { return !if (isNullOrEmpty(description)) isNullOrEmpty(original.notes) else description == stripCarriageReturns(original.notes) } diff --git a/app/src/main/java/org/tasks/ui/ListFragment.kt b/app/src/main/java/org/tasks/ui/ListFragment.kt index 16f2cd960..681503390 100644 --- a/app/src/main/java/org/tasks/ui/ListFragment.kt +++ b/app/src/main/java/org/tasks/ui/ListFragment.kt @@ -119,14 +119,14 @@ class ListFragment : TaskEditControlFragment() { override fun requiresId() = true - override fun apply(task: Task) { + override suspend fun apply(task: Task) { if (isNew || hasChanges()) { task.parent = 0 taskMover.move(listOf(task.id), selectedList) } } - override fun hasChanges(original: Task) = hasChanges() + override suspend fun hasChanges(original: Task) = hasChanges() private fun hasChanges() = selectedList != originalList diff --git a/app/src/main/java/org/tasks/ui/LocationControlSet.kt b/app/src/main/java/org/tasks/ui/LocationControlSet.kt index 0c6cdddc6..133e57cda 100644 --- a/app/src/main/java/org/tasks/ui/LocationControlSet.kt +++ b/app/src/main/java/org/tasks/ui/LocationControlSet.kt @@ -202,7 +202,7 @@ class LocationControlSet : TaskEditControlFragment() { startActivity(Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + location!!.phone))) } - override fun hasChanges(task: Task): Boolean { + override suspend fun hasChanges(task: Task): Boolean { if (original == null) { return location != null } @@ -220,7 +220,7 @@ class LocationControlSet : TaskEditControlFragment() { override fun requiresId() = true - override fun apply(task: Task) { + override suspend fun apply(task: Task) { if (original == null || location == null || original!!.place != location!!.place) { task.putTransitory(SyncFlags.FORCE_CALDAV_SYNC, true) } diff --git a/app/src/main/java/org/tasks/ui/PriorityControlSet.kt b/app/src/main/java/org/tasks/ui/PriorityControlSet.kt index 275fe447e..e06bd6997 100644 --- a/app/src/main/java/org/tasks/ui/PriorityControlSet.kt +++ b/app/src/main/java/org/tasks/ui/PriorityControlSet.kt @@ -68,11 +68,11 @@ class PriorityControlSet : TaskEditControlFragment() { override fun controlId() = TAG - override fun apply(task: Task) { + override suspend fun apply(task: Task) { task.priority = priority } - override fun hasChanges(original: Task): Boolean { + override suspend fun hasChanges(original: Task): Boolean { return original.priority != priority } diff --git a/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt b/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt index a3cfe4153..17ae41f14 100644 --- a/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt +++ b/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt @@ -109,7 +109,7 @@ class SubtaskControlSet : TaskEditControlFragment(), SubtaskViewHolder.Callbacks override fun requiresId() = true - override fun apply(task: Task) { + override suspend fun apply(task: Task) { for (subtask in newSubtasks) { if (isNullOrEmpty(subtask.title)) { continue @@ -138,7 +138,7 @@ class SubtaskControlSet : TaskEditControlFragment(), SubtaskViewHolder.Callbacks } } - override fun hasChanges(original: Task): Boolean { + override suspend fun hasChanges(original: Task): Boolean { return newSubtasks.isNotEmpty() } diff --git a/app/src/main/java/org/tasks/ui/TaskEditControlFragment.kt b/app/src/main/java/org/tasks/ui/TaskEditControlFragment.kt index f55634ba7..c427f016b 100644 --- a/app/src/main/java/org/tasks/ui/TaskEditControlFragment.kt +++ b/app/src/main/java/org/tasks/ui/TaskEditControlFragment.kt @@ -10,6 +10,7 @@ import android.widget.LinearLayout import androidx.fragment.app.Fragment import butterknife.ButterKnife import com.todoroo.astrid.data.Task +import kotlinx.coroutines.runBlocking import org.tasks.R abstract class TaskEditControlFragment : Fragment() { @@ -50,9 +51,19 @@ abstract class TaskEditControlFragment : Fragment() { return false } - abstract fun apply(task: Task) + @Deprecated("use coroutines") + fun applyBlocking(task: Task) = runBlocking { + apply(task) + } + + abstract suspend fun apply(task: Task) + + @Deprecated("use coroutines") + fun hasChangesBlocking(original: Task) = runBlocking { + hasChanges(original) + } - open fun hasChanges(original: Task): Boolean { + open suspend fun hasChanges(original: Task): Boolean { return false }