diff options
| author | Harsh Shandilya <me@msfjarvis.dev> | 2021-04-30 23:22:01 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-30 23:22:01 +0530 |
| commit | f834c754e68eaea8cb32e1368f61f36eaf573aaa (patch) | |
| tree | 5a4e77697eb1a28e0bd328d019c2440063b63c62 /app/src/main/java/dev/msfjarvis/aps/ui/crypto/GetKeyIdsActivity.kt | |
| parent | 4880e1db276b4cf2ec29eeffbd38539ca643b4be (diff) | |
| download | HardenedPass-f834c754e68eaea8cb32e1368f61f36eaf573aaa.tar.gz HardenedPass-f834c754e68eaea8cb32e1368f61f36eaf573aaa.zip | |
Refactor openpgp-ktx to leverage coroutines (#1398)
* openpgp-ktx: leverage coroutines for async IPC
* Unwind nested dispatchers
* Fix name shadowing warning
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/dev/msfjarvis/aps/ui/crypto/GetKeyIdsActivity.kt')
| -rw-r--r-- | app/src/main/java/dev/msfjarvis/aps/ui/crypto/GetKeyIdsActivity.kt | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/GetKeyIdsActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/GetKeyIdsActivity.kt index 9da4044a..120ccaab 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/GetKeyIdsActivity.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/GetKeyIdsActivity.kt @@ -15,6 +15,7 @@ import com.github.michaelbull.result.onFailure import com.github.michaelbull.result.runCatching import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import me.msfjarvis.openpgpktx.util.OpenPgpApi import me.msfjarvis.openpgpktx.util.OpenPgpUtils import org.openintents.openpgp.IOpenPgpService2 @@ -48,26 +49,25 @@ class GetKeyIdsActivity : BasePgpActivity() { /** Get the Key ids from OpenKeychain */ private fun getKeyIds(data: Intent = Intent()) { data.action = OpenPgpApi.ACTION_GET_KEY_IDS - lifecycleScope.launch(Dispatchers.IO) { - api?.executeApiAsync(data, null, null) { result -> - when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { - OpenPgpApi.RESULT_CODE_SUCCESS -> { - runCatching { - val ids = - result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS)?.map { OpenPgpUtils.convertKeyIdToHex(it) } - ?: emptyList() - val keyResult = Intent().putExtra(OpenPgpApi.EXTRA_KEY_IDS, ids.toTypedArray()) - setResult(RESULT_OK, keyResult) - finish() - } - .onFailure { e -> e(e) } + lifecycleScope.launch(Dispatchers.Main) { + val result = withContext(Dispatchers.IO) { checkNotNull(api).executeApi(data, null, null) } + when (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { + OpenPgpApi.RESULT_CODE_SUCCESS -> { + runCatching { + val ids = + result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS)?.map { OpenPgpUtils.convertKeyIdToHex(it) } + ?: emptyList() + val keyResult = Intent().putExtra(OpenPgpApi.EXTRA_KEY_IDS, ids.toTypedArray()) + setResult(RESULT_OK, keyResult) + finish() } - OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> { - val sender = getUserInteractionRequestIntent(result) - userInteractionRequiredResult.launch(IntentSenderRequest.Builder(sender).build()) - } - OpenPgpApi.RESULT_CODE_ERROR -> handleError(result) + .onFailure { e -> e(e) } + } + OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> { + val sender = getUserInteractionRequestIntent(result) + userInteractionRequiredResult.launch(IntentSenderRequest.Builder(sender).build()) } + OpenPgpApi.RESULT_CODE_ERROR -> handleError(result) } } } |
