Files
2026-10-11 16:01:50 -07:00

26 lines
1.0 KiB
Swift

import Foundation
/// Sizes the way the download rows show them: "1.4 GB", "350 MB".
/// Decimal units, as iOS reports storage.
enum ByteFormat {
static func size(_ bytes: UInt64) -> String {
let value = Double(bytes)
if value >= 999_950_000 { return String(format: "%.1f GB", value / 1_000_000_000) }
if value >= 999_500 { return String(format: "%.0f MB", value / 1_000_000) }
if value >= 1_000 { return String(format: "%.0f KB", value / 1_000) }
return "\(bytes) B"
}
/// "1.4 GB / 48.2 GB"
static func progress(done: UInt64, total: UInt64) -> String {
"\(size(min(done, total))) / \(size(total))"
}
/// "12.3 MB/s", or nil while there is no rate yet.
static func speed(_ bytesPerSecond: Double) -> String? {
guard bytesPerSecond >= 1_000 else { return nil }
if bytesPerSecond >= 999_950 { return String(format: "%.1f MB/s", bytesPerSecond / 1_000_000) }
return String(format: "%.0f KB/s", bytesPerSecond / 1_000)
}
}