SPB Git

Repository Public

SyncStatusView.swift 6.8 KB 128 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Shell/SyncStatusView.swift3// Description: The toolbar's sync indicator: state of the account replica, queued changes, and the conflict notices (both versions, keep mine / keep theirs).4import SwiftUI5import VQuantCore6import VQuantKit7import VQuantUI89struct SyncStatusButton: View {10    let app: AppEnvironment11    @State private var open = false1213    var body: some View {14        let ind = app.sync.indicator15        Button { open.toggle() } label: {16            Label(ind.label, systemImage: ind.symbol)17                .symbolRenderingMode(.hierarchical)18                .foregroundStyle(tint(ind))19        }20        .help("Account sync: \(ind.label)")21        .accessibilityIdentifier("toolbar.sync")22        .accessibilityValue(ind.label)23        .popover(isPresented: $open, arrowEdge: .bottom) { SyncPanel(app: app).frame(width: 380) }24    }2526    private func tint(_ i: SyncIndicator) -> Color {27        switch i {28        case .synced: VQTokens.Palette.text229        case .attention: VQTokens.Palette.warn30        case .locked: VQTokens.Palette.critical31        case .offline: VQTokens.Palette.warn32        default: VQTokens.Palette.text333        }34    }35}3637/// What the popover says: one sentence about the state, then each notice that needs a decision.38struct SyncPanel: View {39    let app: AppEnvironment4041    var body: some View {42        VStack(alignment: .leading, spacing: 12) {43            HStack(alignment: .firstTextBaseline) {44                Text("Account sync").font(VQTokens.Typography.title.font)45                Spacer()46                Button("Sync Now") { Task { await app.sync.syncNow() } }.controlSize(.small).disabled(app.sync.active == nil)47            }48            Text(sentence).font(.callout).foregroundStyle(VQTokens.Palette.text2).fixedSize(horizontal: false, vertical: true)49            if let s = app.sync.active?.status.status {50                Grid(alignment: .leading, horizontalSpacing: 14, verticalSpacing: 4) {51                    GridRow { Text("Queued changes").foregroundStyle(VQTokens.Palette.text3); Text("\(s.pendingWrites)").monospacedDigit() }52                    GridRow { Text("Change cursor").foregroundStyle(VQTokens.Palette.text3); Text("\(s.cursor)").monospacedDigit() }53                    GridRow { Text("Last pull").foregroundStyle(VQTokens.Palette.text3); Text(s.lastPulledAt.map { Fmt.relative($0) } ?? "—") }54                    if let e = s.lastError { GridRow { Text("Last error").foregroundStyle(VQTokens.Palette.text3); Text(e).lineLimit(2) } }55                }56                .font(.caption)57            }58            if let note = app.sync.migrationNote {59                Text(note).font(.caption).foregroundStyle(VQTokens.Palette.text2).fixedSize(horizontal: false, vertical: true)60            }61            let conflicts = app.sync.active?.status.conflicts ?? []62            if !conflicts.isEmpty {63                Divider()64                Text("Notices").font(VQTokens.Typography.overline.font).textCase(.uppercase).foregroundStyle(VQTokens.Palette.text3)65                ScrollView {66                    VStack(alignment: .leading, spacing: 10) {67                        ForEach(conflicts) { c in ConflictRow(app: app, record: c) }68                    }69                }70                .frame(maxHeight: 260)71            }72        }73        .padding(16)74    }7576    private var sentence: String {77        switch app.sync.indicator {78        case .off: "Sync is not running. Sign in to keep watchlists, portfolios, charts, projects and preferences identical on every device."79        case .connecting: "Connecting to the VQuant server…"80        case .synced: "Everything on this Mac matches your account. Changes made on the web or another device appear here within seconds."81        case .pending(let n): "\(n) change\(n == 1 ? " is" : "s are") being sent to the server."82        case .offline(let n): n == 0 ? "The server cannot be reached. This Mac shows the last state it received and will reconnect by itself." : "The server cannot be reached. \(n) change\(n == 1 ? " is" : "s are") kept on this Mac and will be sent, in order, when the connection returns."83        case .locked: "The server ended this Mac's session. Sign in again; queued changes are kept until then."84        case .attention(let n): "\(n) change\(n == 1 ? "" : "s") made on this Mac met a different version on the server. Review below."85        }86    }87}8889private struct ConflictRow: View {90    let app: AppEnvironment91    let record: VQConflictRecord9293    var body: some View {94        VStack(alignment: .leading, spacing: 4) {95            HStack(spacing: 6) {96                Image(systemName: record.needsAttention ? "exclamationmark.triangle" : "arrow.triangle.merge").foregroundStyle(record.needsAttention ? VQTokens.Palette.warn : VQTokens.Palette.text3)97                Text(title).font(.callout.weight(.medium)).lineLimit(1)98                Spacer()99                Text(Fmt.relative(record.at)).font(.caption2).foregroundStyle(VQTokens.Palette.text3)100            }101            Text(explanation).font(.caption).foregroundStyle(VQTokens.Palette.text2).fixedSize(horizontal: false, vertical: true)102            HStack {103                if record.needsAttention, record.remote != nil, record.resolution != .rejected {104                    Button("Keep the other version") { Task { await app.sync.active?.status.keepRemote(record.id) } }.controlSize(.small)105                }106                Button(record.needsAttention ? "Keep mine" : "Dismiss") { Task { await app.sync.active?.status.dismiss(record.id) } }.controlSize(.small)107            }108        }109        .padding(10)110        .background(VQTokens.Palette.ink3.opacity(0.5), in: RoundedRectangle(cornerRadius: VQTokens.Radius.sm))111    }112113    private var name: String {114        let d = record.result ?? record.local ?? record.remote115        return d?["name"]?.stringValue ?? d?["title"]?.stringValue ?? d?["symbol"]?.stringValue ?? record.key.id116    }117    private var title: String { "\(record.key.kind.rawValue.capitalized) “\(name)”" }118    private var explanation: String {119        switch record.resolution {120        case .mergedElements: "Edited here and on another device at the same time. The lists (\(record.mergedLists.joined(separator: ", "))) were merged; nothing was lost."121        case .localWon: "Another device changed \(record.differingKeys.isEmpty ? "it" : record.differingKeys.joined(separator: ", ")) while this Mac was editing. This Mac's version was kept."122        case .localDeleteWon: "Deleted here while another device was editing it. The deletion was applied; the other version can be restored."123        case .recreated: "It had been deleted on another device; the edit made here created it again."124        case .rejected: "The server refused the change made here\(record.message.map { ": \($0)" } ?? "."). The edit was dropped."125        }126    }127}128
Files main
Menu

Theme