Sources/OrgstarMobile/VersionScreens.swift

design-themes-widgets
orgstar/Sources/OrgstarMobile/VersionScreens.swift history · blame · raw

152 lines · 6249 bytes

  1#if os(iOS)
  2import OrgApp
  3import OrgDocument
  4import SwiftUI
  5
  6/// The buffer and the file on disk changed differently: what differs, and how to settle it.
  7struct ConflictSheet: View {
  8    let session: DocumentSession
  9    @Environment(\.dismiss) private var dismiss
 10
 11    var body: some View {
 12        NavigationStack {
 13            VStack(alignment: .leading, spacing: 10) {
 14                Text((session.conflict ?? "") + " Lines marked - are on disk, + in your version. The version you don't keep is saved in the recovery folder.")
 15                    .font(.callout)
 16                    .foregroundStyle(.secondary)
 17                    .fixedSize(horizontal: false, vertical: true)
 18                if let versions = session.conflictVersions() {
 19                    DiffView(old: versions.disk, new: versions.mine)
 20                }
 21                Button("Keep Mine") { session.keepMine(); dismiss() }
 22                    .buttonStyle(.borderedProminent)
 23                    .frame(maxWidth: .infinity)
 24                HStack {
 25                    Button("Use Disk Version") { session.useDiskVersion(); dismiss() }
 26                    Spacer()
 27                    Button("Merge with Markers") { session.mergeConflictWithMarkers(); dismiss() }
 28                }
 29                .buttonStyle(.bordered)
 30            }
 31            .padding()
 32            .navigationTitle("Conflicting Changes")
 33            .navigationBarTitleDisplayMode(.inline)
 34            .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Later") { dismiss() } } }
 35        }
 36    }
 37}
 38
 39/// Syncthing's conflict copies of the open file: each against the file, and what to keep.
 40struct SyncConflictSheet: View {
 41    let session: DocumentSession
 42    let report: (String) -> Void
 43    @State private var copies: [URL] = []
 44    @State private var selected: URL?
 45    @Environment(\.dismiss) private var dismiss
 46
 47    var body: some View {
 48        NavigationStack {
 49            VStack(alignment: .leading, spacing: 10) {
 50                if copies.isEmpty {
 51                    Text("No conflict copies of this file.").foregroundStyle(.secondary)
 52                    Spacer()
 53                } else {
 54                    Picker("Copy", selection: $selected) {
 55                        ForEach(copies, id: \.self) { Text($0.lastPathComponent).tag(Optional($0)) }
 56                    }
 57                    Text("Lines marked - are in the file, + in the copy. The copy goes to the recovery folder when it's resolved.")
 58                        .font(.callout)
 59                        .foregroundStyle(.secondary)
 60                    if let selected, let copy = session.recoveryText(selected), let mine = session.document?.text {
 61                        DiffView(old: mine, new: copy)
 62                    }
 63                    if let selected {
 64                        HStack {
 65                            Button("Keep File") { resolve(selected, .keepFile) }
 66                            Spacer()
 67                            Button("Merge") { resolve(selected, .merge) }
 68                            Spacer()
 69                            Button("Use Copy") { resolve(selected, .useCopy) }
 70                        }
 71                        .buttonStyle(.bordered)
 72                    }
 73                }
 74            }
 75            .padding()
 76            .navigationTitle("Sync Conflict Copies")
 77            .navigationBarTitleDisplayMode(.inline)
 78            .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Close") { dismiss() } } }
 79        }
 80        .onAppear(perform: reload)
 81    }
 82
 83    private func reload() {
 84        copies = session.syncConflictCopies()
 85        if selected.map({ !copies.contains($0) }) ?? true { selected = copies.first }
 86    }
 87
 88    private func resolve(_ copy: URL, _ resolution: DocumentSession.CopyResolution) {
 89        if let message = session.resolve(copy: copy, resolution) { report(message) }
 90        reload()
 91        if copies.isEmpty { dismiss() }
 92    }
 93}
 94
 95/// Versions of the open file kept in the recovery folder, newest first; one against the buffer.
 96struct RecoverySheet: View {
 97    let session: DocumentSession
 98    let report: (String) -> Void
 99    @State private var versions: [(url: URL, date: Date, label: String)] = []
100    @Environment(\.dismiss) private var dismiss
101
102    var body: some View {
103        NavigationStack {
104            List(versions, id: \.url) { version in
105                NavigationLink {
106                    detail(version.url)
107                } label: {
108                    VStack(alignment: .leading) {
109                        Text(version.date.formatted(date: .abbreviated, time: .standard))
110                        Text(Self.describe(version.label)).font(.caption).foregroundStyle(.secondary)
111                    }
112                }
113            }
114            .overlay {
115                if versions.isEmpty { ContentUnavailableView("No Kept Versions", systemImage: "clock.arrow.circlepath") }
116            }
117            .navigationTitle("Recovered Versions")
118            .navigationBarTitleDisplayMode(.inline)
119            .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Close") { dismiss() } } }
120        }
121        .onAppear { versions = session.recoveryVersions() }
122    }
123
124    private func detail(_ version: URL) -> some View {
125        VStack(alignment: .leading, spacing: 10) {
126            Text("Lines marked - are in the buffer, + in the kept version.").font(.callout).foregroundStyle(.secondary)
127            if let kept = session.recoveryText(version), let mine = session.document?.text {
128                DiffView(old: mine, new: kept)
129            }
130        }
131        .padding()
132        .toolbar {
133            ToolbarItem(placement: .confirmationAction) {
134                Button("Restore") {
135                    report(session.restore(version) ?? "Restored; undo brings back what was there.")
136                    dismiss()
137                }
138            }
139        }
140    }
141
142    static func describe(_ label: String) -> String {
143        switch label {
144        case "local": "Your version, replaced"
145        case "external": "Disk version, replaced"
146        case "sync-conflict": "Sync conflict copy"
147        case "snapshot": "Snapshot, before a save"
148        default: label
149        }
150    }
151}
152#endif