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?
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()
}

@ -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()
}

@ -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,6 +89,7 @@ class TimerControlSet : TaskEditControlFragment() {
@OnClick(R.id.timer_container)
fun timerClicked() {
lifecycleScope.launch {
if (timerActive()) {
val task = callback.stopTimer()
elapsed.setTimeDuration(task.elapsedSeconds)
@ -100,6 +103,7 @@ class TimerControlSet : TaskEditControlFragment() {
}
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 {

@ -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()
}
fun updateNotifications() {
suspend fun updateNotifications() {
val count = taskDao.activeTimers()
if (count == 0) {
notificationManager.cancel(Constants.NOTIFICATION_TIMER.toLong())

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

Loading…
Cancel
Save