From 1b6353d470a04662a793131503c44a0c46aa9137 Mon Sep 17 00:00:00 2001 From: ligi Date: Sat, 29 Feb 2020 18:26:02 +0100 Subject: [PATCH] Use coroutines --- android/build.gradle | 4 ++++ .../org/ligi/passandroid/ui/PassImportActivity.kt | 15 ++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index d4f009d1..3ee445da 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -111,6 +111,10 @@ dependencies { implementation 'com.github.salomonbrys.kodein:kodein:4.1.0' compileOnly 'org.glassfish:javax.annotation:3.1.1' + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3" + implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0' + androidTestImplementation 'com.github.ligi:trulesk:0.31' androidTestUtil 'com.linkedin.testbutler:test-butler-app:2.1.0' diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassImportActivity.kt b/android/src/main/java/org/ligi/passandroid/ui/PassImportActivity.kt index 22cbab6f..e1cc60fe 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassImportActivity.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassImportActivity.kt @@ -4,7 +4,11 @@ import android.Manifest import android.app.ProgressDialog import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.lifecycleScope import com.github.salomonbrys.kodein.instance +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.ligi.kaxt.dismissIfShowing import org.ligi.kaxt.startActivityFromClass import org.ligi.kaxtui.alert @@ -53,11 +57,12 @@ class PassImportActivity : AppCompatActivity() { @NeedsPermission(Manifest.permission.READ_EXTERNAL_STORAGE) fun doImport(withPermission: Boolean) { - Thread { + lifecycleScope.launch(Dispatchers.IO) { try { - val fromURI = fromURI(this, intent!!.data!!) + val fromURI = fromURI(this@PassImportActivity, intent!!.data!!) + + withContext(Dispatchers.Main) { - runOnUiThread { progressDialog.dismissIfShowing() if (fromURI == null) { @@ -71,7 +76,7 @@ class PassImportActivity : AppCompatActivity() { val spec = UnzipPassController.InputStreamUnzipControllerSpec(fromURI, application, passStore, null, null) UnzipPassController.processInputStream(spec) } else { - UnzipPassDialog.show(fromURI, this, passStore) { path -> + UnzipPassDialog.show(fromURI, this@PassImportActivity, passStore) { path -> // TODO this is kind of a hack - there should be a better way val id = path.split("/".toRegex()).dropLastWhile(String::isEmpty).toTypedArray().last() @@ -93,7 +98,7 @@ class PassImportActivity : AppCompatActivity() { tracker.trackException("Error in import", e, false) } } - }.start() + } } @OnPermissionDenied(Manifest.permission.READ_EXTERNAL_STORAGE)