Hey everybody! I’m really frustrated with two issues I’m facing right now.
After completing the Socialcademy project, I couldn’t resolve two problems. The first issue is that when I build the comments list, I don’t see the TextField to enter text; instead, I only see a paper plane button. I tried using NavigationView
because someone said it would help. After adding it to the project, a small text box appeared, but it was super tiny and kept changing size as I typed more text. After I submitted a few comments and tried to delete them, my app crashed every time, and the comments were never deleted.
Here is the code I’m using for the comments list:
import SwiftUI
// MARK: - CommentsList
struct CommentsList: View {
@StateObject var viewModel: CommentsViewModel
var body: some View {
Group {
switch viewModel.comments {
case .loading:
ProgressView()
.onAppear {
viewModel.fetchComments()
}
case let .error(error):
EmptyListView(
title: "Cannot Load Comments",
message: error.localizedDescription,
retryAction: {
viewModel.fetchComments()
}
)
case .empty:
EmptyListView(
title: "No Comments",
message: "Be the first to leave a comment."
)
case let .loaded(comments):
List(comments) { comment in
CommentRow(viewModel: viewModel.makeCommentRowViewModel(for: comment))
}
.animation(.default, value: comments)
}
}
.navigationTitle("Comments")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .bottomBar) {
NewCommentForm(viewModel: viewModel.makeNewCommentViewModel())
}
}
}
}
// MARK: - NewCommentForm
private extension CommentsList {
struct NewCommentForm: View {
@StateObject var viewModel: FormViewModel<Comment>
var body: some View {
HStack {
TextField("Comment", text: $viewModel.content)
Button(action: viewModel.submit) {
if viewModel.isWorking {
ProgressView()
} else {
Label("Post", systemImage: "paperplane")
}
}
}
.alert("Cannot Post Comment", error: $viewModel.error)
.animation(.default, value: viewModel.isWorking)
.disabled(viewModel.isWorking)
.onSubmit(viewModel.submit)
}
}
}
// MARK: - Previews
#if DEBUG
struct CommentsList_Previews: PreviewProvider {
static var previews: some View {
ListPreview(state: .loaded([Comment.testComment]))
ListPreview(state: .empty)
ListPreview(state: .error)
ListPreview(state: .loading)
}
private struct ListPreview: View {
let state: Loadable<[Comment]>
var body: some View {
NavigationView {
CommentsList(viewModel: CommentsViewModel(commentsRepository: CommentsRepositoryStub(state: state)))
}
}
}
}
#endif
Another problem I have right now is that when I select a specific profile to view their posts (so I’m navigating to the profile), I get an error that the app can’t fetch posts and that I need to create indexes, even though I have already created indexes for the posts collection.
Thank you to everyone who can help me with this!