Modified code based on review comments

Molecule class conforming to TableViewCell instead of Container.
Model class conforming to ListItemModelProtocol instead of MoleculeModelProtocol
Removed extra spaces and added comments
This commit is contained in:
Lekshmi S 2020-02-11 08:39:09 +05:30
parent 5686fea2c8
commit 66f0cf6b8e
2 changed files with 80 additions and 46 deletions

View File

@ -9,24 +9,40 @@
import Foundation
import UIKit
@objcMembers public class LeftVariableIconRightCaretList : Container {
@objcMembers public class LeftVariableIconRightCaretList: TableViewCell {
//-----------------------------------------------------
// MARK: - Outlets
//-------------------------------------------------------
let leftImage = MFLoadImageView()
let leftLabel = Label(frame: .zero)
let rightLabel = Label(frame: .zero)
let leftLabel = Label.commonLabelB2(true)
let rightLabel = Label.commonLabelB2(true)
let containerView = ViewConstrainingView()
//------------------------------------------------------
// MARK: - Properties
//-------------------------------------------------------
let spaceBetweenLabels : CGFloat = 180
let horizontalPadding : CGFloat = 14
let imageSize : CGFloat = 30
//------------------------------------------------------
let cellHeight: CGFloat = 79
let leftPadding: CGFloat = 35
let spaceBetweenImageAndLeftLabel: CGFloat = 16
let imageSize: CGFloat = 30
let spaceBetweenLabels: CGFloat = 40
let rightPadding: CGFloat = 54
//------------------------------------------------------
// MARK: - Initialization
//--------------------------------------------------------
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupView()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
//-----------------------------------------------------
// MARK: - View Lifecycle
@ -34,10 +50,10 @@ import UIKit
open override func updateView(_ size: CGFloat) {
super.updateView(size)
containerView.updateView(size)
leftImage.updateView(size)
leftLabel.updateView(size)
rightLabel.updateView(size)
}
override open func setupView() {
@ -45,30 +61,36 @@ import UIKit
guard leftImage.superview == nil else {
return
}
containerView.translatesAutoresizingMaskIntoConstraints = false
contentView.heightAnchor.constraint(equalToConstant: cellHeight).isActive = true
contentView.addSubview(containerView)
let container = MVMCoreUICommonViewsUtility.commonView()
//containerView constraints
containerView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
containerView.topAnchor.constraint(equalTo: topAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
addAndContain(container)
container.addSubview(leftImage)
container.addSubview(leftLabel)
container.addSubview(rightLabel)
containerView.addSubview(leftImage)
containerView.addSubview(leftLabel)
containerView.addSubview(rightLabel)
self.translatesAutoresizingMaskIntoConstraints = false
leftImage.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
leftImage.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
leftImage.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
//leftImage constraints
leftImage.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: leftPadding).isActive = true
leftImage.centerYAnchor.constraint(equalTo: leftLabel.centerYAnchor).isActive = true
leftImage.widthAnchor.constraint(equalToConstant: imageSize).isActive = true
leftImage.heightAnchor.constraint(equalToConstant: imageSize).isActive = true
leftLabel.leadingAnchor.constraint(equalTo: leftImage.trailingAnchor, constant: horizontalPadding).isActive = true
leftLabel.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
leftLabel.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
//leftLabel constraints
leftLabel.leadingAnchor.constraint(equalTo: leftImage.trailingAnchor, constant: spaceBetweenImageAndLeftLabel).isActive = true
leftLabel.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
leftLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
rightLabel.leadingAnchor.constraint(equalTo: leftLabel.trailingAnchor,constant: spaceBetweenLabels).isActive = true
rightLabel.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
rightLabel.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
//rightLabel constraints
rightLabel.leadingAnchor.constraint(greaterThanOrEqualTo: leftLabel.trailingAnchor, constant: spaceBetweenLabels).isActive = true
rightLabel.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
rightLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo: rightLabel.trailingAnchor,constant: rightPadding).isActive = true
}
//----------------------------------------------------
@ -77,17 +99,14 @@ import UIKit
override open func reset() {
super.reset()
leftImage.reset()
leftLabel.reset()
rightLabel.reset()
}
public override func setWithModel(_ model: MoleculeModelProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
super.setWithModel(model, delegateObject, additionalData)
guard let model = model as? LeftVariableIconRightCaretListModel else { return}
leftImage.setWithModel(model.image, delegateObject, additionalData)
leftLabel.setWithModel(model.leftLabel, delegateObject, additionalData)
rightLabel.setWithModel(model.rightLabel, delegateObject, additionalData)

View File

@ -8,31 +8,38 @@
import Foundation
public class LeftVariableIconRightCaretListModel: MoleculeModelProtocol {
public class LeftVariableIconRightCaretListModel: ContainerModel, ListItemModelProtocol {
public var horizontalAlignment: UIStackView.Alignment?
public var verticalAlignment: UIStackView.Alignment?
public var useHorizontalMargins: Bool?
public var useVerticalMargins: Bool?
public var topMarginPadding: CGFloat?
public var bottomMarginPadding: CGFloat?
public var line: LineModel? = LineModel(type: .standard)
public var hideArrow: Bool?
public var line: LineModel?
public var hideArrow: Bool? = false
public var backgroundColor: Color?
public var action: ActionModelProtocol?
public static var identifier: String = "listLVImg"
public var image: ImageViewModel
public var leftLabel: LabelModel
public var rightLabel: LabelModel
func setDefaults() {
if useHorizontalMargins == nil {
useHorizontalMargins = true
}
if useVerticalMargins == nil {
useVerticalMargins = true
}
if topMarginPadding == nil {
topMarginPadding = 24
}
if bottomMarginPadding == nil {
bottomMarginPadding = 0
}
}
public init(image: ImageViewModel, leftLabel: LabelModel, rightLabel: LabelModel) {
self.image = image
self.leftLabel = leftLabel
self.rightLabel = rightLabel
super.init()
setDefaults()
}
private enum CodingKeys: String, CodingKey {
@ -40,6 +47,8 @@ public class LeftVariableIconRightCaretListModel: MoleculeModelProtocol {
case leftLabel
case rightLabel
case image
case action
case hideArrow
}
required public init(from decoder: Decoder) throws {
@ -47,16 +56,22 @@ public class LeftVariableIconRightCaretListModel: MoleculeModelProtocol {
leftLabel = try typeContainer.decode(LabelModel.self, forKey: .leftLabel)
rightLabel = try typeContainer.decode(LabelModel.self, forKey: .rightLabel)
image = try typeContainer.decode(ImageViewModel.self, forKey: .image)
action = try typeContainer.decodeModelIfPresent(codingKey: .action, typeCodingKey: ActionCodingKey.actionType)
hideArrow = try typeContainer.decodeIfPresent(Bool.self, forKey: .hideArrow)
try super.init(from: decoder)
setDefaults()
}
public func encode(to encoder: Encoder) throws {
public override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(LeftVariableIconRightCaretListModel.identifier, forKey: .moleculeName)
try container.encode(leftLabel, forKey: .leftLabel)
try container.encode(rightLabel, forKey: .rightLabel)
try container.encodeIfPresent(image, forKey: .image)
try container.encode(image, forKey: .image)
try container.encodeModelIfPresent(action, forKey: .action)
try container.encodeIfPresent(hideArrow, forKey: .hideArrow)
}
}