IceCubes/IceCubesApp/App/Main/IceCubesApp+Tabbar.swift

48 lines
1.3 KiB
Swift
Raw Normal View History

2023-10-26 06:23:00 +02:00
import Env
2023-11-01 18:58:44 +01:00
import SwiftUI
2023-10-26 06:23:00 +02:00
extension IceCubesApp {
var tabBarView: some View {
TabView(selection: .init(get: {
selectedTab
}, set: { newTab in
if newTab == selectedTab {
/// Stupid hack to trigger onChange binding in tab views.
popToRootTab = .other
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
popToRootTab = selectedTab
}
}
HapticManager.shared.fireHaptic(.tabSelection)
SoundEffectManager.shared.playSound(.tabSelection)
2023-10-26 06:23:00 +02:00
selectedTab = newTab
})) {
ForEach(availableTabs) { tab in
tab.makeContentView(selectedTab: $selectedTab, popToRootTab: $popToRootTab)
2023-10-26 06:23:00 +02:00
.tabItem {
if userPreferences.showiPhoneTabLabel {
tab.label
} else {
Image(systemName: tab.iconName)
}
}
.tag(tab)
.badge(badgeFor(tab: tab))
.toolbarBackground(theme.primaryBackgroundColor.opacity(0.50), for: .tabBar)
}
}
.id(appAccountsManager.currentClient.id)
}
2023-11-01 18:58:44 +01:00
2023-10-26 06:23:00 +02:00
private func badgeFor(tab: Tab) -> Int {
if tab == .notifications, selectedTab != tab,
let token = appAccountsManager.currentAccount.oauthToken
{
return watcher.unreadNotificationsCount + (userPreferences.notificationsCount[token] ?? 0)
}
return 0
}
}