From 5227f53776fe68b27f33b2c76dfcffb5f286f82c Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Mon, 1 Jun 2020 13:53:08 -0400 Subject: [PATCH] view will appear tab change thread protection --- .../HorizontalCombinationViews/TabBar.swift | 14 +++++++++----- MVMCoreUI/BaseControllers/ViewController.swift | 18 ++++++++++++------ .../MVMCoreUISplitViewController.m | 10 +++++----- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/MVMCoreUI/Atomic/Molecules/HorizontalCombinationViews/TabBar.swift b/MVMCoreUI/Atomic/Molecules/HorizontalCombinationViews/TabBar.swift index a99d820f..9d5ab9a5 100644 --- a/MVMCoreUI/Atomic/Molecules/HorizontalCombinationViews/TabBar.swift +++ b/MVMCoreUI/Atomic/Molecules/HorizontalCombinationViews/TabBar.swift @@ -76,14 +76,18 @@ import Foundation // MARK: - TabBarProtocol public func highlightTab(at index: Int) { - guard let newSelectedItem = items?[model.selectedTab] else { return } - selectedItem = newSelectedItem + MVMCoreDispatchUtility.performBlock(onMainThread: { + guard let newSelectedItem = self.items?[index] else { return } + self.selectedItem = newSelectedItem + }) } public func selectTab(at index: Int) { - guard let newSelectedItem = items?[model.selectedTab] else { return } - selectedItem = newSelectedItem - tabBar(self, didSelect: newSelectedItem) + MVMCoreDispatchUtility.performBlock(onMainThread: { + guard let newSelectedItem = self.items?[index] else { return } + self.selectedItem = newSelectedItem + self.tabBar(self, didSelect: newSelectedItem) + }) } } diff --git a/MVMCoreUI/BaseControllers/ViewController.swift b/MVMCoreUI/BaseControllers/ViewController.swift index 92f26569..51fc1b56 100644 --- a/MVMCoreUI/BaseControllers/ViewController.swift +++ b/MVMCoreUI/BaseControllers/ViewController.swift @@ -314,6 +314,11 @@ import UIKit open override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) + // Select tab if needed. + if let tab: Int = loadObject?.pageJSON?["tabBarIndex"] as? Int { + MVMCoreUISplitViewController.main()?.tabBar.highlightTab(at: tab) + } + // Update the navigation bar ui when view is appearing. Can remove check in the future, see viewControllerReady if manager == nil { setNavigationController() @@ -383,12 +388,13 @@ import UIKit MVMCoreUISession.sharedGlobal()?.splitViewController?.showRightPanel(animated: true) } - // Selects the tab if needed. - if let tab: Int = loadObject?.pageJSON?["tabBarIndex"] as? Int { - MVMCoreUISplitViewController.main()?.tabBar.highlightTab(at: tab) - } else if let tab: Int = loadObject?.requestParameters?.actionMap?["tabBarIndex"] as? Int, - error == nil { - MVMCoreUISplitViewController.main()?.tabBar.highlightTab(at: tab) + // Selects the tab if needed. Page driven takes priority over action driven (see viewWillAppear) + if let tab: Int = loadObject?.requestParameters?.actionMap?["tabBarIndex"] as? Int, + error == nil, + loadObject?.pageJSON?["tabBarIndex"] == nil { + MVMCoreDispatchUtility.performBlock(onMainThread: { + MVMCoreUISplitViewController.main()?.tabBar.highlightTab(at: tab) + }) } } diff --git a/MVMCoreUI/Containers/SplitViewController/MVMCoreUISplitViewController.m b/MVMCoreUI/Containers/SplitViewController/MVMCoreUISplitViewController.m index 054fb88d..b46bb184 100644 --- a/MVMCoreUI/Containers/SplitViewController/MVMCoreUISplitViewController.m +++ b/MVMCoreUI/Containers/SplitViewController/MVMCoreUISplitViewController.m @@ -718,7 +718,7 @@ CGFloat const PanelAnimationDuration = 0.2; self.leftPanelWidth = leftPanelWidth; [NSLayoutConstraint constraintWithItem:self.mainView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.leftView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0].active = YES; [NSLayoutConstraint constraintWithItem:self.leftView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0].active = YES; - [NSLayoutConstraint constraintWithItem:self.leftView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0].active = YES; + [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.leftView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0].active = YES; if ([panel respondsToSelector:@selector(buttonForPanel)]) { self.leftPanelButton = [panel buttonForPanel]; @@ -753,7 +753,7 @@ CGFloat const PanelAnimationDuration = 0.2; self.rightPanelWidth = rightPanelWidth; [NSLayoutConstraint constraintWithItem:self.rightView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0].active = YES; [NSLayoutConstraint constraintWithItem:self.rightView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0].active = YES; - [NSLayoutConstraint constraintWithItem:self.rightView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0].active = YES; + [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.rightView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0].active = YES; if ([panel respondsToSelector:@selector(buttonForPanel)]) { self.rightPanelButton = [panel buttonForPanel]; @@ -780,7 +780,7 @@ CGFloat const PanelAnimationDuration = 0.2; } - (void)addTabBar:(nonnull UIView *)tabBar { - [self.view addSubview:tabBar]; + [self.view insertSubview:tabBar atIndex:0]; [tabBar.topAnchor constraintEqualToAnchor:self.mainView.bottomAnchor].active = YES; [NSLayoutConstraint constraintWithItem:tabBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0].active = YES; [NSLayoutConstraint constraintWithItem:tabBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0].active = YES; @@ -887,8 +887,8 @@ CGFloat const PanelAnimationDuration = 0.2; self.mainViewCoverView = coverView; [NSLayoutConstraint constraintWithItem:coverView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:mainView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0].active = YES; [NSLayoutConstraint constraintWithItem:coverView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:mainView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0].active = YES; - [NSLayoutConstraint constraintWithItem:coverView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:mainView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0].active = YES; - [NSLayoutConstraint constraintWithItem:coverView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:mainView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0].active = YES; + [NSLayoutConstraint constraintWithItem:coverView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0].active = YES; + [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:coverView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0].active = YES; [self setupPanels]; }