SwiftUI-使用按钮切换“收藏”动态项目

时间:2019-12-05 16:39:50

标签: swiftui userdefaults

我是SwiftUI和数据管理的新手。如上所述,我正在尝试使用一个按钮,该按钮将允许用户切换喜欢的项目。我正在尝试将该值写入UserDefaults。

我设法破解了一个版本,在该版本中,按钮成功写入并删除了对象,但是问题是它不会切换视图。仅当您离开视图后再次导航回该项目时,此按钮才会更新。以下是我目前拥有的代码。我希望在用户喜欢或不喜欢某个项目时在视觉上切换按钮。

import SwiftUI

struct ItemDetail: View {
var productID: Int
var productTitle: String
var itemURL: String
var productDescription: String
var productPrice: Double
var venueTitle: String
var newStatus: Bool
var diningPlan: Bool
var kidFriendly: Bool
var vegetarian: Bool
var glutenFree: Bool
var featuredProduct: Bool
var containsAlcohol: Bool

var body: some View {
    GeometryReader { geometry in
        VStack(alignment: .leading) {
            LoadableImageView(with: self.itemURL)
                .aspectRatio(contentMode: .fit)
                .cornerRadius(6)
            HStack(alignment: .top) {
                Text(self.productTitle)
                    .font(.system(size: 22))
                    .fontWeight(.bold)
                Spacer()
                Text("$\(self.productPrice, specifier: "%.2f")")
                    .font(.system(size: 20))
                    .fontWeight(.bold)
                    .padding(.leading, 50)
            }
            HStack {
                Text(self.productDescription)
                    .font(.system(size: 16))
                    .foregroundColor(Color("bodyText"))
                    .frame(width: geometry.size.width / 1.5, alignment: .leading)
                    .fixedSize(horizontal: false, vertical: true)
                Spacer()
            }.padding(.top, 5)
            VStack(alignment: .leading) {
                HStack(alignment: .top, spacing: 8) {
                    if(self.newStatus) {
                        Image("tag-new")
                            .renderingMode(.original)
                    }
                    if(self.diningPlan) {
                        Image("tag-ddp")
                            .renderingMode(.original)
                    }
                    if(self.kidFriendly) {
                        Image("tag-kidfriendly")
                            .renderingMode(.original)

                    }
                    if(self.vegetarian) {
                        Image("tag-vegetarian")
                            .renderingMode(.original)
                    }
                    if(self.glutenFree) {
                        Image("tag-gluten")
                            .renderingMode(.original)
                    }
                    if(self.featuredProduct) {
                        Image("tag-featured")
                            .renderingMode(.original)
                    }
                    if(self.containsAlcohol) {
                        Image("tag-alcohol")
                            .renderingMode(.original)
                    }
                }.padding(.top, 5)
                HStack {
                    Text("Located At:")
                        .font(.system(size: 16))
                        .fontWeight(.semibold)
                    Text(self.venueTitle)
                        .font(.system(size: 16))
                        .foregroundColor(Color("bodyText"))
                    Spacer()
                }.padding(.top, 50)
            }
            Spacer()
            HStack {
                if UserDefaults.standard.object(forKey: self.productTitle) != nil {
                    Button(action: {
                        UserDefaults.standard.removeObject(forKey: self.productTitle)
                        print("Unfavorited \(self.productTitle)")
                        self.isFavorite = ""
                        print("\(self.isFavorite)")
                    }) {
                        Text("Favorite")
                            .padding()
                    }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 50)
                        .foregroundColor(.white)
                        .background(Color.red)
                        .cornerRadius(8)
                } else {
                    Button(action: {
                        UserDefaults.standard.set("Favorite", forKey: self.productTitle)
                        print("Added Favorite \(self.productTitle)")
                        self.isFavorite = "Favorite"
                        print("\(self.isFavorite)")
                    }) {
                        Text("Add to Favorites")
                            .fontWeight(.semibold)
                            .foregroundColor(.red)
                            .padding()
                            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 50)
                            .overlay (
                                RoundedRectangle(cornerRadius: 8)
                                    .stroke(Color.red, lineWidth: 2)
                        )
                    }
                }
            }.padding(.bottom)
        }.padding(.all)
            .navigationBarTitle(Text("Item Details"))
        }
    }
}

1 个答案:

答案 0 :(得分:0)

isFavorite应该是SwiftUI可以观察到的变化的东西。例如,可能是:

@State var isFavorite = false

@Binding var isFavorite: Bool

对于您而言,最好使用Combine并构建一个包含有关该项目的所有信息的ViewModel。然后,您应该在模型中发布更改,观察到的所有View更改都会自动刷新。

This search result可以帮助您更多地了解CombineSwiftUI

请注意,UserDefaults不是存储此类数据的好地方