From 11069d3f7342bf7d64d2c403c8784e6c3ac9b83e Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Wed, 25 Mar 2020 12:59:26 -0400 Subject: [PATCH] add convenience initializers for creating a legacy footer --- MVMCoreUI.xcodeproj/project.pbxproj | 20 +++++++++++ MVMCoreUI/Atoms/Buttons/ButtonModel.swift | 6 ++-- .../Legacy/Adapters/ActionModelAdapter.swift | 20 +++++++++++ .../Legacy/Adapters/FooterModelAdapter.swift | 36 +++++++++++++++++++ .../Legacy/Adapters/LabelModelAdapter.swift | 33 +++++++++++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 MVMCoreUI/Legacy/Adapters/ActionModelAdapter.swift create mode 100644 MVMCoreUI/Legacy/Adapters/FooterModelAdapter.swift create mode 100644 MVMCoreUI/Legacy/Adapters/LabelModelAdapter.swift diff --git a/MVMCoreUI.xcodeproj/project.pbxproj b/MVMCoreUI.xcodeproj/project.pbxproj index 5308ee86..0108ffc0 100644 --- a/MVMCoreUI.xcodeproj/project.pbxproj +++ b/MVMCoreUI.xcodeproj/project.pbxproj @@ -110,6 +110,9 @@ 0AE98BB323FF0934004C5109 /* ExternalLinkModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE98BB223FF0934004C5109 /* ExternalLinkModel.swift */; }; 0AE98BB523FF18D2004C5109 /* Arrow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE98BB423FF18D2004C5109 /* Arrow.swift */; }; 0AE98BB723FF18E9004C5109 /* ArrowModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE98BB623FF18E9004C5109 /* ArrowModel.swift */; }; + 279B1567242BBBF200921D6C /* LabelModelAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279B1566242BBBF200921D6C /* LabelModelAdapter.swift */; }; + 279B1569242BBC2F00921D6C /* ActionModelAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279B1568242BBC2F00921D6C /* ActionModelAdapter.swift */; }; + 279B156D242BBFF100921D6C /* FooterModelAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279B156C242BBFF100921D6C /* FooterModelAdapter.swift */; }; 31BE15CB23D8924D00452370 /* CheckboxLabelModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31BE15C923D8924C00452370 /* CheckboxLabelModel.swift */; }; 31BE15CC23D8924D00452370 /* CheckboxModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31BE15CA23D8924C00452370 /* CheckboxModel.swift */; }; 522679C123FE886900906CBA /* ListLeftVariableCheckboxAllTextAndLinks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 522679BF23FE886900906CBA /* ListLeftVariableCheckboxAllTextAndLinks.swift */; }; @@ -502,6 +505,9 @@ 0AE98BB223FF0934004C5109 /* ExternalLinkModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExternalLinkModel.swift; sourceTree = ""; }; 0AE98BB423FF18D2004C5109 /* Arrow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Arrow.swift; sourceTree = ""; }; 0AE98BB623FF18E9004C5109 /* ArrowModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrowModel.swift; sourceTree = ""; }; + 279B1566242BBBF200921D6C /* LabelModelAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelModelAdapter.swift; sourceTree = ""; }; + 279B1568242BBC2F00921D6C /* ActionModelAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionModelAdapter.swift; sourceTree = ""; }; + 279B156C242BBFF100921D6C /* FooterModelAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FooterModelAdapter.swift; sourceTree = ""; }; 31BE15C923D8924C00452370 /* CheckboxLabelModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CheckboxLabelModel.swift; sourceTree = ""; }; 31BE15CA23D8924C00452370 /* CheckboxModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CheckboxModel.swift; sourceTree = ""; }; 522679BF23FE886900906CBA /* ListLeftVariableCheckboxAllTextAndLinks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListLeftVariableCheckboxAllTextAndLinks.swift; sourceTree = ""; }; @@ -917,6 +923,16 @@ path = Link; sourceTree = ""; }; + 279B1565242BBB8500921D6C /* Adapters */ = { + isa = PBXGroup; + children = ( + 279B1566242BBBF200921D6C /* LabelModelAdapter.swift */, + 279B1568242BBC2F00921D6C /* ActionModelAdapter.swift */, + 279B156C242BBFF100921D6C /* FooterModelAdapter.swift */, + ); + path = Adapters; + sourceTree = ""; + }; 5206F150241144A900658DC5 /* Headers */ = { isa = PBXGroup; children = ( @@ -1260,6 +1276,7 @@ D22D1F582204D2590077CEC0 /* Legacy */ = { isa = PBXGroup; children = ( + 279B1565242BBB8500921D6C /* Adapters */, D213347523842FF5008E41B3 /* Views */, D213347423842FE3008E41B3 /* Controllers */, ); @@ -2088,6 +2105,7 @@ C7F8012123E8303200396FBD /* ListRVWheel.swift in Sources */, D29DF29521E7ADB8003B2FB9 /* MFProgrammaticScrollViewController.m in Sources */, D2FB151B23A2B65B00C20E10 /* MoleculeContainer.swift in Sources */, + 279B1569242BBC2F00921D6C /* ActionModelAdapter.swift in Sources */, BB6C6AC0242232DF005F7224 /* ListOneColumnTextWithWhitespaceDividerTallModel.swift in Sources */, D2A638FD22CA98280052ED1F /* HeadlineBody.swift in Sources */, D29DF16121E69996003B2FB9 /* MFViewController.m in Sources */, @@ -2113,6 +2131,7 @@ D29DF28C21E7AC2B003B2FB9 /* ViewConstrainingView.m in Sources */, 0AE14F64238315D2005417F8 /* TextField.swift in Sources */, D29DF17B21E69E1F003B2FB9 /* PrimaryButton.m in Sources */, + 279B1567242BBBF200921D6C /* LabelModelAdapter.swift in Sources */, D2C78CD224228BBD00B69FDE /* ActionOpenPanelModel.swift in Sources */, 017BEB4A236235BA0024EF95 /* ModelMoleculeViewProtocol.swift in Sources */, C695A68123C9830D00BFB94E /* NumberedListModel.swift in Sources */, @@ -2203,6 +2222,7 @@ 01509D932327ECFB00EF99AA /* ProgressBar.swift in Sources */, D260106523D0CEA700764D80 /* StackModel.swift in Sources */, D29770F521F7C6D600B2F0D0 /* TopLabelsAndBottomButtonsViewController.m in Sources */, + 279B156D242BBFF100921D6C /* FooterModelAdapter.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/MVMCoreUI/Atoms/Buttons/ButtonModel.swift b/MVMCoreUI/Atoms/Buttons/ButtonModel.swift index ac9d1dc1..f441ef64 100644 --- a/MVMCoreUI/Atoms/Buttons/ButtonModel.swift +++ b/MVMCoreUI/Atoms/Buttons/ButtonModel.swift @@ -42,18 +42,18 @@ public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormAction public var updateUI: (() -> Void)? - init(with title: String, action: ActionModelProtocol) { + public init(with title: String, action: ActionModelProtocol) { self.title = title self.action = action } - init(secondaryButtonWith title: String, action: ActionModelProtocol) { + public init(secondaryButtonWith title: String, action: ActionModelProtocol) { self.title = title self.action = action style = .secondary } - init(primaryButtonWith title: String, action: ActionModelProtocol) { + public init(primaryButtonWith title: String, action: ActionModelProtocol) { self.title = title self.action = action style = .primary diff --git a/MVMCoreUI/Legacy/Adapters/ActionModelAdapter.swift b/MVMCoreUI/Legacy/Adapters/ActionModelAdapter.swift new file mode 100644 index 00000000..ba0ce96b --- /dev/null +++ b/MVMCoreUI/Legacy/Adapters/ActionModelAdapter.swift @@ -0,0 +1,20 @@ +// +// ActionModelAdapter.swift +// MVMCoreUI +// +// Created by Kyle on 3/25/20. +// Copyright © 2020 Verizon Wireless. All rights reserved. +// + +public extension Dictionary { + + func asActionModel() -> ActionModelProtocol? { + guard let castedSelf = self as? [String: Any], + let actionType = ModelRegistry.getType(for: castedSelf.stringForkey(KeyActionType), with: ActionModelProtocol.self), + let actionModel = try? actionType.decode(jsonDict: castedSelf) as? ActionModelProtocol else { + return nil + } + return actionModel + } + +} diff --git a/MVMCoreUI/Legacy/Adapters/FooterModelAdapter.swift b/MVMCoreUI/Legacy/Adapters/FooterModelAdapter.swift new file mode 100644 index 00000000..7c923ba0 --- /dev/null +++ b/MVMCoreUI/Legacy/Adapters/FooterModelAdapter.swift @@ -0,0 +1,36 @@ +// +// FooterModelAdapter.swift +// MVMCoreUI +// +// Created by Kyle on 3/25/20. +// Copyright © 2020 Verizon Wireless. All rights reserved. +// + +public extension FooterModel { + + convenience init(withPrimaryAction primaryMap: [AnyHashable: Any]? = nil, secondaryAction secondaryMap: [AnyHashable: Any]? = nil, bottomLinkAction linkMap: [AnyHashable: Any]? = nil) { + + var primaryButtonModel: ButtonModel? + var secondaryButtonModel: ButtonModel? + if let primaryActionModel = primaryMap?.asActionModel() { + primaryButtonModel = ButtonModel(primaryButtonWith: primaryActionModel.title ?? "", action: primaryActionModel) + } + if let secondaryActionModel = secondaryMap?.asActionModel() { + secondaryButtonModel = ButtonModel(secondaryButtonWith: secondaryActionModel.title ?? "", action: secondaryActionModel) + } + let twoButtonModel = TwoButtonViewModel(primaryButtonModel, secondaryButtonModel) + + let footerContentModel: MoleculeModelProtocol + if let linkActionJSON = linkMap { + let labelModel = LabelModel(linkAction: linkActionJSON) + let moleculeStack = MoleculeStackModel(molecules: [MoleculeStackItemModel(with: twoButtonModel), MoleculeStackItemModel(with: labelModel)]) + moleculeStack.spacing = MFStyler.defaultVerticalPaddingForApplicationWidth() + footerContentModel = moleculeStack + } else { + footerContentModel = twoButtonModel + } + + self.init(with: footerContentModel) + } + +} diff --git a/MVMCoreUI/Legacy/Adapters/LabelModelAdapter.swift b/MVMCoreUI/Legacy/Adapters/LabelModelAdapter.swift new file mode 100644 index 00000000..757b2ccb --- /dev/null +++ b/MVMCoreUI/Legacy/Adapters/LabelModelAdapter.swift @@ -0,0 +1,33 @@ +// +// LabelModelAdapter.swift +// MVMCoreUI +// +// Created by Kyle on 3/25/20. +// Copyright © 2020 Verizon Wireless. All rights reserved. +// + +public extension LabelModel { + + convenience init(linkAction: [AnyHashable: Any]) { + // Concatentate the text. + var titlePrefix = linkAction.stringForkey(KeyTitlePrefix) + var titlePostfix = linkAction.stringForkey(KeyTitlePostfix) + if !titlePrefix.isEmpty { + titlePrefix += " " + } + if !titlePostfix.isEmpty { + titlePostfix += " " + } + let title = linkAction.optionalStringForKey(KeyTitle) ?? "" + let text = "\(titlePrefix)\(title)\(titlePostfix)" + + // Initialize with text. + self.init(text: text) + + // Add action attribute. + if let linkActionModel = linkAction.asActionModel() { + attributes = [LabelAttributeActionModel(titlePrefix.isEmpty ? 0 : titlePrefix.count, title.count, action: linkActionModel)] + } + } + +}