diff options
Diffstat (limited to 'app/src/main/java')
5 files changed, 49 insertions, 74 deletions
diff --git a/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt b/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt index 0ce4b3e3..50d7a841 100644 --- a/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt +++ b/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt @@ -16,7 +16,7 @@ import app.passwordstore.injection.prefs.SettingsPreferences import app.passwordstore.util.coroutines.DispatcherProvider import app.passwordstore.util.settings.PreferenceKeys import com.github.michaelbull.result.Result -import com.github.michaelbull.result.getAll +import com.github.michaelbull.result.filterValues import com.github.michaelbull.result.mapBoth import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream @@ -57,7 +57,7 @@ constructor( message: ByteArrayInputStream, out: ByteArrayOutputStream, ): Result<Unit, CryptoHandlerException> { - val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.getAll() + val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues() val decryptionOptions = PGPDecryptOptions.Builder().build() return pgpCryptoHandler.decrypt(keys, password, message, out, decryptionOptions) } @@ -71,7 +71,7 @@ constructor( PGPEncryptOptions.Builder() .withAsciiArmor(settings.getBoolean(PreferenceKeys.ASCII_ARMOR, false)) .build() - val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.getAll() + val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues() return pgpCryptoHandler.encrypt( keys, content, diff --git a/app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt b/app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt index 31934d26..895ac50d 100644 --- a/app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt +++ b/app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt @@ -27,8 +27,6 @@ import app.passwordstore.util.features.Feature.EnablePGPPassphraseCache import app.passwordstore.util.features.Features import app.passwordstore.util.settings.Constants import app.passwordstore.util.settings.PreferenceKeys -import com.github.michaelbull.result.Err -import com.github.michaelbull.result.Ok import com.github.michaelbull.result.runCatching import dagger.hilt.android.AndroidEntryPoint import java.io.ByteArrayOutputStream @@ -214,18 +212,16 @@ class DecryptActivity : BasePGPActivity() { authResult: BiometricResult, onSuccess: suspend () -> Unit = {}, ) { - when (val result = decryptPGPStream(passphrase, identifiers)) { - is Ok -> { - val entry = passwordEntryFactory.create(result.value.toByteArray()) - passwordEntry = entry - createPasswordUI(entry) - startAutoDismissTimer() - onSuccess() - } - is Err -> { - logcat(ERROR) { result.error.stackTraceToString() } - decrypt(isError = true, authResult = authResult) - } + val result = decryptPGPStream(passphrase, identifiers) + if (result.isOk) { + val entry = passwordEntryFactory.create(result.value.toByteArray()) + passwordEntry = entry + createPasswordUI(entry) + startAutoDismissTimer() + onSuccess() + } else { + logcat(ERROR) { result.error.stackTraceToString() } + decrypt(isError = true, authResult = authResult) } } @@ -242,10 +238,7 @@ class DecryptActivity : BasePGPActivity() { message, outputStream, ) - when (result) { - is Ok -> outputStream - is Err -> throw result.error - } + if (result.isOk) outputStream else throw result.error } private suspend fun createPasswordUI(entry: PasswordEntry) = diff --git a/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyImportActivity.kt b/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyImportActivity.kt index 0b3fdd4e..03a8f95c 100644 --- a/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyImportActivity.kt +++ b/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyImportActivity.kt @@ -14,8 +14,6 @@ import app.passwordstore.crypto.KeyUtils.tryGetId import app.passwordstore.crypto.PGPKey import app.passwordstore.crypto.PGPKeyManager import app.passwordstore.crypto.errors.KeyAlreadyExistsException -import com.github.michaelbull.result.Err -import com.github.michaelbull.result.Ok import com.github.michaelbull.result.Result import com.github.michaelbull.result.runCatching import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -69,45 +67,42 @@ class PGPKeyImportActivity : AppCompatActivity() { } private fun handleImportResult(result: Result<PGPKey?, Throwable>) { - when (result) { - is Ok<PGPKey?> -> { - val key = result.value - if (key == null) { - setResult(RESULT_CANCELED) + if (result.isOk) { + val key = result.value + if (key == null) { + setResult(RESULT_CANCELED) + finish() + // This return convinces Kotlin that the control flow for `key == null` definitely + // terminates here and allows for a smart cast below. + return + } + MaterialAlertDialogBuilder(this) + .setTitle(getString(R.string.pgp_key_import_succeeded)) + .setMessage(getString(R.string.pgp_key_import_succeeded_message, tryGetId(key))) + .setPositiveButton(android.R.string.ok) { _, _ -> + setResult(RESULT_OK) finish() - // This return convinces Kotlin that the control flow for `key == null` definitely - // terminates here and allows for a smart cast below. - return } + .setCancelable(false) + .show() + } else { + if (result.error is KeyAlreadyExistsException && lastBytes != null) { MaterialAlertDialogBuilder(this) - .setTitle(getString(R.string.pgp_key_import_succeeded)) - .setMessage(getString(R.string.pgp_key_import_succeeded_message, tryGetId(key))) - .setPositiveButton(android.R.string.ok) { _, _ -> - setResult(RESULT_OK) - finish() + .setTitle(getString(R.string.pgp_key_import_failed)) + .setMessage(getString(R.string.pgp_key_import_failed_replace_message)) + .setPositiveButton(R.string.dialog_yes) { _, _ -> + handleImportResult(runCatching { importKey(lastBytes!!, replace = true) }) } + .setNegativeButton(R.string.dialog_no) { _, _ -> finish() } + .setCancelable(false) + .show() + } else { + MaterialAlertDialogBuilder(this) + .setTitle(getString(R.string.pgp_key_import_failed)) + .setMessage(result.error.message) + .setPositiveButton(android.R.string.ok) { _, _ -> finish() } .setCancelable(false) .show() - } - is Err<Throwable> -> { - if (result.error is KeyAlreadyExistsException && lastBytes != null) { - MaterialAlertDialogBuilder(this) - .setTitle(getString(R.string.pgp_key_import_failed)) - .setMessage(getString(R.string.pgp_key_import_failed_replace_message)) - .setPositiveButton(R.string.dialog_yes) { _, _ -> - handleImportResult(runCatching { importKey(lastBytes!!, replace = true) }) - } - .setNegativeButton(R.string.dialog_no) { _, _ -> finish() } - .setCancelable(false) - .show() - } else { - MaterialAlertDialogBuilder(this) - .setTitle(getString(R.string.pgp_key_import_failed)) - .setMessage(result.error.message) - .setPositiveButton(android.R.string.ok) { _, _ -> finish() } - .setCancelable(false) - .show() - } } } } diff --git a/app/src/main/java/app/passwordstore/util/extensions/Extensions.kt b/app/src/main/java/app/passwordstore/util/extensions/Extensions.kt index c6fd9ceb..a921f8d6 100644 --- a/app/src/main/java/app/passwordstore/util/extensions/Extensions.kt +++ b/app/src/main/java/app/passwordstore/util/extensions/Extensions.kt @@ -5,8 +5,6 @@ package app.passwordstore.util.extensions import app.passwordstore.data.repo.PasswordRepository -import com.github.michaelbull.result.Err -import com.github.michaelbull.result.Result import com.github.michaelbull.result.getOrElse import com.github.michaelbull.result.runCatching import java.io.File @@ -68,8 +66,3 @@ fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE) { ini /** A convenience extension to turn a [Throwable] with a message into a loggable string. */ fun Throwable.asLog(message: String): String = "$message\n${asLog()}" - -/** Extension on [Result] that returns if the type is [Err] */ -fun <V, E> Result<V, E>.isErr(): Boolean { - return this is Err<E> -} diff --git a/app/src/main/java/app/passwordstore/util/viewmodel/PGPKeyListViewModel.kt b/app/src/main/java/app/passwordstore/util/viewmodel/PGPKeyListViewModel.kt index 6711cae1..c5099ace 100644 --- a/app/src/main/java/app/passwordstore/util/viewmodel/PGPKeyListViewModel.kt +++ b/app/src/main/java/app/passwordstore/util/viewmodel/PGPKeyListViewModel.kt @@ -8,9 +8,8 @@ import androidx.lifecycle.viewModelScope import app.passwordstore.crypto.KeyUtils import app.passwordstore.crypto.PGPIdentifier import app.passwordstore.crypto.PGPKeyManager -import com.github.michaelbull.result.Err -import com.github.michaelbull.result.Ok import com.github.michaelbull.result.map +import com.github.michaelbull.result.onSuccess import dagger.hilt.android.lifecycle.HiltViewModel import javax.inject.Inject import kotlinx.collections.immutable.ImmutableList @@ -28,15 +27,10 @@ class PGPKeyListViewModel @Inject constructor(private val keyManager: PGPKeyMana fun updateKeySet() { viewModelScope.launch { - when ( - val result = - keyManager.getAllKeys().map { keys -> - keys.mapNotNull { key -> KeyUtils.tryGetEmail(key) } - } - ) { - is Ok -> keys = result.value.toPersistentList() - is Err -> TODO() - } + keyManager + .getAllKeys() + .map { keys -> keys.mapNotNull { key -> KeyUtils.tryGetEmail(key) } } + .onSuccess { keys = it.toPersistentList() } } } |