kind of working swatch
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
cee429506d
commit
78d97fd936
@ -31,9 +31,6 @@ open class RadioSwatchBase<ModelType: RadioSwatchModel>: Control<ModelType>, Cha
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
public var onChange: Blocks.ActionBlock?
|
public var onChange: Blocks.ActionBlock?
|
||||||
|
|
||||||
@Proxy(\.model.id)
|
|
||||||
open var id: UUID
|
|
||||||
|
|
||||||
//can't bind to @Proxy
|
//can't bind to @Proxy
|
||||||
open override var isSelected: Bool {
|
open override var isSelected: Bool {
|
||||||
@ -160,13 +157,8 @@ open class RadioSwatchBase<ModelType: RadioSwatchModel>: Control<ModelType>, Cha
|
|||||||
/// Follow the SwiftUI View paradigm
|
/// Follow the SwiftUI View paradigm
|
||||||
/// - Parameter viewModel: state
|
/// - Parameter viewModel: state
|
||||||
open override func shouldUpdateView(viewModel: ModelType) -> Bool {
|
open override func shouldUpdateView(viewModel: ModelType) -> Bool {
|
||||||
let update = viewModel.selected != model.selected
|
let should = viewModel != model
|
||||||
|| viewModel.text != model.text
|
return should
|
||||||
|| viewModel.primaryColor != model.primaryColor
|
|
||||||
|| viewModel.secondaryColor != model.secondaryColor
|
|
||||||
|| viewModel.surface != model.surface
|
|
||||||
|| viewModel.disabled != model.disabled
|
|
||||||
return update
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open override func updateView(viewModel: ModelType) {
|
open override func updateView(viewModel: ModelType) {
|
||||||
@ -176,7 +168,7 @@ open class RadioSwatchBase<ModelType: RadioSwatchModel>: Control<ModelType>, Cha
|
|||||||
setAccessibilityHint(enabled)
|
setAccessibilityHint(enabled)
|
||||||
setAccessibilityValue(viewModel.selected)
|
setAccessibilityValue(viewModel.selected)
|
||||||
setAccessibilityLabel(viewModel.selected)
|
setAccessibilityLabel(viewModel.selected)
|
||||||
isUserInteractionEnabled = !viewModel.disabled
|
//isUserInteractionEnabled = !viewModel.disabled
|
||||||
setNeedsLayout()
|
setNeedsLayout()
|
||||||
layoutIfNeeded()
|
layoutIfNeeded()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,3 +6,207 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
import UIKit
|
||||||
|
|
||||||
|
public class RadioSwatchGroup: Control<DefaultRadioSwatchGroupModel>, Changable {
|
||||||
|
public typealias ModelHandlerType = RadioSwatch
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Public Properties
|
||||||
|
//--------------------------------------------------
|
||||||
|
@Proxy(\.model.selectedModel)
|
||||||
|
public var selectedModel: ModelHandlerType.ModelType?
|
||||||
|
|
||||||
|
public var onChange: Blocks.ActionBlock?
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Private Properties
|
||||||
|
//--------------------------------------------------
|
||||||
|
private var mainStackView: UIStackView = {
|
||||||
|
return UIStackView().with {
|
||||||
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
$0.axis = .vertical
|
||||||
|
$0.spacing = 24
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
private var label = Label()
|
||||||
|
|
||||||
|
private let cellSize: CGFloat = 48.0
|
||||||
|
private let lineSpacing: CGFloat = 12.0
|
||||||
|
private let itemSpacing: CGFloat = 16.0
|
||||||
|
|
||||||
|
public var collectionViewHeight: NSLayoutConstraint?
|
||||||
|
public var collectionViewWidth: NSLayoutConstraint?
|
||||||
|
|
||||||
|
private lazy var collectionView: UICollectionView = {
|
||||||
|
let layout = UICollectionViewFlowLayout().with {
|
||||||
|
$0.minimumLineSpacing = lineSpacing
|
||||||
|
$0.minimumInteritemSpacing = itemSpacing
|
||||||
|
$0.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
|
||||||
|
}
|
||||||
|
return UICollectionView(frame: .zero, collectionViewLayout: layout).with {
|
||||||
|
$0.backgroundColor = .clear
|
||||||
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
$0.register(CollectionViewCell<ModelHandlerType>.self, forCellWithReuseIdentifier: "collectionViewCell")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Overrides
|
||||||
|
//--------------------------------------------------
|
||||||
|
override public var disabled: Bool {
|
||||||
|
didSet {
|
||||||
|
updateSelectors()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override public var surface: Surface {
|
||||||
|
didSet {
|
||||||
|
updateSelectors()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
open override func setup() {
|
||||||
|
super.setup()
|
||||||
|
isAccessibilityElement = true
|
||||||
|
accessibilityTraits = .button
|
||||||
|
addSubview(mainStackView)
|
||||||
|
mainStackView.addArrangedSubview(label)
|
||||||
|
mainStackView.addArrangedSubview(collectionView)
|
||||||
|
|
||||||
|
NSLayoutConstraint.activate([
|
||||||
|
mainStackView.topAnchor.constraint(equalTo: topAnchor),
|
||||||
|
mainStackView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
||||||
|
mainStackView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
||||||
|
mainStackView.bottomAnchor.constraint(equalTo: bottomAnchor)
|
||||||
|
])
|
||||||
|
|
||||||
|
collectionViewHeight = collectionView.heightAnchor.constraint(greaterThanOrEqualToConstant: cellSize)
|
||||||
|
collectionViewHeight?.isActive = true
|
||||||
|
collectionViewWidth = collectionView.widthAnchor.constraint(greaterThanOrEqualToConstant: cellSize * 5)
|
||||||
|
collectionViewWidth?.isActive = true
|
||||||
|
}
|
||||||
|
|
||||||
|
open override func shouldUpdateView(viewModel: ModelType) -> Bool {
|
||||||
|
return viewModel != model
|
||||||
|
}
|
||||||
|
|
||||||
|
public override func initialSetup() {
|
||||||
|
super.initialSetup()
|
||||||
|
collectionView.delegate = self
|
||||||
|
collectionView.dataSource = self
|
||||||
|
}
|
||||||
|
|
||||||
|
open override func updateView(viewModel: ModelType) {
|
||||||
|
collectionView.reloadData()
|
||||||
|
setNeedsLayout()
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Lifecycle
|
||||||
|
//--------------------------------------------------
|
||||||
|
open override func layoutSubviews() {
|
||||||
|
super.layoutSubviews()
|
||||||
|
// Accounts for any collection size changes
|
||||||
|
setHeight()
|
||||||
|
DispatchQueue.main.async { [weak self] in
|
||||||
|
self?.collectionView.collectionViewLayout.invalidateLayout()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
open func setHeight() {
|
||||||
|
let bounds = collectionView.bounds
|
||||||
|
if bounds.width <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if model.selectors.count > 0 {
|
||||||
|
// Calculate the height
|
||||||
|
let swatchesInRow = floor(CGFloat(bounds.width/(cellSize + itemSpacing)))
|
||||||
|
let numberOfRows = ceil(CGFloat(model.selectors.count)/swatchesInRow)
|
||||||
|
let height = (numberOfRows * cellSize) + (itemSpacing * (numberOfRows-1))
|
||||||
|
|
||||||
|
if let oldHeight = collectionViewHeight?.constant,
|
||||||
|
height != oldHeight {
|
||||||
|
}
|
||||||
|
collectionViewHeight?.constant = CGFloat(height)
|
||||||
|
} else {
|
||||||
|
collectionViewHeight?.constant = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Refactor into new CollectionView Selector protocol
|
||||||
|
public func updateSelectors(){
|
||||||
|
let selectors = model.selectors.compactMap { existing in
|
||||||
|
return existing.copyWith {
|
||||||
|
$0.disabled = disabled
|
||||||
|
$0.surface = surface
|
||||||
|
}
|
||||||
|
}
|
||||||
|
model.selectors = selectors
|
||||||
|
}
|
||||||
|
|
||||||
|
public func replace(viewModel: ModelHandlerType.ModelType){
|
||||||
|
if let index = model.selectors.firstIndex(where: { element in
|
||||||
|
return element.inputId == viewModel.inputId
|
||||||
|
}) {
|
||||||
|
model.selectors[index] = viewModel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension RadioSwatchGroup: UICollectionViewDelegateFlowLayout {
|
||||||
|
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
||||||
|
return CGSize(width: cellSize, height: cellSize)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension RadioSwatchGroup: UICollectionViewDelegate {
|
||||||
|
open func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
|
||||||
|
return !model.selectors[indexPath.row].disabled
|
||||||
|
}
|
||||||
|
|
||||||
|
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||||
|
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return }
|
||||||
|
|
||||||
|
//reset the old model
|
||||||
|
if let selectedModel {
|
||||||
|
let oldSelectedModel = selectedModel.copyWith {
|
||||||
|
$0.selected = false
|
||||||
|
}
|
||||||
|
replace(viewModel: oldSelectedModel)
|
||||||
|
}
|
||||||
|
|
||||||
|
//set the new model
|
||||||
|
let newSelectedModel = cell.model.copyWith {
|
||||||
|
$0.selected = true
|
||||||
|
}
|
||||||
|
|
||||||
|
label.text = newSelectedModel.text
|
||||||
|
replace(viewModel: newSelectedModel)
|
||||||
|
selectedModel = newSelectedModel
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
open func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
|
||||||
|
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return }
|
||||||
|
cell.isSelected = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension RadioSwatchGroup: UICollectionViewDataSource {
|
||||||
|
public func numberOfSections(in collectionView: UICollectionView) -> Int {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
||||||
|
return model.selectors.count
|
||||||
|
}
|
||||||
|
|
||||||
|
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
||||||
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell<ModelHandlerType>
|
||||||
|
let model = model.selectors[indexPath.row]
|
||||||
|
cell?.modelHandler.isUserInteractionEnabled = false
|
||||||
|
cell?.set(with: model)
|
||||||
|
return cell ?? UICollectionViewCell()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -6,3 +6,19 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
public protocol RadioSwatchGroupModel: SelectorGroupSelectedModelable, Equatable where SelectorModelType: RadioSwatchModel { }
|
||||||
|
|
||||||
|
public struct DefaultRadioSwatchGroupModel: RadioSwatchGroupModel {
|
||||||
|
public typealias SelectorModelType = DefaultRadioSwatchModel
|
||||||
|
public var inputId: String?
|
||||||
|
public var value: AnyHashable?
|
||||||
|
public var surface: Surface = .light
|
||||||
|
public var disabled: Bool = false
|
||||||
|
public var selectors: [SelectorModelType]
|
||||||
|
public var selectedModel: DefaultRadioSwatchModel?
|
||||||
|
public init() { selectors = [] }
|
||||||
|
public init(selectors: [SelectorModelType]){
|
||||||
|
self.selectors = selectors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -8,24 +8,43 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
public protocol RadioSwatchModel: Modelable, FormFieldable, DataTrackable, Accessable, Selectable, BinaryColorable {
|
public protocol RadioSwatchModel: Modelable, FormFieldable, DataTrackable, Accessable, Selectable, BinaryColorable, Equatable {
|
||||||
var fillImage: String? { get set }
|
var fillImage: String? { get set }
|
||||||
var primaryColor: UIColor? { get set }
|
var primaryColor: UIColor? { get set }
|
||||||
var secondaryColor: UIColor? { get set }
|
var secondaryColor: UIColor? { get set }
|
||||||
var text: String { get set }
|
var text: String { get set }
|
||||||
var textAttributes: [LabelAttributeModel]? { get set }
|
|
||||||
var strikethrough: Bool { get set }
|
var strikethrough: Bool { get set }
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct DefaultRadioSwatchModel: RadioSwatchModel {
|
public struct DefaultRadioSwatchModel: RadioSwatchModel {
|
||||||
public var id: UUID = UUID()
|
public static func == (lhs: DefaultRadioSwatchModel, rhs: DefaultRadioSwatchModel) -> Bool {
|
||||||
|
return lhs.selected == rhs.selected &&
|
||||||
|
lhs.fillImage == rhs.fillImage &&
|
||||||
|
lhs.primaryColor == rhs.primaryColor &&
|
||||||
|
lhs.secondaryColor == rhs.secondaryColor &&
|
||||||
|
lhs.secondaryColor == rhs.secondaryColor &&
|
||||||
|
lhs.strikethrough == rhs.strikethrough &&
|
||||||
|
lhs.inputId == rhs.inputId &&
|
||||||
|
lhs.value == rhs.value &&
|
||||||
|
lhs.surface == rhs.surface &&
|
||||||
|
lhs.disabled == rhs.disabled &&
|
||||||
|
lhs.dataAnalyticsTrack == rhs.dataAnalyticsTrack &&
|
||||||
|
lhs.dataClickStream == rhs.dataClickStream &&
|
||||||
|
lhs.accessibilityHintEnabled == rhs.accessibilityHintEnabled &&
|
||||||
|
lhs.accessibilityHintDisabled == rhs.accessibilityHintDisabled &&
|
||||||
|
lhs.accessibilityValueEnabled == rhs.accessibilityValueEnabled &&
|
||||||
|
lhs.accessibilityValueDisabled == rhs.accessibilityValueDisabled &&
|
||||||
|
lhs.accessibilityLabelEnabled == rhs.accessibilityLabelEnabled &&
|
||||||
|
lhs.accessibilityLabelDisabled == rhs.accessibilityLabelDisabled
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public var selected: Bool = false
|
public var selected: Bool = false
|
||||||
|
|
||||||
public var fillImage: String?
|
public var fillImage: String?
|
||||||
@OptionalCodableColor public var primaryColor: UIColor?
|
@OptionalCodableColor public var primaryColor: UIColor?
|
||||||
@OptionalCodableColor public var secondaryColor: UIColor?
|
@OptionalCodableColor public var secondaryColor: UIColor?
|
||||||
public var text: String = ""
|
public var text: String = ""
|
||||||
public var textAttributes: [LabelAttributeModel]?
|
|
||||||
public var strikethrough: Bool = false
|
public var strikethrough: Bool = false
|
||||||
|
|
||||||
public var inputId: String?
|
public var inputId: String?
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import Foundation
|
|||||||
import UIKit
|
import UIKit
|
||||||
import VDSColorTokens
|
import VDSColorTokens
|
||||||
|
|
||||||
public enum Surface: String, Codable {
|
public enum Surface: String, Codable, Equatable {
|
||||||
case light, dark
|
case light, dark
|
||||||
public var color: UIColor {
|
public var color: UIColor {
|
||||||
return self == .dark ? VDSColor.backgroundPrimaryDark : .clear
|
return self == .dark ? VDSColor.backgroundPrimaryDark : .clear
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user