updated cell
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
421ea78991
commit
0ba4fcbbd2
@ -9,15 +9,8 @@ import Foundation
|
||||
import UIKit
|
||||
import Combine
|
||||
|
||||
open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: UICollectionViewCell, ModelHandlerable, ViewProtocol, Resettable {
|
||||
open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: UICollectionViewCell, ViewProtocol {
|
||||
public typealias ModelType = ModelHandlerType.ModelType
|
||||
//--------------------------------------------------
|
||||
// MARK: - Combine Properties
|
||||
//--------------------------------------------------
|
||||
@Published public var model: ModelType = ModelType()
|
||||
public var modelPublisher: Published<ModelType>.Publisher { $model }
|
||||
public var subscribers = Set<AnyCancellable>()
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Properties
|
||||
//--------------------------------------------------
|
||||
@ -25,27 +18,11 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: 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<ModelHandlerType: ModelHandlerable & UIView>: 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<ModelHandlerType: ModelHandlerable & UIView>: UICo
|
||||
public func initialSetup() {
|
||||
if !initialSetupPerformed {
|
||||
initialSetupPerformed = true
|
||||
setupUpdateView()
|
||||
setup()
|
||||
}
|
||||
}
|
||||
@ -94,16 +69,8 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: 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)
|
||||
}
|
||||
|
||||
|
||||
@ -7,27 +7,20 @@
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
import Combine
|
||||
|
||||
public class RadioSwatchGroup: RadioSwatchGroupBase<DefaultRadioSwatchGroupModel, RadioSwatch> {
|
||||
|
||||
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<RadioSwatch> {
|
||||
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<GroupModelType: RadioSwatchGroupModel, ModelHa
|
||||
private var collectionViewHeight: NSLayoutConstraint?
|
||||
private var collectionViewWidth: NSLayoutConstraint?
|
||||
|
||||
private lazy var collectionView: UICollectionView = {
|
||||
fileprivate lazy var collectionView: UICollectionView = {
|
||||
let layout = UICollectionViewFlowLayout().with {
|
||||
$0.minimumLineSpacing = lineSpacing
|
||||
$0.minimumInteritemSpacing = itemSpacing
|
||||
@ -186,12 +179,7 @@ public class RadioSwatchGroupBase<GroupModelType: RadioSwatchGroupModel, ModelHa
|
||||
|
||||
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return }
|
||||
didSelect(selector: cell.model)
|
||||
}
|
||||
|
||||
open func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
|
||||
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return }
|
||||
cell.isSelected = false
|
||||
didSelect(selector: cell.modelHandler)
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
@ -205,15 +193,36 @@ public class RadioSwatchGroupBase<GroupModelType: RadioSwatchGroupModel, ModelHa
|
||||
return model.selectors.count
|
||||
}
|
||||
|
||||
var cellsubs: [Int: AnyCancellable] = [:]
|
||||
|
||||
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
||||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell<ModelHandlerType>
|
||||
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell<ModelHandlerType> 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")
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user