Branchwell

Graphley / app · graphley.ai / Apps / Mac / SettingsView.swift

1import SwiftUI
2import AnalyticsKit
3import AnalyticsUI
4
5/// Settings, as a screen in the main window (⌘, opens it). Replaces the old separate Settings window.
6struct SettingsView: View {
7 @Environment(AppModel.self) private var model
8 @EnvironmentObject private var updates: AppUpdates
9 @Environment(\.openURL) private var openURL
10 @State private var turnedOnKeys = false
11
12 var body: some View {
13 let s = model.workspace.settings
14 VStack(alignment: .leading, spacing: 16) {
15 ScreenHeading("Settings", subtitle: "How Graphley refreshes, shares with your iPhone and iPad, and works with your agents.")
16
17 TideCard("Agents") {
18 VStack(alignment: .leading, spacing: 0) {
19 ToggleRow("Let agents make changes",
20 detail: "Agents can add sites and apps, build dashboards and refresh. Every change is listed on the Agents screen.",
21 isOn: setting(\.agentsCanWrite)).padding(.bottom, 12)
22 RowDivider()
23 VStack(alignment: .leading, spacing: 6) {
24 ToggleRow("Let agents set keys",
25 detail: "Agents can save an App Store Connect key for you. Leave this off unless you're setting one up.",
26 isOn: keysToggle)
27 if turnedOnKeys && s.agentsCanSetKeys {
28 Text("Turn this off again after your agent saves the key.").font(Tide.caption).foregroundStyle(Tide.attentionText)
29 }
30 }
31 .padding(.vertical, 12)
32 RowDivider()
33 Button("Connect an Agent…") { model.selection = .agents }
34 .buttonStyle(.tide(.quiet)).padding(.top, 8).padding(.leading, -14)
35 }
36 }
37
38 TideCard("iPhone and iPad") {
39 ToggleRow("Share with iPhone and iPad through iCloud",
40 detail: "After each refresh, your Mac puts a copy of the numbers in your private iCloud. Your iPhone and iPad read it. Keys are never shared.",
41 isOn: setting(\.shareWithIPhone))
42 Text(iPhoneLine(s)).font(Tide.caption).foregroundStyle(model.failures["iCloud sync"] != nil ? Tide.downText : Tide.muted)
43 }
44
45 TideCard("iCloud Sync", subtitle: "What iCloud has from this Mac. Your iPhone and iPad read the same copy.", icon: "icloud") {
46 CloudSyncSection(status: model.cloudStatus, busy: model.checkingCloud,
47 missingMessage: "iCloud has no snapshot from this Mac yet. Turn on sharing above, then check again.") {
48 Task { await model.checkCloud() }
49 }
50 }
51 .task { if model.cloudStatus.lastCheck == nil { await model.checkCloud(share: false) } }
52
53 TideCard("Diagnostics", icon: "stethoscope") {
54 CloudDiagnosticsSection(status: model.cloudStatus)
55 }
56
57 TideCard("Daily refresh") {
58 HStack(spacing: 10) {
59 Text("Refresh every day at").font(Tide.rowTitle).foregroundStyle(Tide.ink)
60 Menu {
61 ForEach(0..<24, id: \.self) { h in
62 Button(Self.hourLabel(h)) { model.updateSettings { $0.refreshHour = h } }
63 }
64 } label: {
65 HStack(spacing: 6) {
66 Text(Self.hourLabel(s.refreshHour))
67 Image(systemName: "chevron.up.chevron.down").font(.system(size: 10, weight: .semibold))
68 }
69 }
70 .menuStyle(.button)
71 .menuIndicator(.hidden)
72 .buttonStyle(.tide(.secondary))
73 .fixedSize()
74 }
75 Text("Graphley refreshes at this time while it's open. If it was closed, it refreshes when you open it.")
76 .font(Tide.secondary).foregroundStyle(Tide.muted)
77 HStack(spacing: 10) {
78 Button { Task { await model.refresh() } } label: {
79 if model.refreshing { Label("Refreshing…", systemImage: "arrow.clockwise") } else { Text("Refresh Now") }
80 }
81 .buttonStyle(.tide(.secondary)).disabled(model.refreshing)
82 Text("⌘R").font(Tide.caption).foregroundStyle(Tide.muted)
83 }
84 }
85
86 TideCard("Updates") {
87 HStack(alignment: .firstTextBaseline) {
88 Text("Graphley \(Self.version)").font(Tide.rowTitle).foregroundStyle(Tide.ink)
89 Spacer()
90 if updates.isAvailable {
91 Button("Check for Updates") { updates.check() }.buttonStyle(.tide(.secondary)).disabled(!updates.canCheck)
92 }
93 }
94 Text(updates.isAvailable ? "Graphley checks for updates on its own and asks before installing." : "Updates are off in this build.")
95 .font(Tide.secondary).foregroundStyle(Tide.muted)
96 }
97
98 TideCard("About") {
99 HStack(spacing: 12) {
100 TideMark(size: 44)
101 VStack(alignment: .leading, spacing: 2) {
102 Text("Graphley").font(Tide.cardTitle).foregroundStyle(Tide.ink)
103 Text("Version \(Self.version)").font(Tide.caption).foregroundStyle(Tide.muted)
104 }
105 }
106 Text("Free and open source, public domain (Unlicense). Graphley has no server: your numbers stay on this Mac and in your private iCloud.")
107 .font(Tide.secondary).foregroundStyle(Tide.ink2).fixedSize(horizontal: false, vertical: true)
108 HStack(spacing: 4) {
109 link("graphley.ai", "https://graphley.ai")
110 link("Privacy", "https://graphley.ai/privacy/")
111 }
112 .padding(.leading, -10)
113 }
114 }
115 }
116
117 func link(_ title: String, _ url: String) -> some View {
118 Button { openURL(URL(string: url)!) } label: {
119 HStack(spacing: 4) { Text(title); Image(systemName: "arrow.up.right").font(.system(size: 10, weight: .semibold)) }
120 }
121 .buttonStyle(.tide(.quiet, size: .small))
122 }
123
124 var keysToggle: Binding<Bool> {
125 Binding(get: { model.workspace.settings.agentsCanSetKeys },
126 set: { v in model.updateSettings { $0.agentsCanSetKeys = v }; if v { turnedOnKeys = true } })
127 }
128
129 func setting(_ key: WritableKeyPath<AnalyticsKit.Settings, Bool>) -> Binding<Bool> {
130 Binding(get: { model.workspace.settings[keyPath: key] }, set: { v in model.updateSettings { $0[keyPath: key] = v } })
131 }
132
133 func iPhoneLine(_ s: AnalyticsKit.Settings) -> String {
134 if model.failures["iCloud sync"] != nil { return "Couldn't share with your iPhone and iPad. Make sure this Mac is signed in to iCloud." }
135 guard s.shareWithIPhone else { return "Your iPhone and iPad keep showing the last numbers they received." }
136 guard let at = s.lastSharedAt else { return "Not shared yet. It happens after the next refresh." }
137 let day = Calendar.current.isDateInToday(at) ? "today" : Calendar.current.isDateInYesterday(at) ? "yesterday" : at.formatted(.dateTime.month(.abbreviated).day())
138 return "Last shared \(day) at \(at.formatted(date: .omitted, time: .shortened))."
139 }
140
141 static func hourLabel(_ h: Int) -> String {
142 let date = Calendar.current.date(bySettingHour: h, minute: 0, second: 0, of: .now) ?? .now
143 return date.formatted(date: .omitted, time: .shortened)
144 }
145
146 static var version: String {
147 let info = Bundle.main.infoDictionary ?? [:]
148 return "\(info["CFBundleShortVersionString"] as? String ?? "?") (\(info["CFBundleVersion"] as? String ?? "?"))"
149 }
150}