From e40abcf40bb0e2486c22fd2025409d752773640e Mon Sep 17 00:00:00 2001 From: Cuong-Tran Date: Wed, 30 Apr 2025 02:49:27 +0700 Subject: [PATCH] chore(custom-color): refactor EditText and TextInputLayout color handling (#885) --- .../settings/screen/SettingsTrackingScreen.kt | 68 +++--------------- .../theme/colorscheme/CustomColorScheme.kt | 70 ++++++++++++++++++- .../design/MigrationBottomSheetDialog.kt | 23 +----- .../tachiyomi/ui/manga/EditMangaDialog.kt | 25 +------ .../MaterialAlertDialogBuilderExtensions.kt | 45 +++--------- .../main/res/layout/dialog_stub_textinput.xml | 3 +- 6 files changed, 92 insertions(+), 142 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsTrackingScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsTrackingScreen.kt index 601e55a3b..bcc5108bd 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsTrackingScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsTrackingScreen.kt @@ -1,8 +1,6 @@ package eu.kanade.presentation.more.settings.screen import android.content.Context -import android.content.res.ColorStateList -import android.os.Build import android.view.LayoutInflater import android.view.View import androidx.compose.foundation.layout.Arrangement @@ -258,25 +256,11 @@ object SettingsTrackingScreen : SearchableSettings { val usernameEditText = binding.usernameEditText val passwordEditText = binding.passwordEditText - // Apply color scheme and selection handles - listOf(usernameEditText, passwordEditText).forEach { editText -> - editText.setTextColor(colorScheme.textColor) - editText.highlightColor = colorScheme.textHighlightColor - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - editText.textSelectHandle?.let { drawable -> - drawable.setTint(colorScheme.primary) - editText.setTextSelectHandle(drawable) - } - editText.textSelectHandleLeft?.let { drawable -> - drawable.setTint(colorScheme.primary) - editText.setTextSelectHandleLeft(drawable) - } - editText.textSelectHandleRight?.let { drawable -> - drawable.setTint(colorScheme.primary) - editText.setTextSelectHandleRight(drawable) - } - } + listOf( + usernameEditText, + passwordEditText, + ).forEach { + colorScheme.setEditTextColor(it) } usernameInputLayout.hint = usernameHint @@ -306,43 +290,11 @@ object SettingsTrackingScreen : SearchableSettings { val usernameInputLayout = binding.usernameInputLayout val passwordInputLayout = binding.passwordInputLayout - val (strokeColorFocused, strokeColorDefault, hintColor, cursorColor) = if (inputError) { - arrayOf( - colorScheme.error, - colorScheme.error, - colorScheme.error, - colorScheme.error, - ) - } else { - arrayOf( - colorScheme.primary, - colorScheme.onSurfaceVariant, - colorScheme.primary, - colorScheme.primary, - ) - } - - val boxStrokeColorStateList = ColorStateList( - arrayOf( - intArrayOf(android.R.attr.state_focused), - intArrayOf(), // Default state - ), - intArrayOf( - strokeColorFocused, - strokeColorDefault, - ), - ) - val hintTextColorStateList = ColorStateList.valueOf(hintColor) - val endIconTintList = ColorStateList.valueOf(colorScheme.onSurfaceVariant) - val cursorColorStateList = ColorStateList.valueOf(cursorColor) - - listOf(usernameInputLayout, passwordInputLayout).forEach { - it.setBoxStrokeColorStateList(boxStrokeColorStateList) - it.hintTextColor = hintTextColorStateList - it.setEndIconTintList(endIconTintList) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - it.cursorColor = cursorColorStateList - } + listOf( + usernameInputLayout, + passwordInputLayout, + ).forEach { + colorScheme.setTextInputLayoutColor(it, inputError) } }, modifier = Modifier.fillMaxWidth(), diff --git a/app/src/main/java/eu/kanade/presentation/theme/colorscheme/CustomColorScheme.kt b/app/src/main/java/eu/kanade/presentation/theme/colorscheme/CustomColorScheme.kt index 56dff82a6..ff5951484 100644 --- a/app/src/main/java/eu/kanade/presentation/theme/colorscheme/CustomColorScheme.kt +++ b/app/src/main/java/eu/kanade/presentation/theme/colorscheme/CustomColorScheme.kt @@ -4,6 +4,7 @@ import android.app.UiModeManager import android.content.Context import android.content.res.ColorStateList import android.os.Build +import android.widget.EditText import androidx.annotation.ColorInt import androidx.compose.material3.ColorScheme import androidx.compose.material3.surfaceColorAtElevation @@ -11,7 +12,10 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.unit.dp import androidx.core.content.getSystemService +import androidx.core.graphics.drawable.toDrawable import com.google.android.material.progressindicator.LinearProgressIndicator +import com.google.android.material.textfield.TextInputEditText +import com.google.android.material.textfield.TextInputLayout import com.materialkolor.Contrast import com.materialkolor.PaletteStyle import com.materialkolor.dynamicColorScheme @@ -188,7 +192,7 @@ class AndroidViewColorScheme( ), intArrayOf( onPrimary, - onSurface, + onSurfaceVariant, ), ) @@ -199,7 +203,7 @@ class AndroidViewColorScheme( ), intArrayOf( primary, - onSurface, + onSurfaceVariant, ), ) @@ -210,7 +214,7 @@ class AndroidViewColorScheme( ), intArrayOf( primary, - onSurface, + onSurfaceVariant, ), ) @@ -227,6 +231,66 @@ class AndroidViewColorScheme( ), ) + /** + * Set the color of the [TextInputEditText] or [EditText]. + * @param editText The EditText to set the color for. + */ + fun setEditTextColor( + editText: EditText, + ) { + editText.setTextColor(onSurfaceVariant) + editText.highlightColor = inversePrimary + editText.backgroundTintList = editTextBackgroundTintList + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + editText.textCursorDrawable = primary.toDrawable() + editText.textSelectHandle?.let { drawable -> + drawable.setTint(primary) + editText.setTextSelectHandle(drawable) + } + editText.textSelectHandleLeft?.let { drawable -> + drawable.setTint(primary) + editText.setTextSelectHandleLeft(drawable) + } + editText.textSelectHandleRight?.let { drawable -> + drawable.setTint(primary) + editText.setTextSelectHandleRight(drawable) + } + } + } + + fun setTextInputLayoutColor( + inputLayout: TextInputLayout, + isError: Boolean = false, + ) { + val (strokeColorFocused, strokeColorDefault, hintColor, cursorColor) = if (isError) { + arrayOf(error, error, error, error) + } else { + arrayOf(primary, onSurfaceVariant, primary, primary) + } + + val boxStrokeColorStateList = ColorStateList( + arrayOf( + intArrayOf(android.R.attr.state_focused), + intArrayOf(), // Default state + ), + intArrayOf( + strokeColorFocused, + strokeColorDefault, + ), + ) + val hintTextColorStateList = ColorStateList.valueOf(hintColor) + val endIconTintList = ColorStateList.valueOf(onSurfaceVariant) + val cursorColorStateList = ColorStateList.valueOf(cursorColor) + + inputLayout.setBoxStrokeColorStateList(boxStrokeColorStateList) + inputLayout.hintTextColor = hintTextColorStateList + inputLayout.setEndIconTintList(endIconTintList) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + inputLayout.cursorColor = cursorColorStateList + } + } + companion object { fun LinearProgressIndicator.setColors(colorScheme: AndroidViewColorScheme) { trackColor = colorScheme.secondaryContainer diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/advanced/design/MigrationBottomSheetDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/advanced/design/MigrationBottomSheetDialog.kt index cc622c4d0..59d831c4c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/advanced/design/MigrationBottomSheetDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/advanced/design/MigrationBottomSheetDialog.kt @@ -1,6 +1,5 @@ package eu.kanade.tachiyomi.ui.browse.migration.advanced.design -import android.os.Build import android.view.LayoutInflater import android.widget.CompoundButton import android.widget.RadioButton @@ -14,7 +13,6 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Modifier import androidx.compose.ui.viewinterop.AndroidView -import androidx.core.graphics.drawable.toDrawable import androidx.core.view.isVisible import eu.kanade.presentation.components.AdaptiveSheet import eu.kanade.presentation.theme.colorscheme.AndroidViewColorScheme @@ -83,26 +81,7 @@ fun MigrationBottomSheetDialog( binding.HideNotFoundManga.thumbTintList = colorScheme.thumbTintList binding.OnlyShowUpdates.thumbTintList = colorScheme.thumbTintList - with(binding.extraSearchParamText) { - highlightColor = colorScheme.textHighlightColor - backgroundTintList = colorScheme.editTextBackgroundTintList - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - textCursorDrawable = colorScheme.primary.toDrawable() - textSelectHandle?.let { drawable -> - drawable.setTint(colorScheme.primary) - setTextSelectHandle(drawable) - } - textSelectHandleLeft?.let { drawable -> - drawable.setTint(colorScheme.primary) - setTextSelectHandleLeft(drawable) - } - textSelectHandleRight?.let { drawable -> - drawable.setTint(colorScheme.primary) - setTextSelectHandleRight(drawable) - } - } - } + colorScheme.setEditTextColor(binding.extraSearchParamText) // KMK <-- binding.root }, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/EditMangaDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/EditMangaDialog.kt index 24812841e..3e7b5f78c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/EditMangaDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/EditMangaDialog.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.manga import android.content.Context import android.content.res.ColorStateList import android.graphics.Color -import android.os.Build import android.view.LayoutInflater import android.view.View import android.widget.AdapterView @@ -359,23 +358,7 @@ private fun onViewCreated( binding.thumbnailUrl, binding.mangaDescription, ).forEach { - it.setTextColor(colorScheme.textColor) - it.highlightColor = colorScheme.textHighlightColor - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - it.textSelectHandle?.let { drawable -> - drawable.setTint(colorScheme.iconColor) - it.setTextSelectHandle(drawable) - } - it.textSelectHandleLeft?.let { drawable -> - drawable.setTint(colorScheme.iconColor) - it.setTextSelectHandleLeft(drawable) - } - it.textSelectHandleRight?.let { drawable -> - drawable.setTint(colorScheme.iconColor) - it.setTextSelectHandleRight(drawable) - } - } + colorScheme.setEditTextColor(it) } listOf( binding.titleOutline, @@ -384,11 +367,7 @@ private fun onViewCreated( binding.thumbnailUrlOutline, binding.mangaDescriptionOutline, ).forEach { - it.boxStrokeColor = colorScheme.iconColor - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - it.cursorColor = ColorStateList.valueOf(colorScheme.iconColor) - } + colorScheme.setTextInputLayoutColor(it) } binding.autofillFromTracker.setTextColor(colorScheme.btnTextColor) diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/materialdialogs/MaterialAlertDialogBuilderExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/materialdialogs/MaterialAlertDialogBuilderExtensions.kt index 67a93a230..63ee677e7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/materialdialogs/MaterialAlertDialogBuilderExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/materialdialogs/MaterialAlertDialogBuilderExtensions.kt @@ -1,14 +1,12 @@ package eu.kanade.tachiyomi.widget.materialdialogs import android.content.Context -import android.content.res.ColorStateList import android.graphics.Canvas import android.graphics.ColorFilter import android.graphics.Paint import android.graphics.PixelFormat import android.graphics.RectF import android.graphics.drawable.Drawable -import android.os.Build import android.view.LayoutInflater import android.view.inputmethod.InputMethodManager import android.widget.TextView @@ -39,7 +37,7 @@ fun DialogStubTextinputBinding.setPositiveButton(text: String, onClick: (String) positiveButton.text = text positiveButton.setOnClickListener { - val textEdit = textField.editText + val textEdit = textInputLayout.editText onClick(textEdit?.text?.toString() ?: "") } return this @@ -56,13 +54,13 @@ fun DialogStubTextinputBinding.setNegativeButton(text: String, onClick: () -> Un @Suppress("unused") fun DialogStubTextinputBinding.setHint(hint: String? = null): DialogStubTextinputBinding { - textField.hint = hint + textInputLayout.hint = hint return this } fun DialogStubTextinputBinding.setTextEdit(prefill: String? = null): DialogStubTextinputBinding { // KMK <-- - textField.editText?.apply { + textInputLayout.editText?.apply { setText(prefill, TextView.BufferType.EDITABLE) post { requestFocusFromTouch() @@ -73,38 +71,15 @@ fun DialogStubTextinputBinding.setTextEdit(prefill: String? = null): DialogStubT return this } -fun DialogStubTextinputBinding.setColors(colors: AndroidViewColorScheme): DialogStubTextinputBinding { - textField.boxStrokeColor = colors.iconColor +fun DialogStubTextinputBinding.setColors(colorScheme: AndroidViewColorScheme): DialogStubTextinputBinding { + colorScheme.setTextInputLayoutColor(textInputLayout) + colorScheme.setEditTextColor(textInputEdit) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - textField.cursorColor = ColorStateList.valueOf(colors.iconColor) - } + alertTitle.setTextColor(colorScheme.textColor) + positiveButton.setTextColor(colorScheme.iconColor) + negativeButton.setTextColor(colorScheme.iconColor) - textField.editText?.apply { - setTextColor(colors.textColor) - highlightColor = colors.textHighlightColor - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - textSelectHandle?.let { drawable -> - drawable.setTint(colors.iconColor) - setTextSelectHandle(drawable) - } - textSelectHandleLeft?.let { drawable -> - drawable.setTint(colors.iconColor) - setTextSelectHandleLeft(drawable) - } - textSelectHandleRight?.let { drawable -> - drawable.setTint(colors.iconColor) - setTextSelectHandleRight(drawable) - } - } - } - - alertTitle.setTextColor(colors.textColor) - positiveButton.setTextColor(colors.iconColor) - negativeButton.setTextColor(colors.iconColor) - - root.background = RoundedCornerDrawable(color = colors.dialogBgColor) + root.background = RoundedCornerDrawable(color = colorScheme.dialogBgColor) return this } diff --git a/app/src/main/res/layout/dialog_stub_textinput.xml b/app/src/main/res/layout/dialog_stub_textinput.xml index 2f077acb0..6b177a090 100644 --- a/app/src/main/res/layout/dialog_stub_textinput.xml +++ b/app/src/main/res/layout/dialog_stub_textinput.xml @@ -15,12 +15,13 @@ android:layout_marginBottom="16dp" />