refactor: switch DecryptActivity to DispatcherProvider

This commit is contained in:
Harsh Shandilya
2022-10-07 19:45:54 +05:30
parent b313c4216e
commit d68958c3e1

View File

@@ -16,6 +16,7 @@ import app.passwordstore.data.passfile.PasswordEntry
import app.passwordstore.data.password.FieldItem import app.passwordstore.data.password.FieldItem
import app.passwordstore.databinding.DecryptLayoutBinding import app.passwordstore.databinding.DecryptLayoutBinding
import app.passwordstore.ui.adapters.FieldItemAdapter import app.passwordstore.ui.adapters.FieldItemAdapter
import app.passwordstore.util.coroutines.DispatcherProvider
import app.passwordstore.util.extensions.getString import app.passwordstore.util.extensions.getString
import app.passwordstore.util.extensions.unsafeLazy import app.passwordstore.util.extensions.unsafeLazy
import app.passwordstore.util.extensions.viewBinding import app.passwordstore.util.extensions.viewBinding
@@ -30,7 +31,6 @@ import java.io.File
import javax.inject.Inject import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime import kotlin.time.ExperimentalTime
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
@@ -49,6 +49,7 @@ class DecryptActivity : BasePgpActivity() {
private val relativeParentPath by unsafeLazy { getParentPath(fullPath, repoPath) } private val relativeParentPath by unsafeLazy { getParentPath(fullPath, repoPath) }
@Inject lateinit var passwordEntryFactory: PasswordEntry.Factory @Inject lateinit var passwordEntryFactory: PasswordEntry.Factory
@Inject lateinit var repository: CryptoRepository @Inject lateinit var repository: CryptoRepository
@Inject lateinit var dispatcherProvider: DispatcherProvider
private var passwordEntry: PasswordEntry? = null private var passwordEntry: PasswordEntry? = null
private var retries = 0 private var retries = 0
@@ -147,7 +148,7 @@ class DecryptActivity : BasePgpActivity() {
if (isError) { if (isError) {
dialog.setError() dialog.setError()
} }
lifecycleScope.launch(Dispatchers.Main) { lifecycleScope.launch(dispatcherProvider.main()) {
dialog.password.collectLatest { value -> dialog.password.collectLatest { value ->
if (value != null) { if (value != null) {
when (val result = decryptWithPassphrase(value)) { when (val result = decryptWithPassphrase(value)) {
@@ -169,7 +170,7 @@ class DecryptActivity : BasePgpActivity() {
} }
private suspend fun decryptWithPassphrase(password: String) = runCatching { private suspend fun decryptWithPassphrase(password: String) = runCatching {
val message = withContext(Dispatchers.IO) { File(fullPath).readBytes().inputStream() } val message = withContext(dispatcherProvider.io()) { File(fullPath).readBytes().inputStream() }
val outputStream = ByteArrayOutputStream() val outputStream = ByteArrayOutputStream()
val result = val result =
repository.decrypt( repository.decrypt(
@@ -184,7 +185,7 @@ class DecryptActivity : BasePgpActivity() {
} }
private suspend fun createPasswordUI(entry: PasswordEntry) = private suspend fun createPasswordUI(entry: PasswordEntry) =
withContext(Dispatchers.Main) { withContext(dispatcherProvider.main()) {
val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true) val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true)
invalidateOptionsMenu() invalidateOptionsMenu()