field_ios

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

TeraAddPresentation.swift (1471B)


      1 import Foundation
      2 
      3 enum TeraAddPresentation {
      4   static func sorted(_ drafts: [TeraDraftStatus]) -> [TeraDraftStatus] {
      5     drafts.sorted {
      6       if $0.updatedAtUnixMilliseconds == $1.updatedAtUnixMilliseconds {
      7         return $0.id < $1.id
      8       }
      9       return $0.updatedAtUnixMilliseconds > $1.updatedAtUnixMilliseconds
     10     }
     11   }
     12 
     13   static func newForm(
     14     type: TeraAddCommandType,
     15     identifier: @Sendable () -> String,
     16     clock: TeraClock,
     17     timeZone: TimeZone = .current
     18   ) -> TeraAddForm {
     19     var form = TeraAddForm.empty(type)
     20     if type == .createEvent || type == .createFoodAvailability {
     21       let value = identifier()
     22       if isValidIdentifier(value) {
     23         form.identifier = value
     24       }
     25     }
     26     if type == .createEvent {
     27       guard let now = try? clock.unixSeconds() else {
     28         return form
     29       }
     30       TeraCalendarEditing.initialize(&form, now: now, timeZone: timeZone)
     31     }
     32     return form
     33   }
     34 
     35   static func isValidIdentifier(_ value: String) -> Bool {
     36     value.utf8.count == 32
     37       && value.utf8.allSatisfy { byte in
     38         (byte >= 0x30 && byte <= 0x39) || (byte >= 0x61 && byte <= 0x66)
     39       }
     40   }
     41 
     42   static func message(for error: Error) -> String {
     43     TeraUserMessages.text(for: error, fallback: .addOperationFailed)
     44   }
     45 
     46   static func failure(for error: Error) -> TeraRuntimeFailure? {
     47     if case let TeraRuntimeClientError.add(failure) = error {
     48       return failure
     49     }
     50     return error as? TeraRuntimeFailure
     51   }
     52 }