Channel playlists cache

This commit is contained in:
Arkadiusz Fal
2022-12-11 18:44:55 +01:00
parent 38593ed488
commit d4ec360581
6 changed files with 151 additions and 12 deletions

View File

@@ -94,19 +94,19 @@ struct PlaylistsView: View {
}
.onAppear {
model.load()
resource?.load()
loadResource()
}
.onChange(of: accounts.current) { _ in
model.load(force: true)
resource?.load()
loadResource()
}
.onChange(of: currentPlaylist) { _ in
channelPlaylist.clear()
userPlaylist.clear()
resource?.load()
loadResource()
}
.onChange(of: model.reloadPlaylists) { _ in
resource?.load()
loadResource()
}
#if os(iOS)
.refreshControl { refreshControl in
@@ -154,7 +154,7 @@ struct PlaylistsView: View {
#if !os(macOS)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
model.load()
resource?.loadIfNeeded()
loadResource()
}
#endif
#if !os(tvOS)
@@ -168,6 +168,26 @@ struct PlaylistsView: View {
#endif
}
func loadResource() {
loadCachedResource()
resource?.load()
.onSuccess { response in
if let playlist: Playlist = response.typedContent() {
ChannelPlaylistsCacheModel.shared.storePlaylist(playlist: playlist.channelPlaylist)
}
}
}
func loadCachedResource() {
if !selectedPlaylistID.isEmpty,
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(selectedPlaylistID)
{
DispatchQueue.main.async {
self.channelPlaylist.replace(cache)
}
}
}
#if os(iOS)
var playlistsMenu: some View {
Menu {

View File

@@ -2,7 +2,7 @@ import Siesta
import SwiftUI
struct PlaylistVideosView: View {
let playlist: Playlist
var playlist: Playlist
@ObservedObject private var accounts = AccountsModel.shared
var player = PlayerModel.shared
@@ -43,6 +43,24 @@ struct PlaylistVideosView: View {
return resource
}
func loadResource() {
loadCachedResource()
resource?.load()
.onSuccess { response in
if let playlist: Playlist = response.typedContent() {
ChannelPlaylistsCacheModel.shared.storePlaylist(playlist: playlist.channelPlaylist)
}
}
}
func loadCachedResource() {
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist.id) {
DispatchQueue.main.async {
self.channelPlaylist.replace(cache)
}
}
}
var videos: [Video] {
contentItems.compactMap(\.video)
}
@@ -55,10 +73,10 @@ struct PlaylistVideosView: View {
VerticalCells(items: contentItems)
.onAppear {
guard contentItems.isEmpty else { return }
resource?.load()
loadResource()
}
.onChange(of: model.reloadPlaylists) { _ in
resource?.load()
loadResource()
}
#if !os(tvOS)
.navigationTitle("\(playlist.title) Playlist")