← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Reportage/Views/Settings/SettingsView.swift (6522B)

1import SwiftUI
2
3/// Settings: the questions, when random prompts come, and importing and
4/// exporting.
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            // On the screen rather than the row: a cover attached inside
76            // the list is dismissed when the list rebuilds the row.
77            .fullScreenCover(isPresented: $isShowingWelcome) {
78                WelcomeView { isShowingWelcome = false }
79            }
80        #endif
81        // Prompts may have been turned on when the app was first opened,
82        // after this view was made, or changed on the Notifications screen.
83        .onAppear { settings = PromptSettings.load() }
84    }
85}
86
87/// The screen's title, set like the timeline's, with an ink rule beneath.
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/// The line at the foot of the screen, as in the other apps.
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/// A section heading in small spaced capitals, like a day in the timeline.
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/// An explanation beneath a section's settings, in the muted footnote the
137/// timeline uses for context.
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    /// A row of settings on paper, at the screen's margin, ruled like the
153    /// timeline.
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    /// Shows the welcome screen again, to see it on a device where both
163    /// permissions are decided and it wouldn't otherwise appear. Continue
164    /// asks only for permissions still undecided. Debug builds only.
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}