From 0c5dfc92b5281b28b33ed60df9f95cc74b395236 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Tue, 27 Sep 2022 13:08:53 +0200 Subject: [PATCH 1/7] Extract CharSequence.enforceWesternArabicNumerals --- .../util/extensions/StringExtensions.kt | 26 +++++++++++++++++++ .../widgets/MaterialTextViewWithNumerals.kt | 18 +------------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/util/extensions/StringExtensions.kt b/WordPress/src/main/java/org/wordpress/android/util/extensions/StringExtensions.kt index d02a46fd0d3e..bc145e659952 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/extensions/StringExtensions.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/extensions/StringExtensions.kt @@ -1,6 +1,9 @@ package org.wordpress.android.util.extensions import android.annotation.SuppressLint +import android.text.SpannableString +import android.text.Spanned +import android.text.TextUtils import java.util.Locale /** @@ -17,3 +20,26 @@ import java.util.Locale fun String.capitalizeWithLocaleWithoutLint(locale: Locale): String { return this.capitalize(locale) } + +/** + * Converts digits to Western Arabic -- Workaround for an issue in Android that shows Eastern Arabic numerals. + * There is an issue in Google's bug tracker for this: [SO Answer](https://stackoverflow.com/a/34612487/4129245). + * The returned String uses the default Locale. + * @return a String with numerals in Western Arabic format persisting spans if available. + */ +fun CharSequence.enforceWesternArabicNumerals(): CharSequence { + val textWithArabicNumerals = this + // Replace Eastern Arabic numerals + .replace(Regex("[٠-٩]")) { match -> (match.value.single() - '٠').toString() } + // Replace Persian/Urdu numerals + .replace(Regex("[۰-۹]")) { match -> (match.value.single() - '۰').toString() } + + // Restore spans if text is an instance of Spanned + if (this is Spanned) { + val spannableText = SpannableString(textWithArabicNumerals) + TextUtils.copySpansFrom(this, 0, this.length, null, spannableText, 0) + return spannableText + } + + return textWithArabicNumerals +} diff --git a/WordPress/src/main/java/org/wordpress/android/widgets/MaterialTextViewWithNumerals.kt b/WordPress/src/main/java/org/wordpress/android/widgets/MaterialTextViewWithNumerals.kt index 18fb04afb591..3bfdd16f0d9a 100644 --- a/WordPress/src/main/java/org/wordpress/android/widgets/MaterialTextViewWithNumerals.kt +++ b/WordPress/src/main/java/org/wordpress/android/widgets/MaterialTextViewWithNumerals.kt @@ -6,6 +6,7 @@ import android.text.Spanned import android.text.TextUtils import android.util.AttributeSet import com.google.android.material.textview.MaterialTextView +import org.wordpress.android.util.extensions.enforceWesternArabicNumerals /** * MaterialTextView which enforces Western Arabic numerals. @@ -18,21 +19,4 @@ class MaterialTextViewWithNumerals @JvmOverloads constructor( override fun setText(text: CharSequence?, type: BufferType?) { super.setText(text?.enforceWesternArabicNumerals(), type) } - - private fun CharSequence.enforceWesternArabicNumerals(): CharSequence { - val textWithArabicNumerals = this - // Replace Eastern Arabic numerals - .replace(Regex("[٠-٩]")) { match -> (match.value.single() - '٠').toString() } - // Replace Persian/Urdu numerals - .replace(Regex("[۰-۹]")) { match -> (match.value.single() - '۰').toString() } - - val spannableText = SpannableString(textWithArabicNumerals) - - // Restore spans if text is an instance of Spanned - if (this is Spanned) { - TextUtils.copySpansFrom(this, 0, this.length, null, spannableText, 0) - } - - return spannableText - } } From b65822f7f9efb207f19d55a76fb9025683fd2b1a Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Tue, 27 Sep 2022 13:42:17 +0200 Subject: [PATCH 2/7] Enforce Western Arabic numerals in Views and Visitors Stats chart on date lines --- .../lists/sections/insights/usecases/ViewsAndVisitorsMapper.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/sections/insights/usecases/ViewsAndVisitorsMapper.kt b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/sections/insights/usecases/ViewsAndVisitorsMapper.kt index 5b5b2eb4b5a6..2b07e43a1f1f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/sections/insights/usecases/ViewsAndVisitorsMapper.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/sections/insights/usecases/ViewsAndVisitorsMapper.kt @@ -23,6 +23,7 @@ import org.wordpress.android.ui.stats.refresh.utils.MILLION import org.wordpress.android.ui.stats.refresh.utils.StatsDateFormatter import org.wordpress.android.ui.stats.refresh.utils.StatsUtils import org.wordpress.android.ui.utils.ListItemInteraction +import org.wordpress.android.util.extensions.enforceWesternArabicNumerals import org.wordpress.android.viewmodel.ResourceProvider import javax.inject.Inject @@ -117,7 +118,7 @@ class ViewsAndVisitorsMapper val value = it.getValue(selectedType) val date = statsDateFormatter.parseStatsDate(statsGranularity, it.period) Line( - statsDateFormatter.printDayWithoutYear(date), + statsDateFormatter.printDayWithoutYear(date).enforceWesternArabicNumerals() as String, it.period, value.toInt() ) From af6b25c2ef18e8014ebbe49a7d221c5d9b514a6f Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Tue, 27 Sep 2022 13:58:06 +0200 Subject: [PATCH 3/7] Enforce Western Arabic numerals in Stats text blocks --- WordPress/src/main/res/layout/stats_block_text_item.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/src/main/res/layout/stats_block_text_item.xml b/WordPress/src/main/res/layout/stats_block_text_item.xml index b4d8043fdaa0..60d8a6842b43 100644 --- a/WordPress/src/main/res/layout/stats_block_text_item.xml +++ b/WordPress/src/main/res/layout/stats_block_text_item.xml @@ -8,7 +8,7 @@ android:paddingStart="@dimen/margin_extra_large" android:paddingEnd="@dimen/margin_extra_large"> - Date: Tue, 27 Sep 2022 15:30:06 +0200 Subject: [PATCH 4/7] Enforce LTR layout direction for Stats change text view --- WordPress/src/main/res/layout/stats_block_value_item.xml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/WordPress/src/main/res/layout/stats_block_value_item.xml b/WordPress/src/main/res/layout/stats_block_value_item.xml index 7a1781e8ae91..4e588b7fc6ca 100644 --- a/WordPress/src/main/res/layout/stats_block_value_item.xml +++ b/WordPress/src/main/res/layout/stats_block_value_item.xml @@ -8,6 +8,7 @@ android:paddingStart="@dimen/margin_extra_large" android:paddingTop="@dimen/margin_extra_large" android:paddingEnd="@dimen/margin_extra_large" + xmlns:tools="http://schemas.android.com/tools" android:paddingBottom="8dp"> + android:visibility="gone" + tools:text="-7 (-100%)" + android:layoutDirection="ltr" + tools:visibility="visible"/> From 3a004f89b505acb9f7f89b9d5b06e35f58336030 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Tue, 27 Sep 2022 15:30:06 +0200 Subject: [PATCH 5/7] Enforce LTR layout direction for Stats change text view --- WordPress/src/main/res/layout/stats_block_value_item.xml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/WordPress/src/main/res/layout/stats_block_value_item.xml b/WordPress/src/main/res/layout/stats_block_value_item.xml index 7a1781e8ae91..e63aaae3b605 100644 --- a/WordPress/src/main/res/layout/stats_block_value_item.xml +++ b/WordPress/src/main/res/layout/stats_block_value_item.xml @@ -8,6 +8,7 @@ android:paddingStart="@dimen/margin_extra_large" android:paddingTop="@dimen/margin_extra_large" android:paddingEnd="@dimen/margin_extra_large" + xmlns:tools="http://schemas.android.com/tools" android:paddingBottom="8dp"> + android:visibility="gone" + tools:text="-7 (-100%)" + android:layoutDirection="ltr" + tools:visibility="visible" /> From fc743ff882ef8caff2ae0661a9b77c4d2a3c515b Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Tue, 27 Sep 2022 16:37:41 +0200 Subject: [PATCH 6/7] Remove unused imports from MaterialTextViewWithNumerals --- .../wordpress/android/widgets/MaterialTextViewWithNumerals.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/widgets/MaterialTextViewWithNumerals.kt b/WordPress/src/main/java/org/wordpress/android/widgets/MaterialTextViewWithNumerals.kt index 3bfdd16f0d9a..57f63158c293 100644 --- a/WordPress/src/main/java/org/wordpress/android/widgets/MaterialTextViewWithNumerals.kt +++ b/WordPress/src/main/java/org/wordpress/android/widgets/MaterialTextViewWithNumerals.kt @@ -1,9 +1,6 @@ package org.wordpress.android.widgets import android.content.Context -import android.text.SpannableString -import android.text.Spanned -import android.text.TextUtils import android.util.AttributeSet import com.google.android.material.textview.MaterialTextView import org.wordpress.android.util.extensions.enforceWesternArabicNumerals From 32cf991be82e2227d6138672efaee34d65b3caa9 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Tue, 11 Oct 2022 16:16:48 +0200 Subject: [PATCH 7/7] Update release notes for 17217 --- RELEASE-NOTES.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 9c44efd5dca6..093028bcc630 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -4,6 +4,7 @@ ----- * [*] Updates splash screen for Android 12+ [https://github.com/wordpress-mobile/WordPress-Android/pull/17273] * [*] Fix text color of success messages in the QR code login flow [https://github.com/wordpress-mobile/WordPress-Android/pull/17286] +* [*] Stats: Fix Western Arabic Numerals not being shown on every text of the stats screens in Arabic languages [https://github.com/wordpress-mobile/WordPress-Android/pull/17217] 20.9 -----