I am not sure if it is a good concept, but let's start from it here.
I have a simple View:
struct InAppPurchaseView: View {
private let viewModel = InAppPurchaseViewModel()
var body: some View {
VStack {
if !viewModel.currentProgressInfo.isEmpty {
Text(viewModel.currentProgressInfo) // here it relies on the value from viewModel and should update every time when it changes
}
}
}
}
@Observable
class InAppPurchaseViewModel {
private let transactionObserver = TransactionObserver.shared
@Binding var currentProgressInfo: String
// here is the question❓
// How to bind here property from within transactionObserver?
}
@Observable
class TransactionObserver: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver {
static let shared = TransactionObserver()
var currentProgressInfo = ""
// here is the code that updates currentProgressInfo depending on the needs.
}
Example of usage?
In the other view, suppose I have (totally abstract) two instances next to each other. Each has its own viewModel
struct StartView: View {
var body: some View {
VStack {
InAppPurchaseView() // here it need to be updated
InAppPurchaseView() // here it need to be updated THE SAME WAY.
}
}
}
Simply action taken in one of the above InAppPurchaseView should impact and update another one with the same effect.
var currentProgressInfo: String { transactionObserver.currentProgressInfo }. Am I missing something?viewModelshould be a@State.@Bindingis only meaningful in aView, not in aclass, egclass InAppPurchaseViewModel.