// RESTORE AN EARLIER DAY (RESTORE PHONE 2; assets/design/shared/RESTORE-2-CONTRACT.md section 8; the core's // crates/cotton-ffi/src/phone/restore.rs). The owner, on their own signed-in phone, puts the whole account back to the // end of a day the server kept. The phone only asks and watches: `restore.points` (the days, each with its confirm), // `restore.begin {id, anyway?}` (it begins, or first asks when older devices are signed in), `restore.progress` every // second while under way (one line and one fraction), `restore.cancel` while it still checks, and `restore.after` // (what the account shows after). Every word is the `restore` lines'. Where the server's restore is off the row is // not drawn. #if os(iOS) import CottonKit import SwiftUI @MainActor @Observable final class RestoreModel { private(set) var points: JSONValue? private(set) var progress: JSONValue? private(set) var after: JSONValue? private(set) var refused: String? private(set) var busy = false /// The day chosen, waiting on its confirm; then the older-devices question, when the core asks it. var confirming: JSONValue? var older: (id: String, confirm: JSONValue)? @ObservationIgnored let core: any CottonCalling @ObservationIgnored private var poll: Task? init(core: any CottonCalling) { self.core = core } var words: JSONValue? { points?["words"] } var available: Bool { points?["available"]?.yes == true } func read() async { points = try? await core.callJSON("restore.points") after = try? await core.callJSON("restore.after") // Under way (begun here or on another phone) or held: where it stands is shown, and stepped while under way. if let standing = points?["progress"], let stage = standing["stage"]?.string, stage != "none" { take(standing) } } func begin(_ id: String, anyway: Bool = false) async { guard !busy else { return } busy = true defer { busy = false } refused = nil var request: [String: JSONValue] = ["id": .string(id)] if anyway { request["anyway"] = .bool(true) } do { let answer = try await core.callJSON("restore.begin", JSONValue.object(request)) if case .object? = answer["confirm"] { older = (id, answer["confirm"]!) return } take(answer) } catch let error as CottonCallError { refused = error.message } catch { Log.write("restore: begin — \(error)") } } func cancel() async { do { take(try await core.callJSON("restore.cancel")) } catch let error as CottonCallError { refused = error.message } catch { Log.write("restore: cancel — \(error)") } } /// Where it stands; while under way the core is asked again each second (each ask steps it a little further). private func take(_ answer: JSONValue) { progress = answer poll?.cancel() poll = nil // Held by Cotton's operator: its line stands, and nothing is asked in a loop. guard answer["underWay"]?.yes == true else { Task { after = try? await core.callJSON("restore.after") } return } poll = Task { @MainActor [weak self] in try? await Task.sleep(for: .seconds(1)) guard let self, !Task.isCancelled else { return } do { self.take(try await self.core.callJSON("restore.progress")) } catch let error as CottonCallError { self.refused = error.message } catch { Log.write("restore: progress — \(error)") } } } func stop() { poll?.cancel() poll = nil } } /// The row at the foot of Sync, and after a restore the core's line about it; the page opens from the row. struct RestoreRow: View { let core: any CottonCalling let look: SettingsLook @State private var model: RestoreModel? @State private var open = false var body: some View { VStack(alignment: .leading, spacing: 6) { if let model { if let line = model.after?["line"]?.string { Text(verbatim: line).font(look.small).foregroundStyle(look.quiet).fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("restore-after") } if let kept = model.after?["keptLine"]?.string { Text(verbatim: kept).font(look.small).foregroundStyle(look.quiet).fixedSize(horizontal: false, vertical: true) } if model.available, let row = model.words?["row"]?.string { look.act(row, "restore-open", true) { open = true } } } } .padding(.top, 14) .task { let model = self.model ?? RestoreModel(core: core) self.model = model await model.read() } .sheet(isPresented: $open) { if let model { NavigationStack { RestorePage(model: model) .toolbar { ToolbarItem(placement: .cancellationAction) { Button { open = false } label: { Image(systemName: "xmark") }.accessibilityIdentifier("restore-close") } } } } } } } /// The days kept, newest first; a day's confirm before it begins; then where it stands, with Stop while it checks. struct RestorePage: View { let model: RestoreModel var body: some View { List { if let words = model.words { Section { Text(verbatim: words["intro"]?.string ?? "").foregroundStyle(.secondary) // A refusal is a plain line in the person's words, never a red banner. if let refused = model.refused { Text(verbatim: refused).accessibilityIdentifier("restore-refused") } } if let progress = model.progress { Section { Text(verbatim: progress["line"]?.string ?? "").accessibilityIdentifier("restore-line") if case .number(let fraction)? = progress["fraction"], progress["stage"]?.string != "none" { ProgressView(value: min(max(fraction, 0), 1)) } if progress["mayCancel"]?.yes == true, let stop = progress["stop"]?.string { Button(stop, role: .destructive) { Task { await model.cancel() } } .accessibilityIdentifier("restore-stop") } } } if model.progress?["underWay"]?.yes != true { Section { if let empty = model.points?["empty"]?.string { Text(verbatim: empty).foregroundStyle(.secondary) } ForEach(model.points?["days"]?.rows ?? [], id: \.self) { day in Button { model.confirming = day } label: { VStack(alignment: .leading, spacing: 2) { Text(verbatim: day["label"]?.string ?? "") Text(verbatim: words["choose"]?.string ?? "").font(.footnote).foregroundStyle(.tint) } } .disabled(model.busy) .accessibilityIdentifier("restore-day-\(day["id"]?.string ?? "")") } } } } else if let line = model.points?["line"]?.string { Text(verbatim: line).foregroundStyle(.secondary) } else { ProgressView().frame(maxWidth: .infinity) } } .navigationTitle(model.words?["heading"]?.string ?? "") .navigationBarTitleDisplayMode(.inline) .alert(model.confirming?["confirmHeading"]?.string ?? "", isPresented: Binding(get: { model.confirming != nil }, set: { if !$0 { model.confirming = nil } })) { if let day = model.confirming, let id = day["id"]?.string { Button(model.words?["confirmGo"]?.string ?? "", role: .destructive) { Task { await model.begin(id) } } Button(model.words?["confirmBack"]?.string ?? "", role: .cancel) {} } } message: { Text(verbatim: model.confirming?["confirmLine"]?.string ?? "") } .alert(model.older?.confirm["line"]?.string ?? "", isPresented: Binding(get: { model.older != nil }, set: { if !$0 { model.older = nil } })) { if let older = model.older { Button(older.confirm["go"]?.string ?? "", role: .destructive) { Task { await model.begin(older.id, anyway: true) } } Button(older.confirm["back"]?.string ?? "", role: .cancel) {} } } .onDisappear { model.stop() } .accessibilityIdentifier("restore-page") } } #endif