I think I need to use Combine, but I'm not yet sure how...

I have a row view that shows the details of a podcast object.
It includes a Subscribe/Unsubscribe button (displayed text depends on whether the user is currently subscribed.)
(podcast uses a computed property to express isSubscribed)
This means when a user taps the button, the Subscribe/Unsubscribe text should change, but it does not.
I'm pretty sure I need to use combine for the podcast model object to publish an event, but I don't yet know what this code looks like.
Any chance somebody could let me know how I need to extend the code below to do this?
thanks,
Mike

class SubscriptionsManager {
    var subscriptions: [Podcast] = []
    static let shared = SubscriptionsManager()
    func toggleSubscription(podcast: Podcast) {

    }
    func isSubscribed(podcast: Podcast) -> Bool {
        let result = subscriptions.first{ $0.id == podcast.id }
        return result != nil
    }
}

class Podcast: Codable {
    let title: String
    let id: String
    var isSubscribed: Bool {
        return SubscriptionsManager.shared.isSubscribed(podcast: self)
    }
    func toggleSubscription() {
        SubscriptionsManager.shared.toggleSubscription(podcast: self)
    }
}

struct PodcastRowView: View {
    @State var podcast: Podcast
    var buttonText: String {
        return podcast.isSubscribed ? "Unsubscribe" : "Subscribe"
    }
    var body: some View {
        HStack {
            Text(podcast.title)
            Button(action: {
                self.podcast.toggleSubscription()
            }) {
                Text(self.buttonText)
            }
        }
    }
}
Looks like I figured it out:
  1. Podcast needs to conform to ObservableObject

  2. add the following line to Podcast's toggleSubscription function

Code Block
objectWillChange.send()

3. in PodcastRowView replace @State with @ObservableObject
An easy way to work with Combine in SwiftUI is using @Published var.
Code Block
class SubscriptionsManager: ObservableObject {
@Published var subscriptions: [Podcast] = []
//...
}
struct PodcastRowView: View {
@ObservedObject var subscriptionsManager = SubscriptionsManager.shared
//...
}


The changes on @Published vars inside @ObservedObject will trigger the UI update with internally using Combine.
thanks @OOPer I like this alternative approach
I think I need to use Combine, but I'm not yet sure how...
 
 
Q