util: extract file size formatting into ModelUtils

This commit is contained in:
Han Yin 2025-04-14 22:53:25 -07:00
parent 561fe0222f
commit 1bebd1bb07
2 changed files with 20 additions and 17 deletions

View File

@ -1,6 +1,6 @@
package com.example.llama.revamp.data.model
import java.util.Locale
import com.example.llama.revamp.util.formatSize
/**
* Data class containing information about an LLM model.
@ -17,22 +17,7 @@ data class ModelInfo(
val lastUsed: Long? = null
) {
val formattedSize: String
get() {
return when {
sizeInBytes >= 1_000_000_000 -> {
val sizeInGb = sizeInBytes / 1_000_000_000.0
String.format(Locale.getDefault(), "%.2f GB", sizeInGb)
}
sizeInBytes >= 1_000_000 -> {
val sizeInMb = sizeInBytes / 1_000_000.0
String.format(Locale.getDefault(), "%.2f MB", sizeInMb)
}
else -> {
val sizeInKb = sizeInBytes / 1_000.0
String.format(Locale.getDefault(), "%.2f KB", sizeInKb)
}
}
}
get() = formatSize(sizeInBytes)
companion object {
/**

View File

@ -33,6 +33,24 @@ fun getFileSizeFromUri(context: Context, uri: Uri): Long? =
}
}
/**
* Convert bytes into human readable sizes
*/
fun formatSize(sizeInBytes: Long) = when {
sizeInBytes >= 1_000_000_000 -> {
val sizeInGb = sizeInBytes / 1_000_000_000.0
String.format(Locale.getDefault(), "%.2f GB", sizeInGb)
}
sizeInBytes >= 1_000_000 -> {
val sizeInMb = sizeInBytes / 1_000_000.0
String.format(Locale.getDefault(), "%.2f MB", sizeInMb)
}
else -> {
val sizeInKb = sizeInBytes / 1_000.0
String.format(Locale.getDefault(), "%.2f KB", sizeInKb)
}
}
/**
* Try to extract parameters by looking for patterns like 7B, 13B, etc.
*/