Skip to content

Commit

Permalink
Use local Strings for byte sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed May 8, 2024
1 parent 32d8762 commit cc72f9a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
46 changes: 20 additions & 26 deletions src/main/java/com/infomaniak/lib/core/utils/FormatterFileSize.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String, String?> {

fun getSuffix(suffixes: MutableList<String>): Int? {
return if (valueOnly) null else resources.getIdentifier(suffixes.removeFirstOrNull(), "string", "android")
}
fun getSuffix(suffixes: MutableList<Int>) = 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()
Expand Down
11 changes: 11 additions & 0 deletions src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@
<string name="updateAvailableDescription">Afin de profiter d’une expérience optimale de %s, nous vous conseillons de mettre à jour votre application.</string>
<string name="updateAvailableTitle">Mise à jour disponible !</string>
<string name="updateReadyTitle">Mise à jour prête</string>
<string name="sizeByteShort">o</string>
<string name="sizeKiloByteShort">Ko</string>
<string name="sizeMegaByteShort">Mo</string>
<string name="sizeGigaByteShort">Go</string>
<string name="sizeTeraByteShort">To</string>
<string name="sizePetaByteShort">Po</string>
<string name="sizeExaByteShort">Eo</string>
<string name="sizeZettaByteShort">Zo</string>
<string name="sizeYottaByteShort">Yo</string>
<string name="sizeRonnaByteShort">Ro</string>
<string name="sizeQuettaByteShort">Qo</string>
</resources>
11 changes: 11 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,15 @@
<string name="updateAvailableDescription">To get the best out of %s, we recommend you update your application.</string>
<string name="updateAvailableTitle">Update available!</string>
<string name="updateReadyTitle">Update ready</string>
<string name="sizeByteShort">b</string>
<string name="sizeKiloByteShort">kB</string>
<string name="sizeMegaByteShort">MB</string>
<string name="sizeGigaByteShort">GB</string>
<string name="sizeTeraByteShort">TB</string>
<string name="sizePetaByteShort">PB</string>
<string name="sizeExaByteShort">EB</string>
<string name="sizeZettaByteShort">ZB</string>
<string name="sizeYottaByteShort">YB</string>
<string name="sizeRonnaByteShort">RB</string>
<string name="sizeQuettaByteShort">QB</string>
</resources>

0 comments on commit cc72f9a

Please sign in to comment.