Merge branch 'feature/coding' of gitlab.verizon.com:BPHV_MIPS/mvm_core_ui into feature/coding

This commit is contained in:
Suresh, Kamlesh 2020-01-14 15:52:16 -05:00
commit 5560084c1d
10 changed files with 155 additions and 69 deletions

View File

@ -352,6 +352,45 @@
} }
- (void)setWithJSON:(NSDictionary *)json delegateObject:(MVMCoreUIDelegateObject *)delegateObject additionalData:(NSDictionary *)additionalData { - (void)setWithJSON:(NSDictionary *)json delegateObject:(MVMCoreUIDelegateObject *)delegateObject additionalData:(NSDictionary *)additionalData {
// Only treated as a container if we are constraining a molecule.
if (!self.constrainedView) {
[super setWithJSON:json delegateObject:delegateObject additionalData:additionalData];
}
if (self.shouldSetupMoleculeFromJSON) {
NSDictionary *moleculeJSON = [json dict:KeyMolecule];
if (self.molecule) {
[self.molecule setWithJSON:moleculeJSON delegateObject:delegateObject additionalData:additionalData];
} else if (moleculeJSON) {
UIView <MVMCoreUIMoleculeViewProtocol>*molecule = [[MVMCoreUIMoleculeMappingObject sharedMappingObject] createMoleculeForJSON:moleculeJSON delegateObject:delegateObject constrainIfNeeded:true];
if (molecule) {
[self addMolecule:molecule];
}
self.molecule = molecule;
[self setMoleculeAccessibility];
}
} else {
[self.molecule setWithJSON:json delegateObject:delegateObject additionalData:additionalData];
}
NSNumber *useHorizontalMargins = [json optionalNumberForKey:@"useHorizontalMargins"];
if (useHorizontalMargins) {
self.updateViewHorizontalDefaults = [useHorizontalMargins boolValue];
}
NSNumber *useVerticalMargins = [json optionalNumberForKey:@"useVerticalMargins"];
if (useVerticalMargins) {
self.updateViewVerticalDefaults = [useVerticalMargins boolValue];
}
// Set the alignment for the stack in the containing view. The json driven value is for the axis direction alignment.
NSString *alignment = [json string:@"horizontalAlignment"];
if (alignment) {
[self alignHorizontal:[ViewConstrainingView getAlignmentForString:alignment defaultAlignment:UIStackViewAlignmentFill]];
}
alignment = [json string:@"verticalAlignment"];
if (alignment) {
[self alignVertical:[ViewConstrainingView getAlignmentForString:alignment defaultAlignment:UIStackViewAlignmentFill]];
}
if ([self.molecule respondsToSelector:@selector(copyBackgroundColor)] && [self.molecule performSelector:@selector(copyBackgroundColor)]) { if ([self.molecule respondsToSelector:@selector(copyBackgroundColor)] && [self.molecule performSelector:@selector(copyBackgroundColor)]) {
self.backgroundColor = self.molecule.backgroundColor; self.backgroundColor = self.molecule.backgroundColor;
} }

View File

@ -61,6 +61,7 @@ extension View: MVMCoreViewProtocol {
open func setupView() { open func setupView() {
translatesAutoresizingMaskIntoConstraints = false translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false insetsLayoutMarginsFromSafeArea = false
MVMCoreUIUtility.setMarginsFor(self, leading: 0, top: 0, trailing: 0, bottom: 0)
} }
} }

View File

@ -17,10 +17,32 @@ import Foundation
case backgroundColor case backgroundColor
} }
/// Defaults to set
func setDefaults() {
if useHorizontalMargins == nil {
useHorizontalMargins = true
}
if useVerticalMargins == nil {
useVerticalMargins = true
}
if topMarginPadding == nil {
topMarginPadding = PaddingDefaultVerticalSpacing
}
if bottomMarginPadding == nil {
bottomMarginPadding = PaddingDefaultVerticalSpacing
}
}
public override init(with moleculeModel: MoleculeProtocol) {
super.init(with: moleculeModel)
setDefaults()
}
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: FooterCodingKeys.self) let typeContainer = try decoder.container(keyedBy: FooterCodingKeys.self)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
try super.init(from: decoder) try super.init(from: decoder)
setDefaults()
} }
public override func encode(to encoder: Encoder) throws { public override func encode(to encoder: Encoder) throws {

View File

@ -19,18 +19,33 @@ import Foundation
case backgroundColor case backgroundColor
} }
required public init(from decoder: Decoder) throws { /// Defaults to set
try super.init(from: decoder) func setDefaults() {
let typeContainer = try decoder.container(keyedBy: HeaderCodingKeys.self) if useHorizontalMargins == nil {
line = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line) useHorizontalMargins = true
}
// Default Values if useVerticalMargins == nil {
useVerticalMargins = true
}
if topMarginPadding == nil { if topMarginPadding == nil {
topMarginPadding = PaddingDefaultVerticalSpacing topMarginPadding = PaddingDefaultVerticalSpacing
} }
if bottomMarginPadding == nil { if bottomMarginPadding == nil {
bottomMarginPadding = PaddingDefaultVerticalSpacing bottomMarginPadding = PaddingDefaultVerticalSpacing
} }
line?.type = .heavy
}
public override init(with moleculeModel: MoleculeProtocol) {
super.init(with: moleculeModel)
setDefaults()
}
required public init(from decoder: Decoder) throws {
try super.init(from: decoder)
let typeContainer = try decoder.container(keyedBy: HeaderCodingKeys.self)
line = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line)
setDefaults()
} }
public override func encode(to encoder: Encoder) throws { public override func encode(to encoder: Encoder) throws {

View File

@ -25,6 +25,28 @@ import MVMCore
case style case style
} }
/// Defaults to set
func setDefaults() {
style = "standard"
if useHorizontalMargins == nil {
useHorizontalMargins = true
}
if useVerticalMargins == nil {
useVerticalMargins = true
}
if topMarginPadding == nil {
topMarginPadding = 24
}
if bottomMarginPadding == nil {
bottomMarginPadding = 24
}
}
public override init(with moleculeModel: MoleculeProtocol) {
super.init(with: moleculeModel)
setDefaults()
}
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: ListItemCodingKeys.self) let typeContainer = try decoder.container(keyedBy: ListItemCodingKeys.self)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
@ -34,15 +56,8 @@ import MVMCore
if let style = try typeContainer.decodeIfPresent(String.self, forKey: .style) { if let style = try typeContainer.decodeIfPresent(String.self, forKey: .style) {
self.style = style self.style = style
} }
try super.init(from: decoder) try super.init(from: decoder)
setDefaults()
if useHorizontalMargins == nil {
useHorizontalMargins = true
}
if useVerticalMargins == nil {
useVerticalMargins = true
}
} }
public override func encode(to encoder: Encoder) throws { public override func encode(to encoder: Encoder) throws {

View File

@ -13,13 +13,12 @@ import UIKit
// MARK: - MVMCoreUIMoleculeViewProtocol // MARK: - MVMCoreUIMoleculeViewProtocol
public override func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { public override func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
super.setWithModel(model, delegateObject, additionalData) super.setWithModel(model, delegateObject, additionalData)
guard let moleculeModel = (model as? ListItemModel)?.molecule else { return }
guard let model = model, if molecule != nil {
let moleculeModel = (model as? ListItemModel)?.molecule, (molecule as? ModelMoleculeViewProtocol)?.setWithModel(moleculeModel, delegateObject, additionalData)
let moleculeView = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(moleculeModel, delegateObject, true) else { } else if let moleculeView = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(moleculeModel, delegateObject, true) {
return addMolecule(moleculeView)
} }
addMolecule(moleculeView)
} }
public override class func nameForReuse(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?) -> String? { public override class func nameForReuse(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?) -> String? {

View File

@ -12,12 +12,12 @@ import Foundation
public static var identifier: String = "stackItem" public static var identifier: String = "stackItem"
public var backgroundColor: Color? public var backgroundColor: Color?
public var spacing: CGFloat? public var spacing: CGFloat?
public var percentage: Int? = 0 public var percent: Int?
public var gone: Bool = false public var gone: Bool = false
enum MoleculeStackItemCodingKeys: String, CodingKey { enum MoleculeStackItemCodingKeys: String, CodingKey {
case spacing case spacing
case percentage case percent
case gone case gone
} }
@ -28,7 +28,7 @@ import Foundation
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: MoleculeStackItemCodingKeys.self) let typeContainer = try decoder.container(keyedBy: MoleculeStackItemCodingKeys.self)
spacing = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .spacing) spacing = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .spacing)
percentage = try typeContainer.decodeIfPresent(Int.self, forKey: .percentage) percent = try typeContainer.decodeIfPresent(Int.self, forKey: .percent)
if let gone = try typeContainer.decodeIfPresent(Bool.self, forKey: .gone) { if let gone = try typeContainer.decodeIfPresent(Bool.self, forKey: .gone) {
self.gone = gone self.gone = gone
} }
@ -39,7 +39,7 @@ import Foundation
try super.encode(to: encoder) try super.encode(to: encoder)
var container = encoder.container(keyedBy: MoleculeStackItemCodingKeys.self) var container = encoder.container(keyedBy: MoleculeStackItemCodingKeys.self)
try container.encodeIfPresent(spacing, forKey: .spacing) try container.encodeIfPresent(spacing, forKey: .spacing)
try container.encodeIfPresent(percentage, forKey: .percentage) try container.encodeIfPresent(percent, forKey: .percent)
try container.encode(gone, forKey: .gone) try container.encode(gone, forKey: .gone)
} }
} }

