Use coroutines in timer plugin

pull/1055/head
Alex Baker 4 years ago
parent 4bd59b75ba
commit eb70cd86f8

@ -402,11 +402,11 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
val taskEditFragment: TaskEditFragment? val taskEditFragment: TaskEditFragment?
get() = supportFragmentManager.findFragmentByTag(TaskEditFragment.TAG_TASKEDIT_FRAGMENT) as TaskEditFragment? get() = supportFragmentManager.findFragmentByTag(TaskEditFragment.TAG_TASKEDIT_FRAGMENT) as TaskEditFragment?
override fun stopTimer(): Task { override suspend fun stopTimer(): Task {
return taskEditFragment!!.stopTimer() return taskEditFragment!!.stopTimer()
} }
override fun startTimer(): Task { override suspend fun startTimer(): Task {
return taskEditFragment!!.startTimer() return taskEditFragment!!.startTimer()
} }

@ -225,7 +225,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
return false return false
} }
fun stopTimer(): Task { suspend fun stopTimer(): Task {
val model = editViewModel.task!! val model = editViewModel.task!!
timerPlugin.stopTimer(model) timerPlugin.stopTimer(model)
val elapsedTime = DateUtils.formatElapsedTime(model.elapsedSeconds.toLong()) val elapsedTime = DateUtils.formatElapsedTime(model.elapsedSeconds.toLong())
@ -239,7 +239,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
return model return model
} }
fun startTimer(): Task { suspend fun startTimer(): Task {
val model = editViewModel.task!! val model = editViewModel.task!!
timerPlugin.startTimer(model) timerPlugin.startTimer(model)
addComment(String.format( addComment(String.format(
@ -295,10 +295,10 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
dialogBuilder dialogBuilder
.newDialog(R.string.discard_confirmation) .newDialog(R.string.discard_confirmation)
.setPositiveButton(R.string.keep_editing, null) .setPositiveButton(R.string.keep_editing, null)
.setNegativeButton(R.string.discard) { _, _ -> editViewModel.discard() } .setNegativeButton(R.string.discard) { _, _ -> discard() }
.show() .show()
} else { } else {
editViewModel.discard() discard()
} }
} }
@ -310,6 +310,10 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
.show() .show()
} }
private fun discard() = lifecycleScope.launch {
editViewModel.discard()
}
private fun delete() = lifecycleScope.launch { private fun delete() = lifecycleScope.launch {
editViewModel.delete() editViewModel.delete()
} }

@ -16,12 +16,14 @@ import android.widget.Chronometer.OnChronometerTickListener
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import butterknife.BindView import butterknife.BindView
import butterknife.OnClick import butterknife.OnClick
import com.todoroo.andlib.utility.DateUtilities import com.todoroo.andlib.utility.DateUtilities
import com.todoroo.astrid.data.Task import com.todoroo.astrid.data.Task
import com.todoroo.astrid.ui.TimeDurationControlSet import com.todoroo.astrid.ui.TimeDurationControlSet
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.tasks.R import org.tasks.R
import org.tasks.Strings.isNullOrEmpty import org.tasks.Strings.isNullOrEmpty
import org.tasks.dialogs.DialogBuilder import org.tasks.dialogs.DialogBuilder
@ -87,6 +89,7 @@ class TimerControlSet : TaskEditControlFragment() {
@OnClick(R.id.timer_container) @OnClick(R.id.timer_container)
fun timerClicked() { fun timerClicked() {
lifecycleScope.launch {
if (timerActive()) { if (timerActive()) {
val task = callback.stopTimer() val task = callback.stopTimer()
elapsed.setTimeDuration(task.elapsedSeconds) elapsed.setTimeDuration(task.elapsedSeconds)
@ -100,6 +103,7 @@ class TimerControlSet : TaskEditControlFragment() {
} }
updateChronometer() updateChronometer()
} }
}
override val layout = R.layout.control_set_timers override val layout = R.layout.control_set_timers
@ -166,8 +170,8 @@ class TimerControlSet : TaskEditControlFragment() {
private fun timerActive() = viewModel.timerStarted > 0 private fun timerActive() = viewModel.timerStarted > 0
interface TimerControlSetCallback { interface TimerControlSetCallback {
fun stopTimer(): Task suspend fun stopTimer(): Task
fun startTimer(): Task suspend fun startTimer(): Task
} }
companion object { companion object {

@ -12,12 +12,10 @@ import com.todoroo.andlib.sql.Criterion.Companion.and
import com.todoroo.andlib.sql.QueryTemplate import com.todoroo.andlib.sql.QueryTemplate
import com.todoroo.andlib.utility.DateUtilities import com.todoroo.andlib.utility.DateUtilities
import com.todoroo.astrid.api.Filter 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.data.Task
import com.todoroo.astrid.utility.Constants import com.todoroo.astrid.utility.Constants
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import io.reactivex.Completable
import io.reactivex.schedulers.Schedulers
import org.tasks.R import org.tasks.R
import org.tasks.intents.TaskIntents import org.tasks.intents.TaskIntents
import org.tasks.notifications.NotificationManager import org.tasks.notifications.NotificationManager
@ -27,12 +25,12 @@ import javax.inject.Inject
class TimerPlugin @Inject constructor( class TimerPlugin @Inject constructor(
@param:ApplicationContext private val context: Context, @param:ApplicationContext private val context: Context,
private val notificationManager: NotificationManager, private val notificationManager: NotificationManager,
private val taskDao: TaskDaoBlocking) { private val taskDao: TaskDao) {
fun startTimer(task: Task?) { suspend fun startTimer(task: Task?) {
updateTimer(task, true) updateTimer(task, true)
} }
fun stopTimer(task: Task?) { suspend fun stopTimer(task: Task?) {
updateTimer(task, false) updateTimer(task, false)
} }
@ -41,7 +39,7 @@ class TimerPlugin @Inject constructor(
* *
* @param start if true, start timer. else, stop it * @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) { if (task == null) {
return return
} }
@ -56,15 +54,11 @@ class TimerPlugin @Inject constructor(
task.elapsedSeconds = task.elapsedSeconds + newElapsed task.elapsedSeconds = task.elapsedSeconds + newElapsed
} }
} }
Completable.fromAction {
taskDao.save(task) taskDao.save(task)
updateNotifications() updateNotifications()
} }
.subscribeOn(Schedulers.io())
.subscribe()
}
fun updateNotifications() { suspend fun updateNotifications() {
val count = taskDao.activeTimers() val count = taskDao.activeTimers()
if (count == 0) { if (count == 0) {
notificationManager.cancel(Constants.NOTIFICATION_TIMER.toLong()) notificationManager.cancel(Constants.NOTIFICATION_TIMER.toLong())

@ -427,7 +427,7 @@ class TaskEditViewModel @ViewModelInject constructor(
discard() discard()
} }
fun discard() { suspend fun discard() {
task?.let { task?.let {
if (it.isNew) { if (it.isNew) {
timerPlugin.stopTimer(it) timerPlugin.stopTimer(it)

Loading…
Cancel
Save