Using stack as per review comments

This commit is contained in:
Prateek Arora 2020-02-20 20:45:20 +05:30
parent 22285dda62
commit 3f1e37c878
2 changed files with 28 additions and 42 deletions

View File

@ -12,14 +12,13 @@ import Foundation
let wheel = GraphView(frame: .zero)
let leftLabel = Label(frame: .zero)
let rightLabel = Label(frame: .zero)
let stack = Stack<StackModel>(frame: .zero)
//-------------------------------------------------
// MARK: - View Cycle
//-------------------------------------------------
open override func updateView(_ size: CGFloat) {
super.updateView(size)
wheel.updateView(size)
leftLabel.updateView(size)
rightLabel.updateView(size)
stack.updateView(size)
}
//-------------------------------------------------
@ -27,39 +26,10 @@ import Foundation
//-------------------------------------------------
open override func setupView() {
super.setupView()
guard leftLabel.superview == nil else {
return
}
contentView.addSubview(wheel)
contentView.addSubview(leftLabel)
contentView.addSubview(rightLabel)
NSLayoutConstraint.constraintPinSubview(toSuperview: contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
//-------------------------------------------------
// MARK: - Constraining
//-------------------------------------------------
self.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
leftLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
leftLabel.topAnchor.constraint(equalTo: contentView.topAnchor),
leftLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
leftLabel.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.3)
])
NSLayoutConstraint.activate([
wheel.topAnchor.constraint(equalTo: contentView.topAnchor),
wheel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
])
let rightLabelTrailing = rightLabel.trailingAnchor.constraint(greaterThanOrEqualTo: contentView.trailingAnchor)
rightLabelTrailing.priority = UILayoutPriority(900)
NSLayoutConstraint.activate([
rightLabelTrailing,
rightLabel.leadingAnchor.constraint(equalTo: wheel.trailingAnchor),
rightLabel.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.32),
rightLabel.topAnchor.constraint(equalTo: contentView.topAnchor),
rightLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
])
stack.translatesAutoresizingMaskIntoConstraints = false
stack.stackItems = [StackItem(andContain: leftLabel),StackItem(andContain: wheel),StackItem(andContain: rightLabel)]
contentView.addSubview(stack)
containerHelper.constrainView(stack)
}
//-------------------------------------------------
// MARK: - MVMCoreUIMoleculeViewProtocol
@ -70,6 +40,19 @@ import Foundation
leftLabel.setWithModel(model.leftLabel, delegateObject, additionalData)
rightLabel.setWithModel(model.rightLabel, delegateObject, additionalData)
wheel.setWithModel(model.wheel, delegateObject, additionalData)
// Create a stack model to use for the internal stack and set the alignment of models
let leftHeadlineBodyAlignment = StackItemModel(percent: 55)
leftHeadlineBodyAlignment.horizontalAlignment = .leading
let centerHeadLineBodyAlignment = StackItemModel(percent: 20)
centerHeadLineBodyAlignment.horizontalAlignment = .trailing
let rightHeadLineBodyAlignment = StackItemModel(percent: 25)
rightHeadLineBodyAlignment.horizontalAlignment = .trailing
let stackModel = StackModel(molecules: [leftHeadlineBodyAlignment,centerHeadLineBodyAlignment,rightHeadLineBodyAlignment])
stackModel.axis = .horizontal
stack.model = stackModel
stack.restack()
}
//-------------------------------------------------
@ -77,9 +60,7 @@ import Foundation
//-------------------------------------------------
open override func reset() {
super.reset()
leftLabel.reset()
rightLabel.reset()
wheel.reset()
stack.reset()
}
public override class func estimatedHeight(forRow molecule: MoleculeModelProtocol?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return 43

View File

@ -7,8 +7,7 @@
//
import Foundation
public class ListRVWheelModel:MoleculeModelProtocol {
public var backgroundColor: Color?
public class ListRVWheelModel:ListItemModel,MoleculeModelProtocol {
public static var identifier: String = "listRVWheel"
public var leftLabel: LabelModel?
public var rightLabel: LabelModel?
@ -17,20 +16,26 @@ public class ListRVWheelModel:MoleculeModelProtocol {
self.leftLabel = leftLabel
self.rightLabel = rightLabel
self.wheel = wheel
super.init()
}
private enum CodingKeys: String,CodingKey {
case moleculeName
case leftLabel
case rightLabel
case wheel
}
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
leftLabel = try typeContainer.decode(LabelModel.self, forKey: .leftLabel)
rightLabel = try typeContainer.decode(LabelModel.self, forKey: .rightLabel)
wheel = try typeContainer.decode(CircleProgressModel.self, forKey: .wheel)
try super.init(from: decoder)
}
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(ListRVWheelModel.identifier, forKey: .moleculeName)
try container.encode(leftLabel, forKey: .leftLabel)