1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Shared/Home/FavoriteResourceObserver.swift

57 lines
2.1 KiB
Swift
Raw Permalink Normal View History

2021-11-06 04:14:52 +05:30
import Foundation
import Siesta
final class FavoriteResourceObserver: ObservableObject, ResourceObserver {
2021-11-09 23:13:15 +05:30
@Published var contentItems = [ContentItem]()
2021-11-06 04:14:52 +05:30
func resourceChanged(_ resource: Resource, event _: ResourceEvent) {
// 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-06 04:14:52 +05:30
if let videos: [Video] = resource.typedContent() {
newVideos = videos
2021-11-06 04:14:52 +05:30
} else if let channel: Channel = resource.typedContent() {
newChannel = channel
2021-11-06 04:14:52 +05:30
} else if let playlist: ChannelPlaylist = resource.typedContent() {
newChannelPlaylist = playlist
2021-11-06 04:14:52 +05:30
} else if let playlist: Playlist = resource.typedContent() {
newPlaylist = playlist
2022-01-11 00:36:43 +05:30
} else if let page: SearchPage = resource.typedContent() {
newPage = page
2021-11-09 23:13:15 +05:30
} else if let items: [ContentItem] = resource.typedContent() {
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-06 04:14:52 +05:30
}
}
}