fixed box sizes issues

This commit is contained in:
Damodaram 2020-04-07 07:51:44 +05:30
parent 96115b7a5d
commit ade0e043a4
2 changed files with 61 additions and 11 deletions

View File

@ -14,11 +14,14 @@ open class RadioBoxCollectionViewCell: UICollectionViewCell, MoleculeViewProtoco
var bottomView = MVMCoreUICommonViewsUtility.commonView()
public var lineViewHeight: NSLayoutConstraint?
public var fieldValue: String?
open override var isSelected: Bool{
didSet{
self.lineViewHeight?.constant = self.isSelected ? 4.0 : 0
UIView.animate(withDuration: 0.5) {
self.layoutIfNeeded()
}
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)
@ -30,15 +33,23 @@ open class RadioBoxCollectionViewCell: UICollectionViewCell, MoleculeViewProtoco
}
}
}
var isOutofStock: Bool = false{
didSet{
DispatchQueue.main.async { [weak self] in
guard let strongSelf = self else {
return
}
strongSelf.bottomView.layer.sublayers?.filter({$0.name == "outofstock"}).forEach({$0.removeFromSuperlayer()})
if(strongSelf.isOutofStock){
strongSelf.addOutofstockLine()
}
}
}
}
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()
@ -53,6 +64,7 @@ open class RadioBoxCollectionViewCell: UICollectionViewCell, MoleculeViewProtoco
insetsLayoutMarginsFromSafeArea = false
contentView.insetsLayoutMarginsFromSafeArea = false
contentView.preservesSuperviewLayoutMargins = false
bottomView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(bottomView)
NSLayoutConstraint.constraintPinSubview(toSuperview: bottomView)
@ -70,25 +82,51 @@ open class RadioBoxCollectionViewCell: UICollectionViewCell, MoleculeViewProtoco
bodyLabel.bottomAnchor.constraint(lessThanOrEqualTo: bottomView.bottomAnchor, constant: -12.0).isActive = true
}
public func updateView(_ size: CGFloat) {
self.setNeedsLayout()
self.bottomView.setNeedsLayout()
self.bottomView.layer.sublayers?.filter({$0.name == "border"}).forEach({$0.removeFromSuperlayer()})
DispatchQueue.main.async { [weak self] in
self?.addBordertoView()
}
}
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)
}
private func addOutofstockLine(){
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: bottomView.bounds.height))
path.addLine(to: CGPoint(x: bottomView.bounds.width, y: 0))
let shapeLayer = CAShapeLayer()
shapeLayer.name = "outofstock"
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.darkGray.cgColor
shapeLayer.lineWidth = 0.5
bottomView.layer.addSublayer(shapeLayer)
}
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
bottomView.backgroundColor = backgroundColor.uiColor
}
lineView.backgroundColor = collectionModel.selectedAccentColor?.uiColor
bodyLabel.text = collectionModel.text
isSelected = collectionModel.selected ?? false
fieldValue = collectionModel.fieldValue
isOutofStock = collectionModel.strikethrough ?? false
}
}
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {

View File

@ -57,11 +57,23 @@ open class RadioBoxes: View {
prepareMolecules(with: radioBoxesModel)
collectionView.reloadData()
}
@objc override open func updateView(_ size: CGFloat) {
collectionView.collectionViewLayout.invalidateLayout()
DispatchQueue.main.async { [weak self] in
self?.setNeedsDisplay()
self?.collectionView.layoutIfNeeded()
self?.collectionView.reloadData()
}
}
// MARK: - JSON Setters
/// Updates the layout being used
func setupLayout(with carouselModel: RadioBoxesModel?) {
func setupLayout(with radioBoxesModel: RadioBoxesModel?) {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.sectionInset = UIEdgeInsets.init(top: CGFloat(leadingSpacing), left: CGFloat(leadingSpacing), bottom: CGFloat(leadingSpacing), right: CGFloat(leadingSpacing))
@ -92,7 +104,7 @@ open class RadioBoxes: View {
extension RadioBoxes: UICollectionViewDelegateFlowLayout {
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemWidth = (Double(collectionView.bounds.width) - itemSpacing)/2
return CGSize(width: CGFloat(150.5), height: CGFloat(boxHeight))
return CGSize(width: CGFloat(itemWidth), height: CGFloat(boxHeight))
}
}
@ -106,7 +118,7 @@ extension RadioBoxes: UICollectionViewDataSource {
return UICollectionViewCell()
}
cell.set(with: molecule, nil, nil)
(cell as? MVMCoreViewProtocol)?.updateView(collectionView.bounds.width)
cell.updateView(collectionView.bounds.width)
cell.layoutIfNeeded()
return cell
}