diff --git a/MVMCoreUI/Atomic/Molecules/Items/CarouselItem.swift b/MVMCoreUI/Atomic/Molecules/Items/CarouselItem.swift index 235e1f58..a37fe016 100644 --- a/MVMCoreUI/Atomic/Molecules/Items/CarouselItem.swift +++ b/MVMCoreUI/Atomic/Molecules/Items/CarouselItem.swift @@ -8,17 +8,12 @@ import Foundation -@objc public protocol CarouselItemSelected: class { - func itemSelected(fieldValue: String?, index: Int) -} - open class CarouselItem: MoleculeCollectionViewCell, CarouselItemProtocol { open var allowsPeaking = false var peakingLeftArrow = UIImageView(image: MVMCoreUIUtility.imageNamed("peakingRightArrow")?.withRenderingMode(.alwaysTemplate)) var peakingRightArrow = UIImageView(image: MVMCoreUIUtility.imageNamed("peakingRightArrow")?.withRenderingMode(.alwaysTemplate)) var peakingCover = MVMCoreUICommonViewsUtility.commonView() - @objc public weak var carouselDelegate: CarouselItemSelected? open override func addMolecule(_ molecule: MoleculeViewProtocol) { super.addMolecule(molecule) diff --git a/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift b/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift index d13d8294..b9475938 100644 --- a/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift +++ b/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift @@ -386,7 +386,6 @@ extension Carousel: UICollectionViewDataSource { protocolCell.set(with: moleculeInfo.molecule, delegateObject, nil) } (cell as? MVMCoreViewProtocol)?.updateView(size ?? collectionView.bounds.width) - (cell as? CarouselItem)?.carouselDelegate = self setAccessiblity(cell, index: indexPath.row) return cell } @@ -394,7 +393,15 @@ extension Carousel: UICollectionViewDataSource { extension Carousel: UICollectionViewDelegate { open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - (collectionView.cellForItem(at: indexPath) as? CollectionTemplateItemProtocol)?.didSelectCell(at: indexPath, delegateObject: delegateObject, additionalData: nil) + guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionTemplateItemProtocol else { + return + } + 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 + } + carouselModel.selectedIndex = selectedCarouselItem.fieldValue ?? String(indexPath.row) } } @@ -626,13 +633,3 @@ class CarouselAccessibilityElement: UIAccessibilityElement { return false } } - -extension Carousel: CarouselItemSelected { - public func itemSelected(fieldValue: String?, index: Int) { - if fieldValue != nil { - (model as? CarouselModel)?.selectedIndex = fieldValue - } else { - (model as? CarouselModel)?.selectedIndex = String(index) - } - } -} diff --git a/MVMCoreUI/Atomic/Templates/CollectionTemplateItemProtocol.swift b/MVMCoreUI/Atomic/Templates/CollectionTemplateItemProtocol.swift index 55db2c6d..98c9754e 100644 --- a/MVMCoreUI/Atomic/Templates/CollectionTemplateItemProtocol.swift +++ b/MVMCoreUI/Atomic/Templates/CollectionTemplateItemProtocol.swift @@ -19,6 +19,10 @@ public protocol CollectionTemplateItemProtocol: UICollectionViewCell { /// Called when the cell will display. func willDisplay() + + /// Handle the selection of cell + func shouldSelect(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Bool + } // Default implementation does nothing @@ -26,4 +30,9 @@ extension CollectionTemplateItemProtocol { public func didSelectCell(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {} public func willDisplay() {} + + 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 4ac48dfc..aff6dffb 100644 --- a/MVMCoreUI/BaseClasses/CollectionViewCell.swift +++ b/MVMCoreUI/BaseClasses/CollectionViewCell.swift @@ -132,4 +132,9 @@ 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 + } }