add legacy support for navigation molecular

fix navigation bugs.
This commit is contained in:
Pfeil, Scott Robert 2020-06-04 21:19:30 -04:00
parent 69375837ef
commit f858f2e380
4 changed files with 29 additions and 3 deletions

View File

@ -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)

View File

@ -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 }

View File

@ -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

View File

@ -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)
}
}