From eb70cd86f80397d9ca2e6dc9d0fa570d1ec988cd Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 28 Jul 2020 09:17:47 -0500 Subject: [PATCH] Use coroutines in timer plugin --- .../todoroo/astrid/activity/MainActivity.kt | 4 +-- .../astrid/activity/TaskEditFragment.kt | 12 +++++--- .../todoroo/astrid/timers/TimerControlSet.kt | 30 +++++++++++-------- .../com/todoroo/astrid/timers/TimerPlugin.kt | 22 +++++--------- .../java/org/tasks/ui/TaskEditViewModel.kt | 2 +- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt index 030dc7c55..e6dd08f46 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt @@ -402,11 +402,11 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl val taskEditFragment: TaskEditFragment? get() = supportFragmentManager.findFragmentByTag(TaskEditFragment.TAG_TASKEDIT_FRAGMENT) as TaskEditFragment? - override fun stopTimer(): Task { + override suspend fun stopTimer(): Task { return taskEditFragment!!.stopTimer() } - override fun startTimer(): Task { + override suspend fun startTimer(): Task { return taskEditFragment!!.startTimer() } diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt index 0aed2c860..063efe0ab 100755 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt @@ -225,7 +225,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener { return false } - fun stopTimer(): Task { + suspend fun stopTimer(): Task { val model = editViewModel.task!! timerPlugin.stopTimer(model) val elapsedTime = DateUtils.formatElapsedTime(model.elapsedSeconds.toLong()) @@ -239,7 +239,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener { return model } - fun startTimer(): Task { + suspend fun startTimer(): Task { val model = editViewModel.task!! timerPlugin.startTimer(model) addComment(String.format( @@ -295,10 +295,10 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener { dialogBuilder .newDialog(R.string.discard_confirmation) .setPositiveButton(R.string.keep_editing, null) - .setNegativeButton(R.string.discard) { _, _ -> editViewModel.discard() } + .setNegativeButton(R.string.discard) { _, _ -> discard() } .show() } else { - editViewModel.discard() + discard() } } @@ -310,6 +310,10 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener { .show() } + private fun discard() = lifecycleScope.launch { + editViewModel.discard() + } + private fun delete() = lifecycleScope.launch { editViewModel.delete() } 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 5a32c973b..258a4011a 100644 --- a/app/src/main/java/com/todoroo/astrid/timers/TimerControlSet.kt +++ b/app/src/main/java/com/todoroo/astrid/timers/TimerControlSet.kt @@ -16,12 +16,14 @@ import android.widget.Chronometer.OnChronometerTickListener import android.widget.ImageView import android.widget.TextView import androidx.appcompat.app.AlertDialog +import androidx.lifecycle.lifecycleScope import butterknife.BindView import butterknife.OnClick import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.data.Task import com.todoroo.astrid.ui.TimeDurationControlSet import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import org.tasks.R import org.tasks.Strings.isNullOrEmpty import org.tasks.dialogs.DialogBuilder @@ -87,18 +89,20 @@ class TimerControlSet : TaskEditControlFragment() { @OnClick(R.id.timer_container) fun timerClicked() { - if (timerActive()) { - val task = callback.stopTimer() - elapsed.setTimeDuration(task.elapsedSeconds) - viewModel.timerStarted = 0 - chronometer.stop() - refreshDisplayView() - } else { - val task = callback.startTimer() - viewModel.timerStarted = task.timerStart - chronometer.start() + lifecycleScope.launch { + if (timerActive()) { + val task = callback.stopTimer() + elapsed.setTimeDuration(task.elapsedSeconds) + viewModel.timerStarted = 0 + chronometer.stop() + refreshDisplayView() + } else { + val task = callback.startTimer() + viewModel.timerStarted = task.timerStart + chronometer.start() + } + updateChronometer() } - updateChronometer() } override val layout = R.layout.control_set_timers @@ -166,8 +170,8 @@ class TimerControlSet : TaskEditControlFragment() { private fun timerActive() = viewModel.timerStarted > 0 interface TimerControlSetCallback { - fun stopTimer(): Task - fun startTimer(): Task + suspend fun stopTimer(): Task + suspend fun startTimer(): Task } companion object { diff --git a/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.kt b/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.kt index c72576f8b..526720f9b 100644 --- a/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.kt +++ b/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.kt @@ -12,12 +12,10 @@ import com.todoroo.andlib.sql.Criterion.Companion.and import com.todoroo.andlib.sql.QueryTemplate import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.api.Filter -import com.todoroo.astrid.dao.TaskDaoBlocking +import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.data.Task import com.todoroo.astrid.utility.Constants import dagger.hilt.android.qualifiers.ApplicationContext -import io.reactivex.Completable -import io.reactivex.schedulers.Schedulers import org.tasks.R import org.tasks.intents.TaskIntents import org.tasks.notifications.NotificationManager @@ -27,12 +25,12 @@ import javax.inject.Inject class TimerPlugin @Inject constructor( @param:ApplicationContext private val context: Context, private val notificationManager: NotificationManager, - private val taskDao: TaskDaoBlocking) { - fun startTimer(task: Task?) { + private val taskDao: TaskDao) { + suspend fun startTimer(task: Task?) { updateTimer(task, true) } - fun stopTimer(task: Task?) { + suspend fun stopTimer(task: Task?) { updateTimer(task, false) } @@ -41,7 +39,7 @@ class TimerPlugin @Inject constructor( * * @param start if true, start timer. else, stop it */ - private fun updateTimer(task: Task?, start: Boolean) { + private suspend fun updateTimer(task: Task?, start: Boolean) { if (task == null) { return } @@ -56,15 +54,11 @@ class TimerPlugin @Inject constructor( task.elapsedSeconds = task.elapsedSeconds + newElapsed } } - Completable.fromAction { - taskDao.save(task) - updateNotifications() - } - .subscribeOn(Schedulers.io()) - .subscribe() + taskDao.save(task) + updateNotifications() } - fun updateNotifications() { + suspend fun updateNotifications() { val count = taskDao.activeTimers() if (count == 0) { notificationManager.cancel(Constants.NOTIFICATION_TIMER.toLong()) diff --git a/app/src/main/java/org/tasks/ui/TaskEditViewModel.kt b/app/src/main/java/org/tasks/ui/TaskEditViewModel.kt index a950948f8..95a269ab7 100644 --- a/app/src/main/java/org/tasks/ui/TaskEditViewModel.kt +++ b/app/src/main/java/org/tasks/ui/TaskEditViewModel.kt @@ -427,7 +427,7 @@ class TaskEditViewModel @ViewModelInject constructor( discard() } - fun discard() { + suspend fun discard() { task?.let { if (it.isNew) { timerPlugin.stopTimer(it)