From 96a7bbdadd96f731bd48955ee51098888e0feee7 Mon Sep 17 00:00:00 2001 From: jacquarg Date: Tue, 22 Apr 2025 10:02:11 +0200 Subject: [PATCH 1/2] feat:3271: Use bignumber formater in weeklyreports --- .../e/advancedprivacy/common/BigNumberFormatter.kt | 12 ++++++++++++ .../weeklyreport/CallsAndLeaksViewFactory.kt | 13 ++++++++++--- .../features/weeklyreport/CallsPerAppViewFactory.kt | 5 ++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/foundation/e/advancedprivacy/common/BigNumberFormatter.kt b/app/src/main/java/foundation/e/advancedprivacy/common/BigNumberFormatter.kt index a2ead00e..5d39bfc4 100644 --- a/app/src/main/java/foundation/e/advancedprivacy/common/BigNumberFormatter.kt +++ b/app/src/main/java/foundation/e/advancedprivacy/common/BigNumberFormatter.kt @@ -23,4 +23,16 @@ class BigNumberFormatter(context: Context) { private val formatter = NumberFormatter.withLocale(context.resources.configuration.locales[0]) fun format(number: Int): CharSequence = formatter.format(number) + + fun format(number: String?): CharSequence? { + if (number == null) { + return number + } + + return try { + formatter.format(number.toLong()) + } catch (e: Exception) { + number + } + } } diff --git a/app/src/main/java/foundation/e/advancedprivacy/features/weeklyreport/CallsAndLeaksViewFactory.kt b/app/src/main/java/foundation/e/advancedprivacy/features/weeklyreport/CallsAndLeaksViewFactory.kt index 005b49ad..5559410d 100644 --- a/app/src/main/java/foundation/e/advancedprivacy/features/weeklyreport/CallsAndLeaksViewFactory.kt +++ b/app/src/main/java/foundation/e/advancedprivacy/features/weeklyreport/CallsAndLeaksViewFactory.kt @@ -26,11 +26,14 @@ import android.view.ViewGroup import androidx.core.content.ContextCompat import androidx.core.view.isVisible import foundation.e.advancedprivacy.R +import foundation.e.advancedprivacy.common.BigNumberFormatter import foundation.e.advancedprivacy.databinding.WeeklyreportItemCallsAndLeaksBinding import foundation.e.advancedprivacy.domain.entities.weeklyreport.DisplayableReport import foundation.e.advancedprivacy.domain.entities.weeklyreport.WeeklyReport class CallsAndLeaksViewFactory(private val context: Context) { + private val numberFormatter: BigNumberFormatter by lazy { BigNumberFormatter(context) } + fun getShareTitle(): CharSequence { return context.getString(R.string.weeklyreport_call_and_leaks_share_title) } @@ -51,7 +54,7 @@ class CallsAndLeaksViewFactory(private val context: Context) { binding.secondaryValue.text = context.getString( R.string.weeklyreport_label_call_and_leaks_secondaryvalue, - report.report.secondaryValues.firstOrNull() + numberFormatter.format(report.report.secondaryValues.firstOrNull()) ) binding.description2.setText( @@ -81,7 +84,7 @@ class CallsAndLeaksViewFactory(private val context: Context) { binding.secondaryValue.text = context.getString( R.string.weeklyreport_label_call_and_leaks_secondaryvalue_sharing, - report.report.secondaryValues.firstOrNull() + numberFormatter.format(report.report.secondaryValues.firstOrNull()) ) binding.description2.setText( @@ -128,7 +131,11 @@ class CallsAndLeaksViewFactory(private val context: Context) { R.string.empty } - return context.getString(descriptionId, report.report.primaryValue, *report.report.secondaryValues.toTypedArray()) + return context.getString( + descriptionId, + report.report.primaryValue, + numberFormatter.format(report.report.secondaryValues.firstOrNull()) + ) } fun getIconForNotification(): Drawable? = null diff --git a/app/src/main/java/foundation/e/advancedprivacy/features/weeklyreport/CallsPerAppViewFactory.kt b/app/src/main/java/foundation/e/advancedprivacy/features/weeklyreport/CallsPerAppViewFactory.kt index 6f21a407..3e643c46 100644 --- a/app/src/main/java/foundation/e/advancedprivacy/features/weeklyreport/CallsPerAppViewFactory.kt +++ b/app/src/main/java/foundation/e/advancedprivacy/features/weeklyreport/CallsPerAppViewFactory.kt @@ -25,6 +25,7 @@ import android.view.ViewGroup import androidx.core.text.buildSpannedString import androidx.core.text.inSpans import foundation.e.advancedprivacy.R +import foundation.e.advancedprivacy.common.BigNumberFormatter import foundation.e.advancedprivacy.common.ChipSpan import foundation.e.advancedprivacy.databinding.WeeklyReportItemCallsPerAppBinding import foundation.e.advancedprivacy.databinding.WeeklyReportItemCallsPerAppForSharingBinding @@ -32,6 +33,8 @@ import foundation.e.advancedprivacy.domain.entities.weeklyreport.DisplayableRepo import foundation.e.advancedprivacy.domain.entities.weeklyreport.WeeklyReport class CallsPerAppViewFactory(private val context: Context) { + private val numberFormatter: BigNumberFormatter by lazy { BigNumberFormatter(context) } + fun getShareTitle(): CharSequence { return context.getString(R.string.weeklyreport_share_title_base) } @@ -107,7 +110,7 @@ class CallsPerAppViewFactory(private val context: Context) { report.app.label ) - val counts = report.report.secondaryValues.firstOrNull() ?: "" + val counts = numberFormatter.format(report.report.secondaryValues.firstOrNull()) ?: "" val desc2 = context.getString( when { -- GitLab From 82f5d2ce02fe5db8bcad4805216ad69b8286cecb Mon Sep 17 00:00:00 2001 From: jacquarg Date: Wed, 23 Apr 2025 08:28:00 +0200 Subject: [PATCH 2/2] feat:3271: Use specific exception on format number to string. --- .../foundation/e/advancedprivacy/common/BigNumberFormatter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/advancedprivacy/common/BigNumberFormatter.kt b/app/src/main/java/foundation/e/advancedprivacy/common/BigNumberFormatter.kt index 5d39bfc4..1aa3ffc8 100644 --- a/app/src/main/java/foundation/e/advancedprivacy/common/BigNumberFormatter.kt +++ b/app/src/main/java/foundation/e/advancedprivacy/common/BigNumberFormatter.kt @@ -31,7 +31,7 @@ class BigNumberFormatter(context: Context) { return try { formatter.format(number.toLong()) - } catch (e: Exception) { + } catch (_: NumberFormatException) { number } } -- GitLab