Reportage/Views/Settings/SettingsView.swift (6522B)
1import SwiftUI
2
3
4
5struct SettingsView: View {
6 @Environment(AppLock.self) private var appLock: AppLock?
7 @State private var settings = PromptSettings.load()
8 #if DEBUG
9 @State private var isShowingWelcome = false
10 #endif
11
12 var body: some View {
13 NavigationStack {
14 List {
15 SettingsTitle()
16 Section {
17 NavigationLink("Questions") { QuestionsView() }
18 .settingsRow()
19 NavigationLink("Vocabulary") { VocabularyView() }
20 .settingsRow()
21 NavigationLink {
22 NotificationsView()
23 } label: {
24 LabeledContent("Notifications") {
25 Group {
26 if settings.isEnabled {
27 Text("\(settings.perDay) / day", comment: "How many random prompts come each day")
28 } else {
29 Text("Off", comment: "Random prompts are turned off")
30 }
31 }
32 .foregroundStyle(Theme.accent)
33 }
34 }
35 .settingsRow()
36 } header: {
37 SettingsHeading("Reports")
38 }
39 DataSection()
40 DiagnosticsSection()
41 #if DEBUG
42 WelcomeReplay { isShowingWelcome = true }
43 #endif
44 Section {
45 NavigationLink {
46 AppLockView()
47 } label: {
48 LabeledContent("App Lock") {
49 Group {
50 if let appLock, appLock.settings.isEnabled {
51 Text(appLock.methodName)
52 } else {
53 Text("Off", comment: "The app lock is turned off")
54 }
55 }
56 .foregroundStyle(Theme.accent)
57 }
58 }
59 .settingsRow()
60 NavigationLink("About") { AboutView() }
61 .settingsRow()
62 } header: {
63 SettingsHeading("Miscellaneous")
64 }
65 Colophon()
66 }
67 .listStyle(.plain)
68 .scrollContentBackground(.hidden)
69 .tabBarClearance()
70 .background(Theme.paper)
71 .foregroundStyle(Theme.ink)
72 .toolbar(.hidden, for: .navigationBar)
73 }
74 #if DEBUG
75
76
77 .fullScreenCover(isPresented: $isShowingWelcome) {
78 WelcomeView { isShowingWelcome = false }
79 }
80 #endif
81
82
83 .onAppear { settings = PromptSettings.load() }
84 }
85}
86
87
88private struct SettingsTitle: View {
89 var body: some View {
90 Text("Settings")
91 .screenTitle()
92 .frame(minHeight: 44)
93 .listRowInsets(EdgeInsets(top: 8, leading: Theme.margin, bottom: 20, trailing: Theme.margin))
94 .listRowBackground(Theme.paper)
95 .listRowSeparatorTint(Theme.ink)
96 .listRowSeparator(.hidden, edges: .top)
97 }
98}
99
100
101private struct Colophon: View {
102 var body: some View {
103 Text("Made in Tokyo from natural ones and zeros")
104 .font(.footnote)
105 .foregroundStyle(Theme.muted)
106 .frame(maxWidth: .infinity)
107 .multilineTextAlignment(.center)
108 .padding(.vertical, 24)
109 .listRowInsets(EdgeInsets(top: 0, leading: Theme.margin, bottom: 0, trailing: Theme.margin))
110 .listRowBackground(Theme.paper)
111 .listRowSeparator(.hidden)
112 }
113}
114
115
116struct SettingsHeading: View {
117 let title: LocalizedStringKey
118
119 init(_ title: LocalizedStringKey) {
120 self.title = title
121 }
122
123 var body: some View {
124 Text(title)
125 .eyebrow()
126 .accessibilityAddTraits(.isHeader)
127 .frame(maxWidth: .infinity, alignment: .leading)
128 .padding(.horizontal, Theme.margin)
129 .padding(.top, Theme.headingTop)
130 .padding(.bottom, 4)
131 .background(Theme.paper)
132 .listRowInsets(EdgeInsets())
133 }
134}
135
136
137
138struct SettingsNote<Content: View>: View {
139 @ViewBuilder let content: Content
140
141 var body: some View {
142 content
143 .font(.footnote)
144 .foregroundStyle(Theme.muted)
145 .listRowInsets(EdgeInsets(top: 8, leading: Theme.margin, bottom: 12, trailing: Theme.margin))
146 .listRowBackground(Theme.paper)
147 .listRowSeparator(.hidden)
148 }
149}
150
151extension View {
152
153
154 func settingsRow() -> some View {
155 listRowInsets(EdgeInsets(top: 10, leading: Theme.margin, bottom: 10, trailing: Theme.margin))
156 .listRowBackground(Theme.paper)
157 .listRowSeparatorTint(Theme.rule)
158 }
159}
160
161#if DEBUG
162
163
164
165 private struct WelcomeReplay: View {
166 let onShow: () -> Void
167
168 var body: some View {
169 Section {
170 Button(action: onShow) {
171 Text(verbatim: "Show Welcome")
172 }
173 .settingsRow()
174 } header: {
175 SettingsHeading("Debug")
176 }
177 }
178 }
179#endif
180
181#Preview {
182 SettingsView()
183 .tint(Theme.accent)
184}