@@ -17,13 +17,13 @@ import app.passwordstore.data.password.FieldItem
17
17
import app.passwordstore.databinding.DecryptLayoutBinding
18
18
import app.passwordstore.ui.adapters.FieldItemAdapter
19
19
import app.passwordstore.util.extensions.getString
20
- import app.passwordstore.util.extensions.isErr
21
20
import app.passwordstore.util.extensions.unsafeLazy
22
21
import app.passwordstore.util.extensions.viewBinding
23
22
import app.passwordstore.util.settings.Constants
24
23
import app.passwordstore.util.settings.PreferenceKeys
24
+ import com.github.michaelbull.result.Err
25
+ import com.github.michaelbull.result.Ok
25
26
import com.github.michaelbull.result.runCatching
26
- import com.github.michaelbull.result.unwrapError
27
27
import dagger.hilt.android.AndroidEntryPoint
28
28
import java.io.ByteArrayOutputStream
29
29
import java.io.File
@@ -66,7 +66,7 @@ class DecryptActivity : BasePgpActivity() {
66
66
true
67
67
}
68
68
}
69
- decrypt (isError = false )
69
+ askPassphrase (isError = false )
70
70
}
71
71
72
72
override fun onCreateOptionsMenu (menu : Menu ): Boolean {
@@ -137,7 +137,7 @@ class DecryptActivity : BasePgpActivity() {
137
137
)
138
138
}
139
139
140
- private fun decrypt (isError : Boolean ) {
140
+ private fun askPassphrase (isError : Boolean ) {
141
141
if (retries < MAX_RETRIES ) {
142
142
retries + = 1
143
143
} else {
@@ -150,37 +150,40 @@ class DecryptActivity : BasePgpActivity() {
150
150
lifecycleScope.launch(Dispatchers .Main ) {
151
151
dialog.password.collectLatest { value ->
152
152
if (value != null ) {
153
- val res = runCatching { decrypt(value) }
154
- if (res.isErr()) {
155
- logcat(ERROR ) { res.unwrapError().stackTraceToString() }
156
- decrypt(isError = true )
153
+ when (val result = decryptWithPassphrase(value)) {
154
+ is Ok -> {
155
+ val entry = passwordEntryFactory.create(result.value.toByteArray())
156
+ passwordEntry = entry
157
+ createPasswordUI(entry)
158
+ startAutoDismissTimer()
159
+ }
160
+ is Err -> {
161
+ logcat(ERROR ) { result.error.stackTraceToString() }
162
+ askPassphrase(isError = true )
163
+ }
157
164
}
158
165
}
159
166
}
160
167
}
161
168
dialog.show(supportFragmentManager, " PASSWORD_DIALOG" )
162
169
}
163
170
164
- private suspend fun decrypt (password : String ) {
171
+ private suspend fun decryptWithPassphrase (password : String ) = runCatching {
165
172
val message = withContext(Dispatchers .IO ) { File (fullPath).readBytes().inputStream() }
173
+ val outputStream = ByteArrayOutputStream ()
166
174
val result =
167
- withContext(Dispatchers .IO ) {
168
- val outputStream = ByteArrayOutputStream ()
169
- repository.decrypt(
170
- password,
171
- message,
172
- outputStream,
173
- )
174
- outputStream
175
- }
176
- startAutoDismissTimer()
177
-
178
- val entry = passwordEntryFactory.create(result.toByteArray())
179
- passwordEntry = entry
180
- createPasswordUi(entry)
175
+ repository.decrypt(
176
+ password,
177
+ message,
178
+ outputStream,
179
+ )
180
+ when (result) {
181
+ is Ok -> outputStream
182
+ is Err -> throw result.error
183
+ }
181
184
}
182
185
183
- private suspend fun createPasswordUi (entry : PasswordEntry ) =
186
+ private suspend fun createPasswordUI (entry : PasswordEntry ) =
184
187
withContext(Dispatchers .Main ) {
185
188
val showPassword = settings.getBoolean(PreferenceKeys .SHOW_PASSWORD , true )
186
189
invalidateOptionsMenu()
0 commit comments