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 { 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: 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