From 0ba4fcbbd259e4c41db389d648c97d174c2e2883 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Mon, 12 Sep 2022 16:11:59 -0500 Subject: [PATCH] updated cell Signed-off-by: Matt Bruce --- VDS/Classes/CollectionViewCell.swift | 41 ++---------- .../RadioSwatch/RadioSwatchGroup.swift | 65 +++++++++++-------- 2 files changed, 41 insertions(+), 65 deletions(-) diff --git a/VDS/Classes/CollectionViewCell.swift b/VDS/Classes/CollectionViewCell.swift index 7a3a0968..b3e5a13c 100644 --- a/VDS/Classes/CollectionViewCell.swift +++ b/VDS/Classes/CollectionViewCell.swift @@ -9,15 +9,8 @@ import Foundation import UIKit import Combine -open class CollectionViewCell: UICollectionViewCell, ModelHandlerable, ViewProtocol, Resettable { +open class CollectionViewCell: UICollectionViewCell, ViewProtocol { public typealias ModelType = ModelHandlerType.ModelType - //-------------------------------------------------- - // MARK: - Combine Properties - //-------------------------------------------------- - @Published public var model: ModelType = ModelType() - public var modelPublisher: Published.Publisher { $model } - public var subscribers = Set() - //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- @@ -25,27 +18,11 @@ open class CollectionViewCell: UICo public var modelHandler: ModelHandlerType = ModelHandlerType() - @Proxy(\.model.surface) + @Proxy(\.modelHandler.model.surface) open var surface: Surface - @Proxy(\.model.disabled) - open var disabled: Bool { - didSet { - self.isEnabled = !disabled - } - } - - open var isEnabled: Bool { - get { !model.disabled } - set { - //create local vars for clear coding - let disabled = !newValue - if model.disabled != disabled { - model.disabled = disabled - } - isUserInteractionEnabled = isEnabled - } - } + @Proxy(\.modelHandler.model.disabled) + open var disabled: Bool //-------------------------------------------------- // MARK: - Initializers @@ -64,7 +41,6 @@ open class CollectionViewCell: UICo public override init(frame: CGRect) { super.init(frame: frame) initialSetup() - set(with: model) } public required init?(coder: NSCoder) { @@ -79,7 +55,6 @@ open class CollectionViewCell: UICo public func initialSetup() { if !initialSetupPerformed { initialSetupPerformed = true - setupUpdateView() setup() } } @@ -94,16 +69,8 @@ open class CollectionViewCell: UICo open func updateView(viewModel: ModelType) { modelHandler.updateView(viewModel: viewModel) } - - open func reset() { - backgroundColor = .clear - if let model = model as? Resettable { - model.reset() - } - } public func set(with model: ModelType) { - self.model = model modelHandler.set(with: model) } diff --git a/VDS/Components/RadioSwatch/RadioSwatchGroup.swift b/VDS/Components/RadioSwatch/RadioSwatchGroup.swift index a6e6c2fb..add18aac 100644 --- a/VDS/Components/RadioSwatch/RadioSwatchGroup.swift +++ b/VDS/Components/RadioSwatch/RadioSwatchGroup.swift @@ -7,27 +7,20 @@ import Foundation import UIKit +import Combine public class RadioSwatchGroup: RadioSwatchGroupBase { - public override func didSelect(selector: DefaultRadioSwatchModel) { - //reset the old model - //see if there is a selected one and then get the cached version - if let selectedModel { - let oldSelectedModel = selectedModel.copyWith { - $0.selected = false - } - replace(viewModel: oldSelectedModel) + public override func didSelect(selector: RadioSwatch) { + if let index = model.selectors.firstIndex(where: {$0.selected == true }), + let cell = collectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? CollectionViewCell { + cell.modelHandler.toggle() } - - //set the new model - let newSelectedModel = selector.copyWith { - $0.selected = true + selector.toggle() + label.text = selector.model.text + DispatchQueue.main.asyncAfter(deadline: .now() + Constants.ModelStateDebounce) { [weak self] in + self?.sendActions(for: .valueChanged) } - - label.text = newSelectedModel.text - replace(viewModel: newSelectedModel) - sendActions(for: .valueChanged) } } @@ -55,7 +48,7 @@ public class RadioSwatchGroupBase else { return } - didSelect(selector: cell.model) - } - - open func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { - guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell else { return } - cell.isSelected = false + didSelect(selector: cell.modelHandler) } //-------------------------------------------------- @@ -205,15 +193,36 @@ public class RadioSwatchGroupBase UICollectionViewCell { - let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell + guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell else { return UICollectionViewCell() } + let model = model.selectors[indexPath.row] - cell?.modelHandler.isUserInteractionEnabled = false - cell?.set(with: model) - return cell ?? UICollectionViewCell() + cell.modelHandler.isUserInteractionEnabled = false + + //cancel if sub exists + if let sub = cellsubs[indexPath.row] { + sub.cancel() + cellsubs[indexPath.row] = nil + } + + let sub = cell.modelHandler + .handlerPublisher() + .sink { [weak self] changed in + if cell.modelHandler.shouldUpdateView(viewModel: model) { + print("Model Change: \(changed)") + self?.replace(viewModel: changed) + } + } + cellsubs[indexPath.row] = sub + + cell.set(with: model) + return cell + } - open func didSelect(selector: ModelHandlerType.ModelType) { + open func didSelect(selector: ModelHandlerType) { fatalError("Must override didSelect") } }