fixes
This commit is contained in:
parent
9a6fd85405
commit
9195f96a68
@ -15,27 +15,34 @@ import Foundation
|
|||||||
public var molecule: MoleculeProtocol?
|
public var molecule: MoleculeProtocol?
|
||||||
public var seperator: MoleculeProtocol?
|
public var seperator: MoleculeProtocol?
|
||||||
|
|
||||||
public init(molecule: MoleculeProtocol?){
|
public init(molecule: MoleculeProtocol?, backgroundColor: String?, seperator: MoleculeProtocol?){
|
||||||
self.molecule = molecule
|
self.molecule = molecule
|
||||||
self.moleculeName = Self.identifier
|
self.moleculeName = Self.identifier
|
||||||
|
self.backgroundColor = backgroundColor
|
||||||
|
self.seperator = seperator
|
||||||
}
|
}
|
||||||
|
|
||||||
enum CodingKeys: String, CodingKey {
|
enum CodingKeys: String, CodingKey {
|
||||||
case moleculeName
|
case moleculeName
|
||||||
case molecule
|
case molecule
|
||||||
|
case backgroundColor
|
||||||
case separator
|
case separator
|
||||||
}
|
}
|
||||||
|
|
||||||
required public init(from decoder: Decoder) throws {
|
required public init(from decoder: Decoder) throws {
|
||||||
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
self.moleculeName = try typeContainer.decode(String.self, forKey: .moleculeName)
|
self.moleculeName = try typeContainer.decode(String.self, forKey: .moleculeName)
|
||||||
|
self.backgroundColor = try typeContainer.decode(String.self, forKey: .backgroundColor)
|
||||||
self.molecule = try typeContainer.decodeIfPresent(codingKey: .molecule)
|
self.molecule = try typeContainer.decodeIfPresent(codingKey: .molecule)
|
||||||
|
self.seperator = try typeContainer.decodeIfPresent(codingKey: .separator)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encode(to encoder: Encoder) throws {
|
public func encode(to encoder: Encoder) throws {
|
||||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||||
try container.encode(moleculeName, forKey: .moleculeName)
|
try container.encode(moleculeName, forKey: .moleculeName)
|
||||||
try container.encodeIfPresent(self.molecule, forKey: .molecule)
|
try container.encodeIfPresent(self.molecule, forKey: .molecule)
|
||||||
|
try container.encode(backgroundColor, forKey: .backgroundColor)
|
||||||
|
try container.encodeIfPresent(self.seperator, forKey: .separator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -11,5 +11,7 @@ import Foundation
|
|||||||
|
|
||||||
@objcMembers public class LabelModel: MoleculeProtocol {
|
@objcMembers public class LabelModel: MoleculeProtocol {
|
||||||
public static var identifier: String = "label"
|
public static var identifier: String = "label"
|
||||||
|
public var moleculeName: String?
|
||||||
public var text: String?
|
public var text: String?
|
||||||
|
public var spacing: Int?
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,7 @@ import Foundation
|
|||||||
public var percentage: Int?
|
public var percentage: Int?
|
||||||
public var verticalAlignment: String?
|
public var verticalAlignment: String?
|
||||||
public var horizontalAlignment: String?
|
public var horizontalAlignment: String?
|
||||||
public var gone = false
|
public var gone: Bool? = false
|
||||||
|
|
||||||
public init(molecule: MoleculeProtocol?, spacing: CGFloat?, percentage: Int?, verticalAlignment: String?, horizontalAlignment: String?, gone: Bool = false) {
|
public init(molecule: MoleculeProtocol?, spacing: CGFloat?, percentage: Int?, verticalAlignment: String?, horizontalAlignment: String?, gone: Bool = false) {
|
||||||
self.molecule = molecule
|
self.molecule = molecule
|
||||||
@ -44,11 +44,11 @@ import Foundation
|
|||||||
self.moleculeName = try typeContainer.decode(String.self, forKey: .moleculeName)
|
self.moleculeName = try typeContainer.decode(String.self, forKey: .moleculeName)
|
||||||
self.molecule = try typeContainer.decodeIfPresent(codingKey: .molecule)
|
self.molecule = try typeContainer.decodeIfPresent(codingKey: .molecule)
|
||||||
|
|
||||||
self.spacing = try typeContainer.decode(CGFloat.self, forKey: .spacing)
|
self.spacing = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .spacing)
|
||||||
self.percentage = try typeContainer.decode(Int.self, forKey: .percentage)
|
self.percentage = try typeContainer.decodeIfPresent(Int.self, forKey: .percentage)
|
||||||
self.verticalAlignment = try typeContainer.decode(String.self, forKey: .verticalAlignment)
|
self.verticalAlignment = try typeContainer.decodeIfPresent(String.self, forKey: .verticalAlignment)
|
||||||
self.horizontalAlignment = try typeContainer.decode(String.self, forKey: .horizontalAlignment)
|
self.horizontalAlignment = try typeContainer.decodeIfPresent(String.self, forKey: .horizontalAlignment)
|
||||||
self.gone = try typeContainer.decode(Bool.self, forKey: .gone)
|
self.gone = try typeContainer.decodeIfPresent(Bool.self, forKey: .gone)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encode(to encoder: Encoder) throws {
|
public func encode(to encoder: Encoder) throws {
|
||||||
|
|||||||
@ -32,8 +32,8 @@ import Foundation
|
|||||||
|
|
||||||
required public init(from decoder: Decoder) throws {
|
required public init(from decoder: Decoder) throws {
|
||||||
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
self.moleculeName = try typeContainer.decodeIfPresent(String.self, forKey: .moleculeName)
|
self.moleculeName = try typeContainer.decode(String.self, forKey: .moleculeName)
|
||||||
self.molecules = try typeContainer.decode([MoleculeStackItemModel].self, forKey: .molecules)
|
self.molecules = try typeContainer.decodeIfPresent([MoleculeStackItemModel].self, forKey: .molecules)
|
||||||
self.axis = try typeContainer.decodeIfPresent(String.self, forKey: .axis)
|
self.axis = try typeContainer.decodeIfPresent(String.self, forKey: .axis)
|
||||||
self.spacing = try typeContainer.decodeIfPresent(Float.self, forKey: .spacing)
|
self.spacing = try typeContainer.decodeIfPresent(Float.self, forKey: .spacing)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,13 +44,13 @@ import UIKit
|
|||||||
open override func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
|
open override func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
|
||||||
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
eyebrow.setWithJSON(json?.optionalDictionaryForKey("eyebrow"), delegateObject: delegateObject, additionalData: additionalData)
|
eyebrow.setWithJSON(json?.optionalDictionaryForKey("eyebrow"), delegateObject: delegateObject, additionalData: additionalData)
|
||||||
stack.items[0].gone = !eyebrow.hasText
|
stack.stackItems[0].gone = !eyebrow.hasText
|
||||||
headline.setWithJSON(json?.optionalDictionaryForKey("headline"), delegateObject: delegateObject, additionalData: additionalData)
|
headline.setWithJSON(json?.optionalDictionaryForKey("headline"), delegateObject: delegateObject, additionalData: additionalData)
|
||||||
stack.items[1].gone = !headline.hasText
|
stack.stackItems[1].gone = !headline.hasText
|
||||||
body.setWithJSON(json?.optionalDictionaryForKey("body"), delegateObject: delegateObject, additionalData: additionalData)
|
body.setWithJSON(json?.optionalDictionaryForKey("body"), delegateObject: delegateObject, additionalData: additionalData)
|
||||||
stack.items[2].gone = !body.hasText
|
stack.stackItems[2].gone = !body.hasText
|
||||||
link.setWithJSON(json?.optionalDictionaryForKey("link"), delegateObject: delegateObject, additionalData: additionalData)
|
link.setWithJSON(json?.optionalDictionaryForKey("link"), delegateObject: delegateObject, additionalData: additionalData)
|
||||||
stack.items[3].gone = link.titleLabel?.text?.count ?? 0 == 0
|
stack.stackItems[3].gone = link.titleLabel?.text?.count ?? 0 == 0
|
||||||
stack.restack()
|
stack.restack()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -45,7 +45,7 @@ public class StackItem {
|
|||||||
|
|
||||||
public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol {
|
public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol {
|
||||||
var contentView: UIView = MVMCoreUICommonViewsUtility.commonView()
|
var contentView: UIView = MVMCoreUICommonViewsUtility.commonView()
|
||||||
var items: [StackItem] = []
|
var stackItems: [StackItem] = []
|
||||||
var useStackSpacingBeforeFirstItem = false
|
var useStackSpacingBeforeFirstItem = false
|
||||||
var moleculeStackModel: MoleculeStackModel?
|
var moleculeStackModel: MoleculeStackModel?
|
||||||
|
|
||||||
@ -88,12 +88,12 @@ public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol
|
|||||||
|
|
||||||
/// Restacks the existing items.
|
/// Restacks the existing items.
|
||||||
func restack() {
|
func restack() {
|
||||||
setWithStackItems(items)
|
setWithStackItems(stackItems)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes all stack items views from the view.
|
/// Removes all stack items views from the view.
|
||||||
func removeAllItemViews() {
|
func removeAllItemViews() {
|
||||||
for item in items {
|
for item in stackItems {
|
||||||
item.view.removeFromSuperview()
|
item.view.removeFromSuperview()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,7 +130,7 @@ public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol
|
|||||||
|
|
||||||
public override func updateView(_ size: CGFloat) {
|
public override func updateView(_ size: CGFloat) {
|
||||||
super.updateView(size)
|
super.updateView(size)
|
||||||
for item in items {
|
for item in stackItems {
|
||||||
(item.view as? MVMCoreViewProtocol)?.updateView(size)
|
(item.view as? MVMCoreViewProtocol)?.updateView(size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -140,7 +140,7 @@ public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol
|
|||||||
super.reset()
|
super.reset()
|
||||||
backgroundColor = .clear
|
backgroundColor = .clear
|
||||||
updateViewHorizontalDefaults = true
|
updateViewHorizontalDefaults = true
|
||||||
for item in items {
|
for item in stackItems {
|
||||||
if let view = item.view as? MVMCoreUIMoleculeViewProtocol {
|
if let view = item.view as? MVMCoreUIMoleculeViewProtocol {
|
||||||
view.reset?()
|
view.reset?()
|
||||||
}
|
}
|
||||||
@ -163,10 +163,15 @@ public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol
|
|||||||
// If the items in the stack are the same, just update previous items instead of re-allocating.
|
// If the items in the stack are the same, just update previous items instead of re-allocating.
|
||||||
var items: [StackItem]?
|
var items: [StackItem]?
|
||||||
|
|
||||||
if MoleculeStackView.name(forReuse: previousModel, delegateObject: delegateObject) == MoleculeStackView.name(forReuse: model, delegateObject: delegateObject) {
|
if let previousModel = previousModel {
|
||||||
items = self.items
|
let previoudReuseName = MoleculeStackView.name(forReuse: previousModel, delegateObject: delegateObject)
|
||||||
|
let currentReuseName = MoleculeStackView.name(forReuse: model, delegateObject: delegateObject)
|
||||||
|
if previoudReuseName == currentReuseName {
|
||||||
|
items = self.stackItems
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.items = []
|
|
||||||
|
self.stackItems = []
|
||||||
|
|
||||||
guard let molecules = model.molecules else {
|
guard let molecules = model.molecules else {
|
||||||
return
|
return
|
||||||
@ -176,15 +181,15 @@ public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol
|
|||||||
//setAxisWithJSON(json)
|
//setAxisWithJSON(json)
|
||||||
spacing = CGFloat(model.spacing ?? 16)
|
spacing = CGFloat(model.spacing ?? 16)
|
||||||
|
|
||||||
for (index, moleculeContainer) in molecules.enumerated() {
|
for (index, stackItemModel) in molecules.enumerated() {
|
||||||
if let stackItemModel = moleculeContainer.molecule as? MoleculeStackItemModel {
|
if let moleculeModel = stackItemModel.molecule {
|
||||||
var view: UIView?
|
var view: UIView?
|
||||||
if let item = items?[index] {
|
if let item = items?[index] {
|
||||||
item.update(with: stackItemModel)
|
item.update(with: stackItemModel)
|
||||||
view = item.view
|
view = item.view
|
||||||
(view as? ModelMoleculeViewProtocol)?.setWithModel(stackItemModel, delegateObject, nil)
|
(view as? ModelMoleculeViewProtocol)?.setWithModel(moleculeModel, delegateObject, nil)
|
||||||
addStackItem(item, lastItem: index == molecules.count - 1)
|
addStackItem(item, lastItem: index == molecules.count - 1)
|
||||||
} else if let moleculeView = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(stackItemModel, delegateObject, true) {
|
} else if let moleculeView = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(moleculeModel, delegateObject, true) {
|
||||||
view = moleculeView
|
view = moleculeView
|
||||||
addStackItem(StackItem(with: moleculeView, stackItemModel: stackItemModel), lastItem: index == molecules.count - 1)
|
addStackItem(StackItem(with: moleculeView, stackItemModel: stackItemModel), lastItem: index == molecules.count - 1)
|
||||||
}
|
}
|
||||||
@ -254,7 +259,7 @@ public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol
|
|||||||
/// Adds the stack item to the stack.
|
/// Adds the stack item to the stack.
|
||||||
func addStackItem(_ stackItem: StackItem, lastItem: Bool) {
|
func addStackItem(_ stackItem: StackItem, lastItem: Bool) {
|
||||||
guard !stackItem.gone else {
|
guard !stackItem.gone else {
|
||||||
items.append(stackItem)
|
stackItems.append(stackItem)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let view = stackItem.view
|
let view = stackItem.view
|
||||||
@ -268,11 +273,11 @@ public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol
|
|||||||
view.alignHorizontal?(horizontalAlignment)
|
view.alignHorizontal?(horizontalAlignment)
|
||||||
view.alignVertical?(verticalAlignment)
|
view.alignVertical?(verticalAlignment)
|
||||||
}
|
}
|
||||||
let first = items.first { !$0.gone } == nil
|
let first = stackItems.first { !$0.gone } == nil
|
||||||
if axis == .vertical {
|
if axis == .vertical {
|
||||||
if first {
|
if first {
|
||||||
pinView(view, toView: contentView, attribute: .top, relation: .equal, priority: .required, constant: useStackSpacingBeforeFirstItem ? spacing : stackItem.spacing ?? 0)
|
pinView(view, toView: contentView, attribute: .top, relation: .equal, priority: .required, constant: useStackSpacingBeforeFirstItem ? spacing : stackItem.spacing ?? 0)
|
||||||
} else if let previousView = items.last(where: { stackItem in
|
} else if let previousView = stackItems.last(where: { stackItem in
|
||||||
return !stackItem.gone
|
return !stackItem.gone
|
||||||
})?.view {
|
})?.view {
|
||||||
_ = NSLayoutConstraint(pinFirstView: previousView, toSecondView: view, withConstant: spacing, directionVertical: true)
|
_ = NSLayoutConstraint(pinFirstView: previousView, toSecondView: view, withConstant: spacing, directionVertical: true)
|
||||||
@ -289,7 +294,7 @@ public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol
|
|||||||
if first {
|
if first {
|
||||||
// First horizontal item has no spacing by default unless told otherwise.
|
// First horizontal item has no spacing by default unless told otherwise.
|
||||||
pinView(view, toView: contentView, attribute: .leading, relation: .equal, priority: .required, constant: useStackSpacingBeforeFirstItem ? spacing : stackItem.spacing ?? 0)
|
pinView(view, toView: contentView, attribute: .leading, relation: .equal, priority: .required, constant: useStackSpacingBeforeFirstItem ? spacing : stackItem.spacing ?? 0)
|
||||||
} else if let previousView = items.last(where: { stackItem in
|
} else if let previousView = stackItems.last(where: { stackItem in
|
||||||
return !stackItem.gone
|
return !stackItem.gone
|
||||||
})?.view {
|
})?.view {
|
||||||
_ = NSLayoutConstraint(pinFirstView: previousView, toSecondView: view, withConstant: spacing, directionVertical: false)
|
_ = NSLayoutConstraint(pinFirstView: previousView, toSecondView: view, withConstant: spacing, directionVertical: false)
|
||||||
@ -303,12 +308,12 @@ public class MoleculeStackView: ViewConstrainingView, ModelMoleculeViewProtocol
|
|||||||
if lastItem {
|
if lastItem {
|
||||||
pinView(contentView, toView: view, attribute: .right, relation: .equal, priority: .required, constant: 0)
|
pinView(contentView, toView: view, attribute: .right, relation: .equal, priority: .required, constant: 0)
|
||||||
}
|
}
|
||||||
items.append(stackItem)
|
stackItems.append(stackItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setWithStackItems(_ items: [StackItem]) {
|
func setWithStackItems(_ items: [StackItem]) {
|
||||||
removeAllItemViews()
|
removeAllItemViews()
|
||||||
self.items.removeAll()
|
self.stackItems.removeAll()
|
||||||
var previousPresentItem: StackItem? = nil
|
var previousPresentItem: StackItem? = nil
|
||||||
for item in items {
|
for item in items {
|
||||||
if !item.gone {
|
if !item.gone {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user