Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed backup local hint #1357

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.vultisig.wallet.data.models.getVaultPart
import com.vultisig.wallet.data.repositories.VaultDataStoreRepository
import com.vultisig.wallet.data.repositories.VaultRepository
import com.vultisig.wallet.data.usecases.CreateVaultBackupUseCase
import com.vultisig.wallet.data.utils.TextFieldUtils.HINT_MAX_LENGTH
import com.vultisig.wallet.ui.navigation.Destination
import com.vultisig.wallet.ui.navigation.NavigationOptions
import com.vultisig.wallet.ui.navigation.Navigator
Expand All @@ -42,7 +41,6 @@ import javax.inject.Inject
internal data class BackupPasswordState(
val confirmPasswordErrorMessage: UiText? = null,
val passwordErrorMessage: UiText? = null,
val hintPasswordErrorMessage: UiText? = null,
val isPasswordVisible: Boolean = false,
val isConfirmPasswordVisible: Boolean = false,
val disableEncryption: Boolean = false,
Expand All @@ -64,7 +62,6 @@ internal class BackupPasswordViewModel @Inject constructor(

val passwordTextFieldState = TextFieldState()
val confirmPasswordTextFieldState = TextFieldState()
val hintPasswordTextFieldState = TextFieldState()

private val vaultId: String =
requireNotNull(savedStateHandle.get<String>(Destination.BackupPassword.ARG_VAULT_ID))
Expand Down Expand Up @@ -118,7 +115,7 @@ internal class BackupPasswordViewModel @Inject constructor(

fun backupEncryptedVault() {
if (shouldEnableEncryption()) {
if (validateConfirmPassword() && validateHint()) {
if (validateConfirmPassword()) {
val password = passwordTextFieldState.text.toString()
backupVault(password)
}
Expand Down Expand Up @@ -164,18 +161,6 @@ internal class BackupPasswordViewModel @Inject constructor(
return errorMessage == null
}

private fun validateHint(): Boolean {
val errorMessage =
if (hintPasswordTextFieldState.text.length > HINT_MAX_LENGTH)
UiText.StringResource(R.string.vault_password_hint_to_long)
else null

uiState.update {
it.copy(hintPasswordErrorMessage = errorMessage)
}
return errorMessage == null
}

fun togglePasswordVisibility() {
val isPasswordVisible = !uiState.value.isPasswordVisible
uiState.update {
Expand Down Expand Up @@ -207,7 +192,6 @@ internal class BackupPasswordViewModel @Inject constructor(
fun saveContentToUriResult(uri: Uri, content: String) {
if (isFileExtensionValid(uri)) {
val isSuccess = context.saveContentToUri(uri, content)
if (isSuccess) saveHintToDataStore(uri)
completeBackupVault(isSuccess)
} else {
viewModelScope.launch {
Expand All @@ -225,16 +209,6 @@ internal class BackupPasswordViewModel @Inject constructor(
}
}

private fun saveHintToDataStore(uri: Uri) = viewModelScope.launch {
if (hintPasswordTextFieldState.text.isNotEmpty() && passwordTextFieldState.text.isNotEmpty()) {
val fileName = uri.fileName(context)
vaultDataStoreRepository.setBackupHint(
fileName,
hintPasswordTextFieldState.text.toString()
)
}
}

private fun isFileExtensionValid(uri: Uri) =
FILE_ALLOWED_EXTENSIONS.any {
it == File(uri.fileName(context)).extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
Expand All @@ -38,7 +37,6 @@ internal data class ImportFileState(
val password: String? = null,
val isPasswordObfuscated: Boolean = true,
val passwordErrorHint: UiText? = null,
val passwordHint: UiText? = null,
)

internal val FILE_ALLOWED_MIME_TYPES = arrayOf("application/*")
Expand Down Expand Up @@ -93,33 +91,17 @@ internal class ImportFileViewModel @Inject constructor(
saveToDb(fileContent, null)
} catch (e: Exception) {
Timber.e(e)
val passwordHint = getPasswordHint(fileName)
uiModel.update {
it.copy(
showPasswordPrompt = true,
passwordErrorHint = null,
passwordHint = passwordHint,
)
}
}

}
}

private suspend fun getPasswordHint(fileName: String?): UiText? {
if (fileName == null) return null

val passwordHintString =
vaultDataStoreRepository.readBackupHint(vaultFileName = fileName).first()

if (passwordHintString.isEmpty()) return null

return UiText.FormattedText(
R.string.import_file_password_hint_text,
listOf(passwordHintString)
)
}

private suspend fun saveToDb(fileContent: String, password: String?) {
try {
insertVaultToDb(parseVaultFromString(fileContent, password))
Expand Down Expand Up @@ -186,5 +168,4 @@ internal class ImportFileViewModel @Inject constructor(
it.copy(isPasswordObfuscated = !passwordVisibility)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
Expand All @@ -36,7 +35,6 @@ import com.vultisig.wallet.ui.components.TopBar
import com.vultisig.wallet.ui.components.UiAlertDialog
import com.vultisig.wallet.ui.components.UiSpacer
import com.vultisig.wallet.ui.components.library.form.FormBasicSecureTextField
import com.vultisig.wallet.ui.components.library.form.FormTextFieldCard
import com.vultisig.wallet.ui.components.vultiGradient
import com.vultisig.wallet.ui.models.BackupPasswordViewModel
import com.vultisig.wallet.ui.theme.Theme
Expand Down Expand Up @@ -194,16 +192,6 @@ internal fun BackupPasswordScreen(navHostController: NavHostController) {
)
}
})
UiSpacer(size = 12.dp)
FormTextFieldCard(
title = stringResource(
R.string.backup_password_optional_password_protection_hint
),
hint = stringResource(R.string.backup_password_screen_hint_field),
error = uiState.hintPasswordErrorMessage,
keyboardType = KeyboardType.Text,
textFieldState = viewModel.hintPasswordTextFieldState,
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ import com.vultisig.wallet.ui.models.ImportFileState
import com.vultisig.wallet.ui.models.ImportFileViewModel
import com.vultisig.wallet.ui.theme.Theme
import com.vultisig.wallet.ui.utils.ActivityResultContractsGetContentWithMimeTypes
import com.vultisig.wallet.ui.utils.UiText
import com.vultisig.wallet.ui.utils.asString


@Composable
internal fun ImportFileScreen(
navController: NavHostController,
Expand Down Expand Up @@ -175,16 +173,6 @@ private fun ImportFileScreen(
}
)

if (uiModel.passwordErrorHint != null) {
UiSpacer(size = 8.dp)
Text(
modifier = Modifier.fillMaxWidth(),
text = uiModel.passwordHint?.asString()?: "",
color = appColor.neutral0,
style = menloFamily.body2,
)
}

TextButton(onClick = onConfirmPasswordClick) {
Text(
text = stringResource(R.string.import_file_screen_ok),
Expand Down Expand Up @@ -302,9 +290,7 @@ private fun ImportFileScreen(
}
}
}

}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ interface VaultDataStoreRepository {

suspend fun readBackupStatus(vaultId: String): Flow<Boolean>

suspend fun setBackupHint(vaultFileName: String, hint: String)

suspend fun readBackupHint(vaultFileName: String): Flow<String>

suspend fun setFastSignHint(vaultId: String, hint: String)

suspend fun readFastSignHint(vaultId: String): Flow<String>
Expand All @@ -40,15 +36,6 @@ internal class VaultDataStoreRepositoryImpl @Inject constructor(
override suspend fun readBackupStatus(vaultId: String): Flow<Boolean> =
appDataStore.readData(onVaultBackupStatusKey(vaultId), true)

override suspend fun setBackupHint(vaultFileName: String, hint: String) {
appDataStore.editData { preferences ->
preferences[onVaultBackupHintKey(vaultFileName)] = hint
}
}

override suspend fun readBackupHint(vaultFileName: String): Flow<String> =
appDataStore.readData(onVaultBackupHintKey(vaultFileName), "")

override suspend fun setFastSignHint(vaultId: String, hint: String) {
appDataStore.editData { preferences ->
preferences[onVaultFastSignHintKey(vaultId)] = hint
Expand All @@ -72,7 +59,6 @@ internal class VaultDataStoreRepositoryImpl @Inject constructor(
private companion object PreferencesKey {
fun onVaultBackupStatusKey(vaultId: String) = booleanPreferencesKey(name = "vault_backup/$vaultId")
fun onVaultFastSignHintKey(vaultId: String) = stringPreferencesKey(name = "vault_fast_sign_hint/$vaultId")
fun onVaultBackupHintKey(vaultFileName: String) = stringPreferencesKey(name = "vault_backup_hint/$vaultFileName")
fun onGlobalBackupReminderStatusKey() = intPreferencesKey(name = "global_backup_reminder_status")
}
}
Expand Down