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 UIKit
|
||||||
import Combine
|
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
|
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
|
// MARK: - Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -25,27 +18,11 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: UICo
|
|||||||
|
|
||||||
public var modelHandler: ModelHandlerType = ModelHandlerType()
|
public var modelHandler: ModelHandlerType = ModelHandlerType()
|
||||||
|
|
||||||
@Proxy(\.model.surface)
|
@Proxy(\.modelHandler.model.surface)
|
||||||
open var surface: Surface
|
open var surface: Surface
|
||||||
|
|
||||||
@Proxy(\.model.disabled)
|
@Proxy(\.modelHandler.model.disabled)
|
||||||
open var disabled: Bool {
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
@ -64,7 +41,6 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: UICo
|
|||||||
public override init(frame: CGRect) {
|
public override init(frame: CGRect) {
|
||||||
super.init(frame: frame)
|
super.init(frame: frame)
|
||||||
initialSetup()
|
initialSetup()
|
||||||
set(with: model)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public required init?(coder: NSCoder) {
|
public required init?(coder: NSCoder) {
|
||||||
@ -79,7 +55,6 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: UICo
|
|||||||
public func initialSetup() {
|
public func initialSetup() {
|
||||||
if !initialSetupPerformed {
|
if !initialSetupPerformed {
|
||||||
initialSetupPerformed = true
|
initialSetupPerformed = true
|
||||||
setupUpdateView()
|
|
||||||
setup()
|
setup()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,16 +69,8 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: UICo
|
|||||||
open func updateView(viewModel: ModelType) {
|
open func updateView(viewModel: ModelType) {
|
||||||
modelHandler.updateView(viewModel: viewModel)
|
modelHandler.updateView(viewModel: viewModel)
|
||||||
}
|
}
|
||||||
|
|
||||||
open func reset() {
|
|
||||||
backgroundColor = .clear
|
|
||||||
if let model = model as? Resettable {
|
|
||||||
model.reset()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public func set(with model: ModelType) {
|
public func set(with model: ModelType) {
|
||||||
self.model = model
|
|
||||||
modelHandler.set(with: model)
|
modelHandler.set(with: model)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,27 +7,20 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import UIKit
|
import UIKit
|
||||||
|
import Combine
|
||||||
|
|
||||||
public class RadioSwatchGroup: RadioSwatchGroupBase<DefaultRadioSwatchGroupModel, RadioSwatch> {
|
public class RadioSwatchGroup: RadioSwatchGroupBase<DefaultRadioSwatchGroupModel, RadioSwatch> {
|
||||||
|
|
||||||
public override func didSelect(selector: DefaultRadioSwatchModel) {
|
public override func didSelect(selector: RadioSwatch) {
|
||||||
//reset the old model
|
if let index = model.selectors.firstIndex(where: {$0.selected == true }),
|
||||||
//see if there is a selected one and then get the cached version
|
let cell = collectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? CollectionViewCell<RadioSwatch> {
|
||||||
if let selectedModel {
|
cell.modelHandler.toggle()
|
||||||
let oldSelectedModel = selectedModel.copyWith {
|
|
||||||
$0.selected = false
|
|
||||||
}
|
|
||||||
replace(viewModel: oldSelectedModel)
|
|
||||||
}
|
}
|
||||||
|
selector.toggle()
|
||||||
//set the new model
|
label.text = selector.model.text
|
||||||
let newSelectedModel = selector.copyWith {
|
DispatchQueue.main.asyncAfter(deadline: .now() + Constants.ModelStateDebounce) { [weak self] in
|
||||||
$0.selected = true
|
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 collectionViewHeight: NSLayoutConstraint?
|
||||||
private var collectionViewWidth: NSLayoutConstraint?
|
private var collectionViewWidth: NSLayoutConstraint?
|
||||||
|
|
||||||
private lazy var collectionView: UICollectionView = {
|
fileprivate lazy var collectionView: UICollectionView = {
|
||||||
let layout = UICollectionViewFlowLayout().with {
|
let layout = UICollectionViewFlowLayout().with {
|
||||||
$0.minimumLineSpacing = lineSpacing
|
$0.minimumLineSpacing = lineSpacing
|
||||||
$0.minimumInteritemSpacing = itemSpacing
|
$0.minimumInteritemSpacing = itemSpacing
|
||||||
@ -186,12 +179,7 @@ public class RadioSwatchGroupBase<GroupModelType: RadioSwatchGroupModel, ModelHa
|
|||||||
|
|
||||||
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||||
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return }
|
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return }
|
||||||
didSelect(selector: cell.model)
|
didSelect(selector: cell.modelHandler)
|
||||||
}
|
|
||||||
|
|
||||||
open func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
|
|
||||||
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return }
|
|
||||||
cell.isSelected = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -205,15 +193,36 @@ public class RadioSwatchGroupBase<GroupModelType: RadioSwatchGroupModel, ModelHa
|
|||||||
return model.selectors.count
|
return model.selectors.count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cellsubs: [Int: AnyCancellable] = [:]
|
||||||
|
|
||||||
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
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]
|
let model = model.selectors[indexPath.row]
|
||||||
cell?.modelHandler.isUserInteractionEnabled = false
|
cell.modelHandler.isUserInteractionEnabled = false
|
||||||
cell?.set(with: model)
|
|
||||||
return cell ?? UICollectionViewCell()
|
//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")
|
fatalError("Must override didSelect")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user