View File

@ -8,8 +8,8 @@
import UIKit import UIKit
@objcMembers public class CornerLabels: ViewConstrainingView { @objcMembers public class CornerLabels: View {
var middleView: UIView?
let topLeftLabel = Label.commonLabelB1(true) let topLeftLabel = Label.commonLabelB1(true)
let topRightLabel = Label.commonLabelB1(true) let topRightLabel = Label.commonLabelB1(true)
let bottomLeftLabel = Label.commonLabelB3(true) let bottomLeftLabel = Label.commonLabelB3(true)
@ -38,18 +38,19 @@ import UIKit
var topLabelToMoleculeConstraint: NSLayoutConstraint? var topLabelToMoleculeConstraint: NSLayoutConstraint?
var bottomLabelToMoleculeConstraint: NSLayoutConstraint? var bottomLabelToMoleculeConstraint: NSLayoutConstraint?
public override func addMolecule(_ molecule: UIView) { public func addMiddleView(_ view: UIView) {
insertSubview(molecule, at: 0) insertSubview(view, at: 0)
topLabelToMoleculeConstraint?.isActive = false topLabelToMoleculeConstraint?.isActive = false
bottomLabelToMoleculeConstraint?.isActive = false bottomLabelToMoleculeConstraint?.isActive = false
molecule.leftAnchor.constraint(equalTo: layoutMarginsGuide.leftAnchor).isActive = true view.leftAnchor.constraint(equalTo: layoutMarginsGuide.leftAnchor).isActive = true
layoutMarginsGuide.rightAnchor.constraint(equalTo: molecule.rightAnchor).isActive = true layoutMarginsGuide.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
topLabelToMoleculeConstraint = molecule.topAnchor.constraint(equalTo: topLabelsView.bottomAnchor, constant: spaceAboveMolecule) topLabelToMoleculeConstraint = view.topAnchor.constraint(equalTo: topLabelsView.bottomAnchor, constant: spaceAboveMolecule)
topLabelToMoleculeConstraint?.isActive = true topLabelToMoleculeConstraint?.isActive = true
bottomLabelToMoleculeConstraint = bottomLabelsView.topAnchor.constraint(equalTo: molecule.bottomAnchor, constant: spaceBelowMolecule) bottomLabelToMoleculeConstraint = bottomLabelsView.topAnchor.constraint(equalTo: view.bottomAnchor, constant: spaceBelowMolecule)
bottomLabelToMoleculeConstraint?.isActive = true bottomLabelToMoleculeConstraint?.isActive = true
self.middleView = view
} }
// MARK: - MVMCoreViewProtocol // MARK: - MVMCoreViewProtocol
@ -59,12 +60,11 @@ import UIKit
topRightLabel.updateView(size) topRightLabel.updateView(size)
bottomLeftLabel.updateView(size) bottomLeftLabel.updateView(size)
bottomRightLabel.updateView(size) bottomRightLabel.updateView(size)
(middleView as? MVMCoreViewProtocol)?.updateView(size)
} }
public override func setupView() { public override func setupView() {
super.setupView() super.setupView()
shouldSetupMoleculeFromJSON = true
guard topLeftLabel.superview == nil else { guard topLeftLabel.superview == nil else {
return return
} }
@ -141,17 +141,6 @@ import UIKit
} }
// MARK: - MVMCoreUIMoleculeViewProtocol // MARK: - MVMCoreUIMoleculeViewProtocol
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
topLeftLabel.setWithJSON(json?.optionalDictionaryForKey("topLeftLabel"), delegateObject: delegateObject, additionalData: additionalData)
topRightLabel.setWithJSON(json?.optionalDictionaryForKey("topRightLabel"), delegateObject: delegateObject, additionalData: additionalData)
bottomLeftLabel.setWithJSON(json?.optionalDictionaryForKey("bottomLeftLabel"), delegateObject: delegateObject, additionalData: additionalData)
bottomRightLabel.setWithJSON(json?.optionalDictionaryForKey("bottomRightLabel"), delegateObject: delegateObject, additionalData: additionalData)
topLabelToMoleculeConstraint?.constant = (molecule != nil && (topLeftLabel.hasText || topRightLabel.hasText)) ? spaceAboveMolecule : 0
bottomLabelToMoleculeConstraint?.constant = (molecule != nil && (bottomLeftLabel.hasText || bottomRightLabel.hasText)) ? spaceBelowMolecule : 0
}
public override func setAsMolecule() { public override func setAsMolecule() {
super.setAsMolecule() super.setAsMolecule()
styleDefault() styleDefault()
@ -163,8 +152,7 @@ import UIKit
styleDefault() styleDefault()
spaceAboveMolecule = 6.0 spaceAboveMolecule = 6.0
spaceBelowMolecule = 6.0 spaceBelowMolecule = 6.0
(middleView as? MoleculeViewProtocol)?.reset?()
molecule?.reset?()
} }
func styleDefault() { func styleDefault() {
@ -174,25 +162,27 @@ import UIKit
bottomRightLabel.styleB3(true) bottomRightLabel.styleB3(true)
} }
public override class func estimatedHeight(forRow json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat { public class func estimatedHeight(forRow json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat {
return 34 return 34
} }
}
extension CornerLabels: MoleculeViewProtocol { public override func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
public func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { super.setWithModel(model, delegateObject, additionalData)
guard let model = model as? CornerLabelsModel, guard let model = model as? CornerLabelsModel else { return }
let data = try? model.encode(using: JSONEncoder()), if middleView != nil {
let json = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.init()) as? [AnyHashable: Any] else { (middleView as? ModelMoleculeViewProtocol)?.setWithModel(model, delegateObject, additionalData)
return } else {
if let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(model.molecule, delegateObject) {
addMiddleView(molecule)
}
} }
self.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
// topLeftLabel.setWithModel(model.topLeftLabel, delegateObject, additionalData) topLeftLabel.setWithModel(model.topLeftLabel, delegateObject, additionalData)
// topRightLabel.setWithModel(model.topRightLabel, delegateObject, additionalData) topRightLabel.setWithModel(model.topRightLabel, delegateObject, additionalData)
// bottomLeftLabel.setWithModel(model.bottomLeftLabel, delegateObject, additionalData) bottomLeftLabel.setWithModel(model.bottomLeftLabel, delegateObject, additionalData)
// bottomRightLabel.setWithModel(model.bottomRightLabel, delegateObject, additionalData) bottomRightLabel.setWithModel(model.bottomRightLabel, delegateObject, additionalData)
//
// topLabelToMoleculeConstraint?.constant = (molecule != nil && (topLeftLabel.hasText || topRightLabel.hasText)) ? spaceAboveMolecule : 0 topLabelToMoleculeConstraint?.constant = (middleView != nil && (topLeftLabel.hasText || topRightLabel.hasText)) ? spaceAboveMolecule : 0
// bottomLabelToMoleculeConstraint?.constant = (molecule != nil && (bottomLeftLabel.hasText || bottomRightLabel.hasText)) ? spaceBelowMolecule : 0 bottomLabelToMoleculeConstraint?.constant = (middleView != nil && (bottomLeftLabel.hasText || bottomRightLabel.hasText)) ? spaceBelowMolecule : 0
} }
} }

