From 788145332f579c9d44b6b1df3bc0b181049ad46e Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Mon, 13 Jan 2020 12:44:05 -0500 Subject: [PATCH] Two button view temporary model fix primary button model --- MVMCoreUI.xcodeproj/project.pbxproj | 12 +++++ MVMCoreUI/Atoms/Buttons/ButtonModel.swift | 50 +++++++++++++++++++ ...maryButton+MoleculeProtocolExtension.swift | 30 +++++++++++ .../TwoButtonView.swift | 22 ++++++-- .../TwoButtonViewModel.swift | 16 ++++++ .../OtherHandlers/MoleculeObjectMapping.swift | 5 +- 6 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 MVMCoreUI/Atoms/Buttons/ButtonModel.swift create mode 100644 MVMCoreUI/Atoms/Buttons/PrimaryButton+MoleculeProtocolExtension.swift create mode 100644 MVMCoreUI/Molecules/HorizontalCombinationViews/TwoButtonViewModel.swift diff --git a/MVMCoreUI.xcodeproj/project.pbxproj b/MVMCoreUI.xcodeproj/project.pbxproj index dedc9cf9..d9e31e36 100644 --- a/MVMCoreUI.xcodeproj/project.pbxproj +++ b/MVMCoreUI.xcodeproj/project.pbxproj @@ -124,6 +124,9 @@ D28A838523CCCA8900DFE4FC /* ScrollerModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28A838423CCCA8900DFE4FC /* ScrollerModel.swift */; }; D28A838723CCCF6500DFE4FC /* MFTextButton+ModelExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28A838623CCCF6500DFE4FC /* MFTextButton+ModelExtension.swift */; }; D28A838923CCCFCB00DFE4FC /* LinkModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28A838823CCCFCB00DFE4FC /* LinkModel.swift */; }; + D28A838B23CCDA6B00DFE4FC /* ButtonModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28A838A23CCDA6B00DFE4FC /* ButtonModel.swift */; }; + D28A838D23CCDCC200DFE4FC /* PrimaryButton+MoleculeProtocolExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28A838C23CCDCC200DFE4FC /* PrimaryButton+MoleculeProtocolExtension.swift */; }; + D28A838F23CCDEDE00DFE4FC /* TwoButtonViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28A838E23CCDEDE00DFE4FC /* TwoButtonViewModel.swift */; }; D296E14722A5984C0051EBE7 /* MVMCoreUIViewConstrainingProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = D296E14622A597490051EBE7 /* MVMCoreUIViewConstrainingProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; D29770C821F7C4AE00B2F0D0 /* TopLabelsView.m in Sources */ = {isa = PBXBuildFile; fileRef = D29770C621F7C4AE00B2F0D0 /* TopLabelsView.m */; }; D29770C921F7C4AE00B2F0D0 /* TopLabelsView.h in Headers */ = {isa = PBXBuildFile; fileRef = D29770C721F7C4AE00B2F0D0 /* TopLabelsView.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -387,6 +390,9 @@ D28A838423CCCA8900DFE4FC /* ScrollerModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollerModel.swift; sourceTree = ""; }; D28A838623CCCF6500DFE4FC /* MFTextButton+ModelExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MFTextButton+ModelExtension.swift"; sourceTree = ""; }; D28A838823CCCFCB00DFE4FC /* LinkModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkModel.swift; sourceTree = ""; }; + D28A838A23CCDA6B00DFE4FC /* ButtonModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonModel.swift; sourceTree = ""; }; + D28A838C23CCDCC200DFE4FC /* PrimaryButton+MoleculeProtocolExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PrimaryButton+MoleculeProtocolExtension.swift"; sourceTree = ""; }; + D28A838E23CCDEDE00DFE4FC /* TwoButtonViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwoButtonViewModel.swift; sourceTree = ""; }; D296E14622A597490051EBE7 /* MVMCoreUIViewConstrainingProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MVMCoreUIViewConstrainingProtocol.h; sourceTree = ""; }; D29770C621F7C4AE00B2F0D0 /* TopLabelsView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TopLabelsView.m; sourceTree = ""; }; D29770C721F7C4AE00B2F0D0 /* TopLabelsView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TopLabelsView.h; sourceTree = ""; }; @@ -729,6 +735,7 @@ isa = PBXGroup; children = ( D2B1E3E422F37D6A0065F95C /* ImageHeadlineBody.swift */, + D28A838E23CCDEDE00DFE4FC /* TwoButtonViewModel.swift */, D20A9A5D2243D3E300ADE781 /* TwoButtonView.swift */, D28A837E23CCA96400DFE4FC /* TabsModel.swift */, ); @@ -995,6 +1002,8 @@ D29DF16A21E69E1F003B2FB9 /* MFCustomButton.h */, D29DF17021E69E1F003B2FB9 /* MFCustomButton.m */, D28A837623C79FC600DFE4FC /* MFCustomButton+ActionModel.swift */, + D28A838A23CCDA6B00DFE4FC /* ButtonModel.swift */, + D28A838C23CCDCC200DFE4FC /* PrimaryButton+MoleculeProtocolExtension.swift */, D29DF16C21E69E1F003B2FB9 /* PrimaryButton.h */, D29DF17121E69E1F003B2FB9 /* PrimaryButton.m */, D282AACA2243C61700C46919 /* ButtonView.swift */, @@ -1383,6 +1392,8 @@ D28A837B23C928DA00DFE4FC /* MoleculeListCellProtocol.swift in Sources */, 014AA72F23C5059B006F3E93 /* ThreeLayerPageTemplateModel.swift in Sources */, D22D1F572204CE5D0077CEC0 /* MVMCoreUIStackableViewController.m in Sources */, + D28A838B23CCDA6B00DFE4FC /* ButtonModel.swift in Sources */, + D28A838D23CCDCC200DFE4FC /* PrimaryButton+MoleculeProtocolExtension.swift in Sources */, D2A5145F2211DDC100345BFB /* MoleculeStackView.swift in Sources */, D29DF27621E79E81003B2FB9 /* MVMCoreUILoggingHandler.m in Sources */, D28A838723CCCF6500DFE4FC /* MFTextButton+ModelExtension.swift in Sources */, @@ -1448,6 +1459,7 @@ 944589212385D6E900DE9FD4 /* DashLineModel.swift in Sources */, D29DF27A21E7A533003B2FB9 /* MVMCoreUISession.m in Sources */, D2A5146B2214905000345BFB /* ThreeLayerViewController.swift in Sources */, + D28A838F23CCDEDE00DFE4FC /* TwoButtonViewModel.swift in Sources */, 012A88B1238C880100FE3DA1 /* PagingMoleculeProtocol.swift in Sources */, D29DF2C921E7BFC6003B2FB9 /* MFSizeObject.m in Sources */, 9445890E2385C3F800DE9FD4 /* MultiProgressModel.swift in Sources */, diff --git a/MVMCoreUI/Atoms/Buttons/ButtonModel.swift b/MVMCoreUI/Atoms/Buttons/ButtonModel.swift new file mode 100644 index 00000000..9d9ad9c3 --- /dev/null +++ b/MVMCoreUI/Atoms/Buttons/ButtonModel.swift @@ -0,0 +1,50 @@ +// +// ButtonModel.swift +// MVMCoreUI +// +// Created by Scott Pfeil on 1/13/20. +// Copyright © 2020 Verizon Wireless. All rights reserved. +// + +import UIKit + +public enum ButtonStyle: String, Codable { + case primary + case secondary +} + +public class ButtonModel: MoleculeProtocol { + public static var identifier: String = "button" + public var backgroundColor: Color? + public var title: String + public var action: ActionProtocol + public var style: ButtonStyle? + + init(with title: String, action: ActionProtocol) { + self.title = title + self.action = action + } + + enum CodingKeys: String, CodingKey { + case backgroundColor + case title + case action + case style + } + + required public init(from decoder: Decoder) throws { + let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) + title = try typeContainer.decode(String.self, forKey: .title) + action = try typeContainer.decodeModel(codingKey: .action, typeCodingKey: ActionCodingKey.type) + style = try typeContainer.decode(ButtonStyle.self, forKey: .style) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(title, forKey: .title) + try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) + try container.encodeModel(action, forKey: .action) + try container.encodeIfPresent(style, forKey: .style) + } +} diff --git a/MVMCoreUI/Atoms/Buttons/PrimaryButton+MoleculeProtocolExtension.swift b/MVMCoreUI/Atoms/Buttons/PrimaryButton+MoleculeProtocolExtension.swift new file mode 100644 index 00000000..f7192fc9 --- /dev/null +++ b/MVMCoreUI/Atoms/Buttons/PrimaryButton+MoleculeProtocolExtension.swift @@ -0,0 +1,30 @@ +// +// PrimaryButton+MoleculeProtocolExtension.swift +// MVMCoreUI +// +// Created by Scott Pfeil on 1/13/20. +// Copyright © 2020 Verizon Wireless. All rights reserved. +// + +import Foundation + +// temporary until link is finished +extension PrimaryButton: ModelMoleculeViewProtocol { + public func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) { + guard let model = model as? ButtonModel else { return } + setTitle(model.title, for: .normal) + backgroundColor = model.backgroundColor?.uiColor + if let style = model.style { + switch style { + case .primary: + setAsStandardCustom() + case .secondary: + setAsSecondaryCustom() + } + } + //TODO: Use object when handleAction is rewrote to handle action model + if let actionMap = model.action.toJSON() { + MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) + } + } +} diff --git a/MVMCoreUI/Molecules/HorizontalCombinationViews/TwoButtonView.swift b/MVMCoreUI/Molecules/HorizontalCombinationViews/TwoButtonView.swift index 2c5f0fe5..b2f43529 100644 --- a/MVMCoreUI/Molecules/HorizontalCombinationViews/TwoButtonView.swift +++ b/MVMCoreUI/Molecules/HorizontalCombinationViews/TwoButtonView.swift @@ -137,20 +137,25 @@ import UIKit } } + /// Legacy func setupUI(withPrimaryButtonMap primaryButtonMap: [AnyHashable: Any]?, secondaryButtonMap: [AnyHashable: Any]?) { - if primaryButtonMap != nil, secondaryButtonMap != nil { + setupUI(primaryButtonShowing: primaryButtonMap != nil, secondaryButtonShowing: secondaryButtonMap != nil) + } + + func setupUI(primaryButtonShowing: Bool, secondaryButtonShowing: Bool) { + if primaryButtonShowing, secondaryButtonShowing { heightConstraint?.isActive = false if primaryButton == nil || secondaryButton == nil { removeButtons() setupWithTwoButtons() } - } else if primaryButtonMap != nil { + } else if primaryButtonShowing { heightConstraint?.isActive = false if primaryButton == nil || secondaryButton != nil { removeButtons() setupWithPrimaryButton() } - } else if secondaryButtonMap != nil { + } else if secondaryButtonShowing { heightConstraint?.isActive = false if secondaryButton == nil || primaryButton != nil { removeButtons() @@ -291,3 +296,14 @@ extension TwoButtonView { setup(primaryButtonMap: primaryButtonMap, secondaryButtonMap: secondaryButtonMap, actionDelegate: actionDelegate, additionalData: additionalData, buttonDelegate: buttonDelegate) } } + +extension TwoButtonView: MoleculeViewProtocol { + func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { + guard let model = model as? TwoButtonViewModel else { return } + setupUI(primaryButtonShowing: model.primaryButton != nil, secondaryButtonShowing: model.secondaryButton != nil) + setDefaultCustom() + primaryButton?.setWithModel(model.primaryButton, delegateObject, additionalData) + secondaryButton?.setWithModel(model.secondaryButton, delegateObject, additionalData) + super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) + } +} diff --git a/MVMCoreUI/Molecules/HorizontalCombinationViews/TwoButtonViewModel.swift b/MVMCoreUI/Molecules/HorizontalCombinationViews/TwoButtonViewModel.swift new file mode 100644 index 00000000..50643e1c --- /dev/null +++ b/MVMCoreUI/Molecules/HorizontalCombinationViews/TwoButtonViewModel.swift @@ -0,0 +1,16 @@ +// +// TwoButtonViewModel.swift +// MVMCoreUI +// +// Created by Scott Pfeil on 1/13/20. +// Copyright © 2020 Verizon Wireless. All rights reserved. +// + +import UIKit + +public class TwoButtonViewModel: MoleculeProtocol { + public static var identifier: String = "twoButtonView" + public var backgroundColor: Color? + public var primaryButton: ButtonModel? + public var secondaryButton: ButtonModel? +} diff --git a/MVMCoreUI/OtherHandlers/MoleculeObjectMapping.swift b/MVMCoreUI/OtherHandlers/MoleculeObjectMapping.swift index 01677585..21895f3d 100644 --- a/MVMCoreUI/OtherHandlers/MoleculeObjectMapping.swift +++ b/MVMCoreUI/OtherHandlers/MoleculeObjectMapping.swift @@ -16,7 +16,6 @@ import Foundation ModelRegistry.register(MoleculeStackModel.self) ModelRegistry.register(StackItemModel.self) ModelRegistry.register(TextFieldModel.self) - ModelRegistry.register(LineModel.self) ModelRegistry.register(ProgressBarModel.self) ModelRegistry.register(MultiProgressBarModel.self) ModelRegistry.register(CaretViewModel.self) @@ -24,6 +23,10 @@ import Foundation ModelRegistry.register(ImageViewModel.self) ModelRegistry.register(TabsModel.self) ModelRegistry.register(ScrollerModel.self) + // buttons + ModelRegistry.register(LineModel.self) + ModelRegistry.register(ButtonModel.self) + ModelRegistry.register(TwoButtonViewModel.self) // list items ModelRegistry.register(ListItemModel.self) ModelRegistry.register(DropDownListItemModel.self)