Skip to content
Repos

WO_DevPlaceIOS

Swift 6.5 KB219 lines Opens in site

Directory Raw Download

import SwiftUI
import DevPlaceSwiftSDK

struct CreatePostView: View {
    let mode: Mode
    var onComplete: () -> Void = {}

    @Environment(\.api) var api

    var body: some View {
        CreatePostViewContent(viewModel: .init(api: api, mode: mode), onComplete: onComplete)
    }
}

private struct CreatePostViewContent: View {
    @State var viewModel: CreatePostView.ViewModel
    var onComplete: () -> Void = {}

    @Environment(\.dismiss) private var dismiss

    enum FocusableItem: Identifiable {
        case title
        case message

        var id: Self { self }
    }

    @FocusState private var focusedField: FocusableItem?

    var body: some View {
        content()
            .screenStyle(bgColor: .BG_2)
            .alert($viewModel.alertMessage)
            .disabled(viewModel.isLoading)
            .onTapGesture {
                focusedField = nil
            }
            .onReceive(viewModel.dismiss) {
                onComplete()
                dismiss()
            }
            .onDisappear {
                Task {
                    await viewModel.deleteUnsubmittedAttachments()
                }
            }
            .task {
                await viewModel.loadProjects()
            }
            .navigationTitle(Text(viewModel.isEditing ? "Edit Post" : "Create New Post"))
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .topBarLeading) {
                    Button {
                        dismiss()
                    } label: {
                        Label("Close", systemImage: "xmark")
                    }
                }

                ToolbarItem(placement: .topBarTrailing) {
                    if viewModel.isLoading {
                        ProgressView()
                    } else {
                        Button {
                            Task {
                                await viewModel.submit()
                            }
                        } label: {
                            if viewModel.isEditing {
                                Label("Save", systemImage: "checkmark")
                            } else {
                                Label("Create", systemImage: "paperplane.fill")
                            }
                        }
                        .disabled(!viewModel.canSubmit)
                    }
                }
            }
    }

    @ViewBuilder private func content() -> some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 14) {
                topicArea()
                titleArea()
                messageArea()
                if !viewModel.isEditing {
                    attachmentsArea()
                    pollArea()
                }
                projectArea()
            }
            .frame(maxWidth: .infinity, alignment: .leading)
            .padding()
        }
    }

    @ViewBuilder private func topicArea() -> some View {
        HStack {
            Text("Topic")

            Picker(selection: $viewModel.postTopic, label: Text("Topic")) {
                ForEach(PostTopic.allCases, id: \.self) { topic in
                    Text(topic.name).tag(topic)
                }
            }

            Spacer()
        }
    }

    @ViewBuilder private func titleArea() -> some View {
        VStack(alignment: .leading) {
            TextField(
                text: $viewModel.title,
                label: {
                    Text("Title (optional)")
                        .foregroundStyle(.FG_2.opacity(0.5))
                }
            )
            .textFieldStyle(.devPlace)
            .focused($focusedField, equals: .title)
            .onSubmit {
                focusedField = .message
            }

            CharacterCounterView(
                text: viewModel.title,
                maxCount: DevPlaceConstants.maxPostTitleLength,
            )
            .padding(.horizontal, 11)
        }
    }

    @ViewBuilder private func messageArea() -> some View {
        VStack(alignment: .leading) {
            DevPlaceTextEditor(
                text: $viewModel.message,
                placeholder: "What's on your mind?",
                initialLineCount: 7
            )
            .focused($focusedField, equals: .message)

            CharacterCounterView(
                text: viewModel.message,
                minCount: DevPlaceConstants.minPostContentLength,
                maxCount: DevPlaceConstants.maxPostContentLength,
            )
            .padding(.horizontal, 11)
        }
    }

    @ViewBuilder private func attachmentsArea() -> some View {
        AttachmentUploaderView(attachments: viewModel.attachments) { attachments in
            viewModel.attachments = attachments
        }
    }

    @ViewBuilder private func pollArea() -> some View {
        VStack(alignment: .leading, spacing: 10) {
            Button {
                if viewModel.isPollAdded {
                    viewModel.removePoll()
                } else {
                    viewModel.addPoll()
                }
            } label: {
                BoxView {
                    if viewModel.isPollAdded {
                        Label("Remove Poll", systemImage: "chart.bar.yaxis")
                    } else {
                        Label("Add Poll", systemImage: "chart.bar.yaxis")
                    }
                }
            }

            if viewModel.isPollAdded {
                CreatePollView(
                    question: $viewModel.pollQuestion,
                    options: $viewModel.pollOptions,
                )
            }
        }
        .frame(maxWidth: .infinity, alignment: .leading)
    }

    @ViewBuilder private func projectArea() -> some View {
        if !viewModel.projects.isEmpty {
            HStack {
                let title = "Link to Project"

                Text(title)

                Picker(selection: $viewModel.selectedProjectId, label: Text(title)) {
                    Text("None").tag(String?.none)

                    ForEach(viewModel.projects) { project in
                        Text(project.title).tag(String?.some(project.id))
                    }
                }
            }
        }
    }
}

#Preview("Create") {
    NavigationStack {
        CreatePostView(mode: .create)
    }
    .environment(\.api, .mock)
}

#Preview("Edit") {
    NavigationStack {
        CreatePostView(mode: .edit([Post].mock[0]))
    }
    .environment(\.api, .mock)
}
34.2ms