import SwiftUI import AnalyticsKit import AnalyticsUI /// Settings, as a screen in the main window (⌘, opens it). Replaces the old separate Settings window. struct SettingsView: View { @Environment(AppModel.self) private var model @EnvironmentObject private var updates: AppUpdates @Environment(\.openURL) private var openURL @State private var turnedOnKeys = false var body: some View { let s = model.workspace.settings VStack(alignment: .leading, spacing: 16) { ScreenHeading("Settings", subtitle: "How Graphley refreshes, shares with your iPhone and iPad, and works with your agents.") TideCard("Agents") { VStack(alignment: .leading, spacing: 0) { ToggleRow("Let agents make changes", detail: "Agents can add sites and apps, build dashboards and refresh. Every change is listed on the Agents screen.", isOn: setting(\.agentsCanWrite)).padding(.bottom, 12) RowDivider() VStack(alignment: .leading, spacing: 6) { ToggleRow("Let agents set keys", detail: "Agents can save an App Store Connect key for you. Leave this off unless you're setting one up.", isOn: keysToggle) if turnedOnKeys && s.agentsCanSetKeys { Text("Turn this off again after your agent saves the key.").font(Tide.caption).foregroundStyle(Tide.attentionText) } } .padding(.vertical, 12) RowDivider() Button("Connect an Agent…") { model.selection = .agents } .buttonStyle(.tide(.quiet)).padding(.top, 8).padding(.leading, -14) } } TideCard("iPhone and iPad") { ToggleRow("Share with iPhone and iPad through iCloud", 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.", isOn: setting(\.shareWithIPhone)) Text(iPhoneLine(s)).font(Tide.caption).foregroundStyle(model.failures["iCloud sync"] != nil ? Tide.downText : Tide.muted) } TideCard("iCloud Sync", subtitle: "What iCloud has from this Mac. Your iPhone and iPad read the same copy.", icon: "icloud") { CloudSyncSection(status: model.cloudStatus, busy: model.checkingCloud, missingMessage: "iCloud has no snapshot from this Mac yet. Turn on sharing above, then check again.") { Task { await model.checkCloud() } } } .task { if model.cloudStatus.lastCheck == nil { await model.checkCloud(share: false) } } TideCard("Diagnostics", icon: "stethoscope") { CloudDiagnosticsSection(status: model.cloudStatus) } TideCard("Daily refresh") { HStack(spacing: 10) { Text("Refresh every day at").font(Tide.rowTitle).foregroundStyle(Tide.ink) Menu { ForEach(0..<24, id: \.self) { h in Button(Self.hourLabel(h)) { model.updateSettings { $0.refreshHour = h } } } } label: { HStack(spacing: 6) { Text(Self.hourLabel(s.refreshHour)) Image(systemName: "chevron.up.chevron.down").font(.system(size: 10, weight: .semibold)) } } .menuStyle(.button) .menuIndicator(.hidden) .buttonStyle(.tide(.secondary)) .fixedSize() } Text("Graphley refreshes at this time while it's open. If it was closed, it refreshes when you open it.") .font(Tide.secondary).foregroundStyle(Tide.muted) HStack(spacing: 10) { Button { Task { await model.refresh() } } label: { if model.refreshing { Label("Refreshing…", systemImage: "arrow.clockwise") } else { Text("Refresh Now") } } .buttonStyle(.tide(.secondary)).disabled(model.refreshing) Text("⌘R").font(Tide.caption).foregroundStyle(Tide.muted) } } TideCard("Updates") { HStack(alignment: .firstTextBaseline) { Text("Graphley \(Self.version)").font(Tide.rowTitle).foregroundStyle(Tide.ink) Spacer() if updates.isAvailable { Button("Check for Updates") { updates.check() }.buttonStyle(.tide(.secondary)).disabled(!updates.canCheck) } } Text(updates.isAvailable ? "Graphley checks for updates on its own and asks before installing." : "Updates are off in this build.") .font(Tide.secondary).foregroundStyle(Tide.muted) } TideCard("About") { HStack(spacing: 12) { TideMark(size: 44) VStack(alignment: .leading, spacing: 2) { Text("Graphley").font(Tide.cardTitle).foregroundStyle(Tide.ink) Text("Version \(Self.version)").font(Tide.caption).foregroundStyle(Tide.muted) } } Text("Free and open source, public domain (Unlicense). Graphley has no server: your numbers stay on this Mac and in your private iCloud.") .font(Tide.secondary).foregroundStyle(Tide.ink2).fixedSize(horizontal: false, vertical: true) HStack(spacing: 4) { link("graphley.ai", "https://graphley.ai") link("Privacy", "https://graphley.ai/privacy/") } .padding(.leading, -10) } } } func link(_ title: String, _ url: String) -> some View { Button { openURL(URL(string: url)!) } label: { HStack(spacing: 4) { Text(title); Image(systemName: "arrow.up.right").font(.system(size: 10, weight: .semibold)) } } .buttonStyle(.tide(.quiet, size: .small)) } var keysToggle: Binding { Binding(get: { model.workspace.settings.agentsCanSetKeys }, set: { v in model.updateSettings { $0.agentsCanSetKeys = v }; if v { turnedOnKeys = true } }) } func setting(_ key: WritableKeyPath) -> Binding { Binding(get: { model.workspace.settings[keyPath: key] }, set: { v in model.updateSettings { $0[keyPath: key] = v } }) } func iPhoneLine(_ s: AnalyticsKit.Settings) -> String { if model.failures["iCloud sync"] != nil { return "Couldn't share with your iPhone and iPad. Make sure this Mac is signed in to iCloud." } guard s.shareWithIPhone else { return "Your iPhone and iPad keep showing the last numbers they received." } guard let at = s.lastSharedAt else { return "Not shared yet. It happens after the next refresh." } let day = Calendar.current.isDateInToday(at) ? "today" : Calendar.current.isDateInYesterday(at) ? "yesterday" : at.formatted(.dateTime.month(.abbreviated).day()) return "Last shared \(day) at \(at.formatted(date: .omitted, time: .shortened))." } static func hourLabel(_ h: Int) -> String { let date = Calendar.current.date(bySettingHour: h, minute: 0, second: 0, of: .now) ?? .now return date.formatted(date: .omitted, time: .shortened) } static var version: String { let info = Bundle.main.infoDictionary ?? [:] return "\(info["CFBundleShortVersionString"] as? String ?? "?") (\(info["CFBundleVersion"] as? String ?? "?"))" } }