This commit is contained in:
Subhankar Acharya 2020-06-08 11:55:29 +05:30
commit 490d2b0c64
5 changed files with 42 additions and 3 deletions

View File

@ -34,6 +34,18 @@ import Foundation
return type.init(model: model, delegateObject, additionalData) return type.init(model: model, delegateObject, additionalData)
} }
/// Convenience function for legacy classes
public func getMoleculeModelForJSON(_ json: [String: Any]) throws -> MoleculeModelProtocol? {
guard let moleculeName = json.optionalStringForKey(KeyMoleculeName),
let type = ModelRegistry.getType(for: moleculeName, with: MoleculeModelProtocol.self) else {
throw ModelRegistry.Error.decoderErrorModelNotMapped
}
guard let model = try type.decode(jsonDict: json) as? MoleculeModelProtocol else {
throw ModelRegistry.Error.decoderError
}
return model
}
/// Call to register all of the CoreUI molecules. /// Call to register all of the CoreUI molecules.
public static func registerObjects() { public static func registerObjects() {
// Stacks // Stacks

View File

@ -18,6 +18,7 @@ public class NavigationItemModel: NavigationItemModelProtocol, MoleculeModelProt
public var backgroundColor: Color? public var backgroundColor: Color?
public var tintColor: Color public var tintColor: Color
public var line: LineModel? public var line: LineModel?
public var alwaysShowBackButton = false
public var backButton: (NavigationButtonModelProtocol & MoleculeModelProtocol)? = NavigationImageButtonModel(with: "back", action: ActionBackModel()) public var backButton: (NavigationButtonModelProtocol & MoleculeModelProtocol)? = NavigationImageButtonModel(with: "back", action: ActionBackModel())
public var additionalLeftButtons: [(NavigationButtonModelProtocol & MoleculeModelProtocol)]? public var additionalLeftButtons: [(NavigationButtonModelProtocol & MoleculeModelProtocol)]?
public var additionalRightButtons: [(NavigationButtonModelProtocol & MoleculeModelProtocol)]? public var additionalRightButtons: [(NavigationButtonModelProtocol & MoleculeModelProtocol)]?
@ -30,11 +31,13 @@ public class NavigationItemModel: NavigationItemModelProtocol, MoleculeModelProt
} }
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case moleculeName
case title case title
case hidden case hidden
case backgroundColor case backgroundColor
case tintColor case tintColor
case line case line
case alwaysShowBackButton
case backButton case backButton
case showLeftPanelButton case showLeftPanelButton
case showRightPanelButton case showRightPanelButton
@ -49,6 +52,7 @@ public class NavigationItemModel: NavigationItemModelProtocol, MoleculeModelProt
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) ?? Color(uiColor: .white) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) ?? Color(uiColor: .white)
tintColor = try typeContainer.decodeIfPresent(Color.self, forKey: .tintColor) ?? Color(uiColor: .black) tintColor = try typeContainer.decodeIfPresent(Color.self, forKey: .tintColor) ?? Color(uiColor: .black)
line = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line) 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) { if let backButton: (NavigationButtonModelProtocol & MoleculeModelProtocol) = try typeContainer.decodeModelIfPresent(codingKey: .backButton) {
self.backButton = backButton self.backButton = backButton
} }
@ -58,11 +62,13 @@ public class NavigationItemModel: NavigationItemModelProtocol, MoleculeModelProt
open func encode(to encoder: Encoder) throws { open func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self) var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(moleculeName, forKey: .moleculeName)
try container.encodeIfPresent(title, forKey: .title) try container.encodeIfPresent(title, forKey: .title)
try container.encode(hidden, forKey: .hidden) 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.encode(tintColor, forKey: .tintColor)
try container.encodeIfPresent(line, forKey: .line) try container.encodeIfPresent(line, forKey: .line)
try container.encode(alwaysShowBackButton, forKey: .alwaysShowBackButton)
try container.encodeModelIfPresent(backButton, forKey: .backButton) try container.encodeModelIfPresent(backButton, forKey: .backButton)
try container.encodeModelsIfPresent(additionalLeftButtons, forKey: .additionalLeftButtons) try container.encodeModelsIfPresent(additionalLeftButtons, forKey: .additionalLeftButtons)
try container.encodeModelsIfPresent(additionalRightButtons, forKey: .additionalRightButtons) try container.encodeModelsIfPresent(additionalRightButtons, forKey: .additionalRightButtons)

View File

@ -14,6 +14,7 @@ public protocol NavigationItemModelProtocol {
var backgroundColor: Color? { get set } var backgroundColor: Color? { get set }
var tintColor: Color { get set } var tintColor: Color { get set }
var line: LineModel? { get set } var line: LineModel? { get set }
var alwaysShowBackButton: Bool { get set }
var backButton: (NavigationButtonModelProtocol & MoleculeModelProtocol)? { get set } var backButton: (NavigationButtonModelProtocol & MoleculeModelProtocol)? { get set }
var additionalLeftButtons: [(NavigationButtonModelProtocol & MoleculeModelProtocol)]? { get set } var additionalLeftButtons: [(NavigationButtonModelProtocol & MoleculeModelProtocol)]? { get set }
var additionalRightButtons: [(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 } 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 r = Int(CGFloat(components[0]) * 255)
let g = Int(CGFloat(components[1]) * 255) let g = Int(CGFloat(components[1]) * 255)
let b = Int(CGFloat(components[2]) * 255) let b = Int(CGFloat(components[2]) * 255)
@ -294,6 +296,16 @@ extension UIColor {
} }
return String(format: "%02X%02X%02X", r, g, b) 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 return nil

View File

@ -48,7 +48,7 @@ import UIKit
let delegate = (viewController as? MVMCoreViewControllerProtocol)?.delegateObject?() as? MVMCoreUIDelegateObject let delegate = (viewController as? MVMCoreViewControllerProtocol)?.delegateObject?() as? MVMCoreUIDelegateObject
var leftItems: [UIBarButtonItem] = [] var leftItems: [UIBarButtonItem] = []
if let backButtonModel = navigationItemModel.backButton, if let backButtonModel = navigationItemModel.backButton,
navigationController.viewControllers.count > 1 { navigationController.viewControllers.count > 1 || navigationItemModel.alwaysShowBackButton {
leftItems.append(backButtonModel.createNavigationItemButton(delegateObject: delegate, additionalData: nil)) leftItems.append(backButtonModel.createNavigationItemButton(delegateObject: delegate, additionalData: nil))
} }
if let leftItemModels = navigationItemModel.additionalLeftButtons { if let leftItemModels = navigationItemModel.additionalLeftButtons {
@ -90,4 +90,12 @@ import UIKit
// Sets up the navigation buttons. // Sets up the navigation buttons.
setNavigationButtons(navigationController: navigationController, navigationItemModel: navigationItemModel, viewController: viewController) setNavigationButtons(navigationController: navigationController, navigationItemModel: navigationItemModel, viewController: viewController)
} }
/// Convenience setter for legacy files
public static func set(navigationController: UINavigationController, navigationJSON: [String: Any], viewController: UIViewController) throws {
guard let barModel = try MoleculeObjectMapping.shared()?.getMoleculeModelForJSON(navigationJSON) as? (MoleculeModelProtocol & NavigationItemModelProtocol) else {
throw ModelRegistry.Error.decoderOther(message: "Model not a bar model")
}
set(navigationController: navigationController, navigationItemModel: barModel, viewController: viewController)
}
} }