Changes for form validator to work
This commit is contained in:
parent
1c2cd9ff64
commit
8465208d0b
@ -23,6 +23,8 @@ import Foundation
|
||||
public var analyticsData: JSONValueDictionary?
|
||||
public var fieldValue: String?
|
||||
|
||||
public func formFieldValue() -> AnyHashable? { return fieldValue }
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Keys
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 }
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user