From 68274505fbdce0ab49c91c055823179f9ff02b1b Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Fri, 18 Sep 2020 13:08:08 -0400 Subject: [PATCH] some checks for analytics change --- .../TextFields/ItemDropdownEntryField.swift | 12 +++++++++--- .../TextFields/ItemDropdownEntryFieldModel.swift | 16 +++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/ItemDropdownEntryField.swift b/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/ItemDropdownEntryField.swift index ea82a922..6c2a0f4e 100644 --- a/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/ItemDropdownEntryField.swift +++ b/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/ItemDropdownEntryField.swift @@ -32,6 +32,8 @@ open class ItemDropdownEntryField: BaseDropdownEntryField { return model as? ItemDropdownEntryFieldModel } + private var systemWillSetItem = false + //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- @@ -109,8 +111,10 @@ open class ItemDropdownEntryField: BaseDropdownEntryField { pickerData = model.options setPickerDelegates(delegate: self) - if let pickerView = pickerView { - self.pickerView(pickerView, didSelectRow: model.selectedIndex, inComponent: 0) + if let pickerView = pickerView, let index = model.selectedIndex { + systemWillSetItem = true + self.pickerView(pickerView, didSelectRow: index, inComponent: 0) + systemWillSetItem = false } } } @@ -138,7 +142,9 @@ extension ItemDropdownEntryField: UIPickerViewDelegate, UIPickerViewDataSource { observeDropdownChange?(text ?? "", pickerData[row]) text = pickerData[row] itemDropdownEntryFieldModel?.selectedIndex = row - performDropdownAction() + if !systemWillSetItem { + performDropdownAction() + } } } diff --git a/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/ItemDropdownEntryFieldModel.swift b/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/ItemDropdownEntryFieldModel.swift index d89f5e16..0bc1ee0a 100644 --- a/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/ItemDropdownEntryFieldModel.swift +++ b/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/ItemDropdownEntryFieldModel.swift @@ -16,11 +16,14 @@ } public var options: [String] = [] - public var selectedIndex: Int = 0 + public var selectedIndex: Int? public override func formFieldValue() -> AnyHashable? { - guard !options.isEmpty else { return nil } - return options[selectedIndex] + guard !options.isEmpty, + let index = selectedIndex + else { return nil } + + return options[index] } //-------------------------------------------------- @@ -45,13 +48,16 @@ if let selectedIndex = try typeContainer.decodeIfPresent(Int.self, forKey: .selectedIndex) { self.selectedIndex = selectedIndex } - baseValue = options.indices.contains(selectedIndex) ? options[selectedIndex] : nil + + if let index = selectedIndex { + baseValue = options.indices.contains(index) ? options[index] : nil + } } public override func encode(to encoder: Encoder) throws { try super.encode(to: encoder) var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(options, forKey: .options) - try container.encode(options, forKey: .selectedIndex) + try container.encodeIfPresent(options, forKey: .selectedIndex) } }