DomainDig/RootTabView.swift

1715e59287a7b48b29ed8cbf2cd45fcc92ec3126
domain-dig/DomainDig/RootTabView.swift history · blame · raw

77 lines · 2462 bytes

 1import SwiftUI
 2
 3private enum RootTab: Hashable {
 4    case dashboard
 5    case history
 6    case inspect
 7    case settings
 8}
 9
10struct RootTabView: View {
11    @Bindable var viewModel: DomainViewModel
12    @State private var purchaseService = PurchaseService.shared
13    @State private var selectedTab: RootTab = FeatureAccessService.currentTier == .free ? .inspect : .dashboard
14
15    var body: some View {
16        let _ = purchaseService.currentTier
17
18        TabView(selection: $selectedTab) {
19            NavigationStack {
20                DashboardView(viewModel: viewModel)
21            }
22            .tabItem {
23                Label("Dashboard", systemImage: "square.grid.2x2")
24            }
25            .tag(RootTab.dashboard)
26
27            NavigationStack {
28                HistoryView(viewModel: viewModel)
29            }
30            .tabItem {
31                Label("History", systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90")
32            }
33            .tag(RootTab.history)
34
35            ContentView(viewModel: viewModel)
36                .tabItem {
37                    Label("Inspect", systemImage: "magnifyingglass")
38                }
39                .tag(RootTab.inspect)
40
41            NavigationStack {
42                SettingsView(viewModel: viewModel)
43            }
44            .tabItem {
45                Label("Settings", systemImage: "gearshape")
46            }
47            .tag(RootTab.settings)
48        }
49        .sheet(isPresented: Binding(
50            get: { viewModel.isPaywallPresented },
51            set: { viewModel.isPaywallPresented = $0 }
52        )) {
53            PaywallView()
54        }
55        .alert(item: Binding(
56            get: { viewModel.upgradePrompt },
57            set: { viewModel.upgradePrompt = $0 }
58        )) { prompt in
59            Alert(
60                title: Text(prompt.title),
61                message: Text(prompt.message),
62                primaryButton: .default(Text("Open Paywall")) {
63                    viewModel.upgradePrompt = nil
64                    viewModel.isPaywallPresented = true
65                },
66                secondaryButton: .cancel(Text("Continue")) {
67                    viewModel.upgradePrompt = nil
68                }
69            )
70        }
71        .onChange(of: purchaseService.currentTier) { _, newValue in
72            if newValue != .free, selectedTab == .inspect, viewModel.trackedDomains.isEmpty == false {
73                selectedTab = .dashboard
74            }
75        }
76    }
77}