View File

@ -10,6 +10,11 @@ import UIKit
open class MoleculeContainer: Container { open class MoleculeContainer: Container {
/// Can be overriden to change how the molecule is added to the hierarchy.
public func addMolecule(_ molecule: UIView) {
addAndContain(molecule)
}
override public func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) { override public func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
guard let moleculeJSON = json?.optionalDictionaryForKey(KeyMolecule) else { guard let moleculeJSON = json?.optionalDictionaryForKey(KeyMolecule) else {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
@ -31,7 +36,7 @@ open class MoleculeContainer: Container {
(view as? ModelMoleculeViewProtocol)?.setWithModel(casteModel.molecule, delegateObject, additionalData) (view as? ModelMoleculeViewProtocol)?.setWithModel(casteModel.molecule, delegateObject, additionalData)
} else { } else {
if let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(casteModel.molecule, delegateObject) { if let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(casteModel.molecule, delegateObject) {
addAndContain(molecule) addMolecule(molecule)
} }
} }
} }

View File

@ -223,8 +223,8 @@ open class MoleculeStackView: Container {
stackItem.translatesAutoresizingMaskIntoConstraints = false stackItem.translatesAutoresizingMaskIntoConstraints = false
let spacing = model.spacing ?? stackModel.spacing let spacing = model.spacing ?? stackModel.spacing
let verticalAlignment = model.verticalAlignment ?? (stackItem.view as? MVMCoreUIViewConstrainingProtocol)?.verticalAlignment?() ?? (model.percentage == nil && stackModel.axis == .vertical ? .fill : (stackModel.axis == .vertical ? .leading : .center)) let verticalAlignment = model.verticalAlignment ?? (stackItem.view as? MVMCoreUIViewConstrainingProtocol)?.verticalAlignment?() ?? (model.percent == nil && stackModel.axis == .vertical ? .fill : (stackModel.axis == .vertical ? .leading : .center))
let horizontalAlignment = model.horizontalAlignment ?? (stackItem.view as? MVMCoreUIViewConstrainingProtocol)?.horizontalAlignment?() ?? (stackModel.axis == .vertical || model.percentage == nil ? .fill : .leading) let horizontalAlignment = model.horizontalAlignment ?? (stackItem.view as? MVMCoreUIViewConstrainingProtocol)?.horizontalAlignment?() ?? (stackModel.axis == .vertical || model.percent == nil ? .fill : .leading)
stackItem.containerHelper.alignHorizontal(horizontalAlignment) stackItem.containerHelper.alignHorizontal(horizontalAlignment)
stackItem.containerHelper.alignVertical(verticalAlignment) stackItem.containerHelper.alignVertical(verticalAlignment)
@ -239,7 +239,7 @@ open class MoleculeStackView: Container {
} }
pinView(stackItem, toView: contentView, attribute: .leading, relation: .equal, priority: .required, constant: 0) pinView(stackItem, toView: contentView, attribute: .leading, relation: .equal, priority: .required, constant: 0)
pinView(contentView, toView: stackItem, attribute: .trailing, relation: .equal, priority: .required, constant: 0) pinView(contentView, toView: stackItem, attribute: .trailing, relation: .equal, priority: .required, constant: 0)
if let percent = model.percentage { if let percent = model.percent {
stackItem.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: CGFloat(percent)/100.0).isActive = true stackItem.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: CGFloat(percent)/100.0).isActive = true
} }
if lastItem { if lastItem {
@ -256,7 +256,7 @@ open class MoleculeStackView: Container {
} }
pinView(stackItem, toView: contentView, attribute: .top, relation: .equal, priority: .required, constant: 0) pinView(stackItem, toView: contentView, attribute: .top, relation: .equal, priority: .required, constant: 0)
pinView(contentView, toView: stackItem, attribute: .bottom, relation: .equal, priority: .required, constant: 0) pinView(contentView, toView: stackItem, attribute: .bottom, relation: .equal, priority: .required, constant: 0)
if let percent = model.percentage { if let percent = model.percent {
stackItem.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: CGFloat(percent)/100.0).isActive = true stackItem.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: CGFloat(percent)/100.0).isActive = true
} }
if lastItem { if lastItem {