Show library size in About screen

This commit is contained in:
Nolan Hoenert
2025-12-31 20:38:09 -07:00
committed by Alexander Capehart
parent 8fd3ef8eaa
commit 869ab8aadb
5 changed files with 50 additions and 2 deletions
@@ -100,7 +100,8 @@ constructor(
library.albums.size,
library.artists.size,
library.genres.size,
library.songs.sumOf { it.durationMs })
library.songs.sumOf { it.durationMs },
library.songs.sumOf { it.size })
L.d("Updated statistics: ${_statistics.value}")
}
@@ -362,7 +363,8 @@ constructor(
val albums: Int,
val artists: Int,
val genres: Int,
val durationMs: Long
val durationMs: Long,
val totalSizeBytes: Long
)
}
@@ -36,6 +36,7 @@ import org.oxycblt.auxio.playback.formatDurationMs
import org.oxycblt.auxio.ui.ViewBindingFragment
import org.oxycblt.auxio.util.collectImmediately
import org.oxycblt.auxio.util.openInBrowser
import org.oxycblt.auxio.util.formatFileSize
import org.oxycblt.auxio.util.startIntent
import org.oxycblt.auxio.util.systemBarInsetsCompat
@@ -98,6 +99,11 @@ class AboutFragment : ViewBindingFragment<FragmentAboutBinding>() {
getString(
R.string.fmt_lib_total_duration,
(statistics?.durationMs ?: 0).formatDurationMs(false))
binding.aboutTotalSize.text =
getString(
R.string.fmt_lib_total_size,
(statistics?.totalSizeBytes ?: 0L).formatFileSize())
}
private fun Context.sendEmail(recipient: String) {
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2025 Auxio Project
* SizeUtil.kt is part of Auxio.
*/
package org.oxycblt.auxio.util
import java.util.Locale
/**
* Format a byte count into a human readable string (B, KB, MB, GB, ...).
*/
fun Long.formatFileSize(): String {
if (this <= 0L) return "0 B"
var size = this.toDouble()
val units = arrayOf("B", "KB", "MB", "GB", "TB")
var unitIndex = 0
while (size >= 1024.0 && unitIndex < units.size - 1) {
size /= 1024.0
unitIndex++
}
val formatted = when {
size >= 100 -> String.format(Locale.getDefault(), "%.0f", size)
size >= 10 -> String.format(Locale.getDefault(), "%.1f", size)
else -> String.format(Locale.getDefault(), "%.2f", size)
}
return "$formatted ${units[unitIndex]}"
}