How to use @AppStorage inside a model with @Published and store In App Purchased products

2.8k views Asked by At

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

There are 2 answers

0
Paul Colton On

Add a willSet {..} and call send() on your ObservableObject: objectWillChange.send().

For example:

@AppStorage("purchased") 
private var purchased = PurchasedProductIdentifiers(["p1"]) {
   willSet {
      DispatchQueue.main.async {
         self.objectWillChange.send()
      }
   }
}

0
AechoLiu On

There is no need to compose @Published @AppStorage("persisted") because @AppStorage does "publish" its change and can be used inside ObservableObject and work just like @Published.

// Example:

class MyAppState: ObservableObject {

    // AppStorage already has the same functionality as Published
    //@Published @AppStorage("var1") var var1: Int = 1

    @AppStorage("var1") var var1: Int = 1

}

References: