field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

TeraRevisionPreparation.swift (1541B)


      1 import Foundation
      2 
      3 /// Retain the action and exact editing snapshot before the first suspension.
      4 /// A failed/abandoned waiter retries this capture; only explicit New resets it.
      5 @MainActor
      6 final class TeraRevisionPreparation {
      7   private struct Capture {
      8     let id: String
      9     let target: TeraRevisionTarget
     10     let form: TeraAddForm
     11   }
     12 
     13   private let client: TeraRuntimeClient
     14   private let media: (any TeraAddMediaHandling)?
     15   private var capture: Capture?
     16 
     17   init(client: TeraRuntimeClient, media: (any TeraAddMediaHandling)?) {
     18     self.client = client
     19     self.media = media
     20   }
     21 
     22   var isCaptured: Bool {
     23     capture != nil
     24   }
     25 
     26   func reset() {
     27     capture = nil
     28   }
     29 
     30   func prepare(
     31     target: TeraRevisionTarget,
     32     form: TeraAddForm,
     33     identifier: () -> String,
     34     ensureCurrent: () throws -> Void
     35   ) async throws -> TeraRevisionStatus {
     36     if capture == nil {
     37       let id = identifier()
     38       guard TeraAddPresentation.isValidIdentifier(id) else { throw TeraComposerAcknowledgment.unconfirmed }
     39       capture = Capture(id: id, target: target, form: form)
     40     }
     41     guard let capture, capture.target == target else { throw TeraComposerAcknowledgment.unconfirmed }
     42     let opened = try await TeraOpenedMedia.open(capture.form.media, using: media)
     43     defer { opened.close() }
     44     try ensureCurrent()
     45     return try await client.saveRevisionIntent(requestID: capture.id, target: capture.target,
     46                                                replacement: TeraAddRuntimeInput(form: capture.form, media: opened.handles))
     47   }
     48 }