From 3f1e37c8783e571336ac179f8c95f388373faf19 Mon Sep 17 00:00:00 2001 From: Prateek Arora Date: Thu, 20 Feb 2020 20:45:20 +0530 Subject: [PATCH] Using stack as per review comments --- .../ListRVWheel.swift | 59 +++++++------------ .../ListRVWheelModel.swift | 11 +++- 2 files changed, 28 insertions(+), 42 deletions(-) diff --git a/MVMCoreUI/Molecules/VerticalCombinationViews/ListRVWheel.swift b/MVMCoreUI/Molecules/VerticalCombinationViews/ListRVWheel.swift index 47c77ed8..51f1133f 100644 --- a/MVMCoreUI/Molecules/VerticalCombinationViews/ListRVWheel.swift +++ b/MVMCoreUI/Molecules/VerticalCombinationViews/ListRVWheel.swift @@ -12,14 +12,13 @@ import Foundation let wheel = GraphView(frame: .zero) let leftLabel = Label(frame: .zero) let rightLabel = Label(frame: .zero) + let stack = Stack(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 diff --git a/MVMCoreUI/Molecules/VerticalCombinationViews/ListRVWheelModel.swift b/MVMCoreUI/Molecules/VerticalCombinationViews/ListRVWheelModel.swift index ec856bb1..ab5f3c2a 100644 --- a/MVMCoreUI/Molecules/VerticalCombinationViews/ListRVWheelModel.swift +++ b/MVMCoreUI/Molecules/VerticalCombinationViews/ListRVWheelModel.swift @@ -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)