mvm_core_ui/MVMCoreUI/Molecules/LabelRightMoleculesStack.swift
Murugan, Vimal 30d864749a unordered list added
numbered list added
2020-01-11 10:30:40 +05:30

200 lines
8.1 KiB
Swift

//
// ListMoleculeContainer.swift
// MVMCoreUI
//
// Created by Murugan, Vimal on 03/01/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
import UIKit
open class LabelRightMoleculesStack: MoleculeStackView {
var orderedListModel: OrderListProtocol?
public override func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [String : AnyHashable]?) {
let previousModel = self.orderedListModel
//Remove previously drawn views
removeAllItemViews()
guard let orderedListModel = model as? OrderListProtocol else {
return
}
var items: [StackItem]?
if previousModel?.list.count == orderedListModel.list.count {
items = stackItems
}
stackItems = []
self.model = MoleculeStackModel(molecules: [])
for (index, labelModel) in orderedListModel.list.enumerated() {
let labelContainer = items?[index].view as? LeftLabelRightMoleculeContainer ?? LeftLabelRightMoleculeContainer()
labelContainer.leftText = leftTextForIndex(index)
labelContainer.setWithModel(labelModel, delegateObject, additionalData)
addView(labelContainer, lastItem: index == orderedListModel.list.count - 1)
}
self.orderedListModel = orderedListModel
restack()
}
//MARK: - Subclass should overirde this
func leftTextForIndex(_ index: Int) -> String {
return ""
}
}
class LeftLabelRightMoleculeContainer: View {
var label = Label.commonLabelB2(true)
var rightContainer = MVMCoreUICommonViewsUtility.commonView()
var leftContainer = MVMCoreUICommonViewsUtility.commonView()
var rightMoleculeName: String?
var rightMolecule: View?
var leftText: String?
var leftWidthConstraint: NSLayoutConstraint?
var percentage: CGFloat = 5
var constraintBtwViews: NSLayoutConstraint?
var spaceBtwViews: CGFloat = 0 {
didSet {
if spaceBtwViews != oldValue {
constraintBtwViews?.constant = spaceBtwViews
setNeedsDisplay()
}
}
}
// MARK: - Inits
public override init() {
super.init()
}
public override init(frame: CGRect) {
super.init(frame: frame)
}
public init(withJSON json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
super.init(frame: CGRect.zero)
setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setupView() {
super.setupView()
guard rightContainer.superview == nil else {
return
}
translatesAutoresizingMaskIntoConstraints = false
addSubview(rightContainer)
addSubview(leftContainer)
leftContainer.addSubview(label)
NSLayoutConstraint.constraintPinSubview(label, pinTop: true, pinBottom: false, pinLeft: true, pinRight: false)
leftContainer.rightAnchor.constraint(greaterThanOrEqualTo: label.rightAnchor).isActive = true
leftContainer.bottomAnchor.constraint(greaterThanOrEqualTo: label.bottomAnchor).isActive = true
let lowRightConstraint = leftContainer.rightAnchor.constraint(equalTo: label.rightAnchor)
lowRightConstraint.priority = UILayoutPriority(rawValue: 200)
lowRightConstraint.isActive = true
let lowBottomConstraint = leftContainer.bottomAnchor.constraint(equalTo: label.bottomAnchor)
lowBottomConstraint.priority = UILayoutPriority(rawValue: 200)
lowBottomConstraint.isActive = true
NSLayoutConstraint.constraintPinSubview(leftContainer, pinTop: true, pinBottom: false, pinLeft: true, pinRight: false)
bottomAnchor.constraint(greaterThanOrEqualTo: leftContainer.bottomAnchor).isActive = true
NSLayoutConstraint.constraintPinSubview(rightContainer, pinTop: true, pinBottom: true, pinLeft: false, pinRight: true)
constraintBtwViews = rightContainer.leftAnchor.constraint(equalTo: leftContainer.rightAnchor, constant: spaceBtwViews)
constraintBtwViews?.priority = .required
constraintBtwViews?.isActive = true
setContentHuggingPriority(.defaultHigh, for: .vertical)
setContentHuggingPriority(.defaultHigh, for: .horizontal)
rightContainer.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
rightContainer.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
leftContainer.setContentHuggingPriority(.required, for: .horizontal)
leftContainer.setContentHuggingPriority(.required, for: .vertical)
updateLeftViewWidthConstraint(percentage)
}
override func updateView(_ size: CGFloat) {
super.updateView(size)
rightMolecule?.updateView(size)
label.updateView(size)
updateLeftViewWidthConstraint(percentage)
setNeedsDisplay()
}
override func reset() {
super.reset()
rightMolecule?.reset()
}
override func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [String : AnyHashable]?) {
let previousMoleculeName = model?.moleculeName
super.setWithModel(model, delegateObject, additionalData)
removeSubviewsInRightContainer()
guard let labelModel = model as? LabelModel else {
return
}
label.text = leftText
rightMoleculeName = labelModel.moleculeName
//For reuse purpose check that allready added molecule is same
if let rightMolecule = self.rightMolecule, previousMoleculeName == rightMoleculeName {
rightMolecule.setWithModel(labelModel, delegateObject, additionalData)
addView(rightMolecule)
} else {
if let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(labelModel, delegateObject, false) {
addView(molecule)
}
}
}
override func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
let previousMoleculeName = rightMoleculeName
removeSubviewsInRightContainer()
guard let moleculeJSON = json, let _ = moleculeJSON.optionalStringForKey(KeyMoleculeName) else {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
return
}
label.text = leftText
rightMoleculeName = moleculeJSON.optionalStringForKey(KeyMoleculeName)
//For reuse purpose check that allready added molecule is same
if let rightMolecule = self.rightMolecule, previousMoleculeName == rightMoleculeName {
rightMolecule.setWithJSON(moleculeJSON, delegateObject: delegateObject, additionalData: additionalData)
addView(rightMolecule)
} else {
if let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(forJSON: moleculeJSON, delegateObject: delegateObject, constrainIfNeeded: false) {
addView(molecule)
}
}
}
func updateLeftViewWidthConstraint(_ percent: CGFloat) {
percentage = percent
leftWidthConstraint?.isActive = false
leftWidthConstraint = leftContainer.widthAnchor.constraint(equalTo: widthAnchor, multiplier: CGFloat(percent/100), constant: 0)
leftWidthConstraint?.isActive = true
}
func removeSubviewsInRightContainer() {
rightContainer.subviews.forEach({ $0.removeFromSuperview() })
}
func addView(_ view: UIView) {
view.translatesAutoresizingMaskIntoConstraints = false
rightContainer.addSubview(view)
NSLayoutConstraint.constraintPinSubview(view, pinTop: true, pinBottom: true, pinLeft: true, pinRight: true)
rightMolecule = view as? View
}
}