mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
User playlists cache
This commit is contained in:
@@ -13,18 +13,36 @@ struct CacheModel {
|
||||
func clear() {
|
||||
FeedCacheModel.shared.clear()
|
||||
VideosCacheModel.shared.clear()
|
||||
PlaylistsCacheModel.shared.clear()
|
||||
}
|
||||
|
||||
var totalSize: Int {
|
||||
(FeedCacheModel.shared.storage.totalDiskStorageSize ?? 0) +
|
||||
(VideosCacheModel.shared.storage.totalDiskStorageSize ?? 0)
|
||||
(VideosCacheModel.shared.storage.totalDiskStorageSize ?? 0) +
|
||||
(PlaylistsCacheModel.shared.storage.totalDiskStorageSize ?? 0)
|
||||
}
|
||||
|
||||
var totalSizeFormatted: String {
|
||||
totalSizeFormatter.string(fromByteCount: Int64(totalSize))
|
||||
byteCountFormatter.string(fromByteCount: Int64(totalSize))
|
||||
}
|
||||
|
||||
private var totalSizeFormatter: ByteCountFormatter {
|
||||
.init()
|
||||
var dateFormatter: DateFormatter {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateStyle = .short
|
||||
formatter.timeStyle = .medium
|
||||
|
||||
return formatter
|
||||
}
|
||||
|
||||
var dateFormatterForTimeOnly: DateFormatter {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateStyle = .none
|
||||
formatter.timeStyle = .medium
|
||||
|
||||
return formatter
|
||||
}
|
||||
|
||||
var iso8601DateFormatter: ISO8601DateFormatter { .init() }
|
||||
|
||||
private var byteCountFormatter: ByteCountFormatter { .init() }
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@ struct FeedCacheModel {
|
||||
)
|
||||
|
||||
func storeFeed(account: Account, videos: [Video]) {
|
||||
let date = iso8601DateFormatter.string(from: Date())
|
||||
let date = CacheModel.shared.iso8601DateFormatter.string(from: Date())
|
||||
logger.info("caching feed \(account.feedCacheKey) -- \(date)")
|
||||
let feedTimeObject: JSON = ["date": date]
|
||||
let videosObject: JSON = ["videos": videos.prefix(Self.limit).map { $0.json.object }]
|
||||
@@ -41,7 +41,7 @@ struct FeedCacheModel {
|
||||
func getFeedTime(account: Account) -> Date? {
|
||||
if let json = try? storage.object(forKey: feedTimeCacheKey(account.feedCacheKey)),
|
||||
let string = json.dictionaryValue["date"]?.string,
|
||||
let date = iso8601DateFormatter.date(from: string)
|
||||
let date = CacheModel.shared.iso8601DateFormatter.date(from: string)
|
||||
{
|
||||
return date
|
||||
}
|
||||
@@ -56,24 +56,4 @@ struct FeedCacheModel {
|
||||
private func feedTimeCacheKey(_ feedCacheKey: String) -> String {
|
||||
"\(feedCacheKey)-feedTime"
|
||||
}
|
||||
|
||||
private var iso8601DateFormatter: ISO8601DateFormatter {
|
||||
.init()
|
||||
}
|
||||
|
||||
private var dateFormatter: DateFormatter {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateStyle = .short
|
||||
formatter.timeStyle = .medium
|
||||
|
||||
return formatter
|
||||
}
|
||||
|
||||
private var dateFormatterForTimeOnly: DateFormatter {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateStyle = .none
|
||||
formatter.timeStyle = .medium
|
||||
|
||||
return formatter
|
||||
}
|
||||
}
|
||||
|
73
Model/Cache/PlaylistsCacheModel.swift
Normal file
73
Model/Cache/PlaylistsCacheModel.swift
Normal file
@@ -0,0 +1,73 @@
|
||||
import Cache
|
||||
import Foundation
|
||||
import Logging
|
||||
import SwiftyJSON
|
||||
|
||||
struct PlaylistsCacheModel {
|
||||
static let shared = PlaylistsCacheModel()
|
||||
static let limit = 30
|
||||
let logger = Logger(label: "stream.yattee.cache.playlists")
|
||||
|
||||
static let diskConfig = DiskConfig(name: "playlists")
|
||||
static let memoryConfig = MemoryConfig()
|
||||
|
||||
let storage = try! Storage<String, JSON>(
|
||||
diskConfig: Self.diskConfig,
|
||||
memoryConfig: Self.memoryConfig,
|
||||
transformer: CacheModel.jsonTransformer
|
||||
)
|
||||
|
||||
func storePlaylist(account: Account, playlists: [Playlist]) {
|
||||
let date = CacheModel.shared.iso8601DateFormatter.string(from: Date())
|
||||
logger.info("caching \(playlistCacheKey(account)) -- \(date)")
|
||||
let feedTimeObject: JSON = ["date": date]
|
||||
let playlistsObject: JSON = ["playlists": playlists.map { $0.json.object }]
|
||||
try? storage.setObject(feedTimeObject, forKey: playlistTimeCacheKey(account))
|
||||
try? storage.setObject(playlistsObject, forKey: playlistCacheKey(account))
|
||||
}
|
||||
|
||||
func retrievePlaylists(account: Account) -> [Playlist] {
|
||||
logger.info("retrieving cache for \(playlistCacheKey(account))")
|
||||
|
||||
if let json = try? storage.object(forKey: playlistCacheKey(account)),
|
||||
let playlists = json.dictionaryValue["playlists"]
|
||||
{
|
||||
return playlists.arrayValue.map { Playlist.from($0) }
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
func getPlaylistsTime(account: Account) -> Date? {
|
||||
if let json = try? storage.object(forKey: playlistTimeCacheKey(account)),
|
||||
let string = json.dictionaryValue["date"]?.string,
|
||||
let date = CacheModel.shared.iso8601DateFormatter.date(from: string)
|
||||
{
|
||||
return date
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getFormattedPlaylistTime(account: Account) -> String {
|
||||
if let time = getPlaylistsTime(account: account) {
|
||||
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(_ account: Account) -> String {
|
||||
"playlists-\(account.id)"
|
||||
}
|
||||
|
||||
private func playlistTimeCacheKey(_ account: Account) -> String {
|
||||
"\(playlistCacheKey(account))-time"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user