1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import SwiftUI
private struct SharedComposeRequest: Identifiable {
let id = UUID()
let content: SharedImport
}
struct RootView: View {
@AppStorage("hideAvatars") private var hideAvatars = false
@AppStorage("handlesOnly") private var handlesOnly = false
@AppStorage("appFont") private var appFont = AppFont.system.rawValue
@AppStorage("remindMissingAltText") private var remindMissingAltText = true
@Environment(AppModel.self) private var model
@Environment(\.scenePhase) private var scenePhase
@State private var screenSize: CGSize = .zero
@State private var sharedCompose: SharedComposeRequest?
@State private var sharedImportError: String?
@State private var sharedImportRetry = 0
var body: some View {
let _: Void = ViewUpdateMetrics.record("RootView")
GeometryReader { geometry in
let currentScreenSize = screenSize == .zero ? geometry.size : screenSize
Group {
if UIDevice.current.userInterfaceIdiom == .pad,
PadColumnLayout.usesColumns(windowSize: geometry.size, screenSize: currentScreenSize),
model.isRestoringSession || model.account != nil {
SignedInPadView()
.disabled(model.account == nil)
} else if model.isRestoringSession || model.account != nil {
SignedInTabView(
showsFloatingComposeButton: UIDevice.current.userInterfaceIdiom == .pad
&& PadColumnLayout.usesFloatingComposeButton(
windowSize: geometry.size, screenSize: currentScreenSize)
)
.disabled(model.isRestoringSession)
} else {
SignInView()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background {
if UIDevice.current.userInterfaceIdiom == .pad {
PadScreenSizeReader { screenSize = $0 }
.allowsHitTesting(false)
.accessibilityHidden(true)
}
}
}
.overlay {
#if DEBUG
if ProcessInfo.processInfo.arguments.contains("-ui-testing-share-target") {
ShareTestHost()
}
#endif
}
.sheet(
item: $sharedCompose, onDismiss: { sharedImportRetry += 1 },
content: { request in
ComposerSheet(sharedImport: request.content)
}
)
.task(
id: "\(scenePhase == .active)-\(model.account?.id ?? "")-\(model.isRestoringSession)-\(sharedImportRetry)"
) {
guard scenePhase == .active, model.account != nil, !model.isRestoringSession, sharedCompose == nil else {
return
}
do {
if let content = try await SharedImportStore.peekNext() {
sharedCompose = SharedComposeRequest(content: content)
}
} catch {
sharedImportError = "Shared items could not be opened. Try again."
}
}
.alert(
"Shared items unavailable",
isPresented: Binding(
get: { sharedImportError != nil },
set: { if !$0 { sharedImportError = nil } }
)
) {
Button("Retry") {
sharedImportError = nil
sharedImportRetry += 1
}
Button("Cancel", role: .cancel) { sharedImportError = nil }
} message: {
Text(sharedImportError ?? "")
}
.onAppear { syncComposerPreferences() }
.onChange(of: hideAvatars) { _, _ in syncComposerPreferences() }
.onChange(of: handlesOnly) { _, _ in syncComposerPreferences() }
.onChange(of: appFont) { _, _ in syncComposerPreferences() }
.onChange(of: remindMissingAltText) { _, _ in syncComposerPreferences() }
.ignoresSafeArea(.keyboard)
.task(id: scenePhase) {
guard ProcessInfo.processInfo.arguments.contains("-scroll-profiling"), scenePhase == .active else { return }
UIApplication.shared.isIdleTimerDisabled = true
defer { UIApplication.shared.isIdleTimerDisabled = false }
try? await Task.sleep(for: .seconds(600))
}
.onChange(of: scenePhase) { _, phase in
if phase == .active { model.sceneBecameActive() }
}
.alert(
"Something went wrong",
isPresented: Binding(
get: { model.errorMessage != nil },
set: { if !$0 { model.clearError() } }
)
) {
Button("OK", role: .cancel) { model.clearError() }
} message: {
Text(model.errorMessage ?? "Please try again.")
}
}
private func syncComposerPreferences() {
guard !ProcessInfo.processInfo.arguments.contains("-ui-testing"),
let shared = UserDefaults(suiteName: AppGroup.identifier)
else { return }
shared.set(hideAvatars, forKey: "hideAvatars")
shared.set(handlesOnly, forKey: "handlesOnly")
shared.set(appFont, forKey: "appFont")
shared.set(remindMissingAltText, forKey: "remindMissingAltText")
}
}
#if DEBUG
#Preview("Root") {
PreviewHost { _ in RootView() }
}
#Preview("Signed out") {
PreviewHost(signedIn: false) { _ in RootView() }
}
#endif
struct SignedInTabView: View {
var showsFloatingComposeButton = false
@Environment(AppModel.self) private var model
@Environment(\.colorScheme) private var colorScheme
@State private var selection =
ProcessInfo.processInfo.arguments.contains("-ui-testing")
&& ProcessInfo.processInfo.arguments.contains("-ui-testing-start-notifications") ? 1 : 0
@State private var showingComposer = false
@State private var showingSavedPosts = false
@State private var timelineScrollRequest = 0
@State private var notificationsScrollRequest = 0
private var tabSelection: Binding<Int> {
Binding(
get: { selection },
set: { next in
if next == 0, selection == 0 { timelineScrollRequest += 1 }
if next == 1, selection == 1 { notificationsScrollRequest += 1 }
selection = next
})
}
var body: some View {
let _: Void = ViewUpdateMetrics.record("SignedInTabView")
TabView(selection: tabSelection) {
Tab(value: 0) {
withSavedPostsButton(FeedView(isActive: selection == 0, scrollToTopRequest: timelineScrollRequest))
} label: {
Label("Timeline", systemImage: "house").labelStyle(.iconOnly)
}
.accessibilityLabel("Timeline")
Tab(value: 1) {
withSavedPostsButton(
NotificationsView(isActive: selection == 1, scrollToTopRequest: notificationsScrollRequest))
} label: {
Label {
Text("Notifications")
} icon: {
if model.hasUnreadNotifications {
Image(systemName: "bell.badge")
.renderingMode(.original)
.symbolRenderingMode(.palette)
.foregroundStyle(.red, colorScheme == .dark ? .white : .black)
} else {
Image(systemName: "bell")
}
}
.labelStyle(.iconOnly)
}
.accessibilityLabel("Notifications")
.accessibilityValue(
model.hasUnreadNotifications ? Text("Unread notifications") : Text("No unread notifications"))
Tab(value: 4) {
withSavedPostsButton(SearchView())
} label: {
Label("Search", systemImage: "magnifyingglass").labelStyle(.iconOnly)
}
.accessibilityLabel("Search")
if UIDevice.current.userInterfaceIdiom == .pad {
Tab(value: 2) {
withSavedPostsButton(SettingsView())
} label: {
Label("Settings", systemImage: "gearshape").labelStyle(.iconOnly)
}
.accessibilityLabel("Settings")
} else {
Tab(value: 2) {
SavedPostsView(isActive: selection == 2)
} label: {
Label("Saved Posts", systemImage: "bookmark").labelStyle(.iconOnly)
}
.accessibilityLabel("Saved Posts")
}
if !showsFloatingComposeButton {
Tab(value: 3, role: .prominent) {
Color.clear
} label: {
Label("New post", systemImage: "square.and.pencil").labelStyle(.iconOnly)
}
.accessibilityLabel("New post")
}
}
.overlay(alignment: .bottomTrailing) {
if showsFloatingComposeButton {
Button {
showingComposer = true
} label: {
Image(systemName: "square.and.pencil")
.font(.title2.weight(.semibold))
.frame(width: 56, height: 56)
}
.buttonStyle(.glass)
.buttonBorderShape(.circle)
.accessibilityLabel("New post")
.accessibilityIdentifier("floatingComposeButton")
.padding(24)
}
}
.task(id: model.isRestoringSession) {
guard !model.isRestoringSession else { return }
_ = await model.loadNotificationsIfNeeded()
}
.onChange(of: selection) { oldValue, newValue in
if newValue == 3 {
selection = oldValue
showingComposer = true
}
}
.sheet(isPresented: $showingComposer) {
ComposerSheet()
}
.sheet(isPresented: $showingSavedPosts) {
SavedPostsView(showsDoneButton: true)
}
}
@ViewBuilder
private func withSavedPostsButton<Content: View>(_ content: Content) -> some View {
if UIDevice.current.userInterfaceIdiom == .pad {
content.overlay(alignment: .bottomLeading) {
Button {
showingSavedPosts = true
} label: {
Image(systemName: "bookmark")
.font(.title3.weight(.semibold))
.frame(width: 44, height: 44)
}
.buttonStyle(.glass)
.buttonBorderShape(.circle)
.accessibilityLabel("Saved Posts")
.accessibilityIdentifier("padSavedPostsButton")
.padding(24)
}
} else {
content
}
}
}
#if DEBUG
#Preview("Signed-in tabs") {
PreviewHost { _ in SignedInTabView() }
}
#endif