Skip to content

Commit dc4e2c9

Browse files
authored
Use native tabs in WPTabBarController (#26439)
* Use native tabs in WPTabBarController * Clear a replaced tab selection before installing new tabs Before iOS 27, UIKit keeps returning a removed tab from selectedTab when its replacement has the same identifier. Clearing the selection first makes the new tab the selection, so reselect tracking, scroll-to-top and orientation use it. Also report the iOS 26 minimization test as skipped on older systems.
1 parent 9554d7f commit dc4e2c9

8 files changed

Lines changed: 357 additions & 86 deletions

File tree

‎Tests/KeystoneTests/Tests/Misc/RootViewCoordinatorTests.swift‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ final class RootViewCoordinatorTests: XCTestCase {
8282
tabBarController.showNotificationsTab { _ in didRunCompletion = true }
8383

8484
// Then
85-
XCTAssertEqual(tabBarController.selectedIndex, WPTab.notifications.rawValue)
85+
XCTAssertEqual(tabBarController.selectedAppTab, .notifications)
8686
XCTAssertFalse(didRunCompletion)
8787
}
8888

@@ -95,7 +95,7 @@ final class RootViewCoordinatorTests: XCTestCase {
9595
tabBarController.showNotificationsTab { receivedList = $0 }
9696

9797
// Then
98-
XCTAssertEqual(tabBarController.selectedIndex, WPTab.notifications.rawValue)
98+
XCTAssertEqual(tabBarController.selectedAppTab, .notifications)
9999
XCTAssertNotNil(receivedList)
100100
XCTAssertIdentical(receivedList, tabBarController.notificationsViewController)
101101
}
@@ -109,7 +109,7 @@ final class RootViewCoordinatorTests: XCTestCase {
109109
presenter.switchNotificationsTabToNotificationSettings()
110110

111111
// Then
112-
XCTAssertEqual(tabBarController.selectedIndex, WPTab.notifications.rawValue)
112+
XCTAssertEqual(tabBarController.selectedAppTab, .notifications)
113113
}
114114

115115
func testSignInResolvesStaticScreensWithReaderBeforeTheUIReloads() {
@@ -261,7 +261,7 @@ final class RootViewCoordinatorTests: XCTestCase {
261261
JetpackFeaturesRemovalCoordinator.currentAppUIType = .simplified
262262
let presenter = SplitViewRootPresenter()
263263
let tabBar = try XCTUnwrap(compactTabBar(of: presenter))
264-
tabBar.selectedIndex = WPTab.notifications.rawValue
264+
tabBar.selectTab(.notifications)
265265
XCTAssertNotNil(tabBar.notificationsViewController)
266266

267267
// When signing in from the sidebar sets the account, even if the site sync then fails and
@@ -273,7 +273,7 @@ final class RootViewCoordinatorTests: XCTestCase {
273273
XCTAssertTrue(tabBar.showsPoster(in: .notifications))
274274
XCTAssertNil(tabBar.notificationsViewController)
275275
XCTAssertFalse(tabBar.showsPoster(in: .reader))
276-
XCTAssertEqual(tabBar.selectedIndex, WPTab.notifications.rawValue)
276+
XCTAssertEqual(tabBar.selectedAppTab, .notifications)
277277

278278
// And a completed sign-in, which rebuilds every tab, keeps the poster
279279
NotificationCenter.default.post(
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
import Testing
2+
import UIKit
3+
import WordPressShared
4+
5+
@testable import WordPress
6+
@testable import WordPressData
7+
8+
@MainActor
9+
@Suite(.serialized)
10+
struct WPTabBarControllerTests {
11+
@Test func nativeTabsKeepTheExistingDestinations() throws {
12+
try withController { controller in
13+
#expect(controller.tabs.map(\.identifier) == ["mysites", "reader", "notifications", "me"])
14+
#expect(controller.tabs.allSatisfy { $0.preferredPlacement == .fixed })
15+
#expect(
16+
controller.tabs.map(\.accessibilityIdentifier) == [
17+
"tabbar_mysites", "tabbar_reader", "tabbar_notifications", "tabbar_me"
18+
]
19+
)
20+
#expect(controller.mode == .tabBar)
21+
22+
controller.showReaderTab()
23+
#expect(controller.selectedTab?.viewController === controller.readerNavigationController)
24+
controller.showNotificationsTab()
25+
#expect(controller.selectedTab?.viewController === controller.notificationsNavigationController)
26+
controller.showMeTab()
27+
#expect(controller.selectedTab?.viewController === controller.meNavigationController)
28+
controller.showMySitesTab()
29+
#expect(controller.selectedTab?.viewController === controller.mySitesCoordinator.rootViewController)
30+
}
31+
}
32+
33+
@available(iOS 26, *)
34+
@Test func programmaticSelectionUpdatesMinimization() throws {
35+
try withController { controller in
36+
controller.showReaderTab()
37+
#expect(controller.tabBarMinimizeBehavior == .onScrollDown)
38+
controller.showMySitesTab()
39+
#expect(controller.tabBarMinimizeBehavior == .never)
40+
controller.selectedTab = try #require(controller.tab(forAppTab: .reader))
41+
#expect(controller.tabBarMinimizeBehavior == .onScrollDown)
42+
controller.showMeTab()
43+
#expect(controller.tabBarMinimizeBehavior == .never)
44+
}
45+
}
46+
47+
@Test func replacingNotificationsPreservesOtherTabsAndTheirStacks() throws {
48+
try withController { controller in
49+
let originalTabs = controller.tabs
50+
let detail = UIViewController()
51+
controller.readerNavigationController.pushViewController(detail, animated: false)
52+
controller.showNotificationsTab()
53+
54+
controller.shouldUseStaticNotifications = false
55+
56+
#expect(controller.selectedAppTab == .notifications)
57+
#expect(controller.tabs[2] !== originalTabs[2])
58+
for index in [0, 1, 3] {
59+
#expect(controller.tabs[index] === originalTabs[index])
60+
}
61+
#expect(controller.readerNavigationController.topViewController === detail)
62+
#expect(controller.selectedTab?.viewController === controller.notificationsNavigationController)
63+
#expect(controller.notificationsViewController != nil)
64+
}
65+
}
66+
67+
@Test func signInRebuildsAccountTabsAndSelectsMySite() throws {
68+
try withController { controller in
69+
let originalTabs = controller.tabs
70+
controller.showMeTab()
71+
72+
NotificationCenter.default.post(
73+
name: NSNotification.Name(rawValue: WordPressAuthenticationManager.WPSigninDidFinishNotification),
74+
object: nil
75+
)
76+
77+
#expect(controller.selectedAppTab == .mySites)
78+
#expect(controller.tabs[0] === originalTabs[0])
79+
for index in [1, 2, 3] {
80+
#expect(controller.tabs[index] !== originalTabs[index])
81+
}
82+
#expect(controller.selectedTab?.viewController === controller.mySitesCoordinator.rootViewController)
83+
}
84+
}
85+
86+
@Test func nativeSelectionTracksAccessWithoutTrackingReselection() throws {
87+
try withController(staticReader: false) { controller in
88+
let window = show(controller)
89+
defer { window.isHidden = true }
90+
TestAnalyticsTracker.setup()
91+
defer { TestAnalyticsTracker.tearDown() }
92+
let reader = try #require(controller.tab(forAppTab: .reader))
93+
let delegate = try #require(controller.delegate)
94+
95+
#expect(delegate.tabBarController?(controller, shouldSelectTab: reader) == true)
96+
#expect(readerAccessCount == 1)
97+
98+
controller.selectedTab = reader
99+
#expect(delegate.tabBarController?(controller, shouldSelectTab: reader) == true)
100+
#expect(readerAccessCount == 1)
101+
102+
controller.trackTabAccessOnAppDidBecomeActive()
103+
#expect(readerAccessCount == 2)
104+
}
105+
}
106+
107+
@Test func readerPosterDoesNotTrackReaderAccess() throws {
108+
try withController { controller in
109+
let window = show(controller)
110+
defer { window.isHidden = true }
111+
TestAnalyticsTracker.setup()
112+
defer { TestAnalyticsTracker.tearDown() }
113+
114+
let reader = try #require(controller.tab(forAppTab: .reader))
115+
#expect(controller.delegate?.tabBarController?(controller, shouldSelectTab: reader) == true)
116+
controller.selectedTab = reader
117+
controller.trackTabAccessOnAppDidBecomeActive()
118+
#expect(readerAccessCount == 0)
119+
}
120+
}
121+
122+
@Test func notificationIndicatorUpdatesTheNativeTab() throws {
123+
try withController { controller in
124+
let notifications = try #require(controller.tab(forAppTab: .notifications))
125+
notifications.image = UIImage(systemName: "pencil")
126+
notifications.accessibilityLabel = "Pending update"
127+
128+
controller.updateNotificationBadgeVisibility()
129+
controller.viewDidAppear(false)
130+
131+
#expect(notifications.image?.pngData() == UIImage(named: "tab-bar-notifications")?.pngData())
132+
#expect(notifications.accessibilityLabel == "Notifications")
133+
}
134+
}
135+
136+
@Test func unreadActivityUpdatesTheNativeTabAfterAppearance() throws {
137+
let defaults = UserPersistentStoreFactory.userDefaultsInstance()
138+
let key = "welcomeNotificationSeen"
139+
let original = defaults.object(forKey: key)
140+
defaults.set(false, forKey: key)
141+
defer { defaults.set(original, forKey: key) }
142+
143+
try withController { controller in
144+
controller.shouldUseStaticNotifications = false
145+
let notifications = try #require(controller.tab(forAppTab: .notifications))
146+
controller.viewDidAppear(false)
147+
148+
let unreadImage = try #require(UIImage(named: "tab-bar-notifications-unread"))
149+
#expect(notifications.image?.pngData() == unreadImage.pngData())
150+
#expect(notifications.image?.renderingMode == .alwaysOriginal)
151+
#expect(notifications.accessibilityLabel == "Notifications Unread")
152+
}
153+
}
154+
155+
@Test func orientationFollowsTheSelectedNativeTab() throws {
156+
try withController { controller in
157+
let landscape = UITab(title: "Landscape", image: nil, identifier: "landscape") { _ in
158+
LandscapeViewController()
159+
}
160+
controller.tabs = [landscape]
161+
controller.selectedTab = landscape
162+
#expect(controller.supportedInterfaceOrientations == .landscape)
163+
}
164+
}
165+
166+
@Test func tabProvidersDoNotRetainTheTabBarController() throws {
167+
weak var releasedController: WPTabBarController?
168+
try autoreleasepool {
169+
try withController { controller in
170+
releasedController = controller
171+
for tab in controller.tabs {
172+
_ = tab.viewController
173+
}
174+
}
175+
}
176+
#expect(releasedController == nil)
177+
}
178+
179+
private var readerAccessCount: Int {
180+
TestAnalyticsTracker.tracked.filter { $0.stat == .readerAccessed }.count
181+
}
182+
183+
private func withController(
184+
staticReader: Bool = true,
185+
_ body: (WPTabBarController) throws -> Void
186+
) throws {
187+
let original = ContextManager.overrideInstance
188+
let context = ContextManager.forTesting()
189+
ContextManager.overrideInstance = context
190+
defer {
191+
context.mainContext.reset()
192+
ContextManager.overrideInstance = original
193+
}
194+
try body(WPTabBarController(staticReader: staticReader, staticNotifications: true))
195+
}
196+
197+
private func show(_ controller: UIViewController) -> UIWindow {
198+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844))
199+
window.rootViewController = controller
200+
window.isHidden = false
201+
window.layoutIfNeeded()
202+
return window
203+
}
204+
}
205+
206+
private final class LandscapeViewController: UIViewController {
207+
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { .landscape }
208+
}

‎WordPress/Classes/System/Root View/WPTabBarController+RootViewPresenter.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ extension WPTabBarController: RootViewPresenter {
1717
}
1818

1919
func currentlyVisibleBlog() -> Blog? {
20-
guard selectedIndex == WPTab.mySites.rawValue else {
20+
guard selectedAppTab == .mySites else {
2121
return nil
2222
}
2323
return mySitesCoordinator.currentBlog
2424
}
2525

2626
func showNotificationsTab(completion: ((NotificationsViewController) -> Void)?) {
27-
// UITabBarController.selectedIndex must be used from main thread only.
27+
// Tab selection must run on the main thread.
2828
wpAssert(Thread.isMainThread)
2929

30-
selectedIndex = WPTab.notifications.rawValue
30+
selectTab(.notifications)
3131

3232
// A tab with the Moved-to-Jetpack poster has no list, so the request ends on the poster.
3333
if let notificationsViewController {

‎WordPress/Classes/ViewRelated/System/WPTabBarController+MeTab.swift‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,36 @@ extension WPTabBarController {
2525
/// Shows the account avatar on the Me tab item.
2626
@objc public func observeAccountAvatar() {
2727
meTabAvatarSubscription = AccountAvatar.shared.$image.sink { [weak self] avatar in
28-
self?.performTabBarItemUpdate {
29-
self?.meNavigationController.tabBarItem.configureAccountAvatar(avatar)
28+
guard let self else { return }
29+
self.performTabBarItemUpdate { [weak self] in
30+
guard let self, let tab = self.tab(forAppTab: .me) else { return }
31+
tab.configureAccountAvatar(avatar, isSelected: self.selectedAppTab == .me)
3032
}
3133
}
3234
}
35+
36+
@objc func updateAccountAvatarForSelection() {
37+
// Before iOS 26, the inactive Me tab uses a dimmed avatar.
38+
guard #unavailable(iOS 26) else { return }
39+
performTabBarItemUpdate { [weak self] in
40+
guard let self, let tab = self.tab(forAppTab: .me) else { return }
41+
tab.configureAccountAvatar(AccountAvatar.shared.image, isSelected: self.selectedAppTab == .me)
42+
}
43+
}
44+
}
45+
46+
extension UITab {
47+
func configureAccountAvatar(_ avatar: UIImage?, isSelected: Bool) {
48+
guard let icon = avatar?.gravatarIcon(size: 26) else {
49+
image = UIImage(named: "tab-bar-me")
50+
return
51+
}
52+
if #available(iOS 26, *) {
53+
image = icon
54+
} else {
55+
image = isSelected ? icon : icon.blackAndWhite?.withAlpha(0.36)
56+
}
57+
}
3358
}
3459

3560
extension UITabBarItem {

‎WordPress/Classes/ViewRelated/System/WPTabBarController+Swift.swift‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import WordPressUI
55

66
// MARK: - Tab Access Tracking
77

8-
@objc public enum WPTab: Int {
8+
@objc public enum WPTab: Int, CaseIterable {
99
case mySites
1010
case reader
1111
case notifications
@@ -30,7 +30,7 @@ extension WPTabBarController {
3030
/// commands – would otherwise leave the behavior set for whichever tab was tapped last.
3131
@objc public func updateTabBarMinimizeBehavior() {
3232
guard #available(iOS 26.0, *) else { return }
33-
tabBarMinimizeBehavior = selectedIndex == WPTab.reader.rawValue ? .onScrollDown : .never
33+
tabBarMinimizeBehavior = selectedAppTab == .reader ? .onScrollDown : .never
3434
}
3535

3636
@objc public class func readerLocalizedTitle() -> String {
@@ -54,7 +54,7 @@ extension WPTabBarController {
5454
WordPressAuthenticationManager.WPSigninDidFinishNotification
5555
}
5656

57-
private static let tabIndexToStatMap: [WPTab: WPAnalyticsStat] = [
57+
private static let tabToStatMap: [WPTab: WPAnalyticsStat] = [
5858
.mySites: .mySitesTabAccessed, .reader: .readerAccessed
5959
]
6060

@@ -94,7 +94,7 @@ extension WPTabBarController {
9494
}
9595

9696
@objc public func trackTabAccessOnAppDidBecomeActive() {
97-
trackTabAccessForTabIndex(selectedIndex)
97+
trackTabAccessForTab(selectedTab)
9898
}
9999

100100
/// Reset `shouldTrackTabAccessOnViewDidAppear` if the user has logged out.
@@ -116,7 +116,7 @@ extension WPTabBarController {
116116
return
117117
}
118118

119-
if trackTabAccessForTabIndex(selectedIndex) {
119+
if trackTabAccessForTab(selectedTab) {
120120
shouldTrackTabAccessOnViewDidAppear = false
121121
}
122122
}
@@ -129,7 +129,7 @@ extension WPTabBarController {
129129
/// - The user selected a different tab
130130
/// - After logging in (and this VC is shown)
131131
@objc @discardableResult
132-
public func trackTabAccessForTabIndex(_ tabIndex: Int) -> Bool {
132+
public func trackTabAccessForTab(_ tab: UITab?) -> Bool {
133133
// Since this ViewController is a singleton, it can be active **behind** the login view.
134134
// The `isViewonScreen()` prevents us from tracking this. It also helps us in avoiding
135135
// tracking events if a modal dialog is shown and the app is placed in the background
@@ -138,8 +138,8 @@ extension WPTabBarController {
138138
return false
139139
}
140140

141-
guard let tabType = WPTab(rawValue: Int(tabIndex)),
142-
let stat = WPTabBarController.tabIndexToStatMap[tabType]
141+
guard let tabType = WPTab(tab: tab),
142+
let stat = WPTabBarController.tabToStatMap[tabType]
143143
else {
144144
return false
145145
}
@@ -154,7 +154,7 @@ extension WPTabBarController {
154154
}
155155

156156
open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
157-
guard let selectedViewController else {
157+
guard let selectedViewController = selectedTab?.viewController else {
158158
return super.supportedInterfaceOrientations
159159
}
160160
return selectedViewController.supportedInterfaceOrientations
@@ -192,7 +192,7 @@ extension WPTabBarController {
192192

193193
@objc public func switchNotificationsTabToNotificationSettings() {
194194
showNotificationsTab()
195-
notificationsNavigationController?.popToRootViewController(animated: false)
195+
notificationsNavigationController.popToRootViewController(animated: false)
196196
notificationsViewController?.showNotificationSettings()
197197
}
198198
}

0 commit comments

Comments
 (0)