Added new keys and code changes.

This commit is contained in:
Lekshmi S 2020-04-09 19:37:32 +05:30
parent 136365d6a9
commit 45901f3110
3 changed files with 95 additions and 9 deletions

View File

@ -9,12 +9,17 @@
import UIKit
open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
public var bottomText = Label.commonLabelB2(true)
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public var bottomText = Label(frame: .zero)
let circleLayer = CAShapeLayer()
let outerCircleLayer = CAShapeLayer()
var cellView = MVMCoreUICommonViewsUtility.commonView()
var fillColor: Color = Color(uiColor: .mvmBlue)
//------------------------------------------------------
// MARK: - Property Observer
//------------------------------------------------------
open override var isSelected: Bool {
didSet {
drawCircle()
@ -32,6 +37,9 @@ open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
}
}
//------------------------------------------------------
// MARK: - Initialization
//------------------------------------------------------
public override init(frame: CGRect) {
super.init(frame: .zero)
setupView()
@ -42,10 +50,15 @@ open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
setupView()
}
//------------------------------------------------------
// MARK: - Drawing
//------------------------------------------------------
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.fillColor = fillColor.cgColor
circleLayer.strokeColor = UIColor.mvmBlack.cgColor
circleLayer.strokeColor = isUserInteractionEnabled ? UIColor.mvmBlack.cgColor : UIColor.mvmCoolGray6.cgColor
circleLayer.name = "InnerCircle"
circleLayer.lineWidth = 1
cellView.layer.addSublayer(circleLayer)
}
@ -65,7 +78,9 @@ open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
}
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
let outerCircleLayer = CAShapeLayer()
outerCircleLayer.path = UIBezierPath(ovalIn: CGRect(x: 12, y: 1, width: 30, height: 30)).cgPath
outerCircleLayer.name = "OuterCircle"
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()})
}
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
public func setupView() {
guard cellView.superview == nil else {
return
@ -92,6 +110,7 @@ open class RadioSwatchItem: UICollectionViewCell, MoleculeViewProtocol {
NSLayoutConstraint.constraintPinSubview(toSuperview: cellView)
cellView.addSubview(bottomText)
bottomText.textAlignment = .center
bottomText.font = MFFonts.mfFontTXRegular(11.0)
bottomText.topAnchor.constraint(equalTo: cellView.topAnchor, constant: 38).isActive = true
bottomText.leadingAnchor.constraint(equalTo: cellView.leadingAnchor).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]?) {
guard let collectionModel = model as? RadioSwatchItemModel else { return }
fillColor = collectionModel.color
isSelected = collectionModel.selected ?? false
isUserInteractionEnabled = collectionModel.enabled ?? true
isStrikeThrough = collectionModel.strikethrough ?? false
bottomText.text = collectionModel.text

View File

@ -9,13 +9,29 @@
import UIKit
open class RadioSwatches: View {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
var swatches: [MoleculeModelProtocol]?
public var collectionViewHeight: NSLayoutConstraint?
var swatches: [RadioSwatchItemModel]?
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 spacing: Double = 10
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
open override func setupView() {
super.setupView()
guard collectionView.superview == nil else {
@ -33,6 +49,17 @@ open class RadioSwatches: View {
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]?) {
super.set(with: model, delegateObject, additionalData)
guard let radioSwatchesModel = model as? RadioSwatchesModel else { return }
@ -43,6 +70,9 @@ open class RadioSwatches: View {
collectionView.reloadData()
}
//------------------------------------------------------
// MARK: - Methods
//------------------------------------------------------
func registerCells() {
collectionView.register(RadioSwatchItem.self, forCellWithReuseIdentifier: "RadioSwatchItemCollectionViewCell")
}
@ -70,6 +100,9 @@ open class RadioSwatches: View {
}
}
//------------------------------------------------------
// MARK: - Delegate methods
//------------------------------------------------------
extension RadioSwatches: UICollectionViewDelegateFlowLayout {
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: cellSize, height: cellSize)
@ -89,3 +122,14 @@ extension RadioSwatches: UICollectionViewDataSource {
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
}
}

View File

@ -37,7 +37,7 @@ import Foundation
}
}
@objcMembers public class RadioSwatchItemModel: MoleculeModelProtocol {
@objcMembers public class RadioSwatchItemModel: MoleculeModelProtocol, FormFieldProtocol {
public var backgroundColor: Color?
public static var identifier: String = "radioSwatchItem"
public var color: Color = Color(uiColor: .mvmBlue)
@ -45,13 +45,25 @@ import Foundation
public var selected: Bool? = false
public var enabled: Bool? = true
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.text = text
self.selected = selected
self.fieldValue = fieldValue
self.enabled = enabled
self.strikethrough = strikethrough
self.fieldKey = fieldKey
self.groupName = groupName
baseValue = selected
}
public func formFieldValue() -> AnyHashable? {
return selected
}
private enum CodingKeys: String, CodingKey {
@ -62,6 +74,9 @@ import Foundation
case selected
case enabled
case strikethrough
case fieldKey
case fieldValue
case groupName
}
required public init(from decoder: Decoder) throws {
@ -80,6 +95,12 @@ import Foundation
if let strikethrough = try typeContainer.decodeIfPresent(Bool.self, forKey: .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 {
@ -91,6 +112,9 @@ import Foundation
try container.encodeIfPresent(selected, forKey: .selected)
try container.encodeIfPresent(enabled, forKey: .enabled)
try container.encodeIfPresent(strikethrough, forKey: .strikethrough)
try container.encodeIfPresent(fieldKey, forKey: .fieldKey)
try container.encodeIfPresent(fieldValue, forKey: .fieldValue)
try container.encodeIfPresent(groupName, forKey: .groupName)
}
}