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

@@ -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
}
}
}