diff --git a/MVMCoreUI/Atomic/MoleculeObjectMapping.swift b/MVMCoreUI/Atomic/MoleculeObjectMapping.swift index 464238a7..e01bfe3b 100644 --- a/MVMCoreUI/Atomic/MoleculeObjectMapping.swift +++ b/MVMCoreUI/Atomic/MoleculeObjectMapping.swift @@ -120,6 +120,7 @@ import Foundation // Other Container Molecules + MoleculeObjectMapping.shared()?.register(viewClass: MoleculeContainer.self, viewModelClass: MoleculeContainerModel.self) MoleculeObjectMapping.shared()?.register(viewClass: MoleculeHeaderView.self, viewModelClass: MoleculeHeaderModel.self) MoleculeObjectMapping.shared()?.register(viewClass: FooterView.self, viewModelClass: FooterModel.self) MoleculeObjectMapping.shared()?.register(viewClass: Scroller.self, viewModelClass: ScrollerModel.self) diff --git a/MVMCoreUI/Atomic/Molecules/DesignedComponents/Headers/HeadersH2NoButtonsBodyTextModel.swift b/MVMCoreUI/Atomic/Molecules/DesignedComponents/Headers/HeadersH2NoButtonsBodyTextModel.swift index eb5502eb..816db00d 100644 --- a/MVMCoreUI/Atomic/Molecules/DesignedComponents/Headers/HeadersH2NoButtonsBodyTextModel.swift +++ b/MVMCoreUI/Atomic/Molecules/DesignedComponents/Headers/HeadersH2NoButtonsBodyTextModel.swift @@ -25,8 +25,8 @@ public class HeadersH2NoButtonsBodyTextModel: HeaderModel, MoleculeModelProtocol public override func setDefaults() { super.setDefaults() - topMarginPadding = PaddingDefaultVerticalSpacing3 - bottomMarginPadding = PaddingDefaultVerticalSpacing3 + topPadding = PaddingDefaultVerticalSpacing3 + bottomPadding = PaddingDefaultVerticalSpacing3 } //-------------------------------------------------- diff --git a/MVMCoreUI/Atomic/Molecules/FooterModel.swift b/MVMCoreUI/Atomic/Molecules/FooterModel.swift index d61d1194..12e0eca8 100644 --- a/MVMCoreUI/Atomic/Molecules/FooterModel.swift +++ b/MVMCoreUI/Atomic/Molecules/FooterModel.swift @@ -9,47 +9,24 @@ import Foundation -@objcMembers public class FooterModel: MoleculeContainerModel, MoleculeModelProtocol { - public static var identifier: String = "footer" - public var backgroundColor: Color? - - private enum CodingKeys: String, CodingKey { - case moleculeName - case backgroundColor +@objcMembers public class FooterModel: MoleculeContainerModel { + public override class var identifier: String { + return "footer" } /// Defaults to set - func setDefaults() { + public override func setDefaults() { if useHorizontalMargins == nil { useHorizontalMargins = true } if useVerticalMargins == nil { useVerticalMargins = true } - if topMarginPadding == nil { - topMarginPadding = PaddingDefaultVerticalSpacing + if topPadding == nil { + topPadding = PaddingDefaultVerticalSpacing } - if bottomMarginPadding == nil { - bottomMarginPadding = PaddingDefaultVerticalSpacing + if bottomPadding == nil { + bottomPadding = PaddingDefaultVerticalSpacing } } - - public override init(with moleculeModel: MoleculeModelProtocol) { - super.init(with: moleculeModel) - setDefaults() - } - - required public init(from decoder: Decoder) throws { - let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) - try super.init(from: decoder) - setDefaults() - } - - public override func encode(to encoder: Encoder) throws { - try super.encode(to: encoder) - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(moleculeName, forKey: .moleculeName) - try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) - } } diff --git a/MVMCoreUI/Atomic/Molecules/HeaderModel.swift b/MVMCoreUI/Atomic/Molecules/HeaderModel.swift index 6314947c..09fe077f 100644 --- a/MVMCoreUI/Atomic/Molecules/HeaderModel.swift +++ b/MVMCoreUI/Atomic/Molecules/HeaderModel.swift @@ -18,18 +18,18 @@ import Foundation } /// Defaults to set - public func setDefaults() { + public override func setDefaults() { if useHorizontalMargins == nil { useHorizontalMargins = true } if useVerticalMargins == nil { useVerticalMargins = true } - if topMarginPadding == nil { - topMarginPadding = PaddingDefaultVerticalSpacing + if topPadding == nil { + topPadding = PaddingDefaultVerticalSpacing } - if bottomMarginPadding == nil { - bottomMarginPadding = PaddingDefaultVerticalSpacing + if bottomPadding == nil { + bottomPadding = PaddingDefaultVerticalSpacing } if line == nil { line = LineModel(type: .heavy) @@ -38,7 +38,6 @@ import Foundation public override init() { super.init() - setDefaults() } required public init(from decoder: Decoder) throws { @@ -46,7 +45,6 @@ import Foundation let typeContainer = try decoder.container(keyedBy: CodingKeys.self) line = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) - setDefaults() } public override func encode(to encoder: Encoder) throws { diff --git a/MVMCoreUI/Atomic/Molecules/Items/ListItemModel.swift b/MVMCoreUI/Atomic/Molecules/Items/ListItemModel.swift index 9a00a94d..8fdb4d77 100644 --- a/MVMCoreUI/Atomic/Molecules/Items/ListItemModel.swift +++ b/MVMCoreUI/Atomic/Molecules/Items/ListItemModel.swift @@ -38,7 +38,7 @@ import Foundation //-------------------------------------------------- /// Defaults to set - open func setDefaults() { + open override func setDefaults() { if useHorizontalMargins == nil { useHorizontalMargins = true } @@ -54,9 +54,12 @@ import Foundation // MARK: - Initializer //-------------------------------------------------- + public override init(horizontalAlignment: UIStackView.Alignment? = nil, verticalAlignment: UIStackView.Alignment? = nil, useHorizontalMargins: Bool? = nil, leftPadding: CGFloat? = nil, rightPadding: CGFloat? = nil, useVerticalMargins: Bool? = nil, topPadding: CGFloat? = nil, bottomPadding: CGFloat? = nil) { + super.init(horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment, useHorizontalMargins: useHorizontalMargins, leftPadding: leftPadding, rightPadding: rightPadding, useVerticalMargins: useVerticalMargins, topPadding: topPadding, bottomPadding: bottomPadding) + } + public override init() { super.init() - setDefaults() } //-------------------------------------------------- @@ -71,7 +74,6 @@ import Foundation line = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line) style = try typeContainer.decodeIfPresent(String.self, forKey: .style) try super.init(from: decoder) - setDefaults() } open override func encode(to encoder: Encoder) throws { diff --git a/MVMCoreUI/Atomic/Molecules/Items/MoleculeCollectionItemModel.swift b/MVMCoreUI/Atomic/Molecules/Items/MoleculeCollectionItemModel.swift index ed9467fd..4f203813 100644 --- a/MVMCoreUI/Atomic/Molecules/Items/MoleculeCollectionItemModel.swift +++ b/MVMCoreUI/Atomic/Molecules/Items/MoleculeCollectionItemModel.swift @@ -9,49 +9,36 @@ import Foundation /// A model for a collection item that is a container for any molecule. -@objcMembers public class MoleculeCollectionItemModel: MoleculeContainerModel, CollectionItemModelProtocol, MoleculeModelProtocol { - open class var identifier: String { +@objcMembers public class MoleculeCollectionItemModel: MoleculeContainerModel, CollectionItemModelProtocol { + open override class var identifier: String { return "collectionItem" } - public var backgroundColor: Color? /// Defaults to set - public func setDefaults() { + public override func setDefaults() { if useHorizontalMargins == nil { useHorizontalMargins = true } if useVerticalMargins == nil { useVerticalMargins = true } - if topMarginPadding == nil { - topMarginPadding = PaddingDefault + if topPadding == nil { + topPadding = PaddingDefault } - if bottomMarginPadding == nil { - bottomMarginPadding = PaddingDefault + if bottomPadding == nil { + bottomPadding = PaddingDefault } } - private enum CodingKeys: String, CodingKey { - case moleculeName - case backgroundColor - } - public override init(with moleculeModel: MoleculeModelProtocol) { super.init(with: moleculeModel) - setDefaults() } required public init(from decoder: Decoder) throws { - let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) try super.init(from: decoder) - setDefaults() } public override func encode(to encoder: Encoder) throws { try super.encode(to: encoder) - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(moleculeName, forKey: .moleculeName) - try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) } } diff --git a/MVMCoreUI/Atomic/Molecules/Items/MoleculeCollectionViewCell.swift b/MVMCoreUI/Atomic/Molecules/Items/MoleculeCollectionViewCell.swift index 42af1a53..10b51bff 100644 --- a/MVMCoreUI/Atomic/Molecules/Items/MoleculeCollectionViewCell.swift +++ b/MVMCoreUI/Atomic/Molecules/Items/MoleculeCollectionViewCell.swift @@ -54,6 +54,6 @@ open class MoleculeCollectionViewCell: CollectionViewCell { let height = classType.estimatedHeight(with: model.molecule, delegateObject) else { return 100 } - return height + (model.topMarginPadding ?? 0) + (model.bottomMarginPadding ?? 0) + return height + (model.topPadding ?? 0) + (model.bottomPadding ?? 0) } } diff --git a/MVMCoreUI/Atomic/Molecules/Items/MoleculeStackItemModel.swift b/MVMCoreUI/Atomic/Molecules/Items/MoleculeStackItemModel.swift index 1a6bced6..1f6e4d88 100644 --- a/MVMCoreUI/Atomic/Molecules/Items/MoleculeStackItemModel.swift +++ b/MVMCoreUI/Atomic/Molecules/Items/MoleculeStackItemModel.swift @@ -8,16 +8,15 @@ import Foundation -@objcMembers public class MoleculeStackItemModel: MoleculeContainerModel, MoleculeModelProtocol, StackItemModelProtocol { - public static var identifier: String = "stackItem" - public var backgroundColor: Color? +@objcMembers public class MoleculeStackItemModel: MoleculeContainerModel, StackItemModelProtocol { + public override class var identifier: String { + return "stackItem" + } public var spacing: CGFloat? public var percent: Int? public var gone: Bool = false private enum CodingKeys: String, CodingKey { - case moleculeName - case backgroundColor case spacing case percent case gone @@ -34,17 +33,14 @@ import Foundation if let gone = try typeContainer.decodeIfPresent(Bool.self, forKey: .gone) { self.gone = gone } - backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) try super.init(from: decoder) } public override func encode(to encoder: Encoder) throws { try super.encode(to: encoder) var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(moleculeName, forKey: .moleculeName) try container.encodeIfPresent(spacing, forKey: .spacing) try container.encodeIfPresent(percent, forKey: .percent) try container.encode(gone, forKey: .gone) - try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) } } diff --git a/MVMCoreUI/Atomic/Molecules/Items/StackItemModel.swift b/MVMCoreUI/Atomic/Molecules/Items/StackItemModel.swift index 436ed9cd..69fe570d 100644 --- a/MVMCoreUI/Atomic/Molecules/Items/StackItemModel.swift +++ b/MVMCoreUI/Atomic/Molecules/Items/StackItemModel.swift @@ -24,16 +24,17 @@ import Foundation // MARK: - Initializer //-------------------------------------------------- - public convenience init(spacing: CGFloat? = nil, percent: Int? = nil, horizontalAlignment: UIStackView.Alignment? = nil, verticalAlignment: UIStackView.Alignment? = nil, gone: Bool? = nil) { - self.init() - - self.horizontalAlignment = horizontalAlignment - self.verticalAlignment = verticalAlignment + public init(spacing: CGFloat? = nil, percent: Int? = nil, horizontalAlignment: UIStackView.Alignment? = nil, verticalAlignment: UIStackView.Alignment? = nil, gone: Bool? = nil) { self.spacing = spacing self.percent = percent if let gone = gone { self.gone = gone } + super.init(horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment) + } + + required public init(from decoder: Decoder) throws { + fatalError("init(from:) has not been implemented") } } diff --git a/MVMCoreUI/Atomic/Molecules/Items/TabsListItemModel.swift b/MVMCoreUI/Atomic/Molecules/Items/TabsListItemModel.swift index e49b4adb..c83b8933 100644 --- a/MVMCoreUI/Atomic/Molecules/Items/TabsListItemModel.swift +++ b/MVMCoreUI/Atomic/Molecules/Items/TabsListItemModel.swift @@ -24,8 +24,8 @@ public class TabsListItemModel: ListItemModel, MoleculeModelProtocol { hideArrow = true action = nil style = nil - topMarginPadding = 8 - bottomMarginPadding = 0 + topPadding = 8 + bottomPadding = 0 } public init(with tabs: TabsModel, molecules: [[ListItemModelProtocol & MoleculeModelProtocol]]) { diff --git a/MVMCoreUI/Atomic/Molecules/ScrollerModel.swift b/MVMCoreUI/Atomic/Molecules/ScrollerModel.swift index 74256e99..697556b5 100644 --- a/MVMCoreUI/Atomic/Molecules/ScrollerModel.swift +++ b/MVMCoreUI/Atomic/Molecules/ScrollerModel.swift @@ -8,8 +8,9 @@ import UIKit -public class ScrollerModel: MoleculeContainerModel, MoleculeModelProtocol { - public static var identifier: String = "scroller" +public class ScrollerModel: MoleculeContainerModel { + public override class var identifier: String { + return "scroller" + } public var moleculeName: String = ScrollerModel.identifier - public var backgroundColor: Color? } diff --git a/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/BGImageHeadlineBodyButtonModel.swift b/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/BGImageHeadlineBodyButtonModel.swift index 650e4875..f6fd4fcf 100644 --- a/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/BGImageHeadlineBodyButtonModel.swift +++ b/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/BGImageHeadlineBodyButtonModel.swift @@ -20,22 +20,21 @@ public class BGImageHeadlineBodyButtonModel: ContainerModel, MoleculeModelProtoc self.headlineBody = headlineBody self.image = image super.init() - setDefaults() } /// Defaults to set - func setDefaults() { + public override func setDefaults() { if useHorizontalMargins == nil { useHorizontalMargins = true } if useVerticalMargins == nil { useVerticalMargins = true } - if topMarginPadding == nil { - topMarginPadding = PaddingDefault + if topPadding == nil { + topPadding = PaddingDefault } - if bottomMarginPadding == nil { - bottomMarginPadding = PaddingDefault + if bottomPadding == nil { + bottomPadding = PaddingDefault } if image.height == nil { image.height = BGImageHeadlineBodyButton.heightConstant @@ -59,7 +58,6 @@ public class BGImageHeadlineBodyButtonModel: ContainerModel, MoleculeModelProtoc image = try typeContainer.decode(ImageViewModel.self, forKey: .image) button = try typeContainer.decodeIfPresent(ButtonModel.self, forKey: .button) try super.init(from: decoder) - setDefaults() } public override func encode(to encoder: Encoder) throws { diff --git a/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/HeadlineBodyCaretLinkImageModel.swift b/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/HeadlineBodyCaretLinkImageModel.swift index f30b89ff..9fdd8612 100644 --- a/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/HeadlineBodyCaretLinkImageModel.swift +++ b/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/HeadlineBodyCaretLinkImageModel.swift @@ -19,22 +19,21 @@ public class HeadlineBodyCaretLinkImageModel: ContainerModel, MoleculeModelProto self.headlineBody = headlineBody self.image = image super.init() - setDefaults() } /// Defaults to set - func setDefaults() { + public override func setDefaults() { if useHorizontalMargins == nil { useHorizontalMargins = true } if useVerticalMargins == nil { useVerticalMargins = true } - if topMarginPadding == nil { - topMarginPadding = PaddingDefault + if topPadding == nil { + topPadding = PaddingDefault } - if bottomMarginPadding == nil { - bottomMarginPadding = PaddingDefault + if bottomPadding == nil { + bottomPadding = PaddingDefault } if image.height == nil { image.height = HeadLineBodyCaretLinkImage.heightConstant @@ -56,7 +55,6 @@ public class HeadlineBodyCaretLinkImageModel: ContainerModel, MoleculeModelProto image = try typeContainer.decode(ImageViewModel.self, forKey: .image) caretLink = try typeContainer.decodeIfPresent(CaretLinkModel.self, forKey: .caretLink) try super.init(from: decoder) - setDefaults() } public override func encode(to encoder: Encoder) throws { diff --git a/MVMCoreUI/Atomic/Protocols/ModelProtocols/ContainerModelProtocol.swift b/MVMCoreUI/Atomic/Protocols/ModelProtocols/ContainerModelProtocol.swift index bc230731..29e25903 100644 --- a/MVMCoreUI/Atomic/Protocols/ModelProtocols/ContainerModelProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/ModelProtocols/ContainerModelProtocol.swift @@ -11,10 +11,12 @@ import Foundation public protocol ContainerModelProtocol { var horizontalAlignment: UIStackView.Alignment? { get set } - var verticalAlignment: UIStackView.Alignment? { get set } var useHorizontalMargins: Bool? { get set } - + var leftPadding: CGFloat? { get set } + var rightPadding: CGFloat? { get set } + + var verticalAlignment: UIStackView.Alignment? { get set } var useVerticalMargins: Bool? { get set } - var topMarginPadding: CGFloat? { get set } - var bottomMarginPadding: CGFloat? { get set } + var topPadding: CGFloat? { get set } + var bottomPadding: CGFloat? { get set } } diff --git a/MVMCoreUI/BaseClasses/TableViewCell.swift b/MVMCoreUI/BaseClasses/TableViewCell.swift index f27a38d8..30f9df22 100644 --- a/MVMCoreUI/BaseClasses/TableViewCell.swift +++ b/MVMCoreUI/BaseClasses/TableViewCell.swift @@ -51,36 +51,36 @@ import UIKit } open func styleStandard() { - listItemModel?.topMarginPadding = 24 - listItemModel?.bottomMarginPadding = 24 + listItemModel?.topPadding = 24 + listItemModel?.bottomPadding = 24 topSeparatorView?.setStyle(.none) bottomSeparatorView?.setStyle(.standard) } open func styleTallDivider() { - listItemModel?.topMarginPadding = 48 - listItemModel?.bottomMarginPadding = 16 + listItemModel?.topPadding = 48 + listItemModel?.bottomPadding = 16 topSeparatorView?.setStyle(.none) bottomSeparatorView?.setStyle(.thin) } open func styleShortDivider() { - listItemModel?.topMarginPadding = 32 - listItemModel?.bottomMarginPadding = 16 + listItemModel?.topPadding = 32 + listItemModel?.bottomPadding = 16 topSeparatorView?.setStyle(.none) bottomSeparatorView?.setStyle(.thin) } open func styleFooter() { - listItemModel?.topMarginPadding = 24 - listItemModel?.bottomMarginPadding = 0 + listItemModel?.topPadding = 24 + listItemModel?.bottomPadding = 0 topSeparatorView?.setStyle(.none) bottomSeparatorView?.setStyle(.none) } open func styleNone() { - listItemModel?.topMarginPadding = 0 - listItemModel?.bottomMarginPadding = 0 + listItemModel?.topPadding = 0 + listItemModel?.bottomPadding = 0 topSeparatorView?.setStyle(.none) bottomSeparatorView?.setStyle(.none) } diff --git a/MVMCoreUI/Containers/Views/Container/ContainerHelper.swift b/MVMCoreUI/Containers/Views/Container/ContainerHelper.swift index 223589fe..4ce97f5c 100644 --- a/MVMCoreUI/Containers/Views/Container/ContainerHelper.swift +++ b/MVMCoreUI/Containers/Views/Container/ContainerHelper.swift @@ -169,8 +169,15 @@ open class ContainerHelper: NSObject { } } + /// Updates the view margins according to model and size. If useHorizontalMargins is true, we will try to use the left and right padding, else we will default to the normal gutters. If useVerticalMargins is true, we will try to use the top and bottom padding. All else, we use zero. open func updateViewMargins(_ view: UIView, model: ContainerModelProtocol?, size: CGFloat) { - MFStyler.setMarginsFor(view, size: size, defaultHorizontal: model?.useHorizontalMargins ?? false, top: (model?.useVerticalMargins ?? false) ? (model?.topMarginPadding ?? 0) : 0, bottom: (model?.useVerticalMargins ?? false) ? (model?.bottomMarginPadding ?? 0) : 0) + let left = (model?.useHorizontalMargins ?? false) ? (model?.leftPadding ?? Padding.Component.horizontalPaddingForSize(size)) : 0.0 + let right = (model?.useHorizontalMargins ?? false) ? (model?.rightPadding ?? Padding.Component.horizontalPaddingForSize(size)) : 0.0 + let top = (model?.useVerticalMargins ?? false) ? (model?.topPadding ?? 0.0) : 0.0 + let bottom = (model?.useVerticalMargins ?? false) ? (model?.bottomPadding ?? 0.0) : 0.0 + MVMCoreDispatchUtility.performBlock(onMainThread: { + MVMCoreUIUtility.setMarginsFor(view, leading: left, top: top, trailing: right, bottom: bottom) + }) } open func set(with model: ContainerModelProtocol, for contained: MVMCoreUIViewConstrainingProtocol?) { diff --git a/MVMCoreUI/Containers/Views/Container/ContainerModel.swift b/MVMCoreUI/Containers/Views/Container/ContainerModel.swift index ec6f9769..ea7d8012 100644 --- a/MVMCoreUI/Containers/Views/Container/ContainerModel.swift +++ b/MVMCoreUI/Containers/Views/Container/ContainerModel.swift @@ -9,41 +9,61 @@ import Foundation open class ContainerModel: ContainerModelProtocol, Codable { + //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- public var horizontalAlignment: UIStackView.Alignment? - public var verticalAlignment: UIStackView.Alignment? public var useHorizontalMargins: Bool? + public var leftPadding: CGFloat? + public var rightPadding: CGFloat? + public var verticalAlignment: UIStackView.Alignment? public var useVerticalMargins: Bool? - public var topMarginPadding: CGFloat? - public var bottomMarginPadding: CGFloat? + public var topPadding: CGFloat? + public var bottomPadding: CGFloat? //-------------------------------------------------- // MARK: - Keys //-------------------------------------------------- private enum CodingKeys: String, CodingKey { - case verticalAlignment case horizontalAlignment case useHorizontalMargins + case leftPadding + case rightPadding + case verticalAlignment case useVerticalMargins - case topMarginPadding - case bottomMarginPadding + case topPadding + case bottomPadding } + //-------------------------------------------------- + // MARK: - Subclassable + //-------------------------------------------------- + + /// Sets the default values. Should be called on init. + public func setDefaults() {} + //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- - public init() {} + public init() { + setDefaults() + } - public convenience init(horizontalAlignment: UIStackView.Alignment? = nil, verticalAlignment: UIStackView.Alignment? = nil) { - self.init() + public init(horizontalAlignment: UIStackView.Alignment? = nil, verticalAlignment: UIStackView.Alignment? = nil, useHorizontalMargins: Bool? = nil, leftPadding: CGFloat? = nil, rightPadding: CGFloat? = nil, useVerticalMargins: Bool? = nil, topPadding: CGFloat? = nil, bottomPadding: CGFloat? = nil) { self.horizontalAlignment = horizontalAlignment self.verticalAlignment = verticalAlignment + self.useHorizontalMargins = useHorizontalMargins + self.leftPadding = leftPadding + self.rightPadding = rightPadding + self.useVerticalMargins = useVerticalMargins + self.topPadding = topPadding + self.bottomPadding = bottomPadding + setDefaults() } //-------------------------------------------------- @@ -59,9 +79,12 @@ open class ContainerModel: ContainerModelProtocol, Codable { horizontalAlignment = ContainerHelper.getAlignment(for: horizontalAlignmentString) } useHorizontalMargins = try typeContainer.decodeIfPresent(Bool.self, forKey: .useHorizontalMargins) + leftPadding = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .leftPadding) + rightPadding = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .rightPadding) useVerticalMargins = try typeContainer.decodeIfPresent(Bool.self, forKey: .useVerticalMargins) - topMarginPadding = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .topMarginPadding) - bottomMarginPadding = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .bottomMarginPadding) + topPadding = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .topPadding) + bottomPadding = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .bottomPadding) + setDefaults() } open func encode(to encoder: Encoder) throws { @@ -69,9 +92,10 @@ open class ContainerModel: ContainerModelProtocol, Codable { try container.encodeIfPresent(ContainerHelper.getAlignmentString(for: verticalAlignment), forKey: .verticalAlignment) try container.encodeIfPresent(ContainerHelper.getAlignmentString(for: horizontalAlignment), forKey: .horizontalAlignment) try container.encodeIfPresent(useHorizontalMargins, forKey: .useHorizontalMargins) + try container.encodeIfPresent(leftPadding, forKey: .leftPadding) + try container.encodeIfPresent(rightPadding, forKey: .rightPadding) try container.encodeIfPresent(useVerticalMargins, forKey: .useVerticalMargins) - // TODO: can add this back once we have type erasures. - //try container.encodeIfPresent(topMarginPadding, forKey: .topMarginPadding) - //try container.encodeIfPresent(bottomMarginPadding, forKey: .bottomMarginPadding) + try container.encodeIfPresent(topPadding, forKey: .topPadding) + try container.encodeIfPresent(bottomPadding, forKey: .bottomPadding) } } diff --git a/MVMCoreUI/Containers/Views/MoleculeContainer.swift b/MVMCoreUI/Containers/Views/MoleculeContainer.swift index 98ecfa28..4e2c6279 100644 --- a/MVMCoreUI/Containers/Views/MoleculeContainer.swift +++ b/MVMCoreUI/Containers/Views/MoleculeContainer.swift @@ -41,9 +41,9 @@ open class MoleculeContainer: Container { guard let containerModel = model as? MoleculeContainerModelProtocol else { return 0 } guard let moleculeClass = MoleculeObjectMapping.shared()?.getMoleculeClass(containerModel.molecule), let moleculeHeight = moleculeClass.estimatedHeight(with: containerModel.molecule, delegateObject) else { - return (containerModel.topMarginPadding ?? 0) + (containerModel.bottomMarginPadding ?? 0) + return (containerModel.topPadding ?? 0) + (containerModel.bottomPadding ?? 0) } - return moleculeHeight + (containerModel.topMarginPadding ?? 0) + (containerModel.bottomMarginPadding ?? 0) + return moleculeHeight + (containerModel.topPadding ?? 0) + (containerModel.bottomPadding ?? 0) } public override class func requiredModules(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer?) -> [String]? { diff --git a/MVMCoreUI/Containers/Views/MoleculeContainerModel.swift b/MVMCoreUI/Containers/Views/MoleculeContainerModel.swift index 01116a48..86693823 100644 --- a/MVMCoreUI/Containers/Views/MoleculeContainerModel.swift +++ b/MVMCoreUI/Containers/Views/MoleculeContainerModel.swift @@ -8,11 +8,17 @@ import Foundation -public class MoleculeContainerModel: ContainerModel, MoleculeContainerModelProtocol { +public class MoleculeContainerModel: ContainerModel, MoleculeContainerModelProtocol, MoleculeModelProtocol { + public class var identifier: String { + return "container" + } + public var backgroundColor: Color? public var molecule: MoleculeModelProtocol private enum CodingKeys: String, CodingKey { + case moleculeName case molecule + case backgroundColor } public init(with moleculeModel: MoleculeModelProtocol) { @@ -23,12 +29,15 @@ public class MoleculeContainerModel: ContainerModel, MoleculeContainerModelProto required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) molecule = try typeContainer.decodeModel(codingKey: .molecule) + backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) try super.init(from: decoder) } public override func encode(to encoder: Encoder) throws { try super.encode(to: encoder) var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(moleculeName, forKey: .moleculeName) try container.encodeModel(molecule, forKey: .molecule) + try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) } }