Branchwell

Graphley / app · graphley.ai / Sources / AnalyticsUI / CloudSyncSettings.swift

1import SwiftUI
2import AnalyticsKit
3
4// The house "iCloud Sync" and "Diagnostics" settings, copied from Inkspring's iPhone settings
5// (native-mac/iPhone/MobileSettings.swift: syncSection, the Diagnostics section and syncDiagnostics).
6// Same labels, same order, same accessibility identifiers; only the workspace row and the Tide styling
7// are Graphley's. Used on the iPhone, the iPad and in the Mac's "iPhone and iPad" card.
8
9/// The "iCloud Sync" rows and the Check Now button.
10public struct CloudSyncSection: View {
11 let status: CloudSyncStatus
12 let busy: Bool
13 /// Shown when iCloud has no snapshot from the Mac yet.
14 let missingMessage: String
15 let check: () -> Void
16
17 public init(status: CloudSyncStatus, busy: Bool, missingMessage: String, check: @escaping () -> Void) {
18 self.status = status; self.busy = busy; self.missingMessage = missingMessage; self.check = check
19 }
20
21 public var body: some View {
22 VStack(alignment: .leading, spacing: 10) {
23 CloudSyncRow("Apple Account", status.accountStatus)
24 .accessibilityIdentifier("settings.libraryStatus")
25 CloudSyncRow("Mac snapshot", CloudSyncText.snapshotStatus(status))
26 CloudSyncRow("Last check", CloudSyncText.timestamp(status.lastCheck))
27 CloudSyncRow("Last iCloud response", CloudSyncText.timestamp(status.lastCloudContact))
28 if let date = status.snapshotDate {
29 CloudSyncRow("Numbers from", CloudSyncText.timestamp(date))
30 }
31 if let error = status.error {
32 Text(error).font(Tide.secondary).foregroundStyle(Tide.downText)
33 .fixedSize(horizontal: false, vertical: true)
34 } else if status.snapshotFound == false {
35 Text(missingMessage).font(Tide.secondary).foregroundStyle(Tide.muted)
36 .fixedSize(horizontal: false, vertical: true)
37 }
38 Button(action: check) {
39 if busy {
40 Label("Checking iCloud…", systemImage: "arrow.clockwise")
41 } else {
42 Label("Check Now", systemImage: "arrow.clockwise")
43 }
44 }
45 .buttonStyle(.tide(.secondary))
46 .disabled(busy)
47 .accessibilityIdentifier("settings.checkSync")
48 }
49 }
50}
51
52/// The "Diagnostics" rows: the error code and Share Sync Diagnostics.
53public struct CloudDiagnosticsSection: View {
54 let status: CloudSyncStatus
55 let appName: String
56
57 public init(status: CloudSyncStatus, appName: String = "Graphley") {
58 self.status = status; self.appName = appName
59 }
60
61 public var body: some View {
62 VStack(alignment: .leading, spacing: 10) {
63 if let code = status.syncErrorCode {
64 CloudSyncRow("Error code", code).textSelection(.enabled)
65 }
66 ShareLink(item: CloudSyncText.diagnostics(status, appName: appName)) {
67 Label("Share Sync Diagnostics", systemImage: "square.and.arrow.up")
68 }
69 .buttonStyle(.tide(.secondary))
70 .accessibilityIdentifier("settings.shareSyncDiagnostics")
71 Text("Includes sync status and timestamps. Your numbers, keys, and account details are not included.")
72 .font(Tide.caption).foregroundStyle(Tide.muted)
73 .fixedSize(horizontal: false, vertical: true)
74 }
75 }
76}
77
78/// One label and value, like Inkspring's LabeledContent rows.
79public struct CloudSyncRow: View {
80 let label: String, value: String
81 public init(_ label: String, _ value: String) { self.label = label; self.value = value }
82 public var body: some View {
83 LabeledContent {
84 Text(value).font(Tide.secondary).foregroundStyle(Tide.ink2).multilineTextAlignment(.trailing)
85 } label: {
86 Text(label).font(Tide.rowTitle).foregroundStyle(Tide.ink)
87 }
88 }
89}
90
91public enum CloudSyncText {
92 public static func snapshotStatus(_ status: CloudSyncStatus) -> String {
93 switch status.snapshotFound {
94 case true?: "Found"
95 case false?: "Not found yet"
96 case nil: "Not checked"
97 }
98 }
99
100 public static func timestamp(_ date: Date?) -> String {
101 date?.formatted(date: .abbreviated, time: .standard) ?? "—"
102 }
103
104 /// Plain text for Share Sync Diagnostics, in Inkspring's format. No keys, numbers or account names.
105 public static func diagnostics(_ status: CloudSyncStatus, appName: String) -> String {
106 let info = Bundle.main.infoDictionary ?? [:]
107 let version = info["CFBundleShortVersionString"] as? String ?? "—"
108 let build = info["CFBundleVersion"] as? String ?? "—"
109 var lines = [
110 "\(appName) \(version) (\(build))",
111 "Apple Account: \(status.accountStatus)",
112 "Mac snapshot: \(snapshotStatus(status))",
113 "Last check: \(timestamp(status.lastCheck))",
114 "Last iCloud response: \(timestamp(status.lastCloudContact))",
115 "Numbers from: \(timestamp(status.snapshotDate))"
116 ]
117 if let code = status.syncErrorCode { lines.append("Error code: \(code)") }
118 return lines.joined(separator: "\n")
119 }
120}