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

geesawra.industries/old-fella

Old people's social media app

0
1
main
81 lines · 2.8 KB Raw History
import SwiftUI

struct PostLoadingList: View {
    let label: String
    let placeholderIdentifier: String
    let refreshIdentifier: String
    var isActive = true
    let refresh: () async -> Void

    var body: some View {
        GeometryReader { geometry in
            List {
                PostLoadingPlaceholder(label: label, count: max(1, Int(ceil(geometry.size.height / 108))))
                    .accessibilityIdentifier(placeholderIdentifier)
                    .listRowInsets(EdgeInsets())
                    .listRowSeparator(.hidden)
            }
            .listStyle(.plain)
            .refreshable { await refresh() }
            .background {
                ScrollRefreshControl(
                    isRefreshing: true, isActive: isActive,
                    accessibilityIdentifier: refreshIdentifier, accessibilityLabel: label
                )
                .allowsHitTesting(false)
                .accessibilityHidden(true)
            }
        }
    }
}

struct PostLoadingPlaceholder: View {
    @AppStorage("hideAvatars") private var hideAvatars = false
    @AppStorage("handlesOnly") private var handlesOnly = false
    private var displayPreferences: DisplayPreferences {
        DisplayPreferences(hideAvatars: hideAvatars, handlesOnly: handlesOnly)
    }
    let label: String
    var count: Int?

    var body: some View {
        Group {
            if let count {
                rows(count: count)
            } else {
                GeometryReader { geometry in
                    rows(count: max(1, Int(ceil(geometry.size.height / 108))))
                        .frame(maxHeight: .infinity, alignment: .top)
                        .clipped()
                }
            }
        }
        .accessibilityElement(children: .ignore)
        .accessibilityLabel(label)
    }

    private func rows(count: Int) -> some View {
        VStack(alignment: .leading, spacing: 24) {
            ForEach(0..<count, id: \.self) { _ in
                VStack(alignment: .leading, spacing: 12) {
                    HStack(spacing: 10) {
                        if !displayPreferences.hideAvatars {
                            Circle().frame(width: 44, height: 44)
                        }
                        VStack(alignment: .leading, spacing: 6) {
                            Capsule().frame(width: 120, height: 10)
                            if !displayPreferences.handlesOnly {
                                Capsule().frame(width: 90, height: 8)
                            }
                        }
                    }
                    Capsule().frame(height: 10)
                    Capsule().frame(maxWidth: .infinity, alignment: .leading).frame(height: 10).padding(.trailing, 70)
                }
            }
        }
        .foregroundStyle(.quaternary)
        .padding(16)
        .frame(maxWidth: .infinity, alignment: .topLeading)
    }
}