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