Skip to content

Commit

Permalink
Add custom Formatter.formatShortFileSize to use binary format (1024…
Browse files Browse the repository at this point in the history
…) instead of SI one (1000)
  • Loading branch information
KevinBoulongne committed May 3, 2024
1 parent cbcce4d commit 1d13b04
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 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 All @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1d13b04

Please sign in to comment.