@@ -347,21 +347,33 @@ import MaterialComponents.MDCCard
347347 return form [ FormKey . id. key] as? Int == observationForm [ EventKey . formId. key] as? Int
348348 }
349349
350+ let fields : [ [ String : Any ] ] = eventForm ? [ FormKey . fields. key] as? [ [ String : Any ] ] ?? [ ] ;
351+
350352 var formPrimaryValue : String ? = nil ;
351353 var formSecondaryValue : String ? = nil ;
352- if let primaryField = eventForm ? [ FormKey . primaryFeedField. key] as! String ? {
353- if let obsfield = observationForm [ primaryField] as! String ? {
354- formPrimaryValue = obsfield;
354+ if let primaryFieldName = eventForm ? [ FormKey . primaryFeedField. key] as? String {
355+ if let primaryField = fields. first ( where: { field in
356+ return ( field [ FieldKey . name. key] as? String ) == primaryFieldName
357+ } ) {
358+ if let obsfield = observationForm [ primaryFieldName] {
359+ formPrimaryValue = Observation . fieldValueText ( obsfield, field: primaryField)
360+ }
355361 }
356362 }
357- if let secondaryField = eventForm ? [ FormKey . secondaryFeedField. key] as! String ? {
358- if let obsfield = observationForm [ secondaryField] as! String ? {
359- formSecondaryValue = obsfield;
363+
364+ if let secondaryFieldName = eventForm ? [ FormKey . secondaryFeedField. key] as? String {
365+ if let secondaryField = fields. first ( where: { field in
366+ return ( field [ FieldKey . name. key] as? String ) == secondaryFieldName
367+ } ) {
368+ if let obsfield = observationForm [ secondaryFieldName] {
369+ formSecondaryValue = Observation . fieldValueText ( obsfield, field: secondaryField)
370+ }
360371 }
361372 }
373+
362374 let formView = ObservationFormView ( observation: self . observation!, form: observationForm, eventForm: eventForm, formIndex: index, viewController: self , observationFormListener: self , delegate: delegate, attachmentSelectionDelegate: self ) ;
363- if let safeScheme = scheme {
364- formView. applyTheme ( withScheme: safeScheme ) ;
375+ if let scheme = scheme {
376+ formView. applyTheme ( withScheme: scheme ) ;
365377 }
366378 let formSpacerView = UIView ( forAutoLayout: ( ) ) ;
367379 formSpacerView. addSubview ( formView) ;
@@ -387,8 +399,8 @@ import MaterialComponents.MDCCard
387399 button. applyTextTheme ( withScheme: globalErrorContainerScheme ( ) )
388400
389401 var tintColor : UIColor ? = nil ;
390- if let safeColor = eventForm ? [ FormKey . color. key] as? String {
391- tintColor = UIColor ( hex: safeColor ) ;
402+ if let color = eventForm ? [ FormKey . color. key] as? String {
403+ tintColor = UIColor ( hex: color ) ;
392404 } else {
393405 tintColor = scheme? . colorScheme. primaryColor
394406 }
@@ -405,8 +417,8 @@ import MaterialComponents.MDCCard
405417 // or reorder remove the form so the user is not distracted with a refresh
406418 observation? . addForm ( toBeDeleted: sender. tag) ;
407419 cards [ sender. tag] . isHidden = true ;
408- if let safeObservation = self . observation {
409- self . commonFieldView? . setObservation ( observation: safeObservation ) ;
420+ if let observation = self . observation {
421+ self . commonFieldView? . setObservation ( observation: observation ) ;
410422 }
411423
412424 setupFormDependentButtons ( ) ;
@@ -418,8 +430,8 @@ import MaterialComponents.MDCCard
418430 self . cards [ sender. tag] . isHidden = false ;
419431 self . observation? . removeForm ( toBeDeleted: sender. tag) ;
420432 self . setupFormDependentButtons ( ) ;
421- if let safeObservation = self . observation {
422- self . commonFieldView? . setObservation ( observation: safeObservation ) ;
433+ if let observation = self . observation {
434+ self . commonFieldView? . setObservation ( observation: observation ) ;
423435 }
424436 }
425437 messageAction. handler = actionHandler;
@@ -431,19 +443,32 @@ import MaterialComponents.MDCCard
431443 let eventForm : [ String : Any ] ? = self . eventForms. first { ( eventForm) -> Bool in
432444 return eventForm [ FormKey . id. key] as? Int == form [ EventKey . formId. key] as? Int
433445 }
446+
447+ let fields : [ [ String : Any ] ] = eventForm ? [ FormKey . fields. key] as? [ [ String : Any ] ] ?? [ ] ;
448+
434449 var formPrimaryValue : String ? = nil ;
435450 var formSecondaryValue : String ? = nil ;
436- if let primaryField = eventForm ? [ FormKey . primaryField. key] as! String ? {
437- if let obsfield = form [ primaryField] as? String {
438- formPrimaryValue = obsfield;
451+
452+ if let primaryFieldName = eventForm ? [ FormKey . primaryFeedField. key] as? String {
453+ if let primaryField = fields. first ( where: { field in
454+ return ( field [ FieldKey . name. key] as? String ) == primaryFieldName
455+ } ) {
456+ if let obsfield = form [ primaryFieldName] {
457+ formPrimaryValue = Observation . fieldValueText ( obsfield, field: primaryField)
458+ }
439459 }
440460 }
441- if let secondaryField = eventForm ? [ FormKey . secondaryField. key] as! String ? {
442- // TODO: handle non strings
443- if let obsfield = form [ secondaryField] as! String ? {
444- formSecondaryValue = obsfield;
461+
462+ if let secondaryFieldName = eventForm ? [ FormKey . secondaryFeedField. key] as? String {
463+ if let secondaryField = fields. first ( where: { field in
464+ return ( field [ FieldKey . name. key] as? String ) == secondaryFieldName
465+ } ) {
466+ if let obsfield = form [ secondaryFieldName] {
467+ formSecondaryValue = Observation . fieldValueText ( obsfield, field: secondaryField)
468+ }
445469 }
446470 }
471+
447472 cards [ index] . header = formPrimaryValue;
448473 cards [ index] . subheader = formSecondaryValue;
449474 }
@@ -452,10 +477,10 @@ import MaterialComponents.MDCCard
452477 // allow MDCButton ink ripple
453478 DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) { [ self ] in
454479 removeDeletedForms ( ) ;
455- guard let safeObservation = self . observation else {
480+ guard let observation = self . observation else {
456481 return
457482 }
458- self . delegate? . reorderForms ( observation: safeObservation ) ;
483+ self . delegate? . reorderForms ( observation: observation ) ;
459484 }
460485 }
461486
@@ -480,16 +505,16 @@ import MaterialComponents.MDCCard
480505
481506 @objc func saveObservation( sender: UIBarButtonItem ) {
482507 removeDeletedForms ( ) ;
483- guard let safeObservation = self . observation else { return }
508+ guard let observation = self . observation else { return }
484509 if ( checkObservationValidity ( ) ) {
485- self . delegate? . saveObservation ( observation: safeObservation ) ;
510+ self . delegate? . saveObservation ( observation: observation ) ;
486511 }
487512 }
488513
489514 func checkObservationValidity( ) -> Bool {
490515 var valid : Bool = false ;
491- if let safeCommonFieldView = commonFieldView {
492- valid = safeCommonFieldView . checkValidity ( enforceRequired: true )
516+ if let commonFieldView = commonFieldView {
517+ valid = commonFieldView . checkValidity ( enforceRequired: true )
493518 } else {
494519 valid = true ;
495520 }
@@ -543,14 +568,15 @@ import MaterialComponents.MDCCard
543568
544569 // check each form for min max
545570 var formIdCount : [ Int : Int ] = [ : ] ;
546- if let safeObservation = self . observation, let safeProperties = safeObservation. properties {
547- if ( safeProperties. keys. contains ( ObservationKey . forms. key) ) {
548- let observationForms : [ [ String : Any ] ] = safeProperties [ ObservationKey . forms. key] as! [ [ String : Any ] ] ;
549- let formsToBeDeleted = observation? . getFormsToBeDeleted ( ) ?? IndexSet ( ) ;
550- for (index, form) in observationForms. enumerated ( ) {
551- if ( !formsToBeDeleted. contains ( index) ) {
552- let formId = form [ EventKey . formId. key] as! Int ;
553- formIdCount [ formId] = ( formIdCount [ formId] ?? 0 ) + 1 ;
571+ if let observation = self . observation, let properties = observation. properties {
572+ if ( properties. keys. contains ( ObservationKey . forms. key) ) {
573+ if let observationForms: [ [ String : Any ] ] = properties [ ObservationKey . forms. key] as? [ [ String : Any ] ] {
574+ let formsToBeDeleted = observation. getFormsToBeDeleted ( ) ;
575+ for (index, form) in observationForms. enumerated ( ) {
576+ if ( !formsToBeDeleted. contains ( index) ) {
577+ let formId = form [ EventKey . formId. key] as! Int ;
578+ formIdCount [ formId] = ( formIdCount [ formId] ?? 0 ) + 1 ;
579+ }
554580 }
555581 }
556582 }
@@ -638,8 +664,8 @@ import MaterialComponents.MDCCard
638664 self . observation? . properties = observationProperties;
639665 let previousStackViewHeight = stackView. bounds. size. height;
640666 let card : ExpandableCard = addObservationFormView ( observationForm: newForm, index: observationForms. count - 1 ) ;
641- if let safeScheme = scheme {
642- card. applyTheme ( withScheme: safeScheme ) ;
667+ if let scheme = scheme {
668+ card. applyTheme ( withScheme: scheme ) ;
643669 }
644670 card. expanded = true ;
645671 // scroll the view down to the form they just added but not quite all the way down because then it looks like you
@@ -680,8 +706,8 @@ extension ObservationEditCardCollectionViewController: ObservationCommonProperti
680706 observationProperties [ ObservationKey . provider. key] = provider;
681707 observation? . properties = observationProperties;
682708 observation? . setGeometry ( geometry) ;
683- if let safeObservation = self . observation {
684- commonFieldView? . setObservation ( observation: safeObservation ) ;
709+ if let observation = self . observation {
710+ commonFieldView? . setObservation ( observation: observation ) ;
685711 }
686712 }
687713}
@@ -723,20 +749,29 @@ extension ObservationEditCardCollectionViewController: AttachmentSelectionDelega
723749 if ( attachment. url == nil ) {
724750 return ;
725751 }
726- attachmentViewCoordinator = AttachmentViewCoordinator ( rootViewController: self . navigationController!, attachment: attachment, delegate: self , scheme: scheme) ;
752+ guard let nav = self . navigationController else {
753+ return ;
754+ }
755+ attachmentViewCoordinator = AttachmentViewCoordinator ( rootViewController: nav, attachment: attachment, delegate: self , scheme: scheme) ;
727756 attachmentViewCoordinator? . start ( ) ;
728757 }
729758
730759 func selectedUnsentAttachment( _ unsentAttachment: [ AnyHashable : Any ] ! ) {
731- attachmentViewCoordinator = AttachmentViewCoordinator ( rootViewController: self . navigationController!, url: URL ( fileURLWithPath: unsentAttachment [ " localPath " ] as! String ) , contentType: ( unsentAttachment [ " contentType " ] as! String ) , delegate: self , scheme: scheme) ;
760+ guard let nav = self . navigationController else {
761+ return ;
762+ }
763+ attachmentViewCoordinator = AttachmentViewCoordinator ( rootViewController: nav, url: URL ( fileURLWithPath: unsentAttachment [ " localPath " ] as! String ) , contentType: ( unsentAttachment [ " contentType " ] as! String ) , delegate: self , scheme: scheme) ;
732764 attachmentViewCoordinator? . start ( ) ;
733765 }
734766
735767 func selectedNotCachedAttachment( _ attachment: Attachment ! , completionHandler handler: ( ( Bool ) -> Void ) ! ) {
736768 if ( attachment. url == nil ) {
737769 return ;
738770 }
739- attachmentViewCoordinator = AttachmentViewCoordinator ( rootViewController: self . navigationController!, attachment: attachment, delegate: self , scheme: scheme) ;
771+ guard let nav = self . navigationController else {
772+ return ;
773+ }
774+ attachmentViewCoordinator = AttachmentViewCoordinator ( rootViewController: nav, attachment: attachment, delegate: self , scheme: scheme) ;
740775 attachmentViewCoordinator? . start ( ) ;
741776 }
742777}
0 commit comments