diff --git a/Fixtures/ChannelPlaylist+Fixtures.swift b/Fixtures/ChannelPlaylist+Fixtures.swift index 7c72add2..e86609c5 100644 --- a/Fixtures/ChannelPlaylist+Fixtures.swift +++ b/Fixtures/ChannelPlaylist+Fixtures.swift @@ -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, diff --git a/Model/Cache/ChannelPlaylistsCacheModel.swift b/Model/Cache/ChannelPlaylistsCacheModel.swift index a62a4686..60f3d0df 100644 --- a/Model/Cache/ChannelPlaylistsCacheModel.swift +++ b/Model/Cache/ChannelPlaylistsCacheModel.swift @@ -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" } } diff --git a/Model/ChannelPlaylist.swift b/Model/ChannelPlaylist.swift index 444dc10e..a8545cab 100644 --- a/Model/ChannelPlaylist.swift +++ b/Model/ChannelPlaylist.swift @@ -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, diff --git a/Model/ContentItem.swift b/Model/ContentItem.swift index 4384673c..07a3fb40 100644 --- a/Model/ContentItem.swift +++ b/Model/ContentItem.swift @@ -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 + } + } } diff --git a/Shared/Channels/ChannelPlaylistView.swift b/Shared/Channels/ChannelPlaylistView.swift index 60d71478..24cdf585 100644 --- a/Shared/Channels/ChannelPlaylistView.swift +++ b/Shared/Channels/ChannelPlaylistView.swift @@ -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) } diff --git a/Shared/Home/FavoriteItemView.swift b/Shared/Home/FavoriteItemView.swift index 85f73edf..e60fbae5 100644 --- a/Shared/Home/FavoriteItemView.swift +++ b/Shared/Home/FavoriteItemView.swift @@ -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) diff --git a/Shared/Playlists/PlaylistVideosView.swift b/Shared/Playlists/PlaylistVideosView.swift index 1f482287..dcf4c037 100644 --- a/Shared/Playlists/PlaylistVideosView.swift +++ b/Shared/Playlists/PlaylistVideosView.swift @@ -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) } diff --git a/Shared/Playlists/PlaylistsView.swift b/Shared/Playlists/PlaylistsView.swift index c15ba068..b7ed8984 100644 --- a/Shared/Playlists/PlaylistsView.swift +++ b/Shared/Playlists/PlaylistsView.swift @@ -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) diff --git a/Shared/Views/ContentItemView.swift b/Shared/Views/ContentItemView.swift index 16a765d9..f2932e64 100644 --- a/Shared/Views/ContentItemView.swift +++ b/Shared/Views/ContentItemView.swift @@ -18,6 +18,7 @@ struct ContentItemView: View { placeholderItem() } } + .id(item.cacheKey) } @ViewBuilder func videoItem(_ video: Video) -> some View {