From 8465208d0b8f663d57490b0d5bf1f19353993d8e Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Mon, 1 Mar 2021 12:41:54 -0500 Subject: [PATCH] Changes for form validator to work --- .../Molecules/Items/CarouselItemModel.swift | 2 ++ .../Atomic/Organisms/Carousel/Carousel.swift | 28 ++++++++++++++----- .../Organisms/Carousel/CarouselModel.swift | 28 ++++++++++--------- .../CarouselItemModelProtocol.swift | 3 ++ .../CollectionTemplateItemProtocol.swift | 2 -- .../BaseClasses/CollectionViewCell.swift | 15 +++++----- 6 files changed, 48 insertions(+), 30 deletions(-) diff --git a/MVMCoreUI/Atomic/Molecules/Items/CarouselItemModel.swift b/MVMCoreUI/Atomic/Molecules/Items/CarouselItemModel.swift index 163909cb..19348070 100644 --- a/MVMCoreUI/Atomic/Molecules/Items/CarouselItemModel.swift +++ b/MVMCoreUI/Atomic/Molecules/Items/CarouselItemModel.swift @@ -23,6 +23,8 @@ import Foundation public var analyticsData: JSONValueDictionary? public var fieldValue: String? + public func formFieldValue() -> AnyHashable? { return fieldValue } + //-------------------------------------------------- // MARK: - Keys //-------------------------------------------------- diff --git a/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift b/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift index b9475938..a671726c 100644 --- a/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift +++ b/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift @@ -174,6 +174,10 @@ open class Carousel: View { pageIndex = carouselModel.index pagingView?.currentIndex = carouselModel.index collectionView.reloadData() + if let selectedIndex = carouselModel.selectedIndex { + let adjustedIndex = loop ? selectedIndex + 2 : selectedIndex + collectionView.selectItem(at: IndexPath(row: adjustedIndex, section: 0), animated: false, scrollPosition: []) + } } //-------------------------------------------------- @@ -392,16 +396,25 @@ extension Carousel: UICollectionViewDataSource { } extension Carousel: UICollectionViewDelegate { + public func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { + guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionTemplateItemProtocol else { return false } + return cell.shouldSelect(at: indexPath, delegateObject: delegateObject, additionalData: nil) + } + open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionTemplateItemProtocol else { - return + // Set the selection in the model + if let model = model as? CarouselModel { + // Adjust for looping + var adjustedIndex = loop ? indexPath.row - 2 : indexPath.row + if adjustedIndex < 0 { + adjustedIndex = adjustedIndex + numberOfPages + } + model.selectedIndex = adjustedIndex } - cell.didSelectCell(at: indexPath, delegateObject: delegateObject, additionalData: nil) - //Check for selectable carousel item - guard let carouselModel = model as? CarouselModel, let selectedCarouselItem = carouselModel.molecules[indexPath.row] as? CarouselItemModel, cell.shouldSelect(at: indexPath, delegateObject: delegateObject, additionalData: nil) else { - return + if let cell = collectionView.cellForItem(at: indexPath) as? CollectionTemplateItemProtocol { + cell.didSelectCell(at: indexPath, delegateObject: delegateObject, additionalData: nil) } - carouselModel.selectedIndex = selectedCarouselItem.fieldValue ?? String(indexPath.row) + _ = FormValidator.validate(delegate: delegateObject?.formHolderDelegate) } } @@ -423,6 +436,7 @@ extension Carousel: UIScrollViewDelegate { if !animated { scrollViewDidEndScrollingAnimation(collectionView) } + _ = FormValidator.validate(delegate: delegateObject?.formHolderDelegate) } /// Adjusts the current contentOffset if we are going onto buffer cells while looping to help with the endless scrolling appearance. diff --git a/MVMCoreUI/Atomic/Organisms/Carousel/CarouselModel.swift b/MVMCoreUI/Atomic/Organisms/Carousel/CarouselModel.swift index caa9f844..96e7e3f0 100644 --- a/MVMCoreUI/Atomic/Organisms/Carousel/CarouselModel.swift +++ b/MVMCoreUI/Atomic/Organisms/Carousel/CarouselModel.swift @@ -22,6 +22,7 @@ import UIKit public var backgroundColor: Color? public var molecules: [MoleculeModelProtocol & CarouselItemModelProtocol] public var index: Int = 0 + public var selectedIndex: Int? public var spacing: CGFloat? public var border: Bool? public var loop: Bool? @@ -34,7 +35,6 @@ import UIKit public var leftPadding: CGFloat? public var rightPadding: CGFloat? public var accessibilityText: String? - public var selectedIndex: String? public var baseValue: AnyHashable? public var fieldKey: String? public var groupName: String = FormValidator.defaultGroupName @@ -44,18 +44,12 @@ import UIKit } public func formFieldValue() -> AnyHashable? { - var carouselItemFieldValue: AnyHashable? - guard let visibleCarouselItem = molecules[index] as? CarouselItemModel else { - return nil + let indexForForm = selectedIndex ?? index + let item = molecules[indexForForm] + guard let value = item.formFieldValue() else { + return indexForForm } - if selectedIndex != nil { - //For selectableCarouselItem - carouselItemFieldValue = selectedIndex - } else { - //For carouselItem - carouselItemFieldValue = (visibleCarouselItem.fieldValue != nil) ? visibleCarouselItem.fieldValue : String(index) - } - return carouselItemFieldValue + return value } //-------------------------------------------------- @@ -67,6 +61,7 @@ import UIKit case backgroundColor case molecules case index + case selectedIndex case spacing case border case loop @@ -91,6 +86,7 @@ import UIKit let typeContainer = try decoder.container(keyedBy: CodingKeys.self) molecules = try typeContainer.decodeModels(codingKey: .molecules) index = try typeContainer.decodeIfPresent(Int.self, forKey: .index) ?? 0 + selectedIndex = try typeContainer.decodeIfPresent(Int.self, forKey: .selectedIndex) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) spacing = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .spacing) border = try typeContainer.decodeIfPresent(Bool.self, forKey: .border) @@ -112,7 +108,13 @@ import UIKit if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) { self.groupName = groupName } - baseValue = formFieldValue() + if let value = formFieldValue() { + baseValue = value + } else if let value = selectedIndex { + baseValue = value + } else { + baseValue = index + } } public func encode(to encoder: Encoder) throws { diff --git a/MVMCoreUI/Atomic/Protocols/ModelProtocols/CarouselItemModelProtocol.swift b/MVMCoreUI/Atomic/Protocols/ModelProtocols/CarouselItemModelProtocol.swift index c2ade02d..2ed2ca5a 100644 --- a/MVMCoreUI/Atomic/Protocols/ModelProtocols/CarouselItemModelProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/ModelProtocols/CarouselItemModelProtocol.swift @@ -11,11 +11,14 @@ import Foundation public protocol CarouselItemModelProtocol: ContainerModelProtocol { var analyticsData: JSONValueDictionary? { get set } + func formFieldValue() -> AnyHashable? } + public extension CarouselItemModelProtocol { var analyticsData: JSONValueDictionary? { get { return nil } set { analyticsData = newValue } } + func formFieldValue() -> AnyHashable? { return nil } } diff --git a/MVMCoreUI/Atomic/Templates/CollectionTemplateItemProtocol.swift b/MVMCoreUI/Atomic/Templates/CollectionTemplateItemProtocol.swift index 98c9754e..4e9ae26d 100644 --- a/MVMCoreUI/Atomic/Templates/CollectionTemplateItemProtocol.swift +++ b/MVMCoreUI/Atomic/Templates/CollectionTemplateItemProtocol.swift @@ -22,7 +22,6 @@ public protocol CollectionTemplateItemProtocol: UICollectionViewCell { /// Handle the selection of cell func shouldSelect(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Bool - } // Default implementation does nothing @@ -34,5 +33,4 @@ extension CollectionTemplateItemProtocol { public func shouldSelect(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Bool { return false } - } diff --git a/MVMCoreUI/BaseClasses/CollectionViewCell.swift b/MVMCoreUI/BaseClasses/CollectionViewCell.swift index aff6dffb..2b4d81d8 100644 --- a/MVMCoreUI/BaseClasses/CollectionViewCell.swift +++ b/MVMCoreUI/BaseClasses/CollectionViewCell.swift @@ -116,11 +116,15 @@ open class CollectionViewCell: UICollectionViewCell, MoleculeViewProtocol, MVMCo // MARK: - Override - open func didSelectCell(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) { - guard let action = model?.action else { return } - Button.performButtonAction(with: action, button: self, delegateObject: delegateObject, additionalData: additionalData) + open func shouldSelect(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Bool { + if let action = model?.action { + Button.performButtonAction(with: action, button: self, delegateObject: delegateObject, additionalData: additionalData) + } + return true } + open func didSelectCell(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {} + // Column logic, set width. override open func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { let autoLayoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes) @@ -132,9 +136,4 @@ open class CollectionViewCell: UICollectionViewCell, MoleculeViewProtocol, MVMCo autoLayoutAttributes.frame = newFrame return autoLayoutAttributes } - - // Set default to false - open func shouldSelect(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) -> Bool { - return false - } }