我有这段代码,它允许用户像堆栈溢出一样进行赞成或反对。我的问题是Text("\(likes) UpVote").padding(.top, 8)
不会立即刷新,而是用户应该更改视角以获得刷新的赞成/反对票。如何改进这一点,以便用户只能单击其中一个并立即更改它们?
这就是我的代码的样子:
import SwiftUI
import SDWebImageSwiftUI
import Firebase
struct PostCellCard : View {
var user = ""
var image = ""
var id = ""
var likes = ""
var comments = ""
var msg = ""
var body : some View{
VStack(alignment: .leading, content: {
HStack{
Image("person-icon-1675").resizable().frame(width: 35, height: 35).clipShape(Circle()).onTapGesture {
print("slide out menu ....")
}
HStack(alignment: .top){
VStack(alignment: .leading){
Text(user).fontWeight(.heavy)
Text(msg).padding(.top, 8)
}}
Spacer()
Button(action: {
}) {
Image("menu").resizable().frame(width: 15, height: 15)
}.foregroundColor(Color("darkAndWhite"))
}
if self.image != ""{
AnimatedImage(url: URL(string: image)).resizable().frame(height: 250)
}
HStack{
Button(action: {
let db = Firestore.firestore()
let like = Int.init(self.likes)!
db.collection("posts").document(self.id).updateData(["likes": "\(like - 1)"]) { (err) in
if err != nil{
print((err)!)
return
}
print("down updated....")
}
}) {
Image(systemName: "arrow.down.to.line.alt").resizable().frame(width: 26, height: 26)
}.foregroundColor(Color("darkAndWhite"))
Button(action: {
let db = Firestore.firestore()
let like = Int.init(self.likes)!
db.collection("posts").document(self.id).updateData(["likes": "\(like + 1)"]) { (err) in
if err != nil{
print((err)!)
return
}
print("up updated....")
}
}) {
Image(systemName: "arrow.up.to.line.alt").resizable().frame(width: 26, height: 26)
}.foregroundColor(Color("darkAndWhite"))
Spacer()
}.padding(.top, 8)
Text("\(likes) UpVote").padding(.top, 8)
Text("\(likes) DownVote").padding(.top, 8)
Text("View all \(comments) Comments")
}).padding(8)
}
}