Cleanup navigation button creation.
This commit is contained in:
parent
bb50671711
commit
d203285c73
@ -32,4 +32,10 @@ public class NavigationItemButtonModel: Codable {
|
|||||||
try container.encode(imageName, forKey: .imageName)
|
try container.encode(imageName, forKey: .imageName)
|
||||||
try container.encodeModel(action, forKey: .action)
|
try container.encodeModel(action, forKey: .action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convenience function that creates a BarButtonItem for the model.
|
||||||
|
public func createNavigationItem(delegateObject: MVMCoreUIDelegateObject? = nil, additionalData: [AnyHashable: Any]? = nil) -> BarButtonItem {
|
||||||
|
let image = UIImage(named: imageName, in: MVMCoreCache.shared()?.bundleToUseForImages(), compatibleWith: nil)
|
||||||
|
return BarButtonItem.create(with: image, actionModel: action, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,7 +28,7 @@ public typealias BarButtonAction = (BarButtonItem) -> ()
|
|||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|
||||||
public static func create(with image: UIImage) -> Self {
|
public static func create(with image: UIImage?) -> Self {
|
||||||
let actionObject = ActionDelegate()
|
let actionObject = ActionDelegate()
|
||||||
let button = self.init(image: image, style: .plain, target: actionObject, action: #selector(actionObject.callActionBlock(_:)))
|
let button = self.init(image: image, style: .plain, target: actionObject, action: #selector(actionObject.callActionBlock(_:)))
|
||||||
button.actionObject = actionObject
|
button.actionObject = actionObject
|
||||||
@ -36,21 +36,21 @@ public typealias BarButtonAction = (BarButtonItem) -> ()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates the item with the passed in action.
|
/// Creates the item with the passed in action.
|
||||||
public static func create(with image: UIImage, actionModel: ActionModelProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Self {
|
public static func create(with image: UIImage?, actionModel: ActionModelProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Self {
|
||||||
let button = create(with: image)
|
let button = create(with: image)
|
||||||
button.set(with: actionModel, delegateObject: delegateObject, additionalData: additionalData)
|
button.set(with: actionModel, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
return button
|
return button
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates the item with the passed in action map.
|
/// Creates the item with the passed in action map.
|
||||||
public static func create(with image: UIImage, actionMap: [AnyHashable : Any], delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Self {
|
public static func create(with image: UIImage?, actionMap: [AnyHashable : Any], delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Self {
|
||||||
let button = create(with: image)
|
let button = create(with: image)
|
||||||
button.set(with: actionMap, delegateObject: delegateObject, additionalData: additionalData)
|
button.set(with: actionMap, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
return button
|
return button
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates the item with the passed in action.
|
/// Creates the item with the passed in action.
|
||||||
public static func create(with image: UIImage, action: @escaping BarButtonAction) -> Self {
|
public static func create(with image: UIImage?, action: @escaping BarButtonAction) -> Self {
|
||||||
let button = create(with: image)
|
let button = create(with: image)
|
||||||
button.actionObject?.buttonAction = action
|
button.actionObject?.buttonAction = action
|
||||||
return button
|
return button
|
||||||
|
|||||||
@ -11,35 +11,60 @@ import UIKit
|
|||||||
@objcMembers open class NavigationController: UINavigationController {
|
@objcMembers open class NavigationController: UINavigationController {
|
||||||
public var separatorView: Line?
|
public var separatorView: Line?
|
||||||
|
|
||||||
|
/// Getter for the main navigation controller
|
||||||
public static func navigationController() -> Self? {
|
public static func navigationController() -> Self? {
|
||||||
return MVMCoreActionUtility.initializerClassCheck(MVMCoreUISession.sharedGlobal()?.navigationController, classToVerify: self) as? Self
|
return MVMCoreActionUtility.initializerClassCheck(MVMCoreUISession.sharedGlobal()?.navigationController, classToVerify: self) as? Self
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func style(_ navigationBar: UINavigationBar) {
|
/// Provides MVM styling to the navigation bar. Returns a reference to the line.
|
||||||
|
public static func style(_ navigationBar: UINavigationBar) -> Line {
|
||||||
UIColor.mfSetBackgroundColor(forNavigationBar: .white, navigationBar: navigationBar, transparent: false)
|
UIColor.mfSetBackgroundColor(forNavigationBar: .white, navigationBar: navigationBar, transparent: false)
|
||||||
navigationBar.shadowImage = UIImage()
|
navigationBar.shadowImage = UIImage()
|
||||||
navigationBar.isOpaque = true
|
navigationBar.isOpaque = true
|
||||||
navigationBar.tintColor = .black
|
navigationBar.tintColor = .black
|
||||||
navigationBar.titleTextAttributes = [NSAttributedString.Key.font: MFStyler.fontBoldBodySmall(false)];
|
navigationBar.titleTextAttributes = [NSAttributedString.Key.font: MFStyler.fontBoldBodySmall(false)];
|
||||||
|
return Line(pinTo: navigationBar, edge: .bottom, useMargin: false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets up the application with a navigation controller
|
||||||
public static func setupNavigationController() -> Self? {
|
public static func setupNavigationController() -> Self? {
|
||||||
let navigationController = self.init()
|
let navigationController = self.init()
|
||||||
style(navigationController.navigationBar)
|
navigationController.separatorView = style(navigationController.navigationBar)
|
||||||
navigationController.separatorView = Line(pinTo: navigationController.navigationBar, edge: .bottom, useMargin: false)
|
|
||||||
navigationController.separatorView?.setStyle(.standard)
|
|
||||||
MVMCoreUISession.sharedGlobal()?.navigationController = navigationController
|
MVMCoreUISession.sharedGlobal()?.navigationController = navigationController
|
||||||
MVMCoreNavigationHandler.shared()?.viewControllerToPresentOn = navigationController
|
MVMCoreNavigationHandler.shared()?.viewControllerToPresentOn = navigationController
|
||||||
MVMCoreNavigationHandler.shared()?.navigationController = navigationController
|
MVMCoreNavigationHandler.shared()?.navigationController = navigationController
|
||||||
return navigationController
|
return navigationController
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets up the application with a navigation controller as the main container.
|
||||||
public static func setupNavigationControllerAsMainController() -> Self? {
|
public static func setupNavigationControllerAsMainController() -> Self? {
|
||||||
guard let navigationController = setupNavigationController() else { return nil }
|
guard let navigationController = setupNavigationController() else { return nil }
|
||||||
MVMCoreUISession.sharedGlobal()?.setup(asStandardLoadViewDelegate: navigationController)
|
MVMCoreUISession.sharedGlobal()?.setup(asStandardLoadViewDelegate: navigationController)
|
||||||
return navigationController
|
return navigationController
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convenience function for setting the navigation buttons.
|
||||||
|
public static func setNavigationButtons(navigationController: UINavigationController, navigationItemModel: NavigationItemModelProtocol, viewController: UIViewController) {
|
||||||
|
let delegate = (viewController as? MVMCoreViewControllerProtocol)?.delegateObject?() as? MVMCoreUIDelegateObject
|
||||||
|
var items: [UIBarButtonItem] = []
|
||||||
|
if let backButtonModel = navigationItemModel.backButton,
|
||||||
|
navigationController.viewControllers.count > 1 {
|
||||||
|
items.append(backButtonModel.createNavigationItem(delegateObject: delegate, additionalData: nil))
|
||||||
|
}
|
||||||
|
if let itemModels = navigationItemModel.additionalLeftButtons {
|
||||||
|
for item in itemModels {
|
||||||
|
items.append(item.createNavigationItem(delegateObject: delegate, additionalData: nil))
|
||||||
|
}
|
||||||
|
viewController.navigationItem.leftBarButtonItems = items
|
||||||
|
}
|
||||||
|
if let itemModels = navigationItemModel.additionalRightButtons {
|
||||||
|
for item in itemModels {
|
||||||
|
items.append(item.createNavigationItem(delegateObject: delegate, additionalData: nil))
|
||||||
|
}
|
||||||
|
viewController.navigationItem.rightBarButtonItems = items
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Convenience function for setting navigation bar with model.
|
/// Convenience function for setting navigation bar with model.
|
||||||
public static func set(navigationController: UINavigationController, navigationItemModel: NavigationItemModelProtocol, viewController: UIViewController) {
|
public static func set(navigationController: UINavigationController, navigationItemModel: NavigationItemModelProtocol, viewController: UIViewController) {
|
||||||
viewController.navigationItem.title = navigationItemModel.title
|
viewController.navigationItem.title = navigationItemModel.title
|
||||||
@ -60,16 +85,21 @@ import UIKit
|
|||||||
navigationController.separatorView?.isHidden = navigationItemModel.line?.type ?? .standard == .none
|
navigationController.separatorView?.isHidden = navigationItemModel.line?.type ?? .standard == .none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Let legacy splitview controller handle buttons for now.
|
||||||
|
guard navigationController == MVMCoreUISplitViewController.main()?.splitViewController?.navigationController,
|
||||||
|
navigationController.topViewController == viewController else {
|
||||||
|
// Not the main split view controller, add buttons.
|
||||||
|
setNavigationButtons(navigationController: navigationController, navigationItemModel: navigationItemModel, viewController: viewController)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Update icons if main navigation controller.
|
// Update icons if main navigation controller.
|
||||||
if navigationController == MVMCoreUISplitViewController.main()?.navigationController,
|
MVMCoreUISession.sharedGlobal()?.splitViewController?.setNavigationIconColor(tint)
|
||||||
navigationController.topViewController == viewController {
|
|
||||||
MVMCoreUISession.sharedGlobal()?.splitViewController?.setNavigationIconColor(tint)
|
|
||||||
|
|
||||||
// Update Panels
|
// Update Panels
|
||||||
if let model = navigationItemModel as? PanelNavigationItemModelProtocol {
|
if let model = navigationItemModel as? PanelNavigationItemModelProtocol {
|
||||||
MVMCoreUISplitViewController.main()?.setLeftPanelIsAccessible(model.showLeftPanelButton, for: viewController)
|
MVMCoreUISplitViewController.main()?.setLeftPanelIsAccessible(model.showLeftPanelButton, for: viewController)
|
||||||
MVMCoreUISplitViewController.main()?.setRightPanelIsAccessible(model.showRightPanelButton, for: viewController)
|
MVMCoreUISplitViewController.main()?.setRightPanelIsAccessible(model.showRightPanelButton, for: viewController)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user