0

如何在我的 ViewModel 中使用 @AppStorage 来保存一组自定义类型?如何将@AppStorage 与@Published 属性一起使用?

这是我的示例代码:

模型

struct MyModel: Hashable, Codable {
    var text: String
    var number: Int
}

查看模型

class ViewModel: ObservableObject {

    @Published private(set) var myModels = [MyModel]()
    @Published private(set) var subscribedMyModels = [MyModel]() // TODO: sync this with AppStorage

    func fetchMyModels() {
        myModels = [
            MyModel(text: "Setting", number: 1),
            MyModel(text: "Setting", number: 2),
            MyModel(text: "Setting", number: 3),
            MyModel(text: "Setting", number: 4),
            MyModel(text: "Setting", number: 5),
            MyModel(text: "Setting", number: 6),
            MyModel(text: "Setting", number: 7),
            MyModel(text: "Setting", number: 8)
        ]
    }

    func toggleSubscription(for myModel: MyModel) {
        if subscribedMyModels.contains(myModel) {
            subscribedMyModels.removeAll(where: { $0 == myModel } )
        } else {
            subscribedMyModels.append(myModel)
        }
    }

}

看法

struct ContentView: View {

    @StateObject var viewModel = ViewModel()
    @AppStorage(wrappedValue: false, "settings0") private var settings0

    var body: some View {
        List {

            Section(header: Text("Some Settings")) {
                Toggle(isOn: $settings0, label: {
                    Text("Setting 0")
                })
            }
            Section(header: Text("Some other Settings")) {
                ForEach(viewModel.myModels, id: \.self) { myModel in
                    Button(action: {
                        viewModel.toggleSubscription(for: myModel)
                    }, label: {
                        HStack {
                            HStack {
                                Text(myModel.text)
                                Text(String(myModel.number))
                            }
                            Spacer()

                            if viewModel.subscribedMyModels.contains(myModel) {
                                Image(systemName: "checkmark.circle.fill")
                            } else {
                                Image(systemName: "circle")
                            }

                        }

                    })
                }
                .foregroundColor(.black)
            }
        }
        .listStyle(InsetGroupedListStyle())
        .onAppear {
            viewModel.fetchMyModels()
        }
    }
}

用户界面: 用户界面

TODO: 在 AppStorage / UserDefauls 中存储列表“一些其他设置”的检查设置。

4

1 回答 1

0

First of all I recommend using CoreData instead of AppStorage

but to answer your question :

add this to ViewModel :

// this is a computed variable which changes with subscribedMyModels changes
var encodedSubscribedMyModels : Data? {
    let encoder = JSONEncoder()
    return try? encoder.encode(subscribedMyModels)
}

in your View :

    .onChange(of: viewModel.encodedSubscribedMyModels, perform: { newValue in
        // change @AppStore to newValue
        settings0 =  newValue
    })

Note : you will need to decode back the data to your data model using a JSONDecoder

于 2021-09-16T11:39:21.330 回答