From 30a7301a9add62d909008818ecaf32c92a420edf Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Wed, 8 May 2024 11:31:10 +0200 Subject: [PATCH] Use local Strings for byte sizes --- .../lib/core/utils/FormatterFileSize.kt | 46 ++++++++----------- src/main/res/values-fr/strings.xml | 11 +++++ src/main/res/values/strings.xml | 11 +++++ 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/infomaniak/lib/core/utils/FormatterFileSize.kt b/src/main/java/com/infomaniak/lib/core/utils/FormatterFileSize.kt index 47f3502b..2e927b3c 100644 --- a/src/main/java/com/infomaniak/lib/core/utils/FormatterFileSize.kt +++ b/src/main/java/com/infomaniak/lib/core/utils/FormatterFileSize.kt @@ -20,6 +20,7 @@ package com.infomaniak.lib.core.utils import android.content.Context import android.content.res.Resources import android.text.format.Formatter +import com.infomaniak.lib.core.R import kotlin.math.abs object FormatterFileSize { @@ -36,38 +37,31 @@ object FormatterFileSize { valueOnly: Boolean = false, shortValue: Boolean = true, iecUnits: Boolean = true, - ): String { - return runCatching { - val unitsFlag = if (iecUnits) FLAG_IEC_UNITS else FLAG_SI_UNITS - val flags = if (shortValue) (unitsFlag or FLAG_SHORTER) else unitsFlag - val (value, unit) = formatFileSize(resources, bytes, valueOnly, flags) - if (unit == null) { - value - } else { - getString( - resources.getIdentifier("fileSizeSuffix", "string", "android"), - value, - unit, - ) - } - }.getOrElse { - Formatter.formatShortFileSize(this, bytes) - } + ): String = runCatching { + val unitsFlag = if (iecUnits) FLAG_IEC_UNITS else FLAG_SI_UNITS + val flags = if (shortValue) (unitsFlag or FLAG_SHORTER) else unitsFlag + val (value, unit) = formatFileSize(resources, bytes, valueOnly, flags) + return@runCatching "$value${unit?.let { " $it" } ?: ""}" + }.getOrElse { + return@getOrElse Formatter.formatShortFileSize(this, bytes) } private fun formatFileSize(resources: Resources, bytes: Long, valueOnly: Boolean, flags: Int): Pair { - fun getSuffix(suffixes: MutableList): Int? { - return if (valueOnly) null else resources.getIdentifier(suffixes.removeFirstOrNull(), "string", "android") - } + fun getSuffix(suffixes: MutableList) = if (valueOnly) null else suffixes.removeFirstOrNull() val suffixes = mutableListOf( - "byteShort", - "kilobyteShort", - "megabyteShort", - "gigabyteShort", - "terabyteShort", - "petabyteShort", + R.string.sizeByteShort, + R.string.sizeKiloByteShort, + R.string.sizeMegaByteShort, + R.string.sizeGigaByteShort, + R.string.sizeTeraByteShort, + R.string.sizePetaByteShort, + R.string.sizeExaByteShort, + R.string.sizeZettaByteShort, + R.string.sizeYottaByteShort, + R.string.sizeRonnaByteShort, + R.string.sizeQuettaByteShort, ) val unit = if (flags and FLAG_IEC_UNITS != 0) KIBI_BYTE else KILO_BYTE var result = abs(bytes).toFloat() diff --git a/src/main/res/values-fr/strings.xml b/src/main/res/values-fr/strings.xml index 37cbe93e..bbb7623d 100644 --- a/src/main/res/values-fr/strings.xml +++ b/src/main/res/values-fr/strings.xml @@ -48,4 +48,15 @@ Afin de profiter d’une expérience optimale de %s, nous vous conseillons de mettre à jour votre application. Mise à jour disponible ! Mise à jour prête + o + Ko + Mo + Go + To + Po + Eo + Zo + Yo + Ro + Qo diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index aa9633ad..af794271 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -50,4 +50,15 @@ To get the best out of %s, we recommend you update your application. Update available! Update ready + b + kB + MB + GB + TB + PB + EB + ZB + YB + RB + QB