import Foundation import Observation /// Observable, in-memory mirror of `PodcastIndex` so SwiftUI updates immediately /// on changes — the Podcasts tab badge and the library list both read from here. /// Route podcast mutations through this store so the unplayed badge stays in sync. @Observable @MainActor final class PodcastLibrary { static let shared = PodcastLibrary() private(set) var entries: [PodcastEntry] = [] var unplayed: [PodcastEntry] { entries.filter { !$0.isPlayed } } var played: [PodcastEntry] { entries.filter { $0.isPlayed } } var unplayedCount: Int { entries.reduce(0) { $0 + ($1.isPlayed ? 0 : 1) } } private init() { entries = PodcastIndex.all() } func reload() { entries = PodcastIndex.all() } func setPlayed(_ articleUrl: String, _ played: Bool) { PodcastIndex.setPlayed(articleUrl: articleUrl, played) reload() } func remove(_ articleUrl: String) { PodcastIndex.remove(articleUrl: articleUrl) reload() } }