From fd7de720e8df38f488eade48dd66efcfd140e8f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gonz=C3=A1lez?= Date: Wed, 23 Nov 2022 12:51:33 +0100 Subject: [PATCH] ADS DaxTextView: Support for android:textColor (#2546) Task/Issue URL: https://app.asana.com/0/1202857801505092/1203418290512953 ### Description - Support the textColor attribute from Android - When textType and textColor are both set, textType will take priority. --- .../ui/typography/TypographyFragment.kt | 4 +- .../mobile/android/ui/view/TypedArrayUtils.kt | 69 +++++++++++++ .../android/ui/view/text/DaxTextView.kt | 58 ++++++----- .../main/res/layout/bottom_sheet_action.xml | 2 +- .../res/layout/dialog_single_choice_alert.xml | 4 +- .../main/res/layout/dialog_stacked_alert.xml | 4 +- .../src/main/res/layout/dialog_text_alert.xml | 4 +- .../layout/fragment_components_typography.xml | 1 + .../main/res/layout/view_two_line_item.xml | 2 +- .../src/main/res/values/attrs-typography.xml | 3 +- .../res/values/design-system-typograhy.xml | 1 - .../lint/registry/DuckDuckGoIssueRegistry.kt | 2 + .../lint/ui/DaxTextViewStylingDetector.kt | 97 +++++++++++++++++++ 13 files changed, 217 insertions(+), 34 deletions(-) create mode 100644 common-ui/src/main/java/com/duckduckgo/mobile/android/ui/view/TypedArrayUtils.kt create mode 100644 lint-rules/src/main/java/com/duckduckgo/lint/ui/DaxTextViewStylingDetector.kt diff --git a/common-ui/src/main/java/com/duckduckgo/mobile/android/themepreview/ui/typography/TypographyFragment.kt b/common-ui/src/main/java/com/duckduckgo/mobile/android/themepreview/ui/typography/TypographyFragment.kt index 1e73dca65bb1..a0e3d48e6188 100644 --- a/common-ui/src/main/java/com/duckduckgo/mobile/android/themepreview/ui/typography/TypographyFragment.kt +++ b/common-ui/src/main/java/com/duckduckgo/mobile/android/themepreview/ui/typography/TypographyFragment.kt @@ -24,7 +24,7 @@ import android.view.ViewGroup import androidx.fragment.app.Fragment import com.duckduckgo.mobile.android.R import com.duckduckgo.mobile.android.ui.view.text.DaxTextView -import com.duckduckgo.mobile.android.ui.view.text.DaxTextView.Type +import com.duckduckgo.mobile.android.ui.view.text.DaxTextView.Typography /** Fragment to display a list of subsystems that show the values of this app's theme. */ @SuppressLint("NoFragment") // we don't use DI here @@ -43,6 +43,6 @@ class TypographyFragment : Fragment() { savedInstanceBundle: Bundle?, ) { val daxTextView = view.findViewById(R.id.typographyTitle) - daxTextView.setTypography(Type.Body1) + daxTextView.setTypography(Typography.Body1) } } diff --git a/common-ui/src/main/java/com/duckduckgo/mobile/android/ui/view/TypedArrayUtils.kt b/common-ui/src/main/java/com/duckduckgo/mobile/android/ui/view/TypedArrayUtils.kt new file mode 100644 index 000000000000..88e0d927ca79 --- /dev/null +++ b/common-ui/src/main/java/com/duckduckgo/mobile/android/ui/view/TypedArrayUtils.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 DuckDuckGo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.duckduckgo.mobile.android.ui.view + +import android.content.Context +import android.content.res.ColorStateList +import android.content.res.TypedArray +import android.graphics.drawable.Drawable +import androidx.annotation.ColorRes +import androidx.annotation.StyleableRes +import androidx.appcompat.content.res.AppCompatResources +import com.duckduckgo.mobile.android.R + +/** + * Utility methods for extracting [ColorStateList]s and [Drawable]s from a [TypedArray]. + * The resources are inflated using [AppCompatResources], which provides bug fixes and backports + * features introduced on new platforms. + */ +object TypedArrayUtils { + + fun getColorStateList( + context: Context, + typedArray: TypedArray, + @StyleableRes index: Int, + @ColorRes defaultValue: Int = R.color.primary_text_color_selector, + ): ColorStateList? { + if (typedArray.hasValue(index)) { + val resourceId = typedArray.getResourceId(index, defaultValue) + if (resourceId != 0) { + val value = AppCompatResources.getColorStateList(context, resourceId) + if (value != null) { + return value + } + } + } + return typedArray.getColorStateList(index) + } + + fun getDrawable( + context: Context, + typedArray: TypedArray, + @StyleableRes index: Int, + ): Drawable? { + if (typedArray.hasValue(index)) { + val resourceId = typedArray.getResourceId(index, 0) + if (resourceId != 0) { + val value = AppCompatResources.getDrawable(context, resourceId) + if (value != null) { + return value + } + } + } + return typedArray.getDrawable(index) + } +} diff --git a/common-ui/src/main/java/com/duckduckgo/mobile/android/ui/view/text/DaxTextView.kt b/common-ui/src/main/java/com/duckduckgo/mobile/android/ui/view/text/DaxTextView.kt index fa15f95b2c18..3d30afc4d1ed 100644 --- a/common-ui/src/main/java/com/duckduckgo/mobile/android/ui/view/text/DaxTextView.kt +++ b/common-ui/src/main/java/com/duckduckgo/mobile/android/ui/view/text/DaxTextView.kt @@ -20,9 +20,9 @@ import android.content.Context import android.util.AttributeSet import androidx.core.content.ContextCompat import com.duckduckgo.mobile.android.R -import com.duckduckgo.mobile.android.ui.view.text.DaxTextView.Type.Body1 +import com.duckduckgo.mobile.android.ui.view.TypedArrayUtils +import com.duckduckgo.mobile.android.ui.view.text.DaxTextView.Typography.Body1 import com.google.android.material.textview.MaterialTextView -import java.lang.reflect.Array.getInt class DaxTextView @JvmOverloads constructor( @@ -40,33 +40,47 @@ constructor( 0, ) - val typographyType = if (typedArray.hasValue(R.styleable.DaxTextView_typography)) { - Type.from(typedArray.getInt(R.styleable.DaxTextView_typography, 0)) + val typography = if (typedArray.hasValue(R.styleable.DaxTextView_typography)) { + Typography.from(typedArray.getInt(R.styleable.DaxTextView_typography, 0)) } else { Body1 } - setTypography(typographyType) + setTypography(typography) - val textColor = if (typedArray.hasValue(R.styleable.DaxTextView_textColor)) { - TextColor.from(typedArray.getInt(R.styleable.DaxTextView_textColor, 0)) - } else { - TextColor.Primary + val hasType = typedArray.hasValue(R.styleable.DaxTextView_textType) + val hasTextColor = typedArray.hasValue(R.styleable.DaxTextView_android_textColor) + + when { + hasType -> { + val textType = TextType.from(typedArray.getInt(R.styleable.DaxTextView_textType, 0)) + setTextColorStateList(textType) + } + + hasTextColor -> { + val colorStateList = TypedArrayUtils.getColorStateList(context, typedArray, R.styleable.DaxTextView_android_textColor) + if (colorStateList != null) { + setTextColor(colorStateList) + } else { + setTextColor(ContextCompat.getColorStateList(context, R.color.primary_text_color_selector)) + } + } + + else -> setTextColorStateList(TextType.Primary) } - setTextColorStateList(textColor) typedArray.recycle() } - fun setTypography(type: Type) { - setTextAppearance(Type.getTextAppearanceStyle(type)) + fun setTypography(typography: Typography) { + setTextAppearance(Typography.getTextAppearanceStyle(typography)) } - fun setTextColorStateList(textColor: TextColor) { - setTextColor(ContextCompat.getColorStateList(context, TextColor.getTextColorStateList(textColor))) + fun setTextColorStateList(textType: TextType) { + setTextColor(ContextCompat.getColorStateList(context, TextType.getTextColorStateList(textType))) } - enum class Type { + enum class Typography { Title, H1, H2, @@ -80,7 +94,7 @@ constructor( ; companion object { - fun from(type: Int): Type { + fun from(type: Int): Typography { // same order as attrs-typography.xml return when (type) { 0 -> Title @@ -97,8 +111,8 @@ constructor( } } - fun getTextAppearanceStyle(type: Type): Int { - return when (type) { + fun getTextAppearanceStyle(typography: Typography): Int { + return when (typography) { Title -> R.style.Typography_DuckDuckGo_Title H1 -> R.style.Typography_DuckDuckGo_H1 H2 -> R.style.Typography_DuckDuckGo_H2 @@ -114,13 +128,13 @@ constructor( } } - enum class TextColor { + enum class TextType { Primary, Secondary, ; companion object { - fun from(type: Int): TextColor { + fun from(type: Int): TextType { // same order as attrs-typography.xml return when (type) { 0 -> Primary @@ -128,8 +142,8 @@ constructor( } } - fun getTextColorStateList(textColor: TextColor): Int { - return when (textColor) { + fun getTextColorStateList(textType: TextType): Int { + return when (textType) { Primary -> R.color.primary_text_color_selector Secondary -> R.color.secondary_text_color_selector } diff --git a/common-ui/src/main/res/layout/bottom_sheet_action.xml b/common-ui/src/main/res/layout/bottom_sheet_action.xml index 71890b61847d..a161f330ef0f 100644 --- a/common-ui/src/main/res/layout/bottom_sheet_action.xml +++ b/common-ui/src/main/res/layout/bottom_sheet_action.xml @@ -31,7 +31,7 @@ android:paddingTop="@dimen/bottomSheetTitleVerticalPadding" android:paddingBottom="@dimen/bottomSheetTitleVerticalPadding" android:visibility="gone" - app:textColor="secondary" + app:textType="secondary" app:typography="body1" tools:text="Actions" /> diff --git a/common-ui/src/main/res/layout/dialog_single_choice_alert.xml b/common-ui/src/main/res/layout/dialog_single_choice_alert.xml index 32120f4d17f5..0a454923b29a 100644 --- a/common-ui/src/main/res/layout/dialog_single_choice_alert.xml +++ b/common-ui/src/main/res/layout/dialog_single_choice_alert.xml @@ -30,7 +30,7 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - app:textColor="primary" + app:textType="primary" app:typography="h2" /> diff --git a/common-ui/src/main/res/values/attrs-typography.xml b/common-ui/src/main/res/values/attrs-typography.xml index 2da7bbddd6b9..c2a6af94ecb9 100644 --- a/common-ui/src/main/res/values/attrs-typography.xml +++ b/common-ui/src/main/res/values/attrs-typography.xml @@ -30,10 +30,11 @@ - + + diff --git a/common-ui/src/main/res/values/design-system-typograhy.xml b/common-ui/src/main/res/values/design-system-typograhy.xml index 689f3c256fbf..2d74cfa4f9c4 100644 --- a/common-ui/src/main/res/values/design-system-typograhy.xml +++ b/common-ui/src/main/res/values/design-system-typograhy.xml @@ -87,7 +87,6 @@