import SwiftUI import AnalyticsKit // The house "iCloud Sync" and "Diagnostics" settings, copied from Inkspring's iPhone settings // (native-mac/iPhone/MobileSettings.swift: syncSection, the Diagnostics section and syncDiagnostics). // Same labels, same order, same accessibility identifiers; only the workspace row and the Tide styling // are Graphley's. Used on the iPhone, the iPad and in the Mac's "iPhone and iPad" card. /// The "iCloud Sync" rows and the Check Now button. public struct CloudSyncSection: View { let status: CloudSyncStatus let busy: Bool /// Shown when iCloud has no snapshot from the Mac yet. let missingMessage: String let check: () -> Void public init(status: CloudSyncStatus, busy: Bool, missingMessage: String, check: @escaping () -> Void) { self.status = status; self.busy = busy; self.missingMessage = missingMessage; self.check = check } public var body: some View { VStack(alignment: .leading, spacing: 10) { CloudSyncRow("Apple Account", status.accountStatus) .accessibilityIdentifier("settings.libraryStatus") CloudSyncRow("Mac snapshot", CloudSyncText.snapshotStatus(status)) CloudSyncRow("Last check", CloudSyncText.timestamp(status.lastCheck)) CloudSyncRow("Last iCloud response", CloudSyncText.timestamp(status.lastCloudContact)) if let date = status.snapshotDate { CloudSyncRow("Numbers from", CloudSyncText.timestamp(date)) } if let error = status.error { Text(error).font(Tide.secondary).foregroundStyle(Tide.downText) .fixedSize(horizontal: false, vertical: true) } else if status.snapshotFound == false { Text(missingMessage).font(Tide.secondary).foregroundStyle(Tide.muted) .fixedSize(horizontal: false, vertical: true) } Button(action: check) { if busy { Label("Checking iCloud…", systemImage: "arrow.clockwise") } else { Label("Check Now", systemImage: "arrow.clockwise") } } .buttonStyle(.tide(.secondary)) .disabled(busy) .accessibilityIdentifier("settings.checkSync") } } } /// The "Diagnostics" rows: the error code and Share Sync Diagnostics. public struct CloudDiagnosticsSection: View { let status: CloudSyncStatus let appName: String public init(status: CloudSyncStatus, appName: String = "Graphley") { self.status = status; self.appName = appName } public var body: some View { VStack(alignment: .leading, spacing: 10) { if let code = status.syncErrorCode { CloudSyncRow("Error code", code).textSelection(.enabled) } ShareLink(item: CloudSyncText.diagnostics(status, appName: appName)) { Label("Share Sync Diagnostics", systemImage: "square.and.arrow.up") } .buttonStyle(.tide(.secondary)) .accessibilityIdentifier("settings.shareSyncDiagnostics") Text("Includes sync status and timestamps. Your numbers, keys, and account details are not included.") .font(Tide.caption).foregroundStyle(Tide.muted) .fixedSize(horizontal: false, vertical: true) } } } /// One label and value, like Inkspring's LabeledContent rows. public struct CloudSyncRow: View { let label: String, value: String public init(_ label: String, _ value: String) { self.label = label; self.value = value } public var body: some View { LabeledContent { Text(value).font(Tide.secondary).foregroundStyle(Tide.ink2).multilineTextAlignment(.trailing) } label: { Text(label).font(Tide.rowTitle).foregroundStyle(Tide.ink) } } } public enum CloudSyncText { public static func snapshotStatus(_ status: CloudSyncStatus) -> String { switch status.snapshotFound { case true?: "Found" case false?: "Not found yet" case nil: "Not checked" } } public static func timestamp(_ date: Date?) -> String { date?.formatted(date: .abbreviated, time: .standard) ?? "—" } /// Plain text for Share Sync Diagnostics, in Inkspring's format. No keys, numbers or account names. public static func diagnostics(_ status: CloudSyncStatus, appName: String) -> String { let info = Bundle.main.infoDictionary ?? [:] let version = info["CFBundleShortVersionString"] as? String ?? "—" let build = info["CFBundleVersion"] as? String ?? "—" var lines = [ "\(appName) \(version) (\(build))", "Apple Account: \(status.accountStatus)", "Mac snapshot: \(snapshotStatus(status))", "Last check: \(timestamp(status.lastCheck))", "Last iCloud response: \(timestamp(status.lastCloudContact))", "Numbers from: \(timestamp(status.snapshotDate))" ] if let code = status.syncErrorCode { lines.append("Error code: \(code)") } return lines.joined(separator: "\n") } }