如何像stackOverFlow一样增加/减少UpVote / DownVote?

时间:2019-12-07 02:10:17

标签: swift firebase google-cloud-firestore swiftui iosdeployment

我有这段代码,它使用户可以像堆栈溢出一样进行增票或降票。我的问题是Text("\(likes) UpVote").padding(.top, 8) 不会立即刷新,而是用户应更改视角以获取刷新的upvote / downvote。 如何改善它,以使用户只能单击其中之一并立即更改它们?

这是我的代码的样子:

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)
    }

}

2 个答案:

答案 0 :(得分:0)

您将为likes变量使用属性包装,因此,当您更改喜欢的数量时,它也会在Text上进行更改。

在您的情况下,应该使用@State属性包装器,它看起来像这样...

@State var likes = ""

有关属性包装的更多信息,这里有一篇很好的文章对它们进行了解释 https://www.hackingwithswift.com/quick-start/swiftui/understanding-property-wrappers-in-swift-and-swiftui

答案 1 :(得分:0)

不幸的是,我不得不做很多改变,以便它可以运行...所以这是我的代码,它可以正常工作-像这样的变化-当然,您可以将其做成int并增加它,而不仅仅是设置文本< / p>

struct ContentView : View {

    var user = ""
    var image = ""
    var id = ""
    @State var likes = ""
    var comments = ""
    var msg = ""
    var body : some View{
        VStack {

            HStack{

                Image(systemName:"circle").resizable().frame(width: 35, height: 35).clipShape(Circle()).onTapGesture {
                    print("slide out menu ....")
                    self.likes = "tapped"
                }
                HStack(alignment: .top){
                    VStack(alignment: .leading){
                        Text(user).fontWeight(.heavy)
                        Text(msg).padding(.top, 8)
                    }}
                Spacer()
                Button(action: {
                    self.likes = "aha"
                }) {

                    Image(systemName:"circle").resizable().frame(width: 15, height: 15)
                }.foregroundColor(Color("darkAndWhite"))
            }
            if self.image != ""{
                Text("Animated Image")
                //   AnimatedImage(url: URL(string: image)).resizable().frame(height: 250)
            }
            HStack{

                Button(action: {

                    self.likes = "oho"
                    print("button action")
                }) {

                    Image(systemName: "circle").resizable().frame(width: 26, height: 26)
                }.foregroundColor(Color("darkAndWhite"))

                Button(action: {

                    let like = Int.init(self.likes)!
                    print("action")
                    self.likes = "uhu"
                }) {
                    Text("aha")
//                    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)
    }
}