summaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2023-01-31 19:11:58 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2023-01-31 19:14:27 +0530
commit5510fd5a80080db8a0ab85e079cc460cdf60ce36 (patch)
treeb1aa29bb3e8dbf6eca10b938240db8dcd5acbf52 /app/src/main/java
parentf6d3ad86ffd8200d78a16de0fe25167f442f0485 (diff)
Revert "refactor: use a ViewModel to pass around decryption passphrase"
This fails to correctly handle retries. Will re-land after I can figure out why. This reverts commit 2fbad7ef6b42cc30c5eac79d5d166bba7cba42a4.
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt9
-rw-r--r--app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt43
-rw-r--r--app/src/main/java/app/passwordstore/ui/crypto/PasswordDialog.kt10
-rw-r--r--app/src/main/java/app/passwordstore/util/viewmodel/PasswordViewModel.kt27
4 files changed, 29 insertions, 60 deletions
diff --git a/app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt b/app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt
index 74e14b91..e8e1f565 100644
--- a/app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt
+++ b/app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt
@@ -11,7 +11,6 @@ import android.content.IntentSender
import android.os.Build
import android.os.Bundle
import android.view.autofill.AutofillManager
-import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
@@ -22,7 +21,6 @@ import app.passwordstore.util.autofill.AutofillPreferences
import app.passwordstore.util.autofill.AutofillResponseBuilder
import app.passwordstore.util.autofill.DirectoryStructure
import app.passwordstore.util.extensions.asLog
-import app.passwordstore.util.viewmodel.PasswordViewModel
import com.github.androidpasswordstore.autofillparser.AutofillAction
import com.github.androidpasswordstore.autofillparser.Credentials
import com.github.michaelbull.result.getOrElse
@@ -79,7 +77,6 @@ class AutofillDecryptActivity : AppCompatActivity() {
}
}
- private val viewModel: PasswordViewModel by viewModels()
@Inject lateinit var passwordEntryFactory: PasswordEntry.Factory
@Inject lateinit var repository: CryptoRepository
@@ -108,8 +105,10 @@ class AutofillDecryptActivity : AppCompatActivity() {
val dialog = PasswordDialog()
lifecycleScope.launch {
withContext(Dispatchers.Main) {
- viewModel.password.collectLatest { value ->
- decrypt(File(filePath), clientState, action, value)
+ dialog.password.collectLatest { value ->
+ if (value != null) {
+ decrypt(File(filePath), clientState, action, value)
+ }
}
}
}
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 72893089..ce14c64d 100644
--- a/app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt
+++ b/app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt
@@ -9,7 +9,6 @@ import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
-import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import app.passwordstore.R
import app.passwordstore.data.crypto.CryptoRepository
@@ -23,7 +22,6 @@ import app.passwordstore.util.extensions.unsafeLazy
import app.passwordstore.util.extensions.viewBinding
import app.passwordstore.util.settings.Constants
import app.passwordstore.util.settings.PreferenceKeys
-import app.passwordstore.util.viewmodel.PasswordViewModel
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.runCatching
@@ -47,7 +45,6 @@ import logcat.logcat
@AndroidEntryPoint
class DecryptActivity : BasePgpActivity() {
- private val viewModel: PasswordViewModel by viewModels()
private val binding by viewBinding(DecryptLayoutBinding::inflate)
private val relativeParentPath by unsafeLazy { getParentPath(fullPath, repoPath) }
@Inject lateinit var passwordEntryFactory: PasswordEntry.Factory
@@ -70,7 +67,7 @@ class DecryptActivity : BasePgpActivity() {
true
}
}
- askPassphrase()
+ askPassphrase(isError = false)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@@ -141,36 +138,34 @@ class DecryptActivity : BasePgpActivity() {
)
}
- private fun askPassphrase() {
+ private fun askPassphrase(isError: Boolean) {
if (retries < MAX_RETRIES) {
retries += 1
} else {
finish()
}
- showPasswordDialog(isError = false)
+ val dialog = PasswordDialog()
+ if (isError) {
+ dialog.setError()
+ }
lifecycleScope.launch(dispatcherProvider.main()) {
- viewModel.password.collectLatest { value ->
- when (val result = decryptWithPassphrase(value)) {
- is Ok -> {
- val entry = passwordEntryFactory.create(result.value.toByteArray())
- passwordEntry = entry
- createPasswordUI(entry)
- startAutoDismissTimer()
- }
- is Err -> {
- logcat(ERROR) { result.error.stackTraceToString() }
- showPasswordDialog(isError = true)
+ dialog.password.collectLatest { value ->
+ if (value != null) {
+ when (val result = decryptWithPassphrase(value)) {
+ is Ok -> {
+ val entry = passwordEntryFactory.create(result.value.toByteArray())
+ passwordEntry = entry
+ createPasswordUI(entry)
+ startAutoDismissTimer()
+ }
+ is Err -> {
+ logcat(ERROR) { result.error.stackTraceToString() }
+ askPassphrase(isError = true)
+ }
}
}
}
}
- }
-
- private fun showPasswordDialog(isError: Boolean) {
- val dialog = PasswordDialog()
- if (isError) {
- dialog.setError()
- }
dialog.show(supportFragmentManager, "PASSWORD_DIALOG")
}
diff --git a/app/src/main/java/app/passwordstore/ui/crypto/PasswordDialog.kt b/app/src/main/java/app/passwordstore/ui/crypto/PasswordDialog.kt
index 1a50be33..25aa18e4 100644
--- a/app/src/main/java/app/passwordstore/ui/crypto/PasswordDialog.kt
+++ b/app/src/main/java/app/passwordstore/ui/crypto/PasswordDialog.kt
@@ -12,20 +12,22 @@ import android.view.KeyEvent
import android.view.WindowManager
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.DialogFragment
-import androidx.fragment.app.activityViewModels
import app.passwordstore.R
import app.passwordstore.databinding.DialogPasswordEntryBinding
import app.passwordstore.util.extensions.finish
import app.passwordstore.util.extensions.unsafeLazy
-import app.passwordstore.util.viewmodel.PasswordViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.asStateFlow
+import kotlinx.coroutines.flow.update
/** [DialogFragment] to request a password from the user and forward it along. */
class PasswordDialog : DialogFragment() {
- private val viewModel: PasswordViewModel by activityViewModels()
private val binding by unsafeLazy { DialogPasswordEntryBinding.inflate(layoutInflater) }
private var isError: Boolean = false
+ private val _password = MutableStateFlow<String?>(null)
+ val password = _password.asStateFlow()
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = MaterialAlertDialogBuilder(requireContext())
@@ -64,7 +66,7 @@ class PasswordDialog : DialogFragment() {
}
private fun setPasswordAndDismiss() {
- viewModel.setPassword(binding.passwordEditText.text.toString())
+ _password.update { binding.passwordEditText.text.toString() }
dismissAllowingStateLoss()
}
}
diff --git a/app/src/main/java/app/passwordstore/util/viewmodel/PasswordViewModel.kt b/app/src/main/java/app/passwordstore/util/viewmodel/PasswordViewModel.kt
deleted file mode 100644
index 9cd5a597..00000000
--- a/app/src/main/java/app/passwordstore/util/viewmodel/PasswordViewModel.kt
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright © 2014-2022 The Android Password Store Authors. All Rights Reserved.
- * SPDX-License-Identifier: GPL-3.0-only
- */
-
-package app.passwordstore.util.viewmodel
-
-import androidx.lifecycle.ViewModel
-import app.passwordstore.ui.crypto.PasswordDialog
-import kotlinx.coroutines.flow.MutableStateFlow
-import kotlinx.coroutines.flow.asStateFlow
-import kotlinx.coroutines.flow.dropWhile
-import kotlinx.coroutines.flow.update
-
-/**
- * ViewModel for classes that use [PasswordDialog] for getting user input. [PasswordDialog] will
- * ensure that it retrieves an instance of this [ViewModel] from the Activity scope and will post
- * values into [PasswordViewModel.password].
- */
-class PasswordViewModel : ViewModel() {
- private val _password = MutableStateFlow("")
- val password = _password.asStateFlow().dropWhile(String::isBlank)
-
- fun setPassword(pass: String) {
- _password.update { pass }
- }
-}