mirror of
https://github.com/yattee/yattee.git
synced 2024-12-22 21:43:41 +00:00
Caching for favorites
This commit is contained in:
parent
a2c69e0fed
commit
b8f693e213
@ -40,7 +40,7 @@ struct Playlist: Identifiable, Equatable, Hashable {
|
||||
}
|
||||
|
||||
var json: JSON {
|
||||
return [
|
||||
[
|
||||
"id": id,
|
||||
"title": title,
|
||||
"visibility": visibility.rawValue,
|
||||
@ -51,7 +51,7 @@ struct Playlist: Identifiable, Equatable, Hashable {
|
||||
}
|
||||
|
||||
static func from(_ json: JSON) -> Self {
|
||||
return .init(
|
||||
.init(
|
||||
id: json["id"].stringValue,
|
||||
title: json["title"].stringValue,
|
||||
visibility: .init(rawValue: json["visibility"].stringValue) ?? .public,
|
||||
|
@ -38,13 +38,15 @@ final class PlaylistsModel: ObservableObject {
|
||||
}
|
||||
|
||||
func load(force: Bool = false, onSuccess: @escaping () -> Void = {}) {
|
||||
guard accounts.app.supportsUserPlaylists, accounts.signedIn, let account = accounts.current else {
|
||||
guard accounts.app.supportsUserPlaylists, let account = accounts.current else {
|
||||
playlists = []
|
||||
return
|
||||
}
|
||||
|
||||
loadCachedPlaylists(account)
|
||||
|
||||
guard accounts.signedIn else { return }
|
||||
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
guard let self else { return }
|
||||
let request = force ? self.resource?.load() : self.resource?.loadIfNeeded()
|
||||
|
@ -67,12 +67,15 @@ struct ChannelPlaylistView: View {
|
||||
}
|
||||
.environment(\.listingStyle, channelPlaylistListingStyle)
|
||||
.onAppear {
|
||||
if navigationStyle == .tab {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||
resource?.loadIfNeeded()
|
||||
if let playlist = presentedPlaylist,
|
||||
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist.id)
|
||||
{
|
||||
store.replace(cache)
|
||||
}
|
||||
resource?.loadIfNeeded()?.onSuccess { response in
|
||||
if let playlist: ChannelPlaylist = response.typedContent() {
|
||||
ChannelPlaylistsCacheModel.shared.storePlaylist(playlist: playlist)
|
||||
}
|
||||
} else {
|
||||
resource?.loadIfNeeded()
|
||||
}
|
||||
}
|
||||
#if os(tvOS)
|
||||
|
@ -45,21 +45,73 @@ struct FavoriteItemView: View {
|
||||
.contentShape(Rectangle())
|
||||
.onAppear {
|
||||
resource?.addObserver(store)
|
||||
if item.section == .subscriptions {
|
||||
cacheFeed(resource?.loadIfNeeded())
|
||||
} else {
|
||||
resource?.loadIfNeeded()
|
||||
}
|
||||
loadCacheAndResource()
|
||||
}
|
||||
}
|
||||
}
|
||||
.onChange(of: accounts.current) { _ in
|
||||
resource?.addObserver(store)
|
||||
if item.section == .subscriptions {
|
||||
cacheFeed(resource?.load())
|
||||
} else {
|
||||
resource?.load()
|
||||
loadCacheAndResource(force: true)
|
||||
}
|
||||
}
|
||||
|
||||
func loadCacheAndResource(force: Bool = false) {
|
||||
guard var resource else { return }
|
||||
|
||||
var onSuccess: (Entity<Any>) -> Void = { _ in }
|
||||
var contentItems = [ContentItem]()
|
||||
|
||||
switch item.section {
|
||||
case .subscriptions:
|
||||
let feed = FeedCacheModel.shared.retrieveFeed(account: accounts.current)
|
||||
contentItems = ContentItem.array(of: feed)
|
||||
|
||||
onSuccess = { response in
|
||||
if let videos: [Video] = response.typedContent() {
|
||||
FeedCacheModel.shared.storeFeed(account: accounts.current, videos: videos)
|
||||
}
|
||||
}
|
||||
case let .channel(_, id, name):
|
||||
let channel = Channel(app: .invidious, id: id, name: name)
|
||||
if let cache = ChannelsCacheModel.shared.retrieve(channel.cacheKey) {
|
||||
contentItems = ContentItem.array(of: cache.videos)
|
||||
}
|
||||
|
||||
onSuccess = { response in
|
||||
if let channel: Channel = response.typedContent() {
|
||||
ChannelsCacheModel.shared.store(channel)
|
||||
}
|
||||
}
|
||||
case let .channelPlaylist(_, id, _):
|
||||
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(id),
|
||||
!cache.videos.isEmpty
|
||||
{
|
||||
contentItems = ContentItem.array(of: cache.videos)
|
||||
}
|
||||
|
||||
onSuccess = { response in
|
||||
if let playlist: ChannelPlaylist = response.typedContent() {
|
||||
ChannelPlaylistsCacheModel.shared.storePlaylist(playlist: playlist)
|
||||
}
|
||||
}
|
||||
case let .playlist(_, id):
|
||||
let playlists = PlaylistsCacheModel.shared.retrievePlaylists(account: accounts.current)
|
||||
|
||||
if let playlist = playlists.first(where: { $0.id == id }) {
|
||||
contentItems = ContentItem.array(of: playlist.videos)
|
||||
}
|
||||
default:
|
||||
contentItems = []
|
||||
}
|
||||
|
||||
if !contentItems.isEmpty {
|
||||
store.contentItems = contentItems
|
||||
}
|
||||
|
||||
if force {
|
||||
resource.load().onSuccess(onSuccess)
|
||||
} else {
|
||||
resource.loadIfNeeded()?.onSuccess(onSuccess)
|
||||
}
|
||||
}
|
||||
|
||||
@ -173,18 +225,10 @@ struct FavoriteItemView: View {
|
||||
.padding(.trailing, 10)
|
||||
}
|
||||
|
||||
private func cacheFeed(_ request: Request?) {
|
||||
request?.onSuccess { response in
|
||||
if let videos: [Video] = response.typedContent() {
|
||||
FeedCacheModel.shared.storeFeed(account: accounts.current, videos: videos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var isVisible: Bool {
|
||||
switch item.section {
|
||||
case .subscriptions:
|
||||
return accounts.app.supportsSubscriptions && accounts.signedIn
|
||||
return accounts.app.supportsSubscriptions
|
||||
case .popular:
|
||||
return accounts.app.supportsPopular
|
||||
case let .channel(appType, _, _):
|
||||
|
Loading…
Reference in New Issue
Block a user