mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
Refactor
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
|
@@ -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,
|
||||
|
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user