diff --git a/MVMCoreUI/Containers/SplitViewController/MVMCoreUISplitViewController+Extension.swift b/MVMCoreUI/Containers/SplitViewController/MVMCoreUISplitViewController+Extension.swift index 12935302..3c6c878b 100644 --- a/MVMCoreUI/Containers/SplitViewController/MVMCoreUISplitViewController+Extension.swift +++ b/MVMCoreUI/Containers/SplitViewController/MVMCoreUISplitViewController+Extension.swift @@ -12,6 +12,64 @@ import UIKit // Navigation bar update functions public extension MVMCoreUISplitViewController { + /// Updates the state for various controls (navigation, tab, progress) for the controller. + func updateState(with viewController: UIViewController) { + guard let navigationController = navigationController, + navigationController.isDisplayed(viewController: viewController) else { return } + updateNavigationBarFor(viewController: viewController) + updateProgressBar(for: viewController) + updateTabBar(for: viewController) + } + + // MARK: - Progress Bar + /// Updates the progress bar based on the page json for the view controller. + func updateProgressBar(for viewController: UIViewController) { + guard let viewController = viewController as? MVMCoreViewControllerProtocol else { return } + var progress: Float = 0.0 + if let progressString = viewController.loadObject??.pageJSON?.optionalStringForKey(KeyProgressPercent), + let floatValue = Float(progressString) { + progress = floatValue/100 + } + setBottomProgressBarProgress(progress) + } + + // MARK: - Tab Bar + /// Updates the tab bar based on the page json for the view controller. + func updateTabBar(for viewController: UIViewController) { + let mvmViewController = viewController as? MVMCoreViewControllerProtocol + tabBar?.delegateObject = mvmViewController?.delegateObject?() as? MVMCoreUIDelegateObject + + let navigationIndex = (MVMCoreNavigationHandler.shared()?.getViewControllers(for: navigationController)?.count ?? 1) - 1 + + // Set the highlighted index. In terms of priority, Page > Action > Previous. + if let index = ((viewController as? PageProtocol)?.pageModel as? TabPageModelProtocol)?.tabBarIndex { + tabBar?.highlightTab(at: index) + } else if let index = mvmViewController?.loadObject??.requestParameters?.actionMap?["tabBarIndex"] as? Int { + tabBar?.highlightTab(at: index) + } else if navigationIndex < tabBarIndices.count { + let index = (tabBarIndices[navigationIndex] as! NSNumber).intValue + tabBar?.highlightTab(at: index) + } + + // Store current tab index, so we can switch back when going back in hierarchy. + if tabBarIndices.count > 0 { + tabBarIndices.removeObjects(in: NSRange(location: navigationIndex, length: tabBarIndices.count - navigationIndex)) + } + if let currentIndex = tabBar?.currentTabIndex() { + tabBarIndices.add(NSNumber(integerLiteral: currentIndex)) + } + + // Show/Hide. In terms of priority, Page > Action > Always Show. + if let hidden = ((viewController as? PageProtocol)?.pageModel as? TabPageModelProtocol)?.tabBarHidden { + updateTabBarShowing(!hidden) + } else if let hidden = mvmViewController?.loadObject??.requestParameters?.actionMap?["tabBarHidden"] as? Bool { + updateTabBarShowing(!hidden) + } else { + updateTabBarShowing(true) + } + } + + // MARK: - Navigation Bar /// Convenience function. Sets the navigation and split view properties for the view controller. Panel access is determined if view controller is a detail view protocol. func setNavigationBar(for viewController: UIViewController, navigationController: UINavigationController, navigationItemModel: NavigationItemModelProtocol) { guard navigationController == self.navigationController, @@ -122,6 +180,7 @@ public extension MVMCoreUISplitViewController { setStatusBarForCurrentViewController() } + // MARK: - Status Bar /// Returns the bar style for the background color. Light if on a dark background, otherwise default. func getStatusBarStyle(for backgroundColor: UIColor?) -> UIStatusBarStyle { var greyScale: CGFloat = 0 @@ -155,10 +214,10 @@ extension MVMCoreUISplitViewController: MVMCoreViewManagerProtocol { public func willDisplay(_ viewController: UIViewController) { setupPanels() - updateNavigationBarFor(viewController: viewController) + updateState(with: viewController) } public func newDataReceived(in viewController: UIViewController) { - updateNavigationBarFor(viewController: viewController) + updateState(with: viewController) } }