我在 SwiftUI 中实现了评论部分,用户可以在其中发表评论并回复评论。我遇到了有关处理回复的问题。当用户点击回复按钮来回复评论时,我通过切换视图模型中的
boolean
属性来启用回复功能 (viewModel.isReply)
。但是,当用户点击回复按钮来回复其他用户的评论时,它会立即禁用所有评论的回复按钮。这种行为是合乎逻辑的,因为我正在全局切换 isReply 属性。
我的目标是让用户能够回复个人评论并在回复不同评论之间无缝切换。目前,我正在切换 isReply 属性以从视图模型发送数据。如何修改我的方法来分别识别和处理每个评论的回复操作?我希望用户能够通过再次点击回复按钮来取消对特定评论的回复。
主要评论观点:
struct MainCommentView: View {
var data: Comment
@ObservedObject var viewModelObserved: PostCommentsViewModel
@State private var showReplies: Bool = false
@FocusState var isTextFieldFocused: Bool
var body: some View {
VStack(alignment:.leading) {
HStack(alignment:.top) {
// Views for displaying user's profile picture and comment text
// Reply button
Button {
viewModelObserved.commentId = data.id ?? "unknown"
if viewModelObserved.commentId == data.id ?? "unknown" {
viewModelObserved.isReply.toggle()
}
viewModelObserved.mention = "@\(data.user?.firstName ?? "unknown")"
viewModelObserved.commentText = "\(viewModelObserved.mention) "
isTextFieldFocused = true
} label: {
Text("Reply")
.font(.ubuntu(name: .ubuntuRegular, size: 12))
.foregroundStyle(Color.gray)
}
// Displaying timestamp of the comment
Text("\(viewModel.formattedTime(dateString: data.createdAt ?? ""))")
.font(.ubuntu(name: .ubuntuRegular, size: 11))
.foregroundStyle(Color.gray)
}
// Button for showing/hiding replies
Button(showReplies ? "— Hide Replies (\(data.countReplies ?? 0))" : "— Show Replies(\(data.countReplies ?? 0))") {
viewModel.commentId = data.id ?? "unknown"
viewModel.getReplies()
showReplies.toggle()
}
.font(.ubuntu(name: .ubuntuRegular, size: 14))
.foregroundStyle(Color.gray.opacity(0.5))
// Displaying replies if they exist and 'showReplies' is true
if showReplies {
if let repliesData = viewModel.repliesModel?.data?.comments {
ForEach(repliesData, id:\.id) { reply in
RepliesView(data: reply)
}
}
}
}
.padding(.horizontal, 10)
}
}
视图模型:
func addComment(onComplete: (()->())? = nil) {
isLoading = true
var parameters: [String: Any] = [
parameters for main comment
]
if self.isReply {
parameters for reply
}
functionForSendingData()
}
如果有人可以提出建议,我将不胜感激!
这是因为每次单击评论时都可以更改
showReplies
属性。您可以向 Comment 模型添加一个名为 isReplying 的附加属性,如下所示:
struct Comment {
var id: String
var isReplying: Bool = false
// Other properties you have
}
在您的 ViewModel 中,将注释定义为 @Published 对象,以便您可以立即检测视图上的更改。另外,添加另一个函数来更改评论模型的
isReplying
状态:
final class PostCommentsViewModel: ObservableObject {
@Published var comments: [Comment]
func toggleReply(for commentId: String) {
if let index = comments.firstIndex(where: { $0.id == commentId }) {
comments[index].isReplying.toggle()
}
}
}
最后,在您看来,您可以从 ViewModel 中调用必要的函数:
var body: some View {
VStack(alignment: .leading) {
HStack(alignment: .top) {
// Views for displaying user's profile picture and comment text
// Reply button
Button {
viewModelObserved.commentId = data.id ?? "unknown"
if viewModelObserved.commentId == data.id ?? "unknown" {
viewModel.toggleReply(for: comment.id)
}
viewModelObserved.mention = "@\(data.user?.firstName ?? "unknown")"
viewModelObserved.commentText = "\(viewModelObserved.mention) "
isTextFieldFocused = true
} label: {
Text("Reply")
.font(.ubuntu(name: .ubuntuRegular, size: 12))
.foregroundStyle(Color.gray)
}
}
// Other functions
}
}