Repository Public
1// SP Drive iOS2// File: Features/Transfers/TransferPill.swift3// Description: The compact transfer indicator: a pill above the tab bar that says, in one line, what is travelling or why it is not4// (waiting for Wi-Fi, offline, Low Power Mode…), and expands into the transfer centre. Absent when there is nothing to say.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI910extension TransferPolicy.Hold {11 var label: String {12 switch self {13 case .offline: String(localized: "Hors ligne")14 case .waitingForWiFi: String(localized: "En attente du Wi-Fi")15 case .lowDataMode: String(localized: "Mode données réduites")16 case .lowPowerMode: String(localized: "Mode économie d’énergie")17 case .lowBattery: String(localized: "Batterie faible")18 }19 }20 var explanation: String {21 switch self {22 case .offline: String(localized: "Les transferts reprendront au retour du réseau.")23 case .waitingForWiFi: String(localized: "« Wi-Fi seulement » est activé : les transferts attendent un réseau Wi-Fi.")24 case .lowDataMode: String(localized: "En mode données réduites, seuls les fichiers que tu ouvres ou envoies toi-même voyagent. Les copies hors ligne attendent.")25 case .lowPowerMode: String(localized: "En mode économie d’énergie, les copies hors ligne attendent. Les fichiers que tu ouvres ou envoies voyagent.")26 case .lowBattery: String(localized: "Batterie faible : les copies hors ligne attendent que l’appareil soit branché.")27 }28 }29 var symbol: String {30 switch self {31 case .offline: "wifi.slash"; case .waitingForWiFi: "wifi.exclamationmark"; case .lowDataMode: "antenna.radiowaves.left.and.right.slash"32 case .lowPowerMode: "battery.25percent"; case .lowBattery: "battery.0percent"33 }34 }35}3637/// What the pill says right now, derived from the local store (no network).38struct TransferSummary: Equatable {39 var active = 0, uploads = 0, downloads = 0, failed = 0, pendingOps = 040 var done: Int64 = 0, total: Int64 = 041 var hold: TransferPolicy.Hold?42 var online = true43 var fraction: Double { total > 0 ? min(1, Double(done) / Double(total)) : 0 }44 var isVisible: Bool { active > 0 || failed > 0 || (!online && pendingOps > 0) }4546 @MainActor init(model: AppModel, live: [String: Int64] = [:]) {47 _ = model.transfersVersion; _ = model.itemsVersion48 let all = model.store.transfers(states: [.queued, .running, .failed])49 let moving = all.filter(\.isActive)50 active = moving.count51 uploads = moving.filter { $0.kind == .upload }.count52 downloads = active - uploads53 failed = all.filter { $0.state == .failed && $0.errorCode != "conflict" }.count54 done = moving.reduce(0) { $0 + min($1.size, live[$1.id] ?? $1.done) }55 total = moving.reduce(0) { $0 + $1.size }56 online = model.network.online57 hold = !online ? .offline : (moving.contains { $0.state == .running } ? nil : model.dominantHold)58 pendingOps = online ? 0 : model.store.ops().filter { $0.state != .failed }.count59 }6061 var title: String {62 if let hold, active > 0 || pendingOps > 0 {63 let n = max(active, pendingOps)64 return "\(hold.label) · " + (n == 1 ? String(localized: "1 en attente") : String(localized: "\(n) en attente"))65 }66 if active > 0 {67 let what = uploads > 0 && downloads > 0 ? (active == 1 ? String(localized: "1 transfert") : String(localized: "\(active) transferts"))68 : uploads > 0 ? (uploads == 1 ? String(localized: "1 envoi") : String(localized: "\(uploads) envois"))69 : (downloads == 1 ? String(localized: "1 téléchargement") : String(localized: "\(downloads) téléchargements"))70 return "\(what) · \(Int(fraction * 100)) %"71 }72 return failed == 1 ? String(localized: "1 transfert interrompu") : String(localized: "\(failed) transferts interrompus")73 }74}7576struct TransferPill: View {77 @Environment(AppModel.self) private var model78 @Environment(\.accessibilityReduceMotion) private var reduceMotion79 @Environment(\.colorScheme) private var scheme80 @Namespace private var namespace81 @State private var expanded = false82 @State private var live: [String: Int64] = [:]8384 var body: some View {85 let summary = TransferSummary(model: model, live: live)86 let running = model.store.transfers(states: [.running]).map(\.id)87 ZStack {88 if summary.isVisible {89 Button { Haptics.tap(); expanded = true } label: { label(summary) }90 .buttonStyle(.plain)91 .matchedTransitionSource(id: "pill", in: namespace)92 .padding(.horizontal, SPDTheme.Space.lg).padding(.bottom, SPDTheme.Space.sm)93 .transition(reduceMotion ? .opacity : .move(edge: .bottom).combined(with: .opacity))94 .accessibilityIdentifier("transferPill")95 .accessibilityHint(Text("Ouvre le centre de transferts"))96 }97 }98 .animation(SPDTheme.Motion.resolve(SPDTheme.Motion.reveal, reduceMotion: reduceMotion), value: summary.isVisible)99 .sheet(isPresented: $expanded) {100 NavigationStack {101 TransfersScreen().toolbar { ToolbarItem(placement: .cancellationAction) { Button("Fermer") { expanded = false } } }102 }103 .presentationDetents([.medium, .large])104 .presentationDragIndicator(.visible)105 .zoomTransition(id: "pill", in: namespace, reduceMotion: reduceMotion)106 }107 .onChange(of: model.pendingRoute) { _, route in if route == .transfers { model.pendingRoute = nil; expanded = true } }108 .task(id: running) {109 guard !running.isEmpty else { live = [:]; return }110 while !Task.isCancelled {111 var fresh: [String: Int64] = [:]112 for id in running { if let done = await model.engine.progress(of: id) { fresh[id] = done } }113 if fresh != live { live = fresh }114 try? await Task.sleep(for: .milliseconds(500))115 }116 }117 }118119 private func label(_ s: TransferSummary) -> some View {120 HStack(spacing: SPDTheme.Space.md) {121 Group {122 if let hold = s.hold, s.active > 0 || s.pendingOps > 0 { Image(systemName: hold.symbol).foregroundStyle(SPDTheme.Palette.warning) }123 else if s.active > 0 { ProgressRing(fraction: s.fraction, symbol: s.uploads >= s.downloads ? "arrow.up" : "arrow.down").scaleEffect(1.25) }124 else { Image(systemName: "exclamationmark.circle.fill").foregroundStyle(SPDTheme.Palette.critical) }125 }126 .frame(width: 26)127 Text(s.title).font(SPDTheme.Typo.callout.weight(.medium)).monospacedDigit().lineLimit(2).fixedSize(horizontal: false, vertical: true)128 Spacer(minLength: 0)129 Image(systemName: "chevron.up").font(.footnote.weight(.semibold)).foregroundStyle(.secondary)130 }131 .padding(.horizontal, SPDTheme.Space.lg).padding(.vertical, SPDTheme.Space.md)132 .frame(maxWidth: 520)133 .background(.regularMaterial, in: Capsule())134 .overlay(Capsule().strokeBorder(SPDTheme.Palette.hairline.opacity(0.5), lineWidth: 0.5))135 .shadow(color: .black.opacity(SPDTheme.Elevation.e2.opacity(scheme)), radius: 14, y: 5)136 .contentShape(.hoverEffect, Capsule())137 .hoverEffect(.lift)138 .accessibilityElement(children: .ignore)139 .accessibilityLabel(Text("Transferts"))140 .accessibilityValue(Text(s.title))141 .accessibilityAddTraits(.isButton)142 }143}144145extension View {146 @ViewBuilder func zoomTransition(id: String, in namespace: Namespace.ID, reduceMotion: Bool) -> some View {147 if reduceMotion { self } else { navigationTransition(.zoom(sourceID: id, in: namespace)) }148 }149}150