mirror of
https://github.com/yattee/yattee.git
synced 2025-08-06 10:44:06 +00:00
Channel playlists cache
This commit is contained in:
70
Model/Cache/ChannelPlaylistsCacheModel.swift
Normal file
70
Model/Cache/ChannelPlaylistsCacheModel.swift
Normal file
@@ -0,0 +1,70 @@
|
||||
import Cache
|
||||
import Foundation
|
||||
import Logging
|
||||
import SwiftyJSON
|
||||
|
||||
struct ChannelPlaylistsCacheModel {
|
||||
static let shared = ChannelPlaylistsCacheModel()
|
||||
let logger = Logger(label: "stream.yattee.cache.channel-playlists")
|
||||
|
||||
static let diskConfig = DiskConfig(name: "channel-playlists")
|
||||
static let memoryConfig = MemoryConfig()
|
||||
|
||||
let storage = try! Storage<String, JSON>(
|
||||
diskConfig: Self.diskConfig,
|
||||
memoryConfig: Self.memoryConfig,
|
||||
transformer: CacheModel.jsonTransformer
|
||||
)
|
||||
|
||||
func storePlaylist(playlist: ChannelPlaylist) {
|
||||
let date = CacheModel.shared.iso8601DateFormatter.string(from: Date())
|
||||
logger.info("STORE \(playlistCacheKey(playlist.id)) -- \(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))
|
||||
}
|
||||
|
||||
func retrievePlaylist(_ id: ChannelPlaylist.ID) -> ChannelPlaylist? {
|
||||
logger.info("RETRIEVE \(playlistCacheKey(id))")
|
||||
|
||||
if let json = try? storage.object(forKey: playlistCacheKey(id)).dictionaryValue["playlist"] {
|
||||
return ChannelPlaylist.from(json)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getPlaylistsTime(_ id: ChannelPlaylist.ID) -> Date? {
|
||||
if let json = try? storage.object(forKey: playlistTimeCacheKey(id)),
|
||||
let string = json.dictionaryValue["date"]?.string,
|
||||
let date = CacheModel.shared.iso8601DateFormatter.date(from: string)
|
||||
{
|
||||
return date
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getFormattedPlaylistTime(_ id: ChannelPlaylist.ID) -> String {
|
||||
if let time = getPlaylistsTime(id) {
|
||||
let isSameDay = Calendar(identifier: .iso8601).isDate(time, inSameDayAs: Date())
|
||||
let formatter = isSameDay ? CacheModel.shared.dateFormatterForTimeOnly : CacheModel.shared.dateFormatter
|
||||
return formatter.string(from: time)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func clear() {
|
||||
try? storage.removeAll()
|
||||
}
|
||||
|
||||
private func playlistCacheKey(_ playlist: ChannelPlaylist.ID) -> String {
|
||||
"channelplaylists-\(playlist)"
|
||||
}
|
||||
|
||||
private func playlistTimeCacheKey(_ playlist: ChannelPlaylist.ID) -> String {
|
||||
"\(playlistCacheKey(playlist))-time"
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
import Foundation
|
||||
import SwiftyJSON
|
||||
|
||||
struct ChannelPlaylist: Identifiable {
|
||||
var id: String = UUID().uuidString
|
||||
@@ -7,4 +8,26 @@ struct ChannelPlaylist: Identifiable {
|
||||
var channel: Channel?
|
||||
var videos = [Video]()
|
||||
var videosCount: Int?
|
||||
|
||||
var json: JSON {
|
||||
[
|
||||
"id": id,
|
||||
"title": title,
|
||||
"thumbnailURL": thumbnailURL?.absoluteString ?? "",
|
||||
"channel": channel?.json.object ?? "",
|
||||
"videos": videos.map { $0.json.object },
|
||||
"videosCount": String(videosCount ?? 0)
|
||||
]
|
||||
}
|
||||
|
||||
static func from(_ json: JSON) -> Self {
|
||||
ChannelPlaylist(
|
||||
id: json["id"].stringValue,
|
||||
title: json["title"].stringValue,
|
||||
thumbnailURL: json["thumbnailURL"].url,
|
||||
channel: Channel.from(json["channel"]),
|
||||
videos: json["videos"].arrayValue.map { Video.from($0) },
|
||||
videosCount: json["videosCount"].int
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@@ -40,8 +40,6 @@ struct Playlist: Identifiable, Equatable, Hashable {
|
||||
}
|
||||
|
||||
var json: JSON {
|
||||
let dateFormatter = ISO8601DateFormatter()
|
||||
|
||||
return [
|
||||
"id": id,
|
||||
"title": title,
|
||||
@@ -53,8 +51,6 @@ struct Playlist: Identifiable, Equatable, Hashable {
|
||||
}
|
||||
|
||||
static func from(_ json: JSON) -> Self {
|
||||
let dateFormatter = ISO8601DateFormatter()
|
||||
|
||||
return .init(
|
||||
id: json["id"].stringValue,
|
||||
title: json["title"].stringValue,
|
||||
@@ -71,4 +67,8 @@ struct Playlist: Identifiable, Equatable, Hashable {
|
||||
func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(id)
|
||||
}
|
||||
|
||||
var channelPlaylist: ChannelPlaylist {
|
||||
ChannelPlaylist(id: id, title: title, videos: videos, videosCount: videos.count)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user