import SwiftUI
import WidgetKit
struct StorageWidgetEntry: TimelineEntry {
let date: Date
let snapshot: DeviceStorageSnapshot?
}
struct DeviceStorageProvider: TimelineProvider {
func placeholder(in context: Context) -> StorageWidgetEntry { .init(date: .now, snapshot: .example) }
func getSnapshot(in context: Context, completion: @escaping (StorageWidgetEntry) -> Void) {
completion(placeholder(in: context))
}
func getTimeline(in context: Context, completion: @escaping (Timeline<StorageWidgetEntry>) -> Void) {
let report = StorageWidgetEntry(date: .now, snapshot: .example)
completion(Timeline(entries: [report], policy: .never))
}
}
struct DeviceStorageWidget: Widget {
var body: some WidgetConfiguration {
StaticConfiguration(kind: "LumoDeviceStorage", provider: DeviceStorageProvider()) { entry in
StorageWidgetTile(snapshot: entry.snapshot)
.containerBackground(LumoStyle.surface, for: .widget)
.widgetURL(URL(string: "lumo-widget-demo://storage/breakdown"))
}
.configurationDisplayName("Lumo Device Storage")
.description("See used and free space, with a breakdown by storage category.")
.supportedFamilies([.systemSmall, .systemMedium])
}
}
private struct StorageWidgetTile: View {
let snapshot: DeviceStorageSnapshot?
@Environment(\.widgetFamily) private var family
private var compact: Bool { family == .systemSmall }
var body: some View {
VStack(alignment: .leading, spacing: 7) {
LumoHeading(title: compact ? "Storage" : "Device Storage",
icon: "internaldrive", tone: LumoStyle.violet,
subtitle: compact ? nil : snapshot?.device)
.accessibilityIdentifier("storage-widget-title")
if let snapshot {
ViewThatFits(in: .horizontal) {
HStack(alignment: .bottom, spacing: 8) {
usage(snapshot)
Spacer(minLength: 0)
freeSpace(snapshot).fixedSize(horizontal: true, vertical: false)
}
VStack(alignment: .leading, spacing: 3) {
usage(snapshot)
freeSpace(snapshot)
}
}
StorageCapacityBar(snapshot: snapshot, height: compact ? 9 : 13)
if !compact, snapshot.uncategorized != nil {
HStack(spacing: 8) {
ForEach(snapshot.breakdown.prefix(3)) { category in
HStack(spacing: 3) {
Circle().fill(LumoStyle.storageTone(category.tone))
.frame(width: 5, height: 5)
.accessibilityHidden(true)
Text(category.label).lineLimit(1)
}
.font(.caption2)
.foregroundStyle(.secondary)
.accessibilityElement(children: .ignore)
.accessibilityLabel("\(category.label), \(storageLabel(category.bytes))")
}
}
}
} else {
Text("Storage data unavailable.").font(.caption).foregroundStyle(.secondary)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
}
private func usage(_ snapshot: DeviceStorageSnapshot) -> some View {
VStack(alignment: .leading, spacing: 0) {
Text(snapshot.used.map(storageLabel) ?? "Usage unavailable")
.font(.system(compact ? .headline : .title3, design: .rounded, weight: .semibold))
.monospacedDigit()
.lineLimit(1)
.minimumScaleFactor(0.75)
.accessibilityLabel(snapshot.used.map { "\(storageLabel($0)) used" } ?? "Usage unavailable")
if let capacity = snapshot.capacity, !compact {
Text("of \(storageLabel(capacity)) used").font(.caption2).foregroundStyle(.secondary)
}
}
.fixedSize(horizontal: !compact, vertical: false)
}
private func freeSpace(_ snapshot: DeviceStorageSnapshot) -> some View {
Text(snapshot.free.map { "\(storageLabel($0)) free" } ?? "Free space unavailable")
.font(.caption2)
.foregroundStyle(.secondary)
.lineLimit(1)
}
}
#Preview(as: .systemSmall) {
DeviceStorageWidget()
} timeline: {
StorageWidgetEntry(date: .now, snapshot: .example)
StorageWidgetEntry(date: .now, snapshot: nil)
}