Use coroutines in TaskEditActivity and UriHandler

pull/1051/head
Alex Baker 4 years ago
parent 6debb8b0ab
commit e4b2dc932e

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

@ -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"
}
}

@ -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 -> {

Loading…
Cancel
Save