提问人:Dimitri Borgers 提问时间:11/15/2023 更新时间:11/15/2023 访问量:36
修改 viewModel 时跳转的 SwiftUI 视图
SwiftUI views jumping around when modifying viewModel
问:
我正在尝试创建一个允许用户接受或拒绝好友请求的 fullScreenCover。但是,每当我接受好友请求时,视图都会多次更改。这是由于在两个不同的视图之间共享一个 viewModel 吗?
问题(请注意,单击绿色复选标记后,之后发生的一切都发生在我没有触摸屏幕的情况下):
FriendsView.swift:
import SwiftUI
struct FriendsView: View {
@State private var showAddFriendView = false
@EnvironmentObject private var viewModel: AuthenticationModel
var body: some View {
NavigationStack {
if viewModel.currentUser?.friends != nil && viewModel.currentUser!.friends!.contains(where: { $0.status == "ACCEPTED" }) {
List (viewModel.currentUser!.friends ?? []) { friend in
if friend.status == "ACCEPTED" {
NavigationLink(value: friend) {
Text(friend.username)
}
}
}
} else {
VStack() {
Text("To start sharing seecrets, first invite your friends!")
Button {
showAddFriendView.toggle()
} label: {
Text("Add friends")
}.fullScreenCover(isPresented: $showAddFriendView) {
AddFriendView().environmentObject(viewModel)
}
}
}
}
}
}
添加朋友视图.swift:
import SwiftUI
import FirebaseFirestoreSwift
import FirebaseFirestore
import FirebaseFunctions
struct AddFriendView: View {
@State private var friendUsername: String = ""
@EnvironmentObject private var viewModel: AuthenticationModel
@State private var isLoading: Bool = false
@State private var errorText: String = ""
@Environment(\.dismiss) var dismiss
func sendFriendRequest() {
// NOT IMPORTANT
}
func acceptFriendRequest(acceptRequestFriendUsername: String) {
errorText = ""
isLoading = true
let functions = Functions.functions()
// Call the function, passing in the username you want to check
functions.httpsCallable("acceptFriendRequest").call(["friendUsername": acceptRequestFriendUsername]) { (result, error) in
// Handle successful response
if let response = result?.data as? [String: Bool] {
if let updated = response["success"], updated {
// Find the user you just accepted and change friend request status
for index in 0..<(viewModel.currentUser?.friends!.count)! {
if viewModel.currentUser?.friends?[index].username == acceptRequestFriendUsername {
DispatchQueue.main.async {
viewModel.currentUser?.friends?[index].status = "ACCEPTED"
}
break
}
}
DispatchQueue.main.async {
showToastRequestAccepted = true
isLoading = false
}
}
}
}
isLoading = false
}
func denyFriendRequest(denyRequestFriendUsername: String) {
// NOT IMPORTANT
}
var body: some View {
NavigationStack {
ZStack {
VStack(spacing:20) {
// NOT IMPORTANT
if viewModel.currentUser?.friends != nil && viewModel.currentUser!.friends!.contains(where: { $0.status == "PENDING_YOUR_RESPONSE" }) {
List(viewModel.currentUser!.friends ?? []) { friend in
if friend.status == "PENDING_YOUR_RESPONSE" {
HStack {
Text(friend.username)
Button(action: {
denyFriendRequest(denyRequestFriendUsername: friend.username)
}, label: {
Image(systemName: "multiply")
})
.disabled(isLoading)
Button(action: {
acceptFriendRequest(acceptRequestFriendUsername: friend.username)
}, label: {
Image(systemName: "checkmark")
})
.disabled(isLoading)
}
}
}
} else {
// NOT IMPORTANT
}
}
}
.navigationTitle("Add friends")
}
}
}
答: 暂无答案
评论
DispatchQueue.main.async
isLoading=false
isLoading
friends
friends