diff --git a/MVMCoreUI/Atomic/Atoms/Views/RadioBoxCollectionViewCell.swift b/MVMCoreUI/Atomic/Atoms/Views/RadioBoxCollectionViewCell.swift index 532ff34f..c709cf4b 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/RadioBoxCollectionViewCell.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/RadioBoxCollectionViewCell.swift @@ -8,28 +8,37 @@ import Foundation open class RadioBoxCollectionViewCell: UICollectionViewCell, MoleculeViewProtocol { - + public let bodyLabel = Label.commonLabelB2(true) public let lineView = View(frame: .zero) - - var bottomView = MVMCoreUICommonViewsUtility.commonView() - public var lineViewHeight: NSLayoutConstraint? + public var fieldValue: String? open override var isSelected: Bool{ didSet{ - lineViewHeight?.constant = isSelected ? 4.0 : 0 - bottomView.layer.borderColor = isSelected ? UIColor.mfGet(forHex: "#0000").cgColor:UIColor.mfGet(forHex: "#747676").cgColor - + self.lineViewHeight?.constant = self.isSelected ? 4.0 : 0 + self.bottomView.layer.sublayers?.filter({$0.name == "border"}).forEach({$0.removeFromSuperlayer()}) + if(self.isSelected){ + self.bottomView.layer.addBorder(edge: .bottom, color: UIColor.mfGet(forHex: "#0000"), thickness: 1.0) + self.bottomView.layer.addBorder(edge: .left, color: UIColor.mfGet(forHex: "#0000"), thickness: 1.0) + self.bottomView.layer.addBorder(edge: .right, color: UIColor.mfGet(forHex: "#0000"), thickness: 1.0) + } + else { + self.addBordertoView() + } } } public override init(frame: CGRect) { super.init(frame: .zero) setupView() } - + open override func layoutSubviews() { + super.layoutSubviews() + addBordertoView() + + } public required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setupView() @@ -44,7 +53,6 @@ open class RadioBoxCollectionViewCell: UICollectionViewCell, MoleculeViewProtoco insetsLayoutMarginsFromSafeArea = false contentView.insetsLayoutMarginsFromSafeArea = false contentView.preservesSuperviewLayoutMargins = false - contentView.addSubview(bottomView) NSLayoutConstraint.constraintPinSubview(toSuperview: bottomView) @@ -54,34 +62,58 @@ open class RadioBoxCollectionViewCell: UICollectionViewCell, MoleculeViewProtoco lineViewHeight = lineView.heightAnchor.constraint(equalToConstant: 0) lineViewHeight?.isActive = true - bodyLabel.translatesAutoresizingMaskIntoConstraints = false + bodyLabel.numberOfLines = 0 bottomView.addSubview(bodyLabel) NSLayoutConstraint.constraintPinSubview(bodyLabel, pinTop: false, topConstant:0 , pinBottom: false, bottomConstant: 0, pinLeft: true, leftConstant: 12, pinRight: true, rightConstant: 12) bodyLabel.topAnchor.constraint(equalTo: lineView.bottomAnchor, constant: 12).isActive = true - bodyLabel.bottomAnchor.constraint(greaterThanOrEqualTo: bottomView.bottomAnchor, constant: -12.0).isActive = true - - bottomView.layer.borderWidth = 1.0 - bottomView.layer.borderColor = UIColor.mfGet(forHex: "#747676").cgColor - - + bodyLabel.bottomAnchor.constraint(lessThanOrEqualTo: bottomView.bottomAnchor, constant: -12.0).isActive = true } - - + private func addBordertoView(){ + bottomView.layer.addBorder(edge: .bottom, color: UIColor.mfGet(forHex: "#747676"), thickness: 1.0) + bottomView.layer.addBorder(edge: .left, color: UIColor.mfGet(forHex: "#747676"), thickness: 1.0) + bottomView.layer.addBorder(edge: .right, color: UIColor.mfGet(forHex: "#747676"), thickness: 1.0) + bottomView.layer.addBorder(edge: .top, color: UIColor.mfGet(forHex: "#747676"), thickness: 1.0) + } + + public func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { guard let collectionModel = model as? RadioBoxModel else { return } - if let backgroundColor = collectionModel.backgroundColor { self.bottomView.backgroundColor = backgroundColor.uiColor } -// self.bottomView.backgroundColor = .red lineView.backgroundColor = collectionModel.selectedAccentColor?.uiColor bodyLabel.text = collectionModel.text - - - + isSelected = collectionModel.selected ?? false + fieldValue = collectionModel.fieldValue } - - +} +extension CALayer { + + func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) { + + let border = CALayer() + border.name = "border" + switch edge { + case UIRectEdge.top: + border.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: thickness) + break + case UIRectEdge.bottom: + border.frame = CGRect(x: 0, y: self.frame.height - thickness, width: self.frame.width, height: thickness) + break + case UIRectEdge.left: + border.frame = CGRect(x: 0, y: 0, width: thickness, height: self.frame.height) + break + case UIRectEdge.right: + border.frame = CGRect(x: self.frame.width - thickness, y: 0, width: thickness, height: self.frame.height) + break + default: + break + } + + border.backgroundColor = color.cgColor; + self.addSublayer(border) + } + } diff --git a/MVMCoreUI/Atomic/Atoms/Views/RadioBoxModel.swift b/MVMCoreUI/Atomic/Atoms/Views/RadioBoxModel.swift index 21adcb13..0e72f6e5 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/RadioBoxModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/RadioBoxModel.swift @@ -11,8 +11,9 @@ import Foundation public static var identifier: String = "radioBox" public var text: String public var backgroundColor: Color? = Color(uiColor: .white) - public var selectedAccentColor: Color? = Color(uiColor: .red) + public var selectedAccentColor: Color? = try? Color(colorString: "#D52B1E") public var selected: Bool? = false + public var strikethrough: Bool? = false public var fieldValue: String? private enum CodingKeys: String, CodingKey { @@ -21,6 +22,7 @@ import Foundation case selectedAccentColor case backgroundColor case selected + case strikethrough case fieldValue } @@ -38,6 +40,9 @@ import Foundation if let isSelected = try typeContainer.decodeIfPresent(Bool.self, forKey: .selected) { selected = isSelected } + if let isStrikeTrough = try typeContainer.decodeIfPresent(Bool.self, forKey: .strikethrough) { + strikethrough = isStrikeTrough + } fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue) } @@ -48,6 +53,7 @@ import Foundation try container.encode(selectedAccentColor, forKey: .selectedAccentColor) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(selected, forKey: .selected) + try container.encodeIfPresent(strikethrough, forKey: .strikethrough) try container.encodeIfPresent(fieldValue, forKey: .fieldValue) } diff --git a/MVMCoreUI/Atomic/Atoms/Views/RadioBoxes.swift b/MVMCoreUI/Atomic/Atoms/Views/RadioBoxes.swift index 9cc6ae5f..39f03c40 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/RadioBoxes.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/RadioBoxes.swift @@ -7,21 +7,23 @@ // import Foundation -open class RadioBoxes: View { +open class RadioBoxes: View { + public let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout()) /// The models for the molecules. - var molecules: [MoleculeModelProtocol]? + var boxes: [RadioBoxModel]? + public var fieldKey: String? + public var groupName: String? + public var enabled: Bool? - - /// The height of the carousel. Default is 300. public var collectionViewHeight: NSLayoutConstraint? - - public var delegateObject: MVMCoreUIDelegateObject? + private let boxWidth: Double = 151.0 + private let boxHeight: Double = 64.0 + private let itemSpacing: Double = 10.0 + private let leadingSpacing: Double = 0 - - // MARK: - MVMCoreViewProtocol open override func setupView() { super.setupView() @@ -35,22 +37,21 @@ open class RadioBoxes: View { collectionView.backgroundColor = .clear collectionView.isAccessibilityElement = false addSubview(collectionView) - + NSLayoutConstraint.constraintPinSubview(toSuperview: collectionView) collectionViewHeight = collectionView.heightAnchor.constraint(equalToConstant: 300) collectionViewHeight?.isActive = true - NSLayoutConstraint.constraintPinSubview(toSuperview: collectionView) - + } - // MARK: - MoleculeViewProtocol public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { - self.delegateObject = delegateObject super.set(with: model, delegateObject, additionalData) guard let radioBoxesModel = model as? RadioBoxesModel else { return } - backgroundColor = .white - + backgroundColor = radioBoxesModel.backgroundColor?.uiColor + fieldKey = radioBoxesModel.fieldKey + groupName = radioBoxesModel.groupName + enabled = radioBoxesModel.enabled registerCells() setupLayout(with: radioBoxesModel) prepareMolecules(with: radioBoxesModel) @@ -59,64 +60,54 @@ open class RadioBoxes: View { // MARK: - JSON Setters /// Updates the layout being used - + func setupLayout(with carouselModel: RadioBoxesModel?) { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .vertical + layout.sectionInset = UIEdgeInsets.init(top: CGFloat(leadingSpacing), left: CGFloat(leadingSpacing), bottom: CGFloat(leadingSpacing), right: CGFloat(leadingSpacing)) layout.minimumLineSpacing = 10 layout.minimumInteritemSpacing = 10 collectionView.collectionViewLayout = layout } - + func prepareMolecules(with radioBoxesModel: RadioBoxesModel?) { guard let newMolecules = radioBoxesModel?.boxes else { - molecules = nil + boxes = nil return } - molecules = newMolecules - collectionView.reloadData() - + boxes = newMolecules + let height = Double(round(Double((boxes?.count ?? Int(0.0)))/2.0))*(boxHeight+10.0) + collectionViewHeight?.constant = CGFloat(height) + collectionViewHeight?.isActive = true } - - + /// Registers the cells with the collection view func registerCells() { collectionView.register(RadioBoxCollectionViewCell.self, forCellWithReuseIdentifier: "RadioBoxCollectionViewCell") } - - - // MARK: - Convenience - /// Returns the (identifier, class) of the molecule for the given map. - func getMoleculeInfo(with molecule: MoleculeModelProtocol, delegateObject: MVMCoreUIDelegateObject?) -> (identifier: String, class: AnyClass, molecule: MoleculeModelProtocol)? { - guard let className = MoleculeObjectMapping.shared()?.getMoleculeClass(molecule) else { - return nil - } - return (className.nameForReuse(with: molecule, delegateObject) ?? molecule.moleculeName, className, molecule) - } } extension RadioBoxes: UICollectionViewDelegateFlowLayout { open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { - let itemWidth = (collectionView.bounds.width - 30)/2 - return CGSize(width: itemWidth, height: 64) + let itemWidth = (Double(collectionView.bounds.width) - itemSpacing)/2 + return CGSize(width: CGFloat(150.5), height: CGFloat(boxHeight)) } - - } extension RadioBoxes: UICollectionViewDataSource { open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return molecules?.count ?? 0 + return boxes?.count ?? 0 } open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { - guard let molecule = molecules?[indexPath.row], let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RadioBoxCollectionViewCell", for: indexPath) as? RadioBoxCollectionViewCell else { - return UICollectionViewCell() + guard let molecule = boxes?[indexPath.row], let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RadioBoxCollectionViewCell", for: indexPath) as? RadioBoxCollectionViewCell else { + return UICollectionViewCell() } - cell.set(with: molecule, delegateObject, nil) + cell.set(with: molecule, nil, nil) (cell as? MVMCoreViewProtocol)?.updateView(collectionView.bounds.width) + cell.layoutIfNeeded() return cell } } diff --git a/MVMCoreUI/Atomic/Atoms/Views/RadioBoxesModel.swift b/MVMCoreUI/Atomic/Atoms/Views/RadioBoxesModel.swift index 80425bde..ab2b4728 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/RadioBoxesModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/RadioBoxesModel.swift @@ -24,7 +24,6 @@ import Foundation case boxes case fieldKey case groupName - } required public init(from decoder: Decoder) throws {