2021-11-05 22:44:52 +00:00
|
|
|
import Foundation
|
|
|
|
import Siesta
|
|
|
|
|
|
|
|
final class FavoriteResourceObserver: ObservableObject, ResourceObserver {
|
2021-11-09 17:43:15 +00:00
|
|
|
@Published var contentItems = [ContentItem]()
|
2021-11-05 22:44:52 +00:00
|
|
|
|
|
|
|
func resourceChanged(_ resource: Resource, event _: ResourceEvent) {
|
2024-05-17 14:16:48 +00:00
|
|
|
// swiftlint:disable discouraged_optional_collection
|
|
|
|
var newVideos: [Video]?
|
|
|
|
var newItems: [ContentItem]?
|
|
|
|
// swiftlint:enable discouraged_optional_collection
|
|
|
|
|
|
|
|
var newChannel: Channel?
|
|
|
|
var newChannelPlaylist: ChannelPlaylist?
|
|
|
|
var newPlaylist: Playlist?
|
|
|
|
var newPage: SearchPage?
|
|
|
|
|
2021-11-05 22:44:52 +00:00
|
|
|
if let videos: [Video] = resource.typedContent() {
|
2024-05-17 14:16:48 +00:00
|
|
|
newVideos = videos
|
2021-11-05 22:44:52 +00:00
|
|
|
} else if let channel: Channel = resource.typedContent() {
|
2024-05-17 14:16:48 +00:00
|
|
|
newChannel = channel
|
2021-11-05 22:44:52 +00:00
|
|
|
} else if let playlist: ChannelPlaylist = resource.typedContent() {
|
2024-05-17 14:16:48 +00:00
|
|
|
newChannelPlaylist = playlist
|
2021-11-05 22:44:52 +00:00
|
|
|
} else if let playlist: Playlist = resource.typedContent() {
|
2024-05-17 14:16:48 +00:00
|
|
|
newPlaylist = playlist
|
2022-01-10 19:06:43 +00:00
|
|
|
} else if let page: SearchPage = resource.typedContent() {
|
2024-05-17 14:16:48 +00:00
|
|
|
newPage = page
|
2021-11-09 17:43:15 +00:00
|
|
|
} else if let items: [ContentItem] = resource.typedContent() {
|
2024-05-17 14:16:48 +00:00
|
|
|
newItems = items
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.global(qos: .userInitiated).async {
|
|
|
|
var newContentItems: [ContentItem] = []
|
|
|
|
|
|
|
|
if let videos = newVideos {
|
|
|
|
newContentItems = videos.map { ContentItem(video: $0) }
|
|
|
|
} else if let channel = newChannel {
|
|
|
|
newContentItems = channel.videos.map { ContentItem(video: $0) }
|
|
|
|
} else if let playlist = newChannelPlaylist {
|
|
|
|
newContentItems = playlist.videos.map { ContentItem(video: $0) }
|
|
|
|
} else if let playlist = newPlaylist {
|
|
|
|
newContentItems = playlist.videos.map { ContentItem(video: $0) }
|
|
|
|
} else if let page = newPage {
|
|
|
|
newContentItems = page.results
|
|
|
|
} else if let items = newItems {
|
|
|
|
newContentItems = items
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
if !newContentItems.isEmpty {
|
|
|
|
self.contentItems = newContentItems
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 22:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|