field_ios

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

TeraAddAffordances.swift (1837B)


      1 import Foundation
      2 
      3 extension TeraAddStore {
      4   func observeChildAffordances() {
      5     submissions.changed = { [weak self] in self?.objectWillChange.send() }
      6     protection.changed = { [weak self] in self?.objectWillChange.send() }
      7   }
      8 
      9   var canCreateNewComposer: Bool {
     10     !isWorking && submissions.canReplaceEditing && !protection.isWorking && !protection.failed
     11   }
     12 
     13   func selectType(_ type: TeraAddCommandType) {
     14     guard canCreateNewComposer, isFormEditable, form.commandType != type else { return }
     15     newDraft(type: type)
     16   }
     17 
     18   var selectedSchema: TeraAddSchema? {
     19     schemas.first(where: { $0.commandType == form.commandType })
     20   }
     21 
     22   var isFormEditable: Bool {
     23     guard !revisionPreparation.isCaptured, activeDraft?.isRevision != true, activeDraft?.kind != .retraction else { return false }
     24     return activeDraft?.isEditable ?? true
     25   }
     26 
     27   var isProductReady: Bool {
     28     state == .ready && selectedSchema != nil
     29   }
     30 
     31   var canSave: Bool {
     32     isProductReady && (isFormEditable || revisionPreparation.isCaptured) && !isWorking
     33   }
     34 
     35   var canSubmit: Bool {
     36     if activeDraft == nil, submissions.usesCurrentAction {
     37       return isProductReady && !isWorking && submissions.canContinue
     38     }
     39     return isProductReady && !isWorking && !submissions.isWorking
     40       && activeDraft?.coordinateWritable != false
     41       && (activeDraft?.isRevision == true || activeDraft?.canAdvance == true
     42         || activeDraft?.canQueue == true
     43         || isFormEditable || revisionPreparation.isCaptured)
     44   }
     45 
     46   var acceptsMedia: Bool {
     47     mediaLimit > 0
     48   }
     49 
     50   var canAddMedia: Bool {
     51     isFormEditable && acceptsMedia && form.media.count < mediaLimit
     52   }
     53 
     54   var mediaLimit: Int {
     55     guard
     56       let maximum = selectedSchema?.fields
     57         .first(where: { $0.kind == .media })?.maxItems
     58     else { return 0 }
     59     return Int(maximum)
     60   }
     61 }