From f834c754e68eaea8cb32e1368f61f36eaf573aaa Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 30 Apr 2021 23:22:01 +0530 Subject: 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 --- .../dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt | 76 ++++++++++------------ 1 file changed, 36 insertions(+), 40 deletions(-) (limited to 'app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt') diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt index 8edd8f0b..24859852 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt @@ -165,55 +165,51 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound { val inputStream = File(fullPath).inputStream() val outputStream = ByteArrayOutputStream() - lifecycleScope.launch(Dispatchers.IO) { - api?.executeApiAsync(data, inputStream, outputStream) { result -> - when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { - OpenPgpApi.RESULT_CODE_SUCCESS -> { - startAutoDismissTimer() - runCatching { - val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true) - val entry = passwordEntryFactory.create(lifecycleScope, outputStream.toByteArray()) - val items = arrayListOf() - val adapter = FieldItemAdapter(emptyList(), showPassword) { text -> copyTextToClipboard(text) } - - if (settings.getBoolean(PreferenceKeys.COPY_ON_DECRYPT, false)) { - copyPasswordToClipboard(entry.password) - } + lifecycleScope.launch(Dispatchers.Main) { + val result = withContext(Dispatchers.IO) { checkNotNull(api).executeApi(data, inputStream, outputStream) } + when (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { + OpenPgpApi.RESULT_CODE_SUCCESS -> { + startAutoDismissTimer() + runCatching { + val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true) + val entry = passwordEntryFactory.create(lifecycleScope, outputStream.toByteArray()) + val items = arrayListOf() + val adapter = FieldItemAdapter(emptyList(), showPassword) { text -> copyTextToClipboard(text) } + + if (settings.getBoolean(PreferenceKeys.COPY_ON_DECRYPT, false)) { + copyPasswordToClipboard(entry.password) + } - passwordEntry = entry - invalidateOptionsMenu() + passwordEntry = entry + invalidateOptionsMenu() - if (!entry.password.isNullOrBlank()) { - items.add(FieldItem.createPasswordField(entry.password!!)) - } + if (!entry.password.isNullOrBlank()) { + items.add(FieldItem.createPasswordField(entry.password!!)) + } - if (entry.hasTotp()) { - launch(Dispatchers.IO) { - withContext(Dispatchers.Main) { - val code = entry.totp.value - items.add(FieldItem.createOtpField(code)) - } - entry.totp.collect { code -> withContext(Dispatchers.Main) { adapter.updateOTPCode(code) } } - } + if (entry.hasTotp()) { + launch { + items.add(FieldItem.createOtpField(entry.totp.value)) + entry.totp.collect { code -> withContext(Dispatchers.Main) { adapter.updateOTPCode(code) } } } + } - if (!entry.username.isNullOrBlank()) { - items.add(FieldItem.createUsernameField(entry.username!!)) - } + if (!entry.username.isNullOrBlank()) { + items.add(FieldItem.createUsernameField(entry.username!!)) + } - entry.extraContent.forEach { (key, value) -> items.add(FieldItem(key, value, FieldItem.ActionType.COPY)) } + entry.extraContent.forEach { (key, value) -> items.add(FieldItem(key, value, FieldItem.ActionType.COPY)) } - binding.recyclerView.adapter = adapter - adapter.updateItems(items) - } - .onFailure { e -> e(e) } + binding.recyclerView.adapter = adapter + adapter.updateItems(items) } - 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) } } } -- cgit v1.2.3