refactor(util): extract FormatUtils and deduplicate byte formatting

Consolidate formatBytes and formatDuration into util/FormatUtils.
Use in SystemScreen and MemoryScreen, backed by unit tests.
This commit is contained in:
Hy4ri committed 2026-09-11 17:48:13 +03:00
1 parent 4287723ab0
commit f0c2d2f87b
4 files changed
+85 -28

No files matched your search

@@ -62,6 +62,7 @@ import com.m57.hermescontrol.ui.common.listContentPadding
import com.m57.hermescontrol.ui.common.listItemSpacing
import com.m57.hermescontrol.ui.plugins.memoryProviderStatusLabel
import com.m57.hermescontrol.ui.plugins.memoryProviderStatusType
import com.m57.hermescontrol.util.formatBytes
/**
* Memory management home — owns the memory surface that used to live in the
@@ -425,10 +426,3 @@ private fun MemoryProviderRow(
}
}
}
private fun formatBytes(bytes: Long): String =
when {
bytes >= 1024 * 1024 -> "%.1f MB".format(bytes / (1024.0 * 1024.0))
bytes >= 1024 -> "%.1f KB".format(bytes / 1024.0)
else -> "$bytes B"
}
@@ -96,31 +96,12 @@ import com.m57.hermescontrol.ui.common.StatusBadgeType
import com.m57.hermescontrol.ui.common.ToastEffect
import com.m57.hermescontrol.ui.system.components.CredentialEntryRow
import com.m57.hermescontrol.ui.system.components.HookCard
import com.m57.hermescontrol.util.formatBytes
import com.m57.hermescontrol.util.formatDuration
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
// ── Helpers ─────────────────────────────────────────────────────────────
private fun formatBytes(bytes: Long): String =
when {
bytes < 1024 -> "$bytes B"
bytes < 1024 * 1024 -> "${bytes / 1024} KB"
bytes < 1024 * 1024 * 1024 -> "${bytes / (1024 * 1024)} MB"
else -> "${"%.1f".format(bytes.toDouble() / (1024 * 1024 * 1024))} GB"
}
private fun formatDuration(totalSeconds: Double): String {
val days = (totalSeconds / 86400).toInt()
val hours = ((totalSeconds % 86400) / 3600).toInt()
val minutes = ((totalSeconds % 3600) / 60).toInt()
return buildString {
if (days > 0) append("${days}d ")
if (hours > 0 || days > 0) append("${hours}h ")
append("${minutes}m")
}
}
@OptIn(androidx.compose.foundation.layout.ExperimentalLayoutApi::class)
@Composable
fun SystemScreen(
@@ -0,0 +1,28 @@
package com.m57.hermescontrol.util
import java.util.Locale
/**
* Formats a byte count into human-readable B, KB, MB, or GB.
*/
fun formatBytes(bytes: Long): String =
when {
bytes < 1024 -> "$bytes B"
bytes < 1024 * 1024 -> "${bytes / 1024} KB"
bytes < 1024 * 1024 * 1024 -> "${bytes / (1024 * 1024)} MB"
else -> String.format(Locale.US, "%.1f GB", bytes.toDouble() / (1024 * 1024 * 1024))
}
/**
* Formats seconds into a human-readable duration (e.g. "2d 4h 15m").
*/
fun formatDuration(totalSeconds: Double): String {
val days = (totalSeconds / 86400).toInt()
val hours = ((totalSeconds % 86400) / 3600).toInt()
val minutes = ((totalSeconds % 3600) / 60).toInt()
return buildString {
if (days > 0) append("${days}d ")
if (hours > 0 || days > 0) append("${hours}h ")
append("${minutes}m")
}
}