From e4b2dc932e86f56cff342f42a3011c3d10bedc1a Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 7 Jul 2020 16:51:49 -0500 Subject: [PATCH] Use coroutines in TaskEditActivity and UriHandler --- .../astrid/activity/TaskEditActivity.java | 53 ------------------- .../astrid/activity/TaskEditActivity.kt | 38 +++++++++++++ .../java/org/tasks/activities/UriHandler.kt | 25 +++++---- 3 files changed, 50 insertions(+), 66 deletions(-) delete mode 100644 app/src/main/java/com/todoroo/astrid/activity/TaskEditActivity.java create mode 100644 app/src/main/java/com/todoroo/astrid/activity/TaskEditActivity.kt diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskEditActivity.java b/app/src/main/java/com/todoroo/astrid/activity/TaskEditActivity.java deleted file mode 100644 index 2af461fe0..000000000 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskEditActivity.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.todoroo.astrid.activity; - -import com.todoroo.astrid.dao.TaskDaoBlocking; -import com.todoroo.astrid.service.TaskCreator; -import dagger.hilt.android.AndroidEntryPoint; -import io.reactivex.Single; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.disposables.CompositeDisposable; -import io.reactivex.schedulers.Schedulers; -import javax.inject.Inject; -import org.tasks.injection.InjectingAppCompatActivity; -import org.tasks.intents.TaskIntents; - -@AndroidEntryPoint -public class TaskEditActivity extends InjectingAppCompatActivity { - - private static final String TOKEN_ID = "id"; - - @Inject TaskCreator taskCreator; - @Inject TaskDaoBlocking taskDao; - private CompositeDisposable disposables; - - @Override - protected void onResume() { - super.onResume(); - - long taskId = getIntent().getLongExtra(TOKEN_ID, 0); - - disposables = new CompositeDisposable(); - - if (taskId == 0) { - startActivity(TaskIntents.getEditTaskIntent(this, taskCreator.createWithValues(""))); - finish(); - } else { - disposables.add( - Single.fromCallable(() -> taskDao.fetchBlocking(taskId)) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe( - task -> { - startActivity(TaskIntents.getEditTaskIntent(this, task)); - finish(); - })); - } - } - - @Override - protected void onPause() { - super.onPause(); - - disposables.dispose(); - } -} diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskEditActivity.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskEditActivity.kt new file mode 100644 index 000000000..79a7b5fbd --- /dev/null +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskEditActivity.kt @@ -0,0 +1,38 @@ +package com.todoroo.astrid.activity + +import androidx.lifecycle.lifecycleScope +import com.todoroo.astrid.dao.TaskDao +import com.todoroo.astrid.service.TaskCreator +import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch +import org.tasks.injection.InjectingAppCompatActivity +import org.tasks.intents.TaskIntents +import javax.inject.Inject + +@AndroidEntryPoint +class TaskEditActivity : InjectingAppCompatActivity() { + @Inject lateinit var taskCreator: TaskCreator + @Inject lateinit var taskDao: TaskDao + + override fun onResume() { + super.onResume() + + val taskId = intent.getLongExtra(TOKEN_ID, 0) + if (taskId > 0) { + lifecycleScope.launch { + val task = taskDao.fetch(taskId) + task?.let { + startActivity(TaskIntents.getEditTaskIntent(this@TaskEditActivity, it)) + } + finish() + } + } else { + startActivity(TaskIntents.getEditTaskIntent(this, taskCreator.createWithValues(""))) + finish() + } + } + + companion object { + private const val TOKEN_ID = "id" + } +} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/activities/UriHandler.kt b/app/src/main/java/org/tasks/activities/UriHandler.kt index 58e2c2d93..c0e558879 100644 --- a/app/src/main/java/org/tasks/activities/UriHandler.kt +++ b/app/src/main/java/org/tasks/activities/UriHandler.kt @@ -3,22 +3,20 @@ package org.tasks.activities import android.net.Uri import android.os.Bundle import androidx.appcompat.app.AppCompatActivity -import com.todoroo.astrid.dao.TaskDaoBlocking +import androidx.lifecycle.lifecycleScope +import com.todoroo.astrid.dao.TaskDao import dagger.hilt.android.AndroidEntryPoint -import io.reactivex.Single -import io.reactivex.android.schedulers.AndroidSchedulers -import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.launch import org.tasks.intents.TaskIntents import org.tasks.provider.TasksContentProvider import org.tasks.provider.TasksContentProvider.Companion.URI_OPEN_TASK import timber.log.Timber -import java.util.* import javax.inject.Inject @AndroidEntryPoint class UriHandler : AppCompatActivity() { - @Inject lateinit var taskDao: TaskDaoBlocking + @Inject lateinit var taskDao: TaskDao override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -27,15 +25,16 @@ class UriHandler : AppCompatActivity() { URI_OPEN_TASK -> { val id = intent.data?.lastPathSegment?.toLongOrNull() ?: 0 if (id > 0) { - Single.fromCallable { Optional.ofNullable(taskDao.fetchBlocking(id))} - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .doAfterTerminate(this::finish) - .subscribe( - { startActivity(TaskIntents.getEditTaskIntent(this, it.get())) }, - Timber::e) + lifecycleScope.launch { + val task = taskDao.fetch(id) + task?.let { + startActivity(TaskIntents.getEditTaskIntent(this@UriHandler, it)) + } + finish() + } } else { startActivity(TaskIntents.getNewTaskIntent(this, null)) + finish() } } else -> {