From f858f2e380ad259ebc2413dffb6341f992310b92 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Thu, 4 Jun 2020 21:19:30 -0400 Subject: [PATCH] add legacy support for navigation molecular fix navigation bugs. --- .../NavigationBar/NavigationItemModel.swift | 8 +++++++- .../NavigationItemModelProtocol.swift | 1 + MVMCoreUI/Categories/UIColor+Extension.swift | 14 +++++++++++++- MVMCoreUI/Containers/NavigationController.swift | 9 ++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/MVMCoreUI/Atomic/Molecules/NavigationBar/NavigationItemModel.swift b/MVMCoreUI/Atomic/Molecules/NavigationBar/NavigationItemModel.swift index 05aec0a3..bf926878 100644 --- a/MVMCoreUI/Atomic/Molecules/NavigationBar/NavigationItemModel.swift +++ b/MVMCoreUI/Atomic/Molecules/NavigationBar/NavigationItemModel.swift @@ -18,6 +18,7 @@ public class NavigationItemModel: NavigationItemModelProtocol, MoleculeModelProt public var backgroundColor: Color? public var tintColor: Color public var line: LineModel? + public var alwaysShowBackButton = false public var backButton: (NavigationButtonModelProtocol & MoleculeModelProtocol)? = NavigationImageButtonModel(with: "back", action: ActionBackModel()) public var additionalLeftButtons: [(NavigationButtonModelProtocol & MoleculeModelProtocol)]? public var additionalRightButtons: [(NavigationButtonModelProtocol & MoleculeModelProtocol)]? @@ -30,11 +31,13 @@ public class NavigationItemModel: NavigationItemModelProtocol, MoleculeModelProt } private enum CodingKeys: String, CodingKey { + case moleculeName case title case hidden case backgroundColor case tintColor case line + case alwaysShowBackButton case backButton case showLeftPanelButton case showRightPanelButton @@ -49,6 +52,7 @@ public class NavigationItemModel: NavigationItemModelProtocol, MoleculeModelProt backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) ?? Color(uiColor: .white) tintColor = try typeContainer.decodeIfPresent(Color.self, forKey: .tintColor) ?? Color(uiColor: .black) line = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line) + alwaysShowBackButton = try typeContainer.decodeIfPresent(Bool.self, forKey: .alwaysShowBackButton) ?? false if let backButton: (NavigationButtonModelProtocol & MoleculeModelProtocol) = try typeContainer.decodeModelIfPresent(codingKey: .backButton) { self.backButton = backButton } @@ -58,11 +62,13 @@ public class NavigationItemModel: NavigationItemModelProtocol, MoleculeModelProt open func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(moleculeName, forKey: .moleculeName) try container.encodeIfPresent(title, forKey: .title) try container.encode(hidden, forKey: .hidden) - try container.encode(backgroundColor, forKey: .backgroundColor) + try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encode(tintColor, forKey: .tintColor) try container.encodeIfPresent(line, forKey: .line) + try container.encode(alwaysShowBackButton, forKey: .alwaysShowBackButton) try container.encodeModelIfPresent(backButton, forKey: .backButton) try container.encodeModelsIfPresent(additionalLeftButtons, forKey: .additionalLeftButtons) try container.encodeModelsIfPresent(additionalRightButtons, forKey: .additionalRightButtons) diff --git a/MVMCoreUI/Atomic/Protocols/ModelProtocols/NavigationItemModelProtocol.swift b/MVMCoreUI/Atomic/Protocols/ModelProtocols/NavigationItemModelProtocol.swift index e4017428..30db2aa6 100644 --- a/MVMCoreUI/Atomic/Protocols/ModelProtocols/NavigationItemModelProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/ModelProtocols/NavigationItemModelProtocol.swift @@ -14,6 +14,7 @@ public protocol NavigationItemModelProtocol { var backgroundColor: Color? { get set } var tintColor: Color { get set } var line: LineModel? { get set } + var alwaysShowBackButton: Bool { get set } var backButton: (NavigationButtonModelProtocol & MoleculeModelProtocol)? { get set } var additionalLeftButtons: [(NavigationButtonModelProtocol & MoleculeModelProtocol)]? { get set } var additionalRightButtons: [(NavigationButtonModelProtocol & MoleculeModelProtocol)]? { get set } diff --git a/MVMCoreUI/Categories/UIColor+Extension.swift b/MVMCoreUI/Categories/UIColor+Extension.swift index 27816af2..7c82788c 100644 --- a/MVMCoreUI/Categories/UIColor+Extension.swift +++ b/MVMCoreUI/Categories/UIColor+Extension.swift @@ -282,7 +282,9 @@ extension UIColor { guard let components = color.cgColor.components else { return nil } - if color.cgColor.numberOfComponents >= 3 { + let numberOfComponents = color.cgColor.numberOfComponents + if numberOfComponents >= 3 { + // RGB color space let r = Int(CGFloat(components[0]) * 255) let g = Int(CGFloat(components[1]) * 255) let b = Int(CGFloat(components[2]) * 255) @@ -294,6 +296,16 @@ extension UIColor { } return String(format: "%02X%02X%02X", r, g, b) + } else if numberOfComponents == 2 { + // Monochromatic color space + let value = Int(CGFloat(components[0]) * 255) + + // If alpha of color is less than 1.0 then alpha hex is relevant. + if components[1] < 1.0 { + let alpha = Int(CGFloat(components[1]) * 255) + return String(format: "%02X%02X%02X%02X", value, value, value, alpha) + } + return String(format: "%02X%02X%02X", value, value, value) } return nil diff --git a/MVMCoreUI/Containers/NavigationController.swift b/MVMCoreUI/Containers/NavigationController.swift index 6f3bf06f..bac5817c 100644 --- a/MVMCoreUI/Containers/NavigationController.swift +++ b/MVMCoreUI/Containers/NavigationController.swift @@ -48,7 +48,7 @@ import UIKit let delegate = (viewController as? MVMCoreViewControllerProtocol)?.delegateObject?() as? MVMCoreUIDelegateObject var leftItems: [UIBarButtonItem] = [] if let backButtonModel = navigationItemModel.backButton, - navigationController.viewControllers.count > 1 { + navigationController.viewControllers.count > 1 || navigationItemModel.alwaysShowBackButton { leftItems.append(backButtonModel.createNavigationItemButton(delegateObject: delegate, additionalData: nil)) } if let leftItemModels = navigationItemModel.additionalLeftButtons { @@ -90,4 +90,11 @@ import UIKit // Sets up the navigation buttons. setNavigationButtons(navigationController: navigationController, navigationItemModel: navigationItemModel, viewController: viewController) } + + /// Convenience setter for legacy files + public static func set(navigationController: UINavigationController, navigationJSON: [String: Any], viewController: UIViewController) { + let moleculeName = ModelRegistry.getType(for: navigationJSON.stringForkey(KeyMoleculeName), with: MoleculeModelProtocol.self) + guard let barModel = try? moleculeName?.decode(jsonDict: navigationJSON) as? (MoleculeModelProtocol & NavigationItemModelProtocol) else { return } + set(navigationController: navigationController, navigationItemModel: barModel, viewController: viewController) + } }