Added new keys and code changes.
This commit is contained in:
parent
136365d6a9
commit
45901f3110
@ -9,12 +9,17 @@
|
|||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
|
open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
|
||||||
public var bottomText = Label.commonLabelB2(true)
|
//--------------------------------------------------
|
||||||
|
// MARK: - Properties
|
||||||
|
//--------------------------------------------------
|
||||||
|
public var bottomText = Label(frame: .zero)
|
||||||
let circleLayer = CAShapeLayer()
|
let circleLayer = CAShapeLayer()
|
||||||
let outerCircleLayer = CAShapeLayer()
|
|
||||||
var cellView = MVMCoreUICommonViewsUtility.commonView()
|
var cellView = MVMCoreUICommonViewsUtility.commonView()
|
||||||
var fillColor: Color = Color(uiColor: .mvmBlue)
|
var fillColor: Color = Color(uiColor: .mvmBlue)
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Property Observer
|
||||||
|
//------------------------------------------------------
|
||||||
open override var isSelected: Bool {
|
open override var isSelected: Bool {
|
||||||
didSet {
|
didSet {
|
||||||
drawCircle()
|
drawCircle()
|
||||||
@ -32,6 +37,9 @@ open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Initialization
|
||||||
|
//------------------------------------------------------
|
||||||
public override init(frame: CGRect) {
|
public override init(frame: CGRect) {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
setupView()
|
setupView()
|
||||||
@ -42,10 +50,15 @@ open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
|
|||||||
setupView()
|
setupView()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Drawing
|
||||||
|
//------------------------------------------------------
|
||||||
public func drawCircle() {
|
public func drawCircle() {
|
||||||
|
cellView.layer.sublayers?.filter({$0.name == "InnerCircle"}).forEach({$0.removeFromSuperlayer()})
|
||||||
circleLayer.path = UIBezierPath(ovalIn: CGRect(x: 12, y: 1, width: 30, height: 30)).cgPath
|
circleLayer.path = UIBezierPath(ovalIn: CGRect(x: 12, y: 1, width: 30, height: 30)).cgPath
|
||||||
circleLayer.fillColor = fillColor.cgColor
|
circleLayer.fillColor = fillColor.cgColor
|
||||||
circleLayer.strokeColor = UIColor.mvmBlack.cgColor
|
circleLayer.strokeColor = isUserInteractionEnabled ? UIColor.mvmBlack.cgColor : UIColor.mvmCoolGray6.cgColor
|
||||||
|
circleLayer.name = "InnerCircle"
|
||||||
circleLayer.lineWidth = 1
|
circleLayer.lineWidth = 1
|
||||||
cellView.layer.addSublayer(circleLayer)
|
cellView.layer.addSublayer(circleLayer)
|
||||||
}
|
}
|
||||||
@ -65,7 +78,9 @@ open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func drawOuterCircle() {
|
public func drawOuterCircle() {
|
||||||
|
self.cellView.layer.sublayers?.filter({$0.name == "OuterCircle"}).forEach({$0.removeFromSuperlayer()})
|
||||||
circleLayer.path = UIBezierPath(ovalIn: CGRect(x: 15, y: 4, width: 24, height: 24)).cgPath
|
circleLayer.path = UIBezierPath(ovalIn: CGRect(x: 15, y: 4, width: 24, height: 24)).cgPath
|
||||||
|
let outerCircleLayer = CAShapeLayer()
|
||||||
outerCircleLayer.path = UIBezierPath(ovalIn: CGRect(x: 12, y: 1, width: 30, height: 30)).cgPath
|
outerCircleLayer.path = UIBezierPath(ovalIn: CGRect(x: 12, y: 1, width: 30, height: 30)).cgPath
|
||||||
outerCircleLayer.name = "OuterCircle"
|
outerCircleLayer.name = "OuterCircle"
|
||||||
outerCircleLayer.strokeColor = UIColor.mvmBlack.cgColor
|
outerCircleLayer.strokeColor = UIColor.mvmBlack.cgColor
|
||||||
@ -79,6 +94,9 @@ open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
|
|||||||
self.cellView.layer.sublayers?.filter({$0.name == "OuterCircle"}).forEach({$0.removeFromSuperlayer()})
|
self.cellView.layer.sublayers?.filter({$0.name == "OuterCircle"}).forEach({$0.removeFromSuperlayer()})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Lifecycle
|
||||||
|
//--------------------------------------------------
|
||||||
public func setupView() {
|
public func setupView() {
|
||||||
guard cellView.superview == nil else {
|
guard cellView.superview == nil else {
|
||||||
return
|
return
|
||||||
@ -92,6 +110,7 @@ open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
|
|||||||
NSLayoutConstraint.constraintPinSubview(toSuperview: cellView)
|
NSLayoutConstraint.constraintPinSubview(toSuperview: cellView)
|
||||||
cellView.addSubview(bottomText)
|
cellView.addSubview(bottomText)
|
||||||
bottomText.textAlignment = .center
|
bottomText.textAlignment = .center
|
||||||
|
bottomText.font = MFFonts.mfFontTXRegular(11.0)
|
||||||
bottomText.topAnchor.constraint(equalTo: cellView.topAnchor, constant: 38).isActive = true
|
bottomText.topAnchor.constraint(equalTo: cellView.topAnchor, constant: 38).isActive = true
|
||||||
bottomText.leadingAnchor.constraint(equalTo: cellView.leadingAnchor).isActive = true
|
bottomText.leadingAnchor.constraint(equalTo: cellView.leadingAnchor).isActive = true
|
||||||
bottomText.trailingAnchor.constraint(equalTo: cellView.trailingAnchor).isActive = true
|
bottomText.trailingAnchor.constraint(equalTo: cellView.trailingAnchor).isActive = true
|
||||||
@ -101,7 +120,6 @@ open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
|
|||||||
public func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
public func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
||||||
guard let collectionModel = model as? RadioSwatchItemModel else { return }
|
guard let collectionModel = model as? RadioSwatchItemModel else { return }
|
||||||
fillColor = collectionModel.color
|
fillColor = collectionModel.color
|
||||||
isSelected = collectionModel.selected ?? false
|
|
||||||
isUserInteractionEnabled = collectionModel.enabled ?? true
|
isUserInteractionEnabled = collectionModel.enabled ?? true
|
||||||
isStrikeThrough = collectionModel.strikethrough ?? false
|
isStrikeThrough = collectionModel.strikethrough ?? false
|
||||||
bottomText.text = collectionModel.text
|
bottomText.text = collectionModel.text
|
||||||
|
|||||||
@ -9,13 +9,29 @@
|
|||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
open class RadioSwatches: View {
|
open class RadioSwatches: View {
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Properties
|
||||||
|
//--------------------------------------------------
|
||||||
public let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
|
public let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
|
||||||
var swatches: [MoleculeModelProtocol]?
|
var swatches: [RadioSwatchItemModel]?
|
||||||
public var collectionViewHeight: NSLayoutConstraint?
|
|
||||||
|
|
||||||
|
public var selectedSwatchItem: RadioSwatchItemModel? {
|
||||||
|
get{
|
||||||
|
guard let selectedItem = collectionView.indexPathsForSelectedItems?.first else {return nil}
|
||||||
|
return swatches?[selectedItem.item]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Constraints
|
||||||
|
//------------------------------------------------------
|
||||||
|
public var collectionViewHeight: NSLayoutConstraint?
|
||||||
public let cellSize: Double = 54.0
|
public let cellSize: Double = 54.0
|
||||||
public let spacing: Double = 10
|
public let spacing: Double = 10
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Lifecycle
|
||||||
|
//--------------------------------------------------
|
||||||
open override func setupView() {
|
open override func setupView() {
|
||||||
super.setupView()
|
super.setupView()
|
||||||
guard collectionView.superview == nil else {
|
guard collectionView.superview == nil else {
|
||||||
@ -33,6 +49,17 @@ open class RadioSwatches: View {
|
|||||||
collectionViewHeight?.isActive = true
|
collectionViewHeight?.isActive = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override func updateView(_ size: CGFloat) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.collectionView.collectionViewLayout.invalidateLayout()
|
||||||
|
self.collectionView.reloadData()
|
||||||
|
guard let selectedCell = self.swatches?.firstIndex(where: {$0.selected == true}) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.collectionView.selectItem(at: IndexPath(item: selectedCell, section: 0), animated: true, scrollPosition: .centeredHorizontally)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
||||||
super.set(with: model, delegateObject, additionalData)
|
super.set(with: model, delegateObject, additionalData)
|
||||||
guard let radioSwatchesModel = model as? RadioSwatchesModel else { return }
|
guard let radioSwatchesModel = model as? RadioSwatchesModel else { return }
|
||||||
@ -43,6 +70,9 @@ open class RadioSwatches: View {
|
|||||||
collectionView.reloadData()
|
collectionView.reloadData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Methods
|
||||||
|
//------------------------------------------------------
|
||||||
func registerCells() {
|
func registerCells() {
|
||||||
collectionView.register(RadioSwatchItem.self, forCellWithReuseIdentifier: "RadioSwatchItemCollectionViewCell")
|
collectionView.register(RadioSwatchItem.self, forCellWithReuseIdentifier: "RadioSwatchItemCollectionViewCell")
|
||||||
}
|
}
|
||||||
@ -70,6 +100,9 @@ open class RadioSwatches: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Delegate methods
|
||||||
|
//------------------------------------------------------
|
||||||
extension RadioSwatches: UICollectionViewDelegateFlowLayout {
|
extension RadioSwatches: UICollectionViewDelegateFlowLayout {
|
||||||
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
||||||
return CGSize(width: cellSize, height: cellSize)
|
return CGSize(width: cellSize, height: cellSize)
|
||||||
@ -89,3 +122,14 @@ extension RadioSwatches: UICollectionViewDataSource {
|
|||||||
return cell
|
return cell
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension RadioSwatches: UICollectionViewDelegate {
|
||||||
|
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||||
|
guard let swatchItem = swatches?[indexPath.row] else { return }
|
||||||
|
swatchItem.selected = true
|
||||||
|
}
|
||||||
|
public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
|
||||||
|
guard let swatchItem = swatches?[indexPath.row] else { return }
|
||||||
|
swatchItem.selected = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -37,7 +37,7 @@ import Foundation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@objcMembers public class RadioSwatchItemModel: MoleculeModelProtocol {
|
@objcMembers public class RadioSwatchItemModel: MoleculeModelProtocol, FormFieldProtocol {
|
||||||
public var backgroundColor: Color?
|
public var backgroundColor: Color?
|
||||||
public static var identifier: String = "radioSwatchItem"
|
public static var identifier: String = "radioSwatchItem"
|
||||||
public var color: Color = Color(uiColor: .mvmBlue)
|
public var color: Color = Color(uiColor: .mvmBlue)
|
||||||
@ -45,13 +45,25 @@ import Foundation
|
|||||||
public var selected: Bool? = false
|
public var selected: Bool? = false
|
||||||
public var enabled: Bool? = true
|
public var enabled: Bool? = true
|
||||||
public var strikethrough: Bool? = false
|
public var strikethrough: Bool? = false
|
||||||
|
public var fieldKey: String?
|
||||||
|
public var fieldValue: String?
|
||||||
|
public var baseValue: AnyHashable?
|
||||||
|
public var groupName: String = FormValidator.defaultGroupName
|
||||||
|
|
||||||
public init(color: Color, text:String, selected: Bool, enabled: Bool, strikethrough: Bool) {
|
public init(color: Color, text:String, selected: Bool, enabled: Bool, strikethrough: Bool, fieldKey: String, fieldValue: String, groupName:String) {
|
||||||
self.color = color
|
self.color = color
|
||||||
self.text = text
|
self.text = text
|
||||||
self.selected = selected
|
self.selected = selected
|
||||||
|
self.fieldValue = fieldValue
|
||||||
self.enabled = enabled
|
self.enabled = enabled
|
||||||
self.strikethrough = strikethrough
|
self.strikethrough = strikethrough
|
||||||
|
self.fieldKey = fieldKey
|
||||||
|
self.groupName = groupName
|
||||||
|
baseValue = selected
|
||||||
|
}
|
||||||
|
|
||||||
|
public func formFieldValue() -> AnyHashable? {
|
||||||
|
return selected
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
@ -62,6 +74,9 @@ import Foundation
|
|||||||
case selected
|
case selected
|
||||||
case enabled
|
case enabled
|
||||||
case strikethrough
|
case strikethrough
|
||||||
|
case fieldKey
|
||||||
|
case fieldValue
|
||||||
|
case groupName
|
||||||
}
|
}
|
||||||
|
|
||||||
required public init(from decoder: Decoder) throws {
|
required public init(from decoder: Decoder) throws {
|
||||||
@ -80,6 +95,12 @@ import Foundation
|
|||||||
if let strikethrough = try typeContainer.decodeIfPresent(Bool.self, forKey: .strikethrough) {
|
if let strikethrough = try typeContainer.decodeIfPresent(Bool.self, forKey: .strikethrough) {
|
||||||
self.strikethrough = strikethrough
|
self.strikethrough = strikethrough
|
||||||
}
|
}
|
||||||
|
baseValue = self.selected
|
||||||
|
fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey)
|
||||||
|
fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue)
|
||||||
|
if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
|
||||||
|
self.groupName = groupName
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encode(to encoder: Encoder) throws {
|
public func encode(to encoder: Encoder) throws {
|
||||||
@ -91,6 +112,9 @@ import Foundation
|
|||||||
try container.encodeIfPresent(selected, forKey: .selected)
|
try container.encodeIfPresent(selected, forKey: .selected)
|
||||||
try container.encodeIfPresent(enabled, forKey: .enabled)
|
try container.encodeIfPresent(enabled, forKey: .enabled)
|
||||||
try container.encodeIfPresent(strikethrough, forKey: .strikethrough)
|
try container.encodeIfPresent(strikethrough, forKey: .strikethrough)
|
||||||
|
try container.encodeIfPresent(fieldKey, forKey: .fieldKey)
|
||||||
|
try container.encodeIfPresent(fieldValue, forKey: .fieldValue)
|
||||||
|
try container.encodeIfPresent(groupName, forKey: .groupName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user