~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
347 lines · 16.9 KB Raw History
import SwiftUI

#if DEBUG
    #Preview("iPad columns") {
        PreviewHost { _ in SignedInPadView() }
    }
#endif

struct SignedInPadView: View {
    @AppStorage("hideAvatars") private var hideAvatars = false
    @AppStorage("handlesOnly") private var handlesOnly = false
    private var displayPreferences: DisplayPreferences {
        DisplayPreferences(hideAvatars: hideAvatars, handlesOnly: handlesOnly)
    }
    @Environment(AppModel.self) private var model
    @Environment(\.accessibilityReduceMotion) private var reduceMotion
    @State private var drag = PadColumnDragCoordinator()
    @AppStorage("padVisibleColumnCount") private var visibleColumnCount = 2

    private var columnAnimation: Animation? {
        reduceMotion ? nil : .spring(response: 0.35, dampingFraction: 0.82)
    }
    @State private var showingComposer = false
    @State private var showingActions = false
    @State private var showingFeedPicker = false
    @State private var addedColumnID: String?
    @State private var feedScrollRequest: String?

    @State private var showingSearch = false
    @State private var showingSavedPosts = false
    @State private var showingSettings = false
    @State private var showingProfile = false
    @State private var profilePath: [AppRoute] = []
    @State private var profileComposeRequest: ComposeRequest?
    @State private var opensDetailsInColumns = false
    @State private var sheetRoute: AppRoute?

    var body: some View {
        let _: Void = ViewUpdateMetrics.record("SignedInPadView")
        columns
            .sheet(
                isPresented: $showingFeedPicker,
                onDismiss: {
                    feedScrollRequest = addedColumnID
                    addedColumnID = nil
                },
                content: {
                    FeedPickerView(addsPadColumn: true, onAddPadFeed: { feed in
                        guard !model.padColumns.contains(where: { $0.id == feed.id }) else { return }
                        addedColumnID = feed.id
                        withAnimation(columnAnimation) {
                            model.addPadFeed(feed)
                        }
                    })
                }
            )
            .sheet(
                isPresented: $showingSearch,
                onDismiss: openSheetRoute,
                content: {
                    SearchView(
                        openPadRoute: opensDetailsInColumns
                            ? { route in
                                sheetRoute = route
                                showingSearch = false
                            } : nil
                    )
                    .presentationDragIndicator(.visible)
                }
            )
            .sheet(isPresented: $showingSavedPosts, onDismiss: openSheetRoute) {
                SavedPostsView(
                    showsDoneButton: true,
                    openPadRoute: opensDetailsInColumns
                        ? { route in
                            sheetRoute = route
                            showingSavedPosts = false
                        } : nil
                )
                .presentationDragIndicator(.visible)
            }
            .sheet(isPresented: $showingSettings) {
                SettingsView()
                    .overlay(alignment: .topTrailing) {
                        Button("Done") { showingSettings = false }
                            .padding()
                    }
            }
            .sheet(isPresented: $showingComposer) {
                ComposerSheet()
            }
            .sheet(isPresented: $showingProfile) {
                NavigationStack(path: $profilePath) {
                    if let account = model.account {
                        ProfileView(
                            actorID: account.id,
                            openPost: { profilePath.append(.post($0)) },
                            openProfile: { profilePath.append(.profile($0)) },
                            openConnections: { profilePath.append(.profileConnections(account.id, $0)) },
                            openComposer: { profileComposeRequest = ComposeRequest(replyTo: $0, quotePostID: $1) }
                        )
                        .navigationDestination(for: AppRoute.self) { route in
                            AppRouteDestination(
                                route: route,
                                openRoute: { profilePath.append($0) },
                                openComposer: { profileComposeRequest = ComposeRequest(replyTo: $0, quotePostID: $1) }
                            )
                        }
                    }
                }
                .sheet(item: $profileComposeRequest) { request in
                    ComposerSheet(replyTo: request.replyTo, quotePostID: request.quotePostID)
                }
            }
    }

    private func openSheetRoute() {
        guard let route = sheetRoute else { return }
        sheetRoute = nil
        openRoute(route)
    }

