DomainDig/RootTabView.swift
118 lines · 3836 bytes
1import SwiftUI
2
3private enum RootTab: Hashable {
4 case dashboard
5 case audit
6 case history
7 case inspect
8 case settings
9}
10
11struct RootTabView: View {
12 @Bindable var viewModel: DomainViewModel
13 @State private var purchaseService = PurchaseService.shared
14 @State private var intentRouter = DomainDigIntentRouter.shared
15 @State private var selectedTab: RootTab = FeatureAccessService.currentTier == .free ? .inspect : .dashboard
16
17 var body: some View {
18 let _ = purchaseService.currentTier
19
20 TabView(selection: $selectedTab) {
21 NavigationStack {
22 DashboardView(viewModel: viewModel)
23 }
24 .tabItem {
25 Label("Dashboard", systemImage: "square.grid.2x2")
26 }
27 .tag(RootTab.dashboard)
28
29 NavigationStack {
30 AuditListView(viewModel: viewModel)
31 }
32 .tabItem {
33 Label("Audit", systemImage: "checklist")
34 }
35 .tag(RootTab.audit)
36
37 NavigationStack {
38 HistoryView(viewModel: viewModel)
39 }
40 .tabItem {
41 Label("History", systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90")
42 }
43 .tag(RootTab.history)
44
45 ContentView(viewModel: viewModel)
46 .tabItem {
47 Label("Inspect", systemImage: "magnifyingglass")
48 }
49 .tag(RootTab.inspect)
50
51 NavigationStack {
52 SettingsView(viewModel: viewModel)
53 }
54 .tabItem {
55 Label("Settings", systemImage: "gearshape")
56 }
57 .tag(RootTab.settings)
58 }
59 .sheet(isPresented: Binding(
60 get: { viewModel.isPaywallPresented },
61 set: { viewModel.isPaywallPresented = $0 }
62 )) {
63 PaywallView()
64 }
65 .alert(item: Binding(
66 get: { viewModel.upgradePrompt },
67 set: { viewModel.upgradePrompt = $0 }
68 )) { prompt in
69 Alert(
70 title: Text(prompt.title),
71 message: Text(prompt.message),
72 primaryButton: .default(Text("Open Paywall")) {
73 viewModel.upgradePrompt = nil
74 viewModel.isPaywallPresented = true
75 },
76 secondaryButton: .cancel(Text("Continue")) {
77 viewModel.upgradePrompt = nil
78 }
79 )
80 }
81 .onChange(of: purchaseService.currentTier) { _, newValue in
82 if newValue != .free, selectedTab == .inspect, viewModel.trackedDomains.isEmpty == false {
83 selectedTab = .dashboard
84 }
85 }
86 .onOpenURL { url in
87 guard let action = DomainDigDeepLink.action(from: url) else { return }
88 perform(action)
89 }
90 .onChange(of: intentRouter.pendingAction) { _, action in
91 consume(action)
92 }
93 .task {
94 consume(intentRouter.pendingAction)
95 }
96 }
97
98 private func consume(_ action: DomainDigDeepLink.Action?) {
99 guard let action else { return }
100 intentRouter.pendingAction = nil
101 perform(action)
102 }
103
104 private func perform(_ action: DomainDigDeepLink.Action) {
105 switch action {
106 case let .inspect(domain):
107 viewModel.domain = domain
108 selectedTab = .inspect
109 viewModel.run()
110 case let .watch(domain):
111 // On success show the dashboard (where tracked domains live); on
112 // failure stay put so the upgrade/paywall alert surfaces in place.
113 if viewModel.trackDomain(domain: domain, availabilityStatus: nil) {
114 selectedTab = .dashboard
115 }
116 }
117 }
118}