PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / CloudSyncView.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | struct CloudSyncView: View { |
| 4 | @ObservedObject private var cloud = CloudSyncService.shared |
| 5 | var prepare: () -> Void = {} |
| 6 | var openProject: ((Data) -> Void)? = nil |
| 7 | @State private var confirmEnable = false |
| 8 | @State private var removing: CloudSyncChange? |
| 9 | @State private var message: String? |
| 10 | var body: some View { |
| 11 | VStack(alignment: .leading, spacing: 12) { |
| 12 | Toggle("Sync with iCloud", isOn: Binding(get: { cloud.enabled }, set: { value in |
| 13 | if value { confirmEnable = true } else { cloud.setEnabled(false) } |
| 14 | })).accessibilityIdentifier("iCloudSyncToggle") |
| 15 | Text("Keep visualizer favorites, song favorites, saved looks and saved projects available on your Mac, iPhone, iPad and Apple Vision Pro using the same Apple Account.") |
| 16 | .font(.callout).foregroundStyle(.secondary) |
| 17 | Text("Saved projects include their soundtracks and use your iCloud storage. Each device shows the projects and visualizers it can open. Personal Radio albums, playback and volume stay on each device.") |
| 18 | .font(.caption).foregroundStyle(.secondary) |
| 19 | HStack { |
| 20 | Button("Sync now", systemImage: "arrow.triangle.2.circlepath") { cloud.syncNow() } |
| 21 | .disabled(!cloud.enabled || cloud.busy).accessibilityIdentifier("iCloudSyncNow") |
| 22 | if cloud.busy { ProgressView().controlSize(.small) } |
| 23 | } |
| 24 | Text(message ?? cloud.status).font(.callout).textSelection(.enabled).accessibilityIdentifier("iCloudSyncStatus") |
| 25 | if let date = cloud.lastSync { Text("Last checked \(date.formatted(date: .abbreviated, time: .shortened))").font(.caption).foregroundStyle(.secondary) } |
| 26 | if cloud.enabled { |
| 27 | DisclosureGroup("iCloud projects (\(cloud.projects.count))") { |
| 28 | if cloud.projects.isEmpty { Text("Save a project to add it to iCloud. Projects download when you open them.").font(.caption).foregroundStyle(.secondary) } |
| 29 | ForEach(cloud.projects, id: \.event) { change in |
| 30 | if let info = try? JSONDecoder().decode(CloudProjectInfo.self, from: change.payload) { |
| 31 | HStack { |
| 32 | VStack(alignment: .leading) { |
| 33 | Text(info.title).lineLimit(2) |
| 34 | if change.key.hasPrefix("conflict-") { Text("Conflict copy — both edits were kept").font(.caption).foregroundStyle(.secondary) } |
| 35 | } |
| 36 | Spacer() |
| 37 | if let openProject { |
| 38 | Button("Open") { |
| 39 | prepare(); message = nil |
| 40 | Task { |
| 41 | do { openProject(try await cloud.openProject(change)) } |
| 42 | catch { message = String(localized: "Couldn't open the project yet. \(cloud.status)") } |
| 43 | } |
| 44 | }.disabled(cloud.busy) |
| 45 | } |
| 46 | Button { removing = change } label: { Image(systemName: "trash") } |
| 47 | .accessibilityLabel("Remove \(info.title) from iCloud") |
| 48 | }.padding(.vertical, 6) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | }.task { cloud.start() } |
| 54 | .alert("Enable iCloud sync?", isPresented: $confirmEnable) { |
| 55 | Button("Cancel", role: .cancel) {} |
| 56 | Button("Enable iCloud") { cloud.setEnabled(true) } |
| 57 | } message: { |
| 58 | Text("Merge this device’s favorites and saved looks with your Apple Account, and upload saved projects with their music. Changes sync to your other PulseCanvas devices. Turning sync off keeps local data and existing iCloud copies.") |
| 59 | } |
| 60 | .alert("Remove this project from iCloud?", isPresented: Binding(get: { removing != nil }, set: { if !$0 { removing = nil } })) { |
| 61 | Button("Cancel", role: .cancel) { removing = nil } |
| 62 | Button("Remove from iCloud", role: .destructive) { |
| 63 | if let removing { cloud.removeProject(removing) } |
| 64 | removing = nil |
| 65 | } |
| 66 | } message: { Text("This removes it from the synced project list on all devices. Existing local project and audio files are kept.") } |
| 67 | } |
| 68 | } |