Switch to android's text formatter class for download speed formatting

Also drop unused dependency upon lifecycle process

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2025-08-25 22:31:30 +08:00
parent 682adc93d8
commit 3f621d6b07
6 changed files with 6 additions and 51 deletions

View File

@@ -5,6 +5,7 @@
package com.aurora.store.compose.composables
import android.text.format.Formatter
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
@@ -37,7 +38,6 @@ import com.aurora.store.R
import com.aurora.store.compose.preview.AppPreviewProvider
import com.aurora.store.compose.preview.coilPreviewProvider
import com.aurora.store.data.room.download.Download
import com.aurora.store.util.CommonUtil.getDownloadSpeedString
import com.aurora.store.util.CommonUtil.getETAString
/**
@@ -107,7 +107,7 @@ fun DownloadComposable(
style = MaterialTheme.typography.bodySmall
)
Text(
text = getDownloadSpeedString(LocalContext.current, download.speed),
text = "${Formatter.formatShortFileSize(LocalContext.current, download.speed)}/s",
style = MaterialTheme.typography.bodySmall
)
}

View File

@@ -5,6 +5,7 @@
package com.aurora.store.compose.ui.details.components
import android.text.format.Formatter
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
@@ -92,7 +93,7 @@ fun AppDetails(
)
Text(
text = if (inProgress) {
CommonUtil.getDownloadSpeedString(context, speed) +
"${Formatter.formatShortFileSize(context, speed)}/s" +
", " + CommonUtil.getETAString(context, timeRemaining)
} else {
""

View File

@@ -21,11 +21,8 @@ package com.aurora.store.util
import android.content.Context
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner
import com.aurora.store.R
import com.aurora.store.data.model.ProxyInfo
import java.text.DecimalFormat
import java.util.Locale
import kotlin.math.ln
import kotlin.math.pow
@@ -109,40 +106,6 @@ object CommonUtil {
)
}
fun humanReadableByteValue(bytes: Long, si: Boolean): String {
val unit = if (si) 1000 else 1024
if (bytes < unit) return "$bytes B"
val exp = (ln(bytes.toDouble()) / ln(unit.toDouble())).toInt()
val pre = (if (si) "kMGTPE" else "KMGTPE")[exp - 1].toString() + if (si) "" else "i"
return String.format(
Locale.getDefault(), "%.1f %sB",
bytes / unit.toDouble().pow(exp.toDouble()),
pre
)
}
fun getDownloadSpeedString(context: Context, downloadedBytesPerSecond: Long): String {
if (downloadedBytesPerSecond < 0) {
return context.getString(R.string.download_speed_estimating)
}
val kb = downloadedBytesPerSecond.toDouble() / 1000.toDouble()
val mb = kb / 1000.toDouble()
val decimalFormat = DecimalFormat(".##")
return when {
mb >= 1 -> {
context.getString(R.string.download_speed_mb, decimalFormat.format(mb))
}
kb >= 1 -> {
context.getString(R.string.download_speed_kb, decimalFormat.format(kb))
}
else -> {
context.getString(R.string.download_speed_bytes, downloadedBytesPerSecond)
}
}
}
fun cleanupInstallationSessions(context: Context) {
val packageInstaller = context.packageManager.packageInstaller
for (sessionInfo in packageInstaller.mySessions) {
@@ -180,8 +143,4 @@ object CommonUtil {
else -> null
}
}
fun inForeground(): Boolean {
return ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED)
}
}

View File

@@ -20,6 +20,7 @@
package com.aurora.store.view.epoxy.views
import android.content.Context
import android.text.format.Formatter
import android.util.AttributeSet
import coil3.load
import coil3.request.placeholder
@@ -32,7 +33,6 @@ import com.aurora.store.R
import com.aurora.store.data.model.DownloadStatus
import com.aurora.store.data.room.download.Download
import com.aurora.store.databinding.ViewDownloadBinding
import com.aurora.store.util.CommonUtil.getDownloadSpeedString
import com.aurora.store.util.CommonUtil.getETAString
@ModelView(
@@ -61,10 +61,7 @@ class DownloadView @JvmOverloads constructor(
binding.txtProgress.text = ("${download.progress}%")
binding.txtEta.text = getETAString(context, download.timeRemaining)
binding.txtSpeed.text = getDownloadSpeedString(
context,
download.speed
)
binding.txtSpeed.text = "${Formatter.formatShortFileSize(context, download.speed)}/s"
when (download.downloadStatus) {
DownloadStatus.DOWNLOADING, DownloadStatus.QUEUED -> {