From 1d13b04bfab747d0b70c4574027979662bfe49bb Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Fri, 3 May 2024 13:45:33 +0200 Subject: [PATCH] Add custom `Formatter.formatShortFileSize` to use binary format (1024) instead of SI one (1000) --- .../infomaniak/lib/core/utils/Extensions.kt | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/infomaniak/lib/core/utils/Extensions.kt b/src/main/java/com/infomaniak/lib/core/utils/Extensions.kt index 194c22c0..a4278a50 100644 --- a/src/main/java/com/infomaniak/lib/core/utils/Extensions.kt +++ b/src/main/java/com/infomaniak/lib/core/utils/Extensions.kt @@ -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 @@ -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 @@ -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 @@ -79,6 +81,9 @@ import com.infomaniak.lib.core.utils.UtilsUi.generateInitialsAvatarDrawable import com.infomaniak.lib.core.utils.UtilsUi.getBackgroundColorBasedOnId import org.apache.commons.cli.MissingArgumentException import java.io.Serializable +import java.text.StringCharacterIterator +import kotlin.math.abs +import kotlin.math.sign import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty @@ -303,6 +308,28 @@ fun String.guessMimeType(): String { return MimeTypeMap.getSingleton().getMimeTypeFromExtension(substringAfterLast(".")) ?: "*/*" } +fun Context.humanReadableBinaryBytesCount(bytes: Long): String { + val byteChar = "B" + + val absBytes = if (bytes == Long.MIN_VALUE) Long.MAX_VALUE else abs(bytes) + if (absBytes < 1_024L) return "$bytes $byteChar" + + var value = absBytes + val characters = StringCharacterIterator("KMGTPE") + + var i = 40 + while (i >= 0 && absBytes > 0xfffccccccccccccL shr i) { + value = value shr 10 + characters.next() + i -= 10 + } + + value *= bytes.sign.toLong() + + val locale = resources.configuration.getLocales().get(0) + return String.format(locale, "%.1f %c$byteChar", value / 1_024.0f, characters.current()) +} + fun SharedPreferences.transaction(block: SharedPreferences.Editor.() -> Unit) { with(edit()) { block(this)