This commit is contained in:
Arkadiusz Fal 2022-12-16 20:37:05 +01:00
parent b621eba236
commit f39b440b21
9 changed files with 34 additions and 18 deletions

View File

@ -3,6 +3,7 @@ import Foundation
extension ChannelPlaylist {
static var fixture: ChannelPlaylist {
ChannelPlaylist(
id: "fixture-channel-playlist",
title: "Playlist with a very long title that will not fit easily in the screen",
thumbnailURL: URL(string: "https://i.ytimg.com/vi/hT_nvWreIhg/hqdefault.jpg?sqp=-oaymwEWCKgBEF5IWvKriqkDCQgBFQAAiEIYAQ==&rs=AOn4CLAAD21_-Bo6Td1z3cV-UFyoi1flEg")!,
channel: Video.fixture.channel,

View File

@ -18,17 +18,17 @@ struct ChannelPlaylistsCacheModel: CacheModel {
func storePlaylist(playlist: ChannelPlaylist) {
let date = iso8601DateFormatter.string(from: Date())
logger.info("STORE \(playlistCacheKey(playlist.id)) -- \(date)")
logger.info("STORE \(playlist.cacheKey) -- \(date)")
let feedTimeObject: JSON = ["date": date]
let playlistObject: JSON = ["playlist": playlist.json.object]
try? storage?.setObject(feedTimeObject, forKey: playlistTimeCacheKey(playlist.id))
try? storage?.setObject(playlistObject, forKey: playlistCacheKey(playlist.id))
try? storage?.setObject(feedTimeObject, forKey: playlistTimeCacheKey(playlist.cacheKey))
try? storage?.setObject(playlistObject, forKey: playlist.cacheKey)
}
func retrievePlaylist(_ id: ChannelPlaylist.ID) -> ChannelPlaylist? {
logger.info("RETRIEVE \(playlistCacheKey(id))")
func retrievePlaylist(_ playlist: ChannelPlaylist) -> ChannelPlaylist? {
logger.info("RETRIEVE \(playlist.cacheKey)")
if let json = try? storage?.object(forKey: playlistCacheKey(id)).dictionaryValue["playlist"] {
if let json = try? storage?.object(forKey: playlist.cacheKey).dictionaryValue["playlist"] {
return ChannelPlaylist.from(json)
}
@ -50,11 +50,7 @@ struct ChannelPlaylistsCacheModel: CacheModel {
getFormattedDate(getPlaylistsTime(id))
}
private func playlistCacheKey(_ playlist: ChannelPlaylist.ID) -> String {
"channelplaylists-\(playlist)"
}
private func playlistTimeCacheKey(_ playlist: ChannelPlaylist.ID) -> String {
"\(playlistCacheKey(playlist))-time"
private func playlistTimeCacheKey(_ cacheKey: ChannelPlaylist.ID) -> String {
"\(cacheKey)-time"
}
}

View File

@ -2,13 +2,17 @@ import Foundation
import SwiftyJSON
struct ChannelPlaylist: Identifiable {
var id: String = UUID().uuidString
var id: String
var title: String
var thumbnailURL: URL?
var channel: Channel?
var videos = [Video]()
var videosCount: Int?
var cacheKey: String {
"channelplaylists-\(id)"
}
var json: JSON {
[
"id": id,

View File

@ -49,4 +49,17 @@ struct ContentItem: Identifiable {
var contentType: ContentType {
video.isNil ? (channel.isNil ? (playlist.isNil ? .placeholder : .playlist) : .channel) : .video
}
var cacheKey: String {
switch contentType {
case .video:
return video.cacheKey
case .playlist:
return playlist.cacheKey
case .channel:
return channel.cacheKey
case .placeholder:
return id
}
}
}

View File

@ -68,7 +68,7 @@ struct ChannelPlaylistView: View {
.environment(\.listingStyle, channelPlaylistListingStyle)
.onAppear {
if let playlist = presentedPlaylist,
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist.id)
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist)
{
store.replace(cache)
}

View File

@ -82,8 +82,8 @@ struct FavoriteItemView: View {
ChannelsCacheModel.shared.store(channel)
}
}
case let .channelPlaylist(_, id, _):
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(id),
case let .channelPlaylist(_, id, title):
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(.init(id: id, title: title)),
!cache.videos.isEmpty
{
contentItems = ContentItem.array(of: cache.videos)

View File

@ -54,7 +54,7 @@ struct PlaylistVideosView: View {
}
func loadCachedResource() {
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist.id) {
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist.channelPlaylist) {
DispatchQueue.main.async {
self.channelPlaylist.replace(cache)
}

View File

@ -197,7 +197,8 @@ struct PlaylistsView: View {
func loadCachedResource() {
if !selectedPlaylistID.isEmpty,
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(selectedPlaylistID)
let currentPlaylist,
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(currentPlaylist.channelPlaylist)
{
DispatchQueue.main.async {
self.channelPlaylist.replace(cache)

View File

@ -18,6 +18,7 @@ struct ContentItemView: View {
placeholderItem()
}
}
.id(item.cacheKey)
}
@ViewBuilder func videoItem(_ video: Video) -> some View {