Skip to content
Repos

WO_DevPlaceIOS

Swift 5.5 KB182 lines Opens in site

Directory Raw Download

import SwiftUI
import DevPlaceSwiftSDK

struct ReportButton: View {
    let targetType: TargetType
    let targetId: String
    let authorId: String

    @Environment(\.api) var api
    let appSettings = AppSettingsStore.shared

    private var isVisible: Bool {
        appSettings.showReportButtons
            && AppState.shared.isLoggedIn
            && !AppState.shared.isCurrentUser(id: authorId)
    }

    var body: some View {
        if isVisible {
            ReportButtonContent(
                viewModel: .init(
                    targetType: targetType,
                    targetId: targetId,
                    api: api,
                )
            )
        }
    }
}

private struct ReportButtonContent: View {
    @State var viewModel: ReportButton.ViewModel

    @State private var isReporting = false
    @State private var showThankYou = false

    var body: some View {
        Button {
            viewModel.prepareForPresentation()
            isReporting = true
        } label: {
            Label("Report \(viewModel.targetNoun)", systemImage: "flag")
                .labelStyle(.iconOnly)
        }
        .buttonStyle(.plain)
        .foregroundStyle(Color.FG_1)
        .sheet(isPresented: $isReporting) {
            ReportSheet(viewModel: viewModel)
        }
        .onChange(of: viewModel.didReport) { _, didReport in
            if didReport {
                showThankYou = true
            }
        }
        .alert(
            "Thank you",
            isPresented: $showThankYou,
        ) {
            Button("OK", role: .cancel) {}
        } message: {
            Text("This \(viewModel.targetNoun) has been reported for review.")
        }
    }
}

private struct ReportSheet: View {
    @Bindable var viewModel: ReportButton.ViewModel

    @Environment(\.dismiss) private var dismiss

    var body: some View {
        NavigationStack {
            content()
                .navigationTitle(viewModel.sheetTitle)
                .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    ToolbarItem(placement: .cancellationAction) {
                        Button("Cancel") {
                            dismiss()
                        }
                    }
                    ToolbarItem(placement: .confirmationAction) {
                        if viewModel.isSubmitting {
                            ProgressView()
                        } else {
                            Button("Report") {
                                Task {
                                    await viewModel.submit()
                                    if viewModel.didReport {
                                        dismiss()
                                    }
                                }
                            }
                            .disabled(!viewModel.canSubmit)
                        }
                    }
                }
                .task {
                    await viewModel.loadReasons()
                }
                .alert($viewModel.alertMessage)
        }
    }

    @ViewBuilder private func content() -> some View {
        if !viewModel.reasons.isEmpty {
            Form {
                reasonsSection()
                detailSection()
            }
        } else if viewModel.didFailToLoadReasons {
            failedToLoad()
        } else {
            ProgressView()
                .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
    }

    @ViewBuilder private func failedToLoad() -> some View {
        ContentUnavailableView {
            Label("Couldn't load reasons", systemImage: "exclamationmark.triangle")
        } description: {
            Text("Please check your connection and try again.")
        } actions: {
            Button("Retry") {
                Task {
                    await viewModel.loadReasons()
                }
            }
        }
    }

    @ViewBuilder private func reasonsSection() -> some View {
        Section("Why are you reporting this \(viewModel.targetNoun)?") {
            ForEach(viewModel.reasons) { reason in
                let isSelected = viewModel.selectedReasonKey == reason.key
                Button {
                    viewModel.selectedReasonKey = reason.key
                } label: {
                    HStack {
                        Text(reason.label)
                            .foregroundStyle(Color.FG_1)
                        Spacer()
                        if isSelected {
                            Label("Selected", systemImage: "checkmark")
                                .labelStyle(.iconOnly)
                                .foregroundStyle(Color.accentColor)
                        }
                    }
                    .contentShape(.rect)
                }
                .accessibilityAddTraits(isSelected ? .isSelected : [])
            }
        }
    }

    @ViewBuilder private func detailSection() -> some View {
        Section {
            TextField(
                "Add any details for the moderators (optional)",
                text: $viewModel.detail,
                axis: .vertical,
            )
            .lineLimit(3...8)
        } header: {
            Text("Details")
        } footer: {
            HStack {
                Spacer()
                CharacterCounterView(
                    text: viewModel.detail,
                    maxCount: viewModel.detailCharacterLimit,
                )
            }
        }
    }
}

#Preview {
    ReportButton(targetType: .post, targetId: "1", authorId: "other-user")
        .environment(\.api, .mock)
}
30.7ms