I know that @AppStorage
has to be used only inside a view, but I prefer to manage the model and use the advantage of @AppStorage
.
For example, if I would like to store the purchased product in In App Purchasing
, could I use this work around or it is preferable to avoid that?
typealias PurchasedProductIdentifiers = Set<String>
extension PurchasedProductIdentifiers: RawRepresentable {
public init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8),
let result = try? JSONDecoder().decode(PurchasedProductIdentifiers.self, from: data)
else {
return nil
}
self = result
}
public var rawValue: String {
guard let data = try? JSONEncoder().encode(self),
let result = String(data: data, encoding: .utf8)
else {
return "[]"
}
return result
}
}
class MyModel: NSObject, ObservableObject {
@Published var purchasedPub: PurchasedProductIdentifiers = [] {
didSet {
purchased = purchasedPub
}
}
@AppStorage("purchased") private var purchased = PurchasedProductIdentifiers(["p1"])
override init() {
super.init()
self.purchasedPub = self.purchased
}
}
struct TestView: View {
@StateObject var model: MyModel = MyModel()
var body: some View {
VStack (alignment: .leading){
HStack {
Image(systemName: model.purchasedPub.contains("p1") ? "lock.open":"lock")
Text("\("p1")")
Button(action: {
model.purchasedPub.insert("p1")
}, label: {
Text("buy")
})
Button(action: {
model.purchasedPub.remove("p1")
}, label: {
Text("remove")
})
}
HStack {
Image(systemName: model.purchasedPub.contains("p2") ? "lock.open":"lock")
Text("\("p2")")
Button(action: {
model.purchasedPub.insert("p2")
}, label: {
Text("buy")
})
Button(action: {
model.purchasedPub.remove("p2")
}, label: {
Text("remove")
})
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
Add a
willSet {..}
and call send() on your ObservableObject:objectWillChange.send()
.For example: