Skip to content

Commit

Permalink
ADS DaxTextView: Support for android:textColor (#2546)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
malmstein committed Jan 12, 2023
1 parent 9b13f63 commit 880298c
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,6 +43,6 @@ class TypographyFragment : Fragment() {
savedInstanceBundle: Bundle?,
) {
val daxTextView = view.findViewById<DaxTextView>(R.id.typographyTitle)
daxTextView.setTypography(Type.Body1)
daxTextView.setTypography(Typography.Body1)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -114,22 +128,22 @@ 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
else -> Secondary
}
}

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
}
Expand Down
2 changes: 1 addition & 1 deletion common-ui/src/main/res/layout/bottom_sheet_action.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

Expand Down
4 changes: 2 additions & 2 deletions common-ui/src/main/res/layout/dialog_single_choice_alert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<com.duckduckgo.mobile.android.ui.view.text.DaxTextView
Expand All @@ -41,7 +41,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioListDialogTitle"
app:textColor="secondary"
app:textType="secondary"
app:typography="body1" />

<RadioGroup
Expand Down
4 changes: 2 additions & 2 deletions common-ui/src/main/res/layout/dialog_stacked_alert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/stackedAlertDialogImage"
app:textColor="primary"
app:textType="primary"
app:typography="h2" />

<com.duckduckgo.mobile.android.ui.view.text.DaxTextView
Expand All @@ -52,7 +52,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/stackedAlertDialogTitle"
app:textColor="secondary"
app:textType="secondary"
app:typography="body1" />

<LinearLayout
Expand Down
4 changes: 2 additions & 2 deletions common-ui/src/main/res/layout/dialog_text_alert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textAlertDialogImage"
app:textColor="primary"
app:textType="primary"
app:typography="h2" />

<com.duckduckgo.mobile.android.ui.view.text.DaxTextView
Expand All @@ -52,7 +52,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textAlertDialogTitle"
app:textColor="secondary"
app:textType="secondary"
app:typography="body1" />

<com.duckduckgo.mobile.android.ui.view.button.DaxButtonPrimary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
android:layout_marginTop="@dimen/keyline_2"
android:layout_marginBottom="@dimen/keyline_2"
android:text="Text Appearance Body 1 set manually"
android:textColor="@color/red_text_color_selector"
tools:ignore="HardcodedText"/>

<com.duckduckgo.mobile.android.ui.view.listitem.SectionHeaderListItem
Expand Down
2 changes: 1 addition & 1 deletion common-ui/src/main/res/layout/view_two_line_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
app:layout_constraintEnd_toStartOf="@+id/trailingContainer"
app:layout_constraintStart_toStartOf="@id/primaryText"
app:layout_constraintTop_toBottomOf="@id/primaryText"
app:textColor="secondary"
app:textType="secondary"
app:typography="body2"
tools:text="Secondary Text" />

Expand Down
3 changes: 2 additions & 1 deletion common-ui/src/main/res/values/attrs-typography.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
<enum name="button" value="8"/>
<enum name="caption" value="9"/>
</attr>
<attr name="textColor" format="enum" >
<attr name="textType" format="enum" >
<enum name="primary" value="0"/>
<enum name="secondary" value="1"/>
</attr>
<attr name="android:textColor" format="color"/>
</declare-styleable>

<!-- Attributes for all Design System Text Appearances -->
Expand Down
1 change: 0 additions & 1 deletion common-ui/src/main/res/values/design-system-typograhy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@

<style name="Typography"/>
<style name="Typography.DuckDuckGo">
<item name="android:textColor">?attr/daxColorPrimaryText</item>
<item name="android:textStyle">normal</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:letterSpacing">0.0</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.duckduckgo.lint.NoSingletonDetector.Companion.NO_SINGLETON_ISSUE
import com.duckduckgo.lint.NoSystemLoadLibraryDetector.Companion.NO_SYSTEM_LOAD_LIBRARY
import com.duckduckgo.lint.strings.MissingInstructionDetector.Companion.MISSING_INSTRUCTION
import com.duckduckgo.lint.strings.PlaceholderDetector.Companion.PLACEHOLDER_MISSING_POSITION
import com.duckduckgo.lint.ui.DaxTextViewStylingDetector.Companion.INVALID_DAX_TEXT_VIEW_PROPERTY
import com.duckduckgo.lint.ui.DeprecatedAndroidButtonUsedInXmlDetector.Companion.DEPRECATED_BUTTON_IN_XML
import com.duckduckgo.lint.ui.DeprecatedSwitchUsedInXmlDetector.Companion.DEPRECATED_SWITCH_IN_XML
import com.duckduckgo.lint.ui.MissingDividerDetector.Companion.MISSING_HORIZONTAL_DIVIDER
Expand Down Expand Up @@ -59,6 +60,7 @@ class DuckDuckGoIssueRegistry : IssueRegistry() {
MISSING_VERTICAL_DIVIDER,
MISSING_HORIZONTAL_DIVIDER,
NO_BOTTOM_SHEET,
INVALID_DAX_TEXT_VIEW_PROPERTY,
)

override val api: Int
Expand Down

0 comments on commit 880298c

Please sign in to comment.