import SwiftUI struct MainTabView: View { @State private var selectedTab: AppTab = .home @StateObject private var notificationVM = NotificationsViewModel() var body: some View { TabView(selection: $selectedTab) { Group { TrainingPlanView() .tag(AppTab.home) .tabItem { Label(AppTab.home.title, systemImage: AppTab.home.icon) } ChallengesView() .tag(AppTab.challenges) .tabItem { Label(AppTab.challenges.title, systemImage: AppTab.challenges.icon) } ClubsView() .tag(AppTab.clubs) .tabItem { Label(AppTab.clubs.title, systemImage: AppTab.clubs.icon) } NotificationsView(viewModel: notificationVM) .tag(AppTab.notifications) .tabItem { Label(AppTab.notifications.title, systemImage: AppTab.notifications.icon) } .badge(notificationVM.badgeCount) SettingsView() .tag(AppTab.profile) .tabItem { Label(AppTab.profile.title, systemImage: AppTab.profile.icon) } } } .onAppear { Task { await notificationVM.fetchUnreadCount() } } .onChange(of: selectedTab) { _, newTab in if newTab == .notifications { Task { await notificationVM.fetchNotifications() await notificationVM.fetchUnreadCount() } } } } } enum AppTab: String, CaseIterable { case home case challenges case clubs case notifications case profile var title: String { switch self { case .home: return "Home" case .challenges: return "Challenges" case .clubs: return "Clubs" case .notifications: return "Notifications" case .profile: return "Profile" } } var icon: String { switch self { case .home: return "house.fill" case .challenges: return "flag.fill" case .clubs: return "person.3.fill" case .notifications: return "bell.fill" case .profile: return "person.circle" } } } #Preview { MainTabView() }