OpenTalon / app / native / Sources / OpenTalon / DailyBackupView.swift
| 1 | import AppKit |
| 2 | import ServiceManagement |
| 3 | import SwiftUI |
| 4 | |
| 5 | struct DailyBackupStatus: Decodable { |
| 6 | let enabled: Bool |
| 7 | let folder: String |
| 8 | let usesDefaultFolder: Bool |
| 9 | let lastBackupAt: String? |
| 10 | let lastError: String |
| 11 | } |
| 12 | |
| 13 | enum BackupAgent { |
| 14 | static let plist = "com.protomota.opentalon.native.daily-backup.plist" |
| 15 | static var service: SMAppService { .agent(plistName: plist) } |
| 16 | } |
| 17 | |
| 18 | struct DailyBackupView: View { |
| 19 | @Environment(WorkspaceModel.self) private var model |
| 20 | @Environment(\.palette) private var palette |
| 21 | @State private var status: DailyBackupStatus? |
| 22 | @State private var background = SMAppService.Status.notRegistered |
| 23 | @State private var error: String? |
| 24 | @State private var busy = false |
| 25 | |
| 26 | var body: some View { |
| 27 | VStack(alignment: .leading, spacing: 8) { |
| 28 | Text("Daily backups").font(.system(size: 13, weight: .semibold)) |
| 29 | if let status { |
| 30 | Toggle("Automatically save a backup each day", isOn: Binding( |
| 31 | get: { status.enabled }, |
| 32 | set: { configure(["enabled": $0]) } |
| 33 | )) |
| 34 | Text("OpenTalon checks for today’s backup when it opens and while it runs, including when its window is closed.") |
| 35 | .font(.caption).foregroundStyle(palette.muted) |
| 36 | if !model.isolatedProfile && status.enabled { |
| 37 | Toggle("Back up even when OpenTalon is quit", isOn: Binding( |
| 38 | get: { background == .enabled || background == .requiresApproval }, |
| 39 | set: configureBackground |
| 40 | )) |
| 41 | if background == .requiresApproval { |
| 42 | HStack { |
| 43 | Text("Allow OpenTalon in Login Items & Extensions to finish enabling daily background backups.") |
| 44 | .font(.caption).foregroundStyle(palette.muted) |
| 45 | Button("Open System Settings") { SMAppService.openSystemSettingsLoginItems() } |
| 46 | } |
| 47 | } else if background == .enabled { |
| 48 | Text("macOS checks at 3:00 a.m. local time while you’re signed in.") |
| 49 | .font(.caption).foregroundStyle(palette.muted) |
| 50 | } |
| 51 | } |
| 52 | Label(status.folder, systemImage: "folder").font(.caption) |
| 53 | .foregroundStyle(palette.muted).lineLimit(2).textSelection(.enabled) |
| 54 | HStack { |
| 55 | Button("Choose folder…") { |
| 56 | run { |
| 57 | guard let folder = await chooseDirectory("Choose a daily backup folder", prompt: "Use this folder") else { return } |
| 58 | statusUpdate(try await model.service.call("native_daily_configure", ["folder": folder.path])) |
| 59 | } |
| 60 | } |
| 61 | if !status.usesDefaultFolder { |
| 62 | Button("Use default folder") { configure(["folder": NSNull()]) } |
| 63 | } |
| 64 | Button("Show backups") { |
| 65 | let url = URL(fileURLWithPath: status.folder, isDirectory: true) |
| 66 | if !NSWorkspace.shared.open(url) { error = "The backup folder is unavailable." } |
| 67 | } |
| 68 | .disabled(!FileManager.default.fileExists(atPath: status.folder)) |
| 69 | Spacer() |
| 70 | Button("Back up now") { |
| 71 | run { statusUpdate(try await model.service.call("native_daily_run")) } |
| 72 | } |
| 73 | } |
| 74 | if let stamp = status.lastBackupAt { |
| 75 | let iso = ISO8601DateFormatter() |
| 76 | let date = iso.date(from: stamp) ?? ISO8601DateFormatter.backupDate(stamp) |
| 77 | Text("Last saved: \(date?.formatted(date: .abbreviated, time: .shortened) ?? stamp)") |
| 78 | .font(.caption).foregroundStyle(palette.muted) |
| 79 | } |
| 80 | if !status.lastError.isEmpty { Text(status.lastError).font(.caption).foregroundStyle(palette.danger) } |
| 81 | } else { |
| 82 | ProgressView().controlSize(.small) |
| 83 | } |
| 84 | if let error { Text(error).font(.caption).foregroundStyle(palette.danger) } |
| 85 | } |
| 86 | .padding(13) |
| 87 | .surface() |
| 88 | .disabled(busy) |
| 89 | .task(id: model.backupSignal) { await reload() } |
| 90 | .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in |
| 91 | if !model.isolatedProfile { background = BackupAgent.service.status } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private func reload() async { |
| 96 | do { |
| 97 | statusUpdate(try await model.service.call("native_daily_status")) |
| 98 | if !model.isolatedProfile { background = BackupAgent.service.status } |
| 99 | } catch { self.error = error.localizedDescription } |
| 100 | } |
| 101 | |
| 102 | private func statusUpdate(_ status: DailyBackupStatus) { self.status = status } |
| 103 | |
| 104 | private func configure(_ fields: [String: Any]) { |
| 105 | run { |
| 106 | statusUpdate(try await model.service.call("native_daily_configure", fields)) |
| 107 | if fields["enabled"] as? Bool == false, |
| 108 | background == .enabled || background == .requiresApproval { |
| 109 | try await BackupAgent.service.unregister() |
| 110 | background = BackupAgent.service.status |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private func configureBackground(_ enable: Bool) { |
| 116 | run { |
| 117 | defer { background = BackupAgent.service.status } |
| 118 | if enable { try BackupAgent.service.register() } |
| 119 | else { try await BackupAgent.service.unregister() } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private func run(_ operation: @escaping @MainActor () async throws -> Void) { |
| 124 | busy = true |
| 125 | error = nil |
| 126 | Task { |
| 127 | defer { busy = false } |
| 128 | do { try await operation() } |
| 129 | catch let failure { error = failure.localizedDescription } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private extension ISO8601DateFormatter { |
| 135 | static func backupDate(_ stamp: String) -> Date? { |
| 136 | let iso = ISO8601DateFormatter() |
| 137 | iso.formatOptions = [.withInternetDateTime, .withFractionalSeconds] |
| 138 | return iso.date(from: stamp) |
| 139 | } |
| 140 | } |