Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 48067b4

Browse files
fix(deps): update kotlinresult to v1.1.21 (#2958)
* fix(deps): update kotlinresult to v1.1.21 * refactor: fix deprecation warnings --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Harsh Shandilya <[email protected]>
1 parent b01fdda commit 48067b4

File tree

8 files changed

+72
-102
lines changed

8 files changed

+72
-102
lines changed

app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import app.passwordstore.injection.prefs.SettingsPreferences
1616
import app.passwordstore.util.coroutines.DispatcherProvider
1717
import app.passwordstore.util.settings.PreferenceKeys
1818
import com.github.michaelbull.result.Result
19-
import com.github.michaelbull.result.getAll
19+
import com.github.michaelbull.result.filterValues
2020
import com.github.michaelbull.result.mapBoth
2121
import java.io.ByteArrayInputStream
2222
import java.io.ByteArrayOutputStream
@@ -57,7 +57,7 @@ constructor(
5757
message: ByteArrayInputStream,
5858
out: ByteArrayOutputStream,
5959
): Result<Unit, CryptoHandlerException> {
60-
val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.getAll()
60+
val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues()
6161
val decryptionOptions = PGPDecryptOptions.Builder().build()
6262
return pgpCryptoHandler.decrypt(keys, password, message, out, decryptionOptions)
6363
}
@@ -71,7 +71,7 @@ constructor(
7171
PGPEncryptOptions.Builder()
7272
.withAsciiArmor(settings.getBoolean(PreferenceKeys.ASCII_ARMOR, false))
7373
.build()
74-
val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.getAll()
74+
val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues()
7575
return pgpCryptoHandler.encrypt(
7676
keys,
7777
content,

app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import app.passwordstore.util.features.Feature.EnablePGPPassphraseCache
2727
import app.passwordstore.util.features.Features
2828
import app.passwordstore.util.settings.Constants
2929
import app.passwordstore.util.settings.PreferenceKeys
30-
import com.github.michaelbull.result.Err
31-
import com.github.michaelbull.result.Ok
3230
import com.github.michaelbull.result.runCatching
3331
import dagger.hilt.android.AndroidEntryPoint
3432
import java.io.ByteArrayOutputStream
@@ -214,18 +212,16 @@ class DecryptActivity : BasePGPActivity() {
214212
authResult: BiometricResult,
215213
onSuccess: suspend () -> Unit = {},
216214
) {
217-
when (val result = decryptPGPStream(passphrase, identifiers)) {
218-
is Ok -> {
219-
val entry = passwordEntryFactory.create(result.value.toByteArray())
220-
passwordEntry = entry
221-
createPasswordUI(entry)
222-
startAutoDismissTimer()
223-
onSuccess()
224-
}
225-
is Err -> {
226-
logcat(ERROR) { result.error.stackTraceToString() }
227-
decrypt(isError = true, authResult = authResult)
228-
}
215+
val result = decryptPGPStream(passphrase, identifiers)
216+
if (result.isOk) {
217+
val entry = passwordEntryFactory.create(result.value.toByteArray())
218+
passwordEntry = entry
219+
createPasswordUI(entry)
220+
startAutoDismissTimer()
221+
onSuccess()
222+
} else {
223+
logcat(ERROR) { result.error.stackTraceToString() }
224+
decrypt(isError = true, authResult = authResult)
229225
}
230226
}
231227

@@ -242,10 +238,7 @@ class DecryptActivity : BasePGPActivity() {
242238
message,
243239
outputStream,
244240
)
245-
when (result) {
246-
is Ok -> outputStream
247-
is Err -> throw result.error
248-
}
241+
if (result.isOk) outputStream else throw result.error
249242
}
250243

251244
private suspend fun createPasswordUI(entry: PasswordEntry) =

app/src/main/java/app/passwordstore/ui/pgp/PGPKeyImportActivity.kt

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import app.passwordstore.crypto.KeyUtils.tryGetId
1414
import app.passwordstore.crypto.PGPKey
1515
import app.passwordstore.crypto.PGPKeyManager
1616
import app.passwordstore.crypto.errors.KeyAlreadyExistsException
17-
import com.github.michaelbull.result.Err
18-
import com.github.michaelbull.result.Ok
1917
import com.github.michaelbull.result.Result
2018
import com.github.michaelbull.result.runCatching
2119
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@@ -69,45 +67,42 @@ class PGPKeyImportActivity : AppCompatActivity() {
6967
}
7068

7169
private fun handleImportResult(result: Result<PGPKey?, Throwable>) {
72-
when (result) {
73-
is Ok<PGPKey?> -> {
74-
val key = result.value
75-
if (key == null) {
76-
setResult(RESULT_CANCELED)
70+
if (result.isOk) {
71+
val key = result.value
72+
if (key == null) {
73+
setResult(RESULT_CANCELED)
74+
finish()
75+
// This return convinces Kotlin that the control flow for `key == null` definitely
76+
// terminates here and allows for a smart cast below.
77+
return
78+
}
79+
MaterialAlertDialogBuilder(this)
80+
.setTitle(getString(R.string.pgp_key_import_succeeded))
81+
.setMessage(getString(R.string.pgp_key_import_succeeded_message, tryGetId(key)))
82+
.setPositiveButton(android.R.string.ok) { _, _ ->
83+
setResult(RESULT_OK)
7784
finish()
78-
// This return convinces Kotlin that the control flow for `key == null` definitely
79-
// terminates here and allows for a smart cast below.
80-
return
8185
}
86+
.setCancelable(false)
87+
.show()
88+
} else {
89+
if (result.error is KeyAlreadyExistsException && lastBytes != null) {
8290
MaterialAlertDialogBuilder(this)
83-
.setTitle(getString(R.string.pgp_key_import_succeeded))
84-
.setMessage(getString(R.string.pgp_key_import_succeeded_message, tryGetId(key)))
85-
.setPositiveButton(android.R.string.ok) { _, _ ->
86-
setResult(RESULT_OK)
87-
finish()
91+
.setTitle(getString(R.string.pgp_key_import_failed))
92+
.setMessage(getString(R.string.pgp_key_import_failed_replace_message))
93+
.setPositiveButton(R.string.dialog_yes) { _, _ ->
94+
handleImportResult(runCatching { importKey(lastBytes!!, replace = true) })
8895
}
96+
.setNegativeButton(R.string.dialog_no) { _, _ -> finish() }
97+
.setCancelable(false)
98+
.show()
99+
} else {
100+
MaterialAlertDialogBuilder(this)
101+
.setTitle(getString(R.string.pgp_key_import_failed))
102+
.setMessage(result.error.message)
103+
.setPositiveButton(android.R.string.ok) { _, _ -> finish() }
89104
.setCancelable(false)
90105
.show()
91-
}
92-
is Err<Throwable> -> {
93-
if (result.error is KeyAlreadyExistsException && lastBytes != null) {
94-
MaterialAlertDialogBuilder(this)
95-
.setTitle(getString(R.string.pgp_key_import_failed))
96-
.setMessage(getString(R.string.pgp_key_import_failed_replace_message))
97-
.setPositiveButton(R.string.dialog_yes) { _, _ ->
98-
handleImportResult(runCatching { importKey(lastBytes!!, replace = true) })
99-
}
100-
.setNegativeButton(R.string.dialog_no) { _, _ -> finish() }
101-
.setCancelable(false)
102-
.show()
103-
} else {
104-
MaterialAlertDialogBuilder(this)
105-
.setTitle(getString(R.string.pgp_key_import_failed))
106-
.setMessage(result.error.message)
107-
.setPositiveButton(android.R.string.ok) { _, _ -> finish() }
108-
.setCancelable(false)
109-
.show()
110-
}
111106
}
112107
}
113108
}

app/src/main/java/app/passwordstore/util/extensions/Extensions.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package app.passwordstore.util.extensions
66

77
import app.passwordstore.data.repo.PasswordRepository
8-
import com.github.michaelbull.result.Err
9-
import com.github.michaelbull.result.Result
108
import com.github.michaelbull.result.getOrElse
119
import com.github.michaelbull.result.runCatching
1210
import java.io.File
@@ -68,8 +66,3 @@ fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE) { ini
6866

6967
/** A convenience extension to turn a [Throwable] with a message into a loggable string. */
7068
fun Throwable.asLog(message: String): String = "$message\n${asLog()}"
71-
72-
/** Extension on [Result] that returns if the type is [Err] */
73-
fun <V, E> Result<V, E>.isErr(): Boolean {
74-
return this is Err<E>
75-
}

app/src/main/java/app/passwordstore/util/viewmodel/PGPKeyListViewModel.kt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import androidx.lifecycle.viewModelScope
88
import app.passwordstore.crypto.KeyUtils
99
import app.passwordstore.crypto.PGPIdentifier
1010
import app.passwordstore.crypto.PGPKeyManager
11-
import com.github.michaelbull.result.Err
12-
import com.github.michaelbull.result.Ok
1311
import com.github.michaelbull.result.map
12+
import com.github.michaelbull.result.onSuccess
1413
import dagger.hilt.android.lifecycle.HiltViewModel
1514
import javax.inject.Inject
1615
import kotlinx.collections.immutable.ImmutableList
@@ -28,15 +27,10 @@ class PGPKeyListViewModel @Inject constructor(private val keyManager: PGPKeyMana
2827

2928
fun updateKeySet() {
3029
viewModelScope.launch {
31-
when (
32-
val result =
33-
keyManager.getAllKeys().map { keys ->
34-
keys.mapNotNull { key -> KeyUtils.tryGetEmail(key) }
35-
}
36-
) {
37-
is Ok -> keys = result.value.toPersistentList()
38-
is Err -> TODO()
39-
}
30+
keyManager
31+
.getAllKeys()
32+
.map { keys -> keys.mapNotNull { key -> KeyUtils.tryGetEmail(key) } }
33+
.onSuccess { keys = it.toPersistentList() }
4034
}
4135
}
4236

crypto/pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import app.passwordstore.crypto.errors.KeyAlreadyExistsException
77
import app.passwordstore.crypto.errors.KeyNotFoundException
88
import app.passwordstore.crypto.errors.NoKeysAvailableException
99
import app.passwordstore.crypto.errors.UnusableKeyException
10-
import com.github.michaelbull.result.Err
11-
import com.github.michaelbull.result.Ok
1210
import com.github.michaelbull.result.unwrap
1311
import com.github.michaelbull.result.unwrapError
1412
import java.io.File
@@ -18,8 +16,8 @@ import kotlin.test.assertEquals
1816
import kotlin.test.assertIs
1917
import kotlin.test.assertNotEquals
2018
import kotlin.test.assertNotNull
19+
import kotlin.test.assertTrue
2120
import kotlinx.coroutines.test.StandardTestDispatcher
22-
import kotlinx.coroutines.test.TestScope
2321
import kotlinx.coroutines.test.runTest
2422
import org.junit.Rule
2523
import org.junit.rules.TemporaryFolder
@@ -28,7 +26,6 @@ class PGPKeyManagerTest {
2826

2927
@get:Rule val temporaryFolder: TemporaryFolder = TemporaryFolder()
3028
private val dispatcher = StandardTestDispatcher()
31-
private val scope = TestScope(dispatcher)
3229
private val filesDir by unsafeLazy { temporaryFolder.root }
3330
private val keysDir by unsafeLazy { File(filesDir, PGPKeyManager.KEY_DIR_NAME) }
3431
private val keyManager by unsafeLazy { PGPKeyManager(filesDir.absolutePath, dispatcher) }
@@ -170,24 +167,24 @@ class PGPKeyManagerTest {
170167
@Test
171168
fun replaceSecretKeyWithPublicKey() =
172169
runTest(dispatcher) {
173-
assertIs<Ok<PGPKey>>(keyManager.addKey(secretKey))
174-
assertIs<Err<KeyAlreadyExistsException>>(keyManager.addKey(publicKey))
170+
assertTrue(keyManager.addKey(secretKey).isOk)
171+
assertTrue(keyManager.addKey(publicKey).isErr)
175172
}
176173

177174
@Test
178175
fun replacePublicKeyWithSecretKey() =
179176
runTest(dispatcher) {
180-
assertIs<Ok<PGPKey>>(keyManager.addKey(publicKey))
181-
assertIs<Ok<PGPKey>>(keyManager.addKey(secretKey))
177+
assertTrue(keyManager.addKey(publicKey).isOk)
178+
assertTrue(keyManager.addKey(secretKey).isOk)
182179
}
183180

184181
@Test
185182
fun replacePublicKeyWithPublicKey() =
186183
runTest(dispatcher) {
187-
assertIs<Ok<PGPKey>>(keyManager.addKey(publicKey))
188-
assertIs<Ok<PGPKey>>(keyManager.addKey(publicKey))
184+
assertTrue(keyManager.addKey(publicKey).isOk)
185+
assertTrue(keyManager.addKey(publicKey).isOk)
189186
val allKeys = keyManager.getAllKeys()
190-
assertIs<Ok<List<PGPKey>>>(allKeys)
187+
assertTrue(allKeys.isOk)
191188
assertEquals(1, allKeys.value.size)
192189
val key = allKeys.value[0]
193190
assertContentEquals(publicKey.contents, key.contents)
@@ -196,8 +193,8 @@ class PGPKeyManagerTest {
196193
@Test
197194
fun replaceSecretKeyWithSecretKey() =
198195
runTest(dispatcher) {
199-
assertIs<Ok<PGPKey>>(keyManager.addKey(secretKey))
200-
assertIs<Err<KeyAlreadyExistsException>>(keyManager.addKey(secretKey))
196+
assertTrue(keyManager.addKey(secretKey).isOk)
197+
assertTrue(keyManager.addKey(secretKey).isErr)
201198
}
202199

203200
@Test
@@ -207,11 +204,11 @@ class PGPKeyManagerTest {
207204
PGPKey(this::class.java.classLoader.getResource("alice_owner@example_com")!!.readBytes())
208205
val bobby =
209206
PGPKey(this::class.java.classLoader.getResource("bobby_owner@example_com")!!.readBytes())
210-
assertIs<Ok<PGPKey>>(keyManager.addKey(alice))
211-
assertIs<Ok<PGPKey>>(keyManager.addKey(bobby))
207+
assertTrue(keyManager.addKey(alice).isOk)
208+
assertTrue(keyManager.addKey(bobby).isOk)
212209

213210
keyManager.getAllKeys().apply {
214-
assertIs<Ok<List<PGPKey>>>(this)
211+
assertTrue(this.isOk)
215212
assertEquals(2, this.value.size)
216213
}
217214
val longKeyIds =
@@ -228,8 +225,8 @@ class PGPKeyManagerTest {
228225
for (idCollection in arrayOf(longKeyIds, userIds)) {
229226
val alice1 = keyManager.getKeyById(idCollection[0])
230227
val bobby1 = keyManager.getKeyById(idCollection[1])
231-
assertIs<Ok<PGPKey>>(alice1)
232-
assertIs<Ok<PGPKey>>(bobby1)
228+
assertTrue(alice1.isOk)
229+
assertTrue(bobby1.isOk)
233230
assertNotEquals(alice1.value.contents, bobby1.value.contents)
234231
}
235232
}

crypto/pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ package app.passwordstore.crypto
99
import app.passwordstore.crypto.CryptoConstants.KEY_PASSPHRASE
1010
import app.passwordstore.crypto.CryptoConstants.PLAIN_TEXT
1111
import app.passwordstore.crypto.errors.IncorrectPassphraseException
12-
import com.github.michaelbull.result.Err
13-
import com.github.michaelbull.result.Ok
1412
import com.github.michaelbull.result.getError
1513
import com.google.testing.junit.testparameterinjector.TestParameter
1614
import com.google.testing.junit.testparameterinjector.TestParameterInjector
@@ -48,7 +46,7 @@ class PGPainlessCryptoHandlerTest {
4846
ciphertextStream,
4947
PGPEncryptOptions.Builder().build(),
5048
)
51-
assertIs<Ok<Unit>>(encryptRes)
49+
assertTrue(encryptRes.isOk)
5250
val plaintextStream = ByteArrayOutputStream()
5351
val decryptRes =
5452
cryptoHandler.decrypt(
@@ -58,7 +56,7 @@ class PGPainlessCryptoHandlerTest {
5856
plaintextStream,
5957
PGPDecryptOptions.Builder().build(),
6058
)
61-
assertIs<Ok<Unit>>(decryptRes)
59+
assertTrue(decryptRes.isOk)
6260
assertEquals(PLAIN_TEXT, plaintextStream.toString(Charsets.UTF_8))
6361
}
6462

@@ -72,7 +70,7 @@ class PGPainlessCryptoHandlerTest {
7270
ciphertextStream,
7371
PGPEncryptOptions.Builder().build(),
7472
)
75-
assertIs<Ok<Unit>>(encryptRes)
73+
assertTrue(encryptRes.isOk)
7674
val plaintextStream = ByteArrayOutputStream()
7775
val result =
7876
cryptoHandler.decrypt(
@@ -82,7 +80,7 @@ class PGPainlessCryptoHandlerTest {
8280
plaintextStream,
8381
PGPDecryptOptions.Builder().build(),
8482
)
85-
assertIs<Err<Throwable>>(result)
83+
assertTrue(result.isErr)
8684
assertIs<IncorrectPassphraseException>(result.getError())
8785
}
8886

@@ -96,7 +94,7 @@ class PGPainlessCryptoHandlerTest {
9694
ciphertextStream,
9795
PGPEncryptOptions.Builder().withAsciiArmor(true).build(),
9896
)
99-
assertIs<Ok<Unit>>(encryptRes)
97+
assertTrue(encryptRes.isOk)
10098
val ciphertext = ciphertextStream.toString(Charsets.UTF_8)
10199
assertContains(ciphertext, "Version: PGPainless")
102100
assertContains(ciphertext, "-----BEGIN PGP MESSAGE-----")
@@ -118,7 +116,7 @@ class PGPainlessCryptoHandlerTest {
118116
ciphertextStream,
119117
PGPEncryptOptions.Builder().withAsciiArmor(true).build(),
120118
)
121-
assertIs<Ok<Unit>>(encryptRes)
119+
assertTrue(encryptRes.isOk)
122120
val message = ciphertextStream.toByteArray().decodeToString()
123121
val info = MessageInspector.determineEncryptionInfoForMessage(message)
124122
assertTrue(info.isEncrypted)
@@ -135,7 +133,7 @@ class PGPainlessCryptoHandlerTest {
135133
plaintextStream,
136134
PGPDecryptOptions.Builder().build(),
137135
)
138-
assertIs<Ok<Unit>>(res)
136+
assertTrue(res.isOk)
139137
}
140138
}
141139

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ coroutines = "1.8.0"
88
flowbinding = "1.2.0"
99
hilt = "2.51"
1010
kotlin = "1.9.23"
11-
kotlinResult = "1.1.20"
11+
kotlinResult = "1.1.21"
1212
lifecycle = "2.7.0"
1313
moshi = "1.15.1"
1414

0 commit comments

Comments
 (0)