Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Components/Formatting.swift3// Description: Shared formatting (sizes, dates, speeds), file icons from the system, and small reusable views: status badges, empty states, the banner.4import AppKit5import SPDriveCore6import SPDriveSync7import SwiftUI8import UniformTypeIdentifiers910enum Fmt {11 static func bytes(_ n: Int64) -> String { ByteCountFormatter.string(fromByteCount: n, countStyle: .file) }12 static func speed(_ bps: Double) -> String { bps < 1 ? "" : "\(ByteCountFormatter.string(fromByteCount: Int64(bps), countStyle: .file))/s" }13 static func date(_ d: Date) -> String { d.formatted(date: .abbreviated, time: .shortened) }14 static func relative(_ d: Date) -> String { d.formatted(.relative(presentation: .named)) }15 /// Date without the time (grid captions, pills).16 static func day(_ d: Date) -> String { d.formatted(date: .abbreviated, time: .omitted) }17 /// A time left, rounded the way a person says it: "8 s", "3 min", "1 h 20".18 static func duration(_ seconds: Double) -> String {19 guard seconds.isFinite, seconds >= 0 else { return "—" }20 let s = Int(seconds.rounded())21 if s < 60 { return "\(max(s, 1)) s" }22 if s < 3600 { return "\(Int((Double(s) / 60).rounded())) min" }23 if s < 86400 { return "\(s / 3600) h \(String(format: "%02d", (s % 3600) / 60))" }24 return "\(s / 86400) j"25 }26 static func kind(_ item: DriveItem) -> String {27 if item.isFolder { return "Dossier" }28 if let t = UTType(filenameExtension: item.fileExtension), let d = t.localizedDescription { return d.prefix(1).uppercased() + d.dropFirst() }29 return item.fileExtension.isEmpty ? "Document" : "Fichier \(item.fileExtension.uppercased())"30 }31}3233/// The icon the Finder would show for this kind of item.34struct ItemIcon: View {35 let item: DriveItem36 var size: CGFloat = 2037 var body: some View {38 Image(nsImage: ItemIcon.image(for: item))39 .resizable().interpolation(.high).frame(width: size, height: size)40 .opacity(item.id.hasPrefix(OperationJournal.localPrefix) ? 0.6 : 1)41 .accessibilityHidden(true)42 }43 static func image(for item: DriveItem) -> NSImage {44 if item.isFolder { return NSWorkspace.shared.icon(for: .folder) }45 return NSWorkspace.shared.icon(for: UTType(filenameExtension: item.fileExtension) ?? .data)46 }47}4849/// Small symbols after a name: offline, on this Mac, shared, favourite, waiting to be sent, antivirus.50struct ItemBadges: View {51 let model: AppModel // given explicitly: table cells are hosted outside the SwiftUI environment52 let item: DriveItem53 var body: some View {54 HStack(spacing: 5) {55 if model.isPending(item.id) { badge("arrow.triangle.2.circlepath", "En attente d’envoi au serveur", SPDTheme.warning) }56 if item.starred { badge("star.fill", "Favori", SPDTheme.warning) }57 if model.sharedIDs.contains(item.id) { badge("link", "Partagé par un lien", SPDTheme.brand) }58 if model.isPinned(item.id) { badge("arrow.down.circle.fill", "Disponible hors ligne", SPDTheme.positive) }59 else if !item.isFolder, model.isLocal(item.id) { badge("checkmark.circle", "Copie sur ce Mac", SPDTheme.textSecondary) }60 if item.scan == "infected" { badge("exclamationmark.shield.fill", "Bloqué par l’antivirus", SPDTheme.critical) }61 }62 .font(.caption)63 }64 private func badge(_ symbol: String, _ label: String, _ color: Color) -> some View {65 Image(systemName: symbol).foregroundStyle(color).help(label).accessibilityLabel(label)66 }67}6869struct SyncStateLabel: View {70 let model: AppModel71 let item: DriveItem72 var body: some View {73 let (symbol, text): (String, String) = {74 if model.isPending(item.id) { return ("arrow.triangle.2.circlepath", "En attente") }75 if item.isTrashed { return ("trash", "Corbeille") }76 if model.isPinned(item.id) { return ("arrow.down.circle.fill", "Hors ligne") }77 if !item.isFolder, model.isLocal(item.id) { return ("checkmark.circle", "Sur ce Mac") }78 return ("cloud", "Sur SP Drive")79 }()80 Label(text, systemImage: symbol).labelStyle(.titleAndIcon).foregroundStyle(.secondary)81 }82}8384struct BannerView: View {85 @Environment(AppModel.self) private var model86 var body: some View {87 if let b = model.banner {88 HStack(spacing: 10) {89 Image(systemName: b.isError ? "exclamationmark.triangle.fill" : "info.circle.fill").foregroundStyle(b.isError ? SPDTheme.warning : SPDTheme.brand)90 Text(b.text).font(.callout).lineLimit(3).textSelection(.enabled)91 Spacer(minLength: 8)92 Button { model.banner = nil } label: { Image(systemName: "xmark") }.buttonStyle(.plain).accessibilityLabel("Fermer le message")93 }94 .padding(.horizontal, 14).padding(.vertical, 10)95 .background(.regularMaterial, in: RoundedRectangle(cornerRadius: 12, style: .continuous))96 .overlay(RoundedRectangle(cornerRadius: 12, style: .continuous).strokeBorder(SPDTheme.hairline))97 .spdElevation(.e2)98 .padding(.horizontal, 16).padding(.bottom, 12)99 .transition(.move(edge: .bottom).combined(with: .opacity))100 .task(id: b.id) { try? await Task.sleep(nanoseconds: 9_000_000_000); if model.banner?.id == b.id { withAnimation { model.banner = nil } } }101 }102 }103}104105/// The SP Drive mark, drawn in SwiftUI: a calm two-tone tile (indigo to violet, the kinship with the web app) — no gloss, no heavy gradient.106struct DriveMark: View {107 var size: CGFloat = 84108 var body: some View {109 ZStack {110 RoundedRectangle(cornerRadius: size * 0.225, style: .continuous)111 .fill(LinearGradient(colors: [SPDTheme.brandIndigo, SPDTheme.brand], startPoint: .topLeading, endPoint: .bottomTrailing))112 Image(systemName: "folder.fill").font(.system(size: size * 0.5, weight: .regular)).foregroundStyle(.white.opacity(0.96))113 Image(systemName: "arrow.triangle.2.circlepath").font(.system(size: size * 0.2, weight: .bold)).foregroundStyle(SPDTheme.Palette.brand.color).offset(y: size * 0.05).environment(\.colorScheme, .light)114 }115 .frame(width: size, height: size).spdElevation(size > 40 ? .e2 : .e1)116 .accessibilityHidden(true)117 }118}119