DomainDig/RootTabView.swift
185 lines · 6025 bytes
1import SwiftUI
2
3private enum RootTab: Hashable, CaseIterable {
4 case dashboard
5 case audit
6 case history
7 case inspect
8 case settings
9
10 var title: String {
11 switch self {
12 case .dashboard: return "Dashboard"
13 case .audit: return "Audit"
14 case .history: return "History"
15 case .inspect: return "Inspect"
16 case .settings: return "Settings"
17 }
18 }
19
20 var systemImage: String {
21 switch self {
22 case .dashboard: return "square.grid.2x2"
23 case .audit: return "checklist"
24 case .history: return "clock.arrow.trianglehead.counterclockwise.rotate.90"
25 case .inspect: return "magnifyingglass"
26 case .settings: return "gearshape"
27 }
28 }
29}
30
31struct RootTabView: View {
32 @Bindable var viewModel: DomainViewModel
33 @Environment(\.horizontalSizeClass) private var horizontalSizeClass
34 @State private var purchaseService = PurchaseService.shared
35 @State private var intentRouter = DomainDigIntentRouter.shared
36 @State private var detailDomain: TrackedDomain?
37 @State private var selectedTab: RootTab = FeatureAccessService.currentTier == .free ? .inspect : .dashboard
38
39 var body: some View {
40 Group {
41 if horizontalSizeClass == .regular {
42 splitLayout
43 } else {
44 tabLayout
45 }
46 }
47 .sheet(isPresented: Binding(
48 get: { viewModel.isPaywallPresented },
49 set: { viewModel.isPaywallPresented = $0 }
50 )) {
51 PaywallView()
52 }
53 .alert(item: Binding(
54 get: { viewModel.upgradePrompt },
55 set: { viewModel.upgradePrompt = $0 }
56 )) { prompt in
57 Alert(
58 title: Text(prompt.title),
59 message: Text(prompt.message),
60 primaryButton: .default(Text("Open Paywall")) {
61 viewModel.upgradePrompt = nil
62 viewModel.isPaywallPresented = true
63 },
64 secondaryButton: .cancel(Text("Continue")) {
65 viewModel.upgradePrompt = nil
66 }
67 )
68 }
69 .sheet(item: $detailDomain) { trackedDomain in
70 NavigationStack {
71 TrackedDomainDetailView(viewModel: viewModel, trackedDomain: trackedDomain)
72 }
73 }
74 .onChange(of: purchaseService.currentTier) { _, newValue in
75 if newValue != .free, selectedTab == .inspect, viewModel.trackedDomains.isEmpty == false {
76 selectedTab = .dashboard
77 }
78 }
79 .onOpenURL { url in
80 guard let action = DomainDigDeepLink.action(from: url) else { return }
81 perform(action)
82 }
83 .onChange(of: intentRouter.pendingAction) { _, action in
84 consume(action)
85 }
86 .task {
87 consume(intentRouter.pendingAction)
88 }
89 }
90
91 // MARK: Layouts
92
93 /// Compact (iPhone, iPad slide-over): the classic tab bar.
94 private var tabLayout: some View {
95 TabView(selection: $selectedTab) {
96 ForEach(RootTab.allCases, id: \.self) { tab in
97 section(for: tab)
98 .tabItem {
99 Label(tab.title, systemImage: tab.systemImage)
100 }
101 .tag(tab)
102 }
103 }
104 }
105
106 /// Regular width (iPad, large iPhone landscape): two-column split view.
107 private var splitLayout: some View {
108 NavigationSplitView {
109 List(RootTab.allCases, id: \.self, selection: sidebarSelection) { tab in
110 Label(tab.title, systemImage: tab.systemImage)
111 .tag(tab)
112 }
113 .navigationTitle("DomainDig")
114 } detail: {
115 section(for: selectedTab)
116 }
117 }
118
119 private var sidebarSelection: Binding<RootTab?> {
120 Binding(
121 get: { selectedTab },
122 set: { selectedTab = $0 ?? selectedTab }
123 )
124 }
125
126 @ViewBuilder
127 private func section(for tab: RootTab) -> some View {
128 switch tab {
129 case .dashboard:
130 NavigationStack {
131 DashboardView(viewModel: viewModel)
132 }
133 case .audit:
134 NavigationStack {
135 AuditListView(viewModel: viewModel)
136 }
137 case .history:
138 NavigationStack {
139 HistoryView(viewModel: viewModel)
140 }
141 case .inspect:
142 ContentView(viewModel: viewModel)
143 case .settings:
144 NavigationStack {
145 SettingsView(viewModel: viewModel)
146 }
147 }
148 }
149
150 // MARK: Intent / deep-link routing
151
152 private func consume(_ action: DomainDigDeepLink.Action?) {
153 guard let action else { return }
154 intentRouter.pendingAction = nil
155 perform(action)
156 }
157
158 private func perform(_ action: DomainDigDeepLink.Action) {
159 switch action {
160 case let .inspect(domain):
161 viewModel.domain = domain
162 selectedTab = .inspect
163 viewModel.run()
164 case let .watch(domain):
165 // On success show the dashboard (where tracked domains live); on
166 // failure stay put so the upgrade/paywall alert surfaces in place.
167 if viewModel.trackDomain(domain: domain, availabilityStatus: nil) {
168 selectedTab = .dashboard
169 }
170 case let .detail(domain):
171 // Open the tracked domain's detail (e.g. from a widget tap). If it is
172 // no longer tracked, fall back to inspecting it.
173 if let tracked = viewModel.trackedDomains.first(
174 where: { $0.domain.caseInsensitiveCompare(domain) == .orderedSame }
175 ) {
176 detailDomain = tracked
177 } else {
178 perform(.inspect(domain))
179 }
180 case .sweep:
181 selectedTab = .dashboard
182 viewModel.refreshAllTrackedDomains()
183 }
184 }
185}