    private func openRoute(_ route: AppRoute, after sourceID: String? = nil) {
        guard let detail = route.padDetail else { return }
        withAnimation(columnAnimation) {
            feedScrollRequest = model.openPadDetail(detail, after: sourceID)
        }
    }

}

extension SignedInPadView {
    fileprivate var columns: some View {
        GeometryReader { geometry in
            let columnWidth = PadColumnLayout.columnWidth(
                windowWidth: geometry.size.width, visibleColumnCount: visibleColumnCount)
            let usesDetailColumns = PadColumnLayout.opensDetailsInColumns(windowSize: geometry.size)
            VStack(spacing: 0) {
                ScrollViewReader { scrollProxy in
                    ScrollView(.horizontal) {
                        HStack(spacing: 12) {
                            ForEach(model.padColumns) { column in
                                Group {
                                    if let detail = column.detail {
                                        PadDetailColumnView(
                                            detail: detail,
                                            controls: { isHandle in
                                                AnyView(
                                                    PadColumnControls(
                                                        column: column, stride: columnWidth + 12, drag: drag,
                                                        isHandle: isHandle))
                                            },
                                            openPadRoute: usesDetailColumns ? { openRoute($0, after: column.id) } : nil)
                                    } else if column.isNotifications {
                                        NotificationsView(
                                            isActive: true,
                                            padColumnControls: { isHandle in
                                                AnyView(
                                                    PadColumnControls(
                                                        column: column, stride: columnWidth + 12, drag: drag,
                                                        isHandle: isHandle))
                                            },
                                            openPadRoute: usesDetailColumns ? { openRoute($0, after: column.id) } : nil)
                                    } else {
                                        FeedView(
                                            showProfileButton: false, padColumnID: column.id,
                                            padColumnControls: { isHandle in
                                                AnyView(
                                                    PadColumnControls(
                                                        column: column, stride: columnWidth + 12, drag: drag,
                                                        isHandle: isHandle))
                                            },
                                            openPadRoute: usesDetailColumns ? { openRoute($0, after: column.id) } : nil)
                                    }
                                }
                                .frame(width: columnWidth)
                                .ipadColumnSurface()
                                .modifier(
                                    PadColumnMotion(
                                        id: column.id, ids: model.padColumns.map(\.id), stride: columnWidth + 12,
                                        drag: drag)
                                )
                                .transition(
                                    reduceMotion
                                        ? .opacity
                                        : .asymmetric(
                                            insertion: .move(edge: .trailing).combined(with: .opacity),
                                            removal: .scale(scale: 0.94).combined(with: .opacity)
                                        )
                                )
                                .id(column.id)
                                .accessibilityElement(children: .contain)
                                .accessibilityIdentifier("padColumn-\(column.id)")
                            }
                            if model.isRestoringSession {
                                ForEach(
                                    0..<PadColumnLayout.loadingColumnCount(
                                        visibleColumnCount: visibleColumnCount,
                                        existingColumnCount: model.padColumns.count), id: \.self
                                ) { index in
                                    NavigationStack {
                                        PostLoadingList(
                                            label: "Loading feed",
                                            placeholderIdentifier: "initialPadLoadingCards-\(index)",
                                            refreshIdentifier: "initialPadRefreshControl-\(index)", refresh: {})
                                    }
                                    .frame(width: columnWidth)
                                    .ipadColumnSurface()
                                    .accessibilityIdentifier("loadingPadColumn-\(index)")
                                }
                            }
                        }
                        .scrollTargetLayout()
                        .padding(.horizontal, 12)
                        .padding(.vertical, 12)
                    }
                    .accessibilityIdentifier("padColumnsScrollView")
                    .modifier(PadColumnFlickNavigation(columnWidth: columnWidth, columnCount: model.padColumns.count))
                    .modifier(
                        PadColumnEdgeNavigation(
                            columns: model.padColumns,
                            columnWidth: columnWidth,
                            windowWidth: geometry.size.width,
                            scrollProxy: scrollProxy
                        )
                    )
                    .onChange(of: feedScrollRequest) { _, request in
                        guard let id = request else { return }
                        withAnimation(columnAnimation) { scrollProxy.scrollTo(id, anchor: .trailing) }
                        feedScrollRequest = nil
                    }
                }
            }
            .background(Color(uiColor: .systemGroupedBackground))
            .overlay(alignment: .bottomTrailing) {
                VStack(alignment: .composeButtonCenter, spacing: PadColumnLayout.actionSpacing) {
                    if showingActions {
                        VStack(spacing: PadColumnLayout.actionSpacing) {
                            Button {
                                showingSettings = true
                            } label: {
                                Image(systemName: "gearshape")
                                    .font(.title3.weight(.semibold))
                                    .frame(width: 44, height: 44)
                            }
                            .accessibilityLabel("Settings")
                            .accessibilityIdentifier("padSettingsButton")

                            Button {
                                if usesDetailColumns, let account = model.account {
                                    openRoute(.profile(account.id))
                                } else {
                                    profilePath = []
                                    showingProfile = true
                                }
                            } label: {
                                Group {
                                    if displayPreferences.hideAvatars {
                                        Text("Profile")
                                    } else {
                                        AccountAvatar(account: model.account, size: 32, showsDuringSessionRestore: true)
                                    }
                                }
                                .frame(width: 44, height: 44)
                            }
                            .accessibilityLabel("Your profile")
                            .accessibilityIdentifier("yourProfileButton")
                        }
                        .transition(reduceMotion ? .opacity : .move(edge: .bottom).combined(with: .opacity))
                    }

                    Button {
                        withAnimation(columnAnimation) { showingActions.toggle() }
                    } label: {
                        Image(systemName: showingActions ? "chevron.down" : "chevron.up")
                            .font(.body.weight(.semibold))
                            .frame(width: 44, height: 44)
                    }
                    .accessibilityLabel(showingActions ? "Hide actions" : "Show actions")
                    .accessibilityIdentifier("togglePadActionsButton")

                    HStack(spacing: PadColumnLayout.actionSpacing) {
                        if showingActions {
                            HStack(spacing: PadColumnLayout.actionSpacing) {
                                Button {
                                    showingSavedPosts = true
                                } label: {
                                    Image(systemName: "bookmark")
                                        .font(.title3.weight(.semibold))
                                        .frame(width: 44, height: 44)
                                }
                                .accessibilityLabel("Saved Posts")
                                .accessibilityIdentifier("padSavedPostsButton")

                                Button {
                                    showingFeedPicker = true
                                } label: {
                                    Image(systemName: "rectangle.stack.badge.plus")
                                        .font(.title3.weight(.semibold))
                                        .frame(width: 44, height: 44)
                                }
                                .accessibilityLabel("Add feed")
                                .accessibilityIdentifier("addFeedColumnButton")

                                Button {
                                    showingSearch = true
                                } label: {
                                    Image(systemName: "magnifyingglass")
                                        .font(.title3.weight(.semibold))
                                        .frame(width: 44, height: 44)
                                }
                                .accessibilityLabel("Search")
                                .accessibilityIdentifier("padSearchButton")
                            }
                            .transition(reduceMotion ? .opacity : .move(edge: .trailing).combined(with: .opacity))
                        }

                        Button {
                            showingComposer = true
                        } label: {
                            Image(systemName: "square.and.pencil")
                                .font(.title2.weight(.semibold))
                                .frame(width: 56, height: 56)
                        }
                        .accessibilityLabel("New post")
                        .accessibilityIdentifier("floatingComposeButton")
                        .alignmentGuide(.composeButtonCenter) { $0[HorizontalAlignment.center] }
                    }
                }
                .buttonStyle(.glass)
                .buttonBorderShape(.circle)
                .padding(24)
            }
            .onChange(of: usesDetailColumns, initial: true) { _, enabled in
                opensDetailsInColumns = enabled
            }
        }
    }
}

extension HorizontalAlignment {
    fileprivate enum ComposeButtonCenter: AlignmentID {
        static func defaultValue(in context: ViewDimensions) -> CGFloat { context[HorizontalAlignment.center] }
    }

    fileprivate static let composeButtonCenter = HorizontalAlignment(ComposeButtonCenter.self)
}