From f3a48118f0eca87064565fc0a79c473f8e31d2ce Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Tue, 26 Dec 2023 13:51:41 +0100 Subject: [PATCH] Fix #1772 --- IceCubesApp/App/Main/IceCubesApp+Tabbar.swift | 11 +---------- IceCubesApp/App/Tabs/NotificationTab.swift | 7 +++---- IceCubesApp/App/Tabs/Tabs.swift | 11 +++++++++++ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/IceCubesApp/App/Main/IceCubesApp+Tabbar.swift b/IceCubesApp/App/Main/IceCubesApp+Tabbar.swift index 7d492557..b0808297 100644 --- a/IceCubesApp/App/Main/IceCubesApp+Tabbar.swift +++ b/IceCubesApp/App/Main/IceCubesApp+Tabbar.swift @@ -18,16 +18,6 @@ extension IceCubesApp { SoundEffectManager.shared.playSound(.tabSelection) selectedTab = newTab - - DispatchQueue.main.async { - if selectedTab == .notifications, - let token = appAccountsManager.currentAccount.oauthToken - { - userPreferences.notificationsCount[token] = 0 - watcher.unreadNotificationsCount = 0 - } - } - })) { ForEach(availableTabs) { tab in tab.makeContentView(popToRootTab: $popToRootTab) @@ -44,6 +34,7 @@ extension IceCubesApp { } } .id(appAccountsManager.currentClient.id) + .environment(\.selectedTab, selectedTab) } private func badgeFor(tab: Tab) -> Int { diff --git a/IceCubesApp/App/Tabs/NotificationTab.swift b/IceCubesApp/App/Tabs/NotificationTab.swift index 6a300443..d8dcd8b6 100644 --- a/IceCubesApp/App/Tabs/NotificationTab.swift +++ b/IceCubesApp/App/Tabs/NotificationTab.swift @@ -11,6 +11,7 @@ import Timeline struct NotificationsTab: View { @Environment(\.isSecondaryColumn) private var isSecondaryColumn: Bool @Environment(\.scenePhase) private var scenePhase + @Environment(\.selectedTab) private var selectedTab: Tab @Environment(Theme.self) private var theme @Environment(Client.self) private var client @@ -58,9 +59,7 @@ struct NotificationsTab: View { } .onAppear { routerPath.client = client - if isSecondaryColumn { - clearNotifications() - } + clearNotifications() } .withSafariRouter() .environment(routerPath) @@ -101,7 +100,7 @@ struct NotificationsTab: View { } private func clearNotifications() { - if isSecondaryColumn { + if selectedTab == .notifications || isSecondaryColumn { if let token = appAccount.currentAccount.oauthToken { userPreferences.notificationsCount[token] = 0 } diff --git a/IceCubesApp/App/Tabs/Tabs.swift b/IceCubesApp/App/Tabs/Tabs.swift index c1f4b9b2..b6105cf3 100644 --- a/IceCubesApp/App/Tabs/Tabs.swift +++ b/IceCubesApp/App/Tabs/Tabs.swift @@ -113,3 +113,14 @@ enum Tab: Int, Identifiable, Hashable { } } } + +private struct SelectedTab: EnvironmentKey { + static let defaultValue: Tab = .timeline +} + +extension EnvironmentValues { + var selectedTab: Tab { + get { self[SelectedTab.self] } + set { self[SelectedTab.self] = newValue } + } +}