Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put back custom FormatterFileSize to use binary format (1024) instead of SI one (1000) #173

Merged
merged 12 commits into from
May 14, 2024
8 changes: 5 additions & 3 deletions src/main/java/com/infomaniak/lib/core/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager.*
import android.view.WindowManager.LayoutParams
import android.view.inputmethod.InputMethodManager
import android.webkit.MimeTypeMap
import android.widget.ImageView
Expand All @@ -52,7 +52,7 @@ import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle.*
import androidx.lifecycle.Lifecycle.Event
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.navigation.NavDirections
Expand All @@ -66,9 +66,11 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import coil.ImageLoader
import coil.load
import com.github.razir.progressbutton.*
import com.github.razir.progressbutton.DrawableButton.Companion.GRAVITY_CENTER
import com.github.razir.progressbutton.attachTextChangeAnimator
import com.github.razir.progressbutton.bindProgressButton
import com.github.razir.progressbutton.hideProgress
import com.github.razir.progressbutton.showProgress
import com.google.android.material.button.MaterialButton
import com.infomaniak.lib.core.models.user.User
import com.infomaniak.lib.core.utils.CoilUtils.simpleImageLoader
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/com/infomaniak/lib/core/utils/FormatterFileSize.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Infomaniak Core - Android
* Copyright (C) 2022-2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.lib.core.utils

import android.content.Context
import android.text.format.Formatter
import kotlin.math.abs

object FormatterFileSize {

private const val KIBI_BYTE = 1_024L
private const val KILO_BYTE = 1_000L

private const val FLAG_IEC_UNITS = 1 shl 3
private const val FLAG_SI_UNITS = 1 shl 2
private const val FLAG_SHORTER = 1 shl 0

fun Context.formatShortFileSize(bytes: Long, valueOnly: Boolean = false): String = runCatching {
val (value, unit) = formatFileSize(bytes, FLAG_IEC_UNITS or FLAG_SHORTER, valueOnly)
if (unit == null) {
value
} else {
getString(resources.getIdentifier("fileSizeSuffix", "string", "android"), value, unit)
}
}.getOrElse {
Formatter.formatShortFileSize(this, bytes)
}

private fun Context.formatFileSize(bytes: Long, flags: Int, valueOnly: Boolean): Pair<String, String?> {

fun getSuffix(suffixes: MutableList<String>): Int? {
return if (valueOnly) null else resources.getIdentifier(suffixes.removeFirstOrNull(), "string", "android")
}

val suffixes = mutableListOf(
"byteShort",
"kilobyteShort",
"megabyteShort",
"gigabyteShort",
"terabyteShort",
"petabyteShort",
)
val unit = if (flags and FLAG_IEC_UNITS != 0) KIBI_BYTE else KILO_BYTE
var multiplier = 1L

var result = abs(bytes).toFloat()
val suffixesCount = suffixes.count() - 1
var suffix = getSuffix(suffixes)

repeat(suffixesCount) {
if (result > 900) {
suffix = getSuffix(suffixes)
multiplier *= unit
result /= unit
}
}

val roundFormat = when {
multiplier == 1L || result >= 100 -> "%.0f"
result < 1 -> "%.2f"
result < 10 -> if (flags and FLAG_SHORTER != 0) "%.1f" else "%.2f"
else -> if (flags and FLAG_SHORTER != 0) "%.0f" else "%.2f" // 10 <= result < 100
}

result = abs(result)
val resultValue = String.format(roundFormat, result)
val resultUnit = suffix?.let(resources::getString)

return resultValue to resultUnit
}
}
Loading