Cotton code
50 lines · 2.2 KB · SwiftRaw
// The pure vocabulary the three media screens share: a size, a length, a// bucket, and the months a grid is grouped into.//// **Every one of these is CLOCK-FREE.** A month key is read off the row's own// `takenAt`, never off `Date()`, which is what lets a grid's headings be tested// without a device, a locale or a fake timer. Where a function does need "now"// it takes it as a parameter — the lane's rule.//// `MediaBytes` is the app's ONE byte formatter: `/photos` kept a copy of its// own (`PhotosFileSize`) until phase 28 retired it onto this one.import CottonKitimport Foundation// MARK: - Bytespublic enum MediaBytes {    /// "812 B" · "44 KB" · "3.2 MB" · "412 MB".    ///    /// **Null in, null out, and zero is null too.**    /// A row whose size was never recorded states nothing rather than claiming    /// it is empty, which is why every caller writes `?? nothing` rather than    /// `?? "0 B"`. The one screen that DOES want a zero says so itself: a    /// storage bucket with nothing in it draws `storageZeroBytes`, because that    /// list is also the answer to "what does Cotton keep on my phone".    public static func sizeLabel(_ bytes: Int?) -> String? {        guard let bytes, bytes > 0 else { return nil }        if bytes < 1024 { return "\(bytes) B" }        let kb = Double(bytes) / 1024        if kb < 1024 { return "\(Int(kb.rounded())) KB" }        let mb = kb / 1024        // `mb.rounded()` and not `mb`, so the rung is crossed where the LABEL        // would read `1024 MB` rather than where the arithmetic does.        if mb.rounded() < 1024 {            return mb < 10                ? "\(String(format: "%.1f", mb)) MB"                : "\(Int(mb.rounded())) MB"        }        // A GIGABYTE RUNG. Release assets are where one appears — this        // repository's own nightly database backups are 2.36 GB each — and        // `2418 MB` is a figure a person has to divide in their head before it        // means anything. Storage totals reach the same rung.        let gb = mb / 1024        return gb < 10            ? "\(String(format: "%.1f", gb)) GB"            : "\(Int(gb.rounded())) GB"    }}// MARK: - The five storage buckets