Commit 2a740022 by Orkhan Alikhanov

Fixed tabBar item is selected when TabsController shouldSelect returns false

parent 3ae5c61a
...@@ -183,13 +183,13 @@ public protocol TabBarDelegate { ...@@ -183,13 +183,13 @@ public protocol TabBarDelegate {
@objc(_TabBarDelegate) @objc(_TabBarDelegate)
internal protocol _TabBarDelegate { internal protocol _TabBarDelegate {
/** /**
A delegation method that is executed when the tabItem will trigger the A delegation method that is executed to determine if the TabBar should
animation to the next tab. transition to the next tab.
- Parameter tabBar: A TabBar. - Parameter tabBar: A TabBar.
- Parameter tabItem: A TabItem. - Parameter tabItem: A TabItem.
- Returns: A Boolean.
*/ */
@objc func _tabBar(tabBar: TabBar, shouldSelect tabItem: TabItem) -> Bool
optional func _tabBar(tabBar: TabBar, willSelect tabItem: TabItem)
} }
@objc(TabBarStyle) @objc(TabBarStyle)
...@@ -546,6 +546,9 @@ fileprivate extension TabBar { ...@@ -546,6 +546,9 @@ fileprivate extension TabBar {
return return
} }
guard !(false == _delegate?._tabBar(tabBar: self, shouldSelect: tabItem)) else { return }
animate(to: tabItem, isTriggeredByUserInteraction: true) animate(to: tabItem, isTriggeredByUserInteraction: true)
} }
} }
...@@ -592,7 +595,6 @@ fileprivate extension TabBar { ...@@ -592,7 +595,6 @@ fileprivate extension TabBar {
*/ */
func animate(to tabItem: TabItem, isTriggeredByUserInteraction: Bool, completion: ((TabItem) -> Void)? = nil) { func animate(to tabItem: TabItem, isTriggeredByUserInteraction: Bool, completion: ((TabItem) -> Void)? = nil) {
if isTriggeredByUserInteraction { if isTriggeredByUserInteraction {
_delegate?._tabBar?(tabBar: self, willSelect: tabItem)
delegate?.tabBar?(tabBar: self, willSelect: tabItem) delegate?.tabBar?(tabBar: self, willSelect: tabItem)
} }
......
...@@ -398,15 +398,17 @@ extension TabsController { ...@@ -398,15 +398,17 @@ extension TabsController {
- Parameter at index: An Int. - Parameter at index: An Int.
- Parameter isTriggeredByUserInteraction: A boolean indicating whether the - Parameter isTriggeredByUserInteraction: A boolean indicating whether the
state was changed by a user interaction, true if yes, false otherwise. state was changed by a user interaction, true if yes, false otherwise.
- Returns: A boolean indicating whether the transition will take place.
*/ */
private func internalSelect(at index: Int, isTriggeredByUserInteraction: Bool) { @discardableResult
private func internalSelect(at index: Int, isTriggeredByUserInteraction: Bool) -> Bool {
guard index != selectedIndex else { guard index != selectedIndex else {
return return false
} }
if isTriggeredByUserInteraction { if isTriggeredByUserInteraction {
guard !(false == delegate?.tabsController?(tabsController: self, shouldSelect: viewControllers[index])) else { guard !(false == delegate?.tabsController?(tabsController: self, shouldSelect: viewControllers[index])) else {
return return false
} }
} }
...@@ -425,20 +427,18 @@ extension TabsController { ...@@ -425,20 +427,18 @@ extension TabsController {
self?.selectedIndex = index self?.selectedIndex = index
} }
} }
return true
} }
} }
extension TabsController: _TabBarDelegate { extension TabsController: _TabBarDelegate {
@objc @objc
func _tabBar(tabBar: TabBar, willSelect tabItem: TabItem) { func _tabBar(tabBar: TabBar, shouldSelect tabItem: TabItem) -> Bool {
guard !(false == tabBar.delegate?.tabBar?(tabBar: tabBar, shouldSelect: tabItem)) else {
return
}
guard let i = tabBar.tabItems.index(of: tabItem) else { guard let i = tabBar.tabItems.index(of: tabItem) else {
return return false
} }
internalSelect(at: i, isTriggeredByUserInteraction: true) return internalSelect(at: i, isTriggeredByUserInteraction: true)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment