From 6688d381a29b8c9294ebc6d19c49183e1e9457be Mon Sep 17 00:00:00 2001 From: Alan Lee Date: Thu, 4 Apr 2024 17:50:25 -0700 Subject: [PATCH] convert DefaultStyleValuesUtil to Kotlin (#43884) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43884 convert Java to Kotlin: `/react/view/text/DefaultStyleValuesUtil.java` Changelog: [Internal] internal Differential Revision: D55777322 --- .../ReactAndroid/api/ReactAndroid.api | 7 +- .../views/text/DefaultStyleValuesUtil.java | 77 ------------------- .../views/text/DefaultStyleValuesUtil.kt | 53 +++++++++++++ 3 files changed, 57 insertions(+), 80 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index f92d688153612d..daa691e7ad3dd7 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -6874,9 +6874,10 @@ public class com/facebook/react/views/switchview/ReactSwitchManager$ReactSwitchS } public final class com/facebook/react/views/text/DefaultStyleValuesUtil { - public static fun getDefaultTextColor (Landroid/content/Context;)Landroid/content/res/ColorStateList; - public static fun getDefaultTextColorHighlight (Landroid/content/Context;)I - public static fun getDefaultTextColorHint (Landroid/content/Context;)Landroid/content/res/ColorStateList; + public static final field INSTANCE Lcom/facebook/react/views/text/DefaultStyleValuesUtil; + public static final fun getDefaultTextColor (Landroid/content/Context;)Landroid/content/res/ColorStateList; + public static final fun getDefaultTextColorHighlight (Landroid/content/Context;)I + public static final fun getDefaultTextColorHint (Landroid/content/Context;)Landroid/content/res/ColorStateList; } public class com/facebook/react/views/text/FontMetricsUtil { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.java deleted file mode 100644 index 86e64ac748862e..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.views.text; - -import android.content.Context; -import android.content.res.ColorStateList; -import android.content.res.Resources; -import android.content.res.TypedArray; -import androidx.annotation.Nullable; -import com.facebook.infer.annotation.Nullsafe; - -/** Utility class that access default values from style */ -@Nullsafe(Nullsafe.Mode.LOCAL) -public final class DefaultStyleValuesUtil { - - private DefaultStyleValuesUtil() { - throw new AssertionError("Never invoke this for an Utility class!"); - } - - /** - * Utility method that returns the default text hint color as define by the theme - * - * @param context The Context - * @return The ColorStateList for the hint text as defined in the style - */ - @Nullable - public static ColorStateList getDefaultTextColorHint(Context context) { - return getDefaultTextAttribute(context, android.R.attr.textColorHint); - } - - /** - * Utility method that returns the default text color as define by the theme - * - * @param context The Context - * @return The ColorStateList for the text as defined in the style - */ - @Nullable - public static ColorStateList getDefaultTextColor(Context context) { - return getDefaultTextAttribute(context, android.R.attr.textColor); - } - - /** - * Utility method that returns the default text highlight color as define by the theme - * - * @param context The Context - * @return The int for the highlight color as defined in the style - */ - public static int getDefaultTextColorHighlight(Context context) { - ColorStateList defaultTextAttribute = - getDefaultTextAttribute(context, android.R.attr.textColorHighlight); - if (defaultTextAttribute == null) { - // it would be rare that the highlight color is not defined in the theme, - // but if it is, return black - return 0; - } - return defaultTextAttribute.getDefaultColor(); - } - - @Nullable - private static ColorStateList getDefaultTextAttribute(Context context, int attribute) { - Resources.Theme theme = context.getTheme(); - TypedArray textAppearances = null; - try { - textAppearances = theme.obtainStyledAttributes(new int[] {attribute}); - return textAppearances.getColorStateList(0); - } finally { - if (textAppearances != null) { - textAppearances.recycle(); - } - } - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.kt new file mode 100644 index 00000000000000..e6f37974089639 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.kt @@ -0,0 +1,53 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.views.text + +import android.R +import android.content.Context +import android.content.res.ColorStateList + +/** Utility class that access default values from style */ +public object DefaultStyleValuesUtil { + + /** + * Utility method that returns the default text hint color as define by the theme + * + * @param context The Context + * @return The ColorStateList for the hint text as defined in the style + */ + @JvmStatic + public fun getDefaultTextColorHint(context: Context): ColorStateList? = + getDefaultTextAttribute(context, R.attr.textColorHint) + + /** + * Utility method that returns the default text color as define by the theme + * + * @param context The Context + * @return The ColorStateList for the text as defined in the style + */ + @JvmStatic + public fun getDefaultTextColor(context: Context): ColorStateList? = + getDefaultTextAttribute(context, R.attr.textColor) + + /** + * Utility method that returns the default text highlight color as define by the theme + * + * @param context The Context + * @return The int for the highlight color as defined in the style + */ + @JvmStatic + public fun getDefaultTextColorHighlight(context: Context): Int = + getDefaultTextAttribute(context, R.attr.textColorHighlight)?.defaultColor + ?: 0 // if the highlight color is not defined in the theme, return black + + private fun getDefaultTextAttribute(context: Context, attribute: Int): ColorStateList? { + context.theme.obtainStyledAttributes(intArrayOf(attribute)).use { typedArray -> + return typedArray.getColorStateList(0) + } + } +}