From c433fb643c63d41a94bac6a28edf9b6bda51af14 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sat, 4 Nov 2023 17:33:40 +0100 Subject: [PATCH] fix: change password color dynamically #1490 --- CHANGELOG | 1 + .../keepass/view/TextEditFieldView.kt | 38 +++++++++++++++---- .../metadata/android/en-US/changelogs/126.txt | 1 + .../metadata/android/fr-FR/changelogs/126.txt | 1 + 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3a84a8155..003bc9d4f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ KeePassDX(4.0.3) * Fix "Save as" in Read Only mode #1666 * Fix username autofill #1665 #530 #1572 #1426 #1523 #1556 #1653 #1658 #1508 #1667 * Fix regex OTP recognition #1596 + * Change password color dynamically #1490 * Small fixes #1641 #1656 KeePassDX(4.0.2) diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TextEditFieldView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TextEditFieldView.kt index e426dc3b8..04024b915 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TextEditFieldView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TextEditFieldView.kt @@ -4,6 +4,7 @@ import android.content.Context import android.os.Build import android.text.InputFilter import android.text.InputType +import android.text.Spannable import android.text.SpannableString import android.util.AttributeSet import android.util.TypedValue @@ -17,6 +18,7 @@ import androidx.appcompat.widget.AppCompatImageButton import androidx.core.content.ContextCompat import androidx.core.view.ViewCompat import androidx.core.view.isVisible +import androidx.core.widget.doAfterTextChanged import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputLayout import com.kunzisoft.keepass.R @@ -34,6 +36,9 @@ class TextEditFieldView @JvmOverloads constructor(context: Context, private var valueViewId = ViewCompat.generateViewId() private var actionImageButtonId = ViewCompat.generateViewId() + private var textModified = false + private var isColorizedPasswordActivated = PreferencesUtil.colorizePassword(context) + private val labelView = TextInputLayout(context).apply { layoutParams = LayoutParams( LayoutParams.MATCH_PARENT, @@ -78,6 +83,20 @@ class TextEditFieldView @JvmOverloads constructor(context: Context, init { // Manually write view to avoid view id bugs buildViews() + // To change the password color dynamically + valueView.doAfterTextChanged { editable -> + editable?.let { text -> + if (textModified) { + textModified = false + } else { + textModified = true + val selectionStart = valueView.selectionStart + val selectionEnd = valueView.selectionEnd + value = spannableValue(text.toString()).toString() + valueView.setSelection(selectionStart, selectionEnd) + } + } + } labelView.addView(valueView) addView(labelView) addView(actionImageButton) @@ -110,6 +129,15 @@ class TextEditFieldView @JvmOverloads constructor(context: Context, return actionImageButton } + private fun spannableValue(value: String?): Spannable? { + if (value == null) + return null + return if (isColorizedPasswordActivated && TemplateField.isStandardPasswordName(context, label)) + PasswordGenerator.getColorizedPassword(value) + else + SpannableString(value) + } + override var label: String get() { return labelView.hint?.toString() ?: "" @@ -128,13 +156,7 @@ class TextEditFieldView @JvmOverloads constructor(context: Context, return valueView.text?.toString() ?: "" } set(value) { - val spannableString = - if (PreferencesUtil.colorizePassword(context) - && TemplateField.isStandardPasswordName(context, label)) - PasswordGenerator.getColorizedPassword(value) - else - SpannableString(value) - valueView.setText(spannableString) + valueView.setText(spannableValue(value)) } override var default: String = "" @@ -145,6 +167,7 @@ class TextEditFieldView @JvmOverloads constructor(context: Context, valueView.filters += InputFilter.LengthFilter(MAX_CHARS_LIMIT) } else -> { + @Suppress("KotlinConstantConditions") val chars = if (numberChars > MAX_CHARS_LIMIT) MAX_CHARS_LIMIT else numberChars valueView.filters += InputFilter.LengthFilter(chars) } @@ -164,6 +187,7 @@ class TextEditFieldView @JvmOverloads constructor(context: Context, valueView.maxLines = MAX_LINES_LIMIT } else -> { + @Suppress("KotlinConstantConditions") val lines = if (numberLines > MAX_LINES_LIMIT) MAX_LINES_LIMIT else numberLines valueView.inputType = valueView.inputType or InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE diff --git a/fastlane/metadata/android/en-US/changelogs/126.txt b/fastlane/metadata/android/en-US/changelogs/126.txt index 13e86f89c..ba029fb53 100644 --- a/fastlane/metadata/android/en-US/changelogs/126.txt +++ b/fastlane/metadata/android/en-US/changelogs/126.txt @@ -1,4 +1,5 @@ * Fix "Save as" in Read Only mode #1666 * Fix username autofill #1665 #530 #1572 #1426 #1523 #1556 #1653 #1658 #1508 #1667 * Fix regex OTP recognition #1596 + * Change password color dynamically #1490 * Small fixes #1641 #1656 \ No newline at end of file diff --git a/fastlane/metadata/android/fr-FR/changelogs/126.txt b/fastlane/metadata/android/fr-FR/changelogs/126.txt index 5186ad6f3..1211b0c18 100644 --- a/fastlane/metadata/android/fr-FR/changelogs/126.txt +++ b/fastlane/metadata/android/fr-FR/changelogs/126.txt @@ -1,4 +1,5 @@ * Correction "Sauvegarder comme" en mode lecture seule #1666 * Correction du nom d'utilisateur dans la reconnaissance automatique #1665 #530 #1572 #1426 #1523 #1556 #1653 #1658 #1508 #1667 * Correction de la regex de reconnaissance OTP #1596 + * Changement de couleur de mot passe dynamique #1490 * Petites corrections #1641 #1656