From de5b1d4ead26219d0564e79c2050ec41792b38ce Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Sun, 24 Nov 2024 10:39:14 +0100 Subject: [PATCH] Better number formatting --- .../lunatracker/MainActivity.kt | 7 ++- .../adapters/LunaEventRecyclerAdapter.kt | 39 ++------------ app/src/main/java/utils/NumericUtils.kt | 51 +++++++++++++++++++ app/src/main/res/values-it/strings.xml | 4 +- app/src/main/res/values/strings.xml | 2 + 5 files changed, 65 insertions(+), 38 deletions(-) create mode 100644 app/src/main/java/utils/NumericUtils.kt diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index 82db47a..97a0b1b 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -31,6 +31,7 @@ import kotlinx.coroutines.Runnable import okio.IOException import org.json.JSONException import utils.DateUtils +import utils.NumericUtils import java.text.DateFormat import java.util.Date @@ -262,12 +263,14 @@ class MainActivity : AppCompatActivity() { fun showEventDetailDialog(event: LunaEvent) { val dateFormat = DateFormat.getDateTimeInstance(); val d = AlertDialog.Builder(this) - d.setTitle(event.getTypeDescription(this)) + d.setTitle(R.string.dialog_event_detail_title) val dialogView = layoutInflater.inflate(R.layout.dialog_event_detail, null) dialogView.findViewById(R.id.dialog_event_detail_type_emoji).setText(event.getTypeEmoji(this)) dialogView.findViewById(R.id.dialog_event_detail_type_description).setText(event.getTypeDescription(this)) dialogView.findViewById(R.id.dialog_event_detail_type_date).setText(dateFormat.format(Date(event.time * 1000))) - dialogView.findViewById(R.id.dialog_event_detail_type_quantity).setText(event.quantity.toString()) + dialogView.findViewById(R.id.dialog_event_detail_type_quantity).setText( + NumericUtils(this).formatEventQuantity(event) + ) dialogView.findViewById(R.id.dialog_event_detail_type_notes).setText(event.notes) d.setView(dialogView) d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> dialogInterface.dismiss() } diff --git a/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt b/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt index 82b4212..2264660 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt @@ -1,8 +1,6 @@ package it.danieleverducci.lunatracker.adapters import android.content.Context -import android.icu.util.LocaleData -import android.icu.util.ULocale import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -11,36 +9,17 @@ import androidx.recyclerview.widget.RecyclerView import it.danieleverducci.lunatracker.entities.LunaEvent import it.danieleverducci.lunatracker.R import utils.DateUtils +import utils.NumericUtils class LunaEventRecyclerAdapter: RecyclerView.Adapter { private val context: Context val items = ArrayList() - val measurement_unit_liquid_base: String - val measurement_unit_weight_base: String - val measurement_unit_weight_tiny: String + val numericUtils: NumericUtils var onItemClickListener: OnItemClickListener? = null constructor(context: Context) { this.context = context - val measurementSystem = LocaleData.getMeasurementSystem(ULocale.getDefault()) - this.measurement_unit_liquid_base = context.getString( - if (measurementSystem == LocaleData. MeasurementSystem.SI) - R.string.measurement_unit_liquid_base_metric - else - R.string.measurement_unit_liquid_base_imperial - ) - this.measurement_unit_weight_base = context.getString( - if (measurementSystem == LocaleData. MeasurementSystem.SI) - R.string.measurement_unit_weight_base_metric - else - R.string.measurement_unit_weight_base_imperial - ) - this.measurement_unit_weight_tiny = context.getString( - if (measurementSystem == LocaleData. MeasurementSystem.SI) - R.string.measurement_unit_weight_tiny_metric - else - R.string.measurement_unit_weight_tiny_imperial - ) + this.numericUtils = NumericUtils(context) } override fun onCreateViewHolder( @@ -70,17 +49,7 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter item.getTypeDescription(context) } holder.time.text = DateUtils.formatTimeAgo(context, item.time) - val qtyText = if ((item.quantity ?: 0) > 0) { - item.quantity.toString() + " " + when (item.type) { - LunaEvent.TYPE_BABY_BOTTLE -> measurement_unit_liquid_base - LunaEvent.TYPE_WEIGHT -> measurement_unit_weight_base - LunaEvent.TYPE_MEDICINE -> measurement_unit_weight_tiny - else -> "" - } - } else { - "" - } - holder.quantity.text = qtyText + holder.quantity.text = numericUtils.formatEventQuantity(item) // Listeners if (onItemClickListener != null) { holder.root.setOnClickListener({ diff --git a/app/src/main/java/utils/NumericUtils.kt b/app/src/main/java/utils/NumericUtils.kt new file mode 100644 index 0000000..81669c6 --- /dev/null +++ b/app/src/main/java/utils/NumericUtils.kt @@ -0,0 +1,51 @@ +package utils + +import android.content.Context +import android.icu.util.LocaleData +import android.icu.util.ULocale +import it.danieleverducci.lunatracker.R +import it.danieleverducci.lunatracker.entities.LunaEvent +import java.text.NumberFormat + +class NumericUtils (val context: Context) { + val numberFormat: NumberFormat + val measurement_unit_liquid_base: String + val measurement_unit_weight_base: String + val measurement_unit_weight_tiny: String + + init { + this.numberFormat = NumberFormat.getInstance() + val measurementSystem = LocaleData.getMeasurementSystem(ULocale.getDefault()) + this.measurement_unit_liquid_base = context.getString( + if (measurementSystem == LocaleData. MeasurementSystem.SI) + R.string.measurement_unit_liquid_base_metric + else + R.string.measurement_unit_liquid_base_imperial + ) + this.measurement_unit_weight_base = context.getString( + if (measurementSystem == LocaleData. MeasurementSystem.SI) + R.string.measurement_unit_weight_base_metric + else + R.string.measurement_unit_weight_base_imperial + ) + this.measurement_unit_weight_tiny = context.getString( + if (measurementSystem == LocaleData. MeasurementSystem.SI) + R.string.measurement_unit_weight_tiny_metric + else + R.string.measurement_unit_weight_tiny_imperial + ) + } + + fun formatEventQuantity(item: LunaEvent): String { + return if ((item.quantity ?: 0) > 0) { + numberFormat.format(item.quantity) + " " + when (item.type) { + LunaEvent.TYPE_BABY_BOTTLE -> measurement_unit_liquid_base + LunaEvent.TYPE_WEIGHT -> measurement_unit_weight_base + LunaEvent.TYPE_MEDICINE -> measurement_unit_weight_tiny + else -> "" + } + } else { + "" + } + } +} \ No newline at end of file diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index fc63997..9ea6dd9 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -48,7 +48,7 @@ Sul dispositivo La soluzione più privacy-friendly: i dati non escono mai dal tuo dispositivo Su un server WebDAV - Puoi usare un qualunque servizio che supporti WebDAV (come ad esempio Nextcloud, Owncloud, Dropbox, Box...) per salvare i dati. In questo modo puoi sincronizzarli tra più dispositivi, ad esempio quello del papà, della mamma, dei nonni... Ti servirà l\'url WebDAV, che trovi nella documentazione del tuo provider (ad es. in Nextcloud è nelle impostazioni di Files) + Puoi usare un qualunque servizio che supporti WebDAV (come ad esempio Nextcloud, Owncloud, Dropbox, Box…) per salvare i dati. In questo modo puoi sincronizzarli tra più dispositivi, ad esempio quello del papà, della mamma, dei nonni… Ti servirà l\'url WebDAV, che trovi nella documentazione del tuo provider (ad es. in Nextcloud è nelle impostazioni di Files) Url WebDAV https:// Username @@ -71,4 +71,6 @@ Nome della medicina, quantità, formato, note…: Quantità (numero intero), o lascia vuoto + Dettaglio evento + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a517c04..719adea 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -90,4 +90,6 @@ oz gr + Event detail + \ No newline at end of file