Use coroutines

This commit is contained in:
ligi 2020-02-29 18:26:02 +01:00
parent dd187ab69f
commit 1b6353d470
No known key found for this signature in database
GPG key ID: 8E81894010ABF23D
2 changed files with 14 additions and 5 deletions

View file

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

View file

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