2022-12-10 02:01:59 +00:00
|
|
|
import Cache
|
|
|
|
import Foundation
|
|
|
|
import Logging
|
|
|
|
import SwiftyJSON
|
|
|
|
|
|
|
|
struct FeedCacheModel {
|
|
|
|
static let shared = FeedCacheModel()
|
2022-12-11 15:15:06 +00:00
|
|
|
static let limit = 30
|
2022-12-10 02:01:59 +00:00
|
|
|
let logger = Logger(label: "stream.yattee.cache.feed")
|
|
|
|
|
|
|
|
static let diskConfig = DiskConfig(name: "feed")
|
|
|
|
static let memoryConfig = MemoryConfig()
|
|
|
|
|
|
|
|
let storage = try! Storage<String, JSON>(
|
|
|
|
diskConfig: Self.diskConfig,
|
|
|
|
memoryConfig: Self.memoryConfig,
|
|
|
|
transformer: CacheModel.jsonTransformer
|
|
|
|
)
|
|
|
|
|
|
|
|
func storeFeed(account: Account, videos: [Video]) {
|
2022-12-11 11:38:57 +00:00
|
|
|
let date = iso8601DateFormatter.string(from: Date())
|
2022-12-10 02:01:59 +00:00
|
|
|
logger.info("caching feed \(account.feedCacheKey) -- \(date)")
|
|
|
|
let feedTimeObject: JSON = ["date": date]
|
2022-12-11 15:15:06 +00:00
|
|
|
let videosObject: JSON = ["videos": videos.prefix(Self.limit).map { $0.json.object }]
|
2022-12-10 02:01:59 +00:00
|
|
|
try? storage.setObject(feedTimeObject, forKey: feedTimeCacheKey(account.feedCacheKey))
|
|
|
|
try? storage.setObject(videosObject, forKey: account.feedCacheKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func retrieveFeed(account: Account) -> [Video] {
|
|
|
|
logger.info("retrieving cache for \(account.feedCacheKey)")
|
|
|
|
|
|
|
|
if let json = try? storage.object(forKey: account.feedCacheKey),
|
|
|
|
let videos = json.dictionaryValue["videos"]
|
|
|
|
{
|
|
|
|
return videos.arrayValue.map { Video.from($0) }
|
|
|
|
}
|
|
|
|
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFeedTime(account: Account) -> Date? {
|
|
|
|
if let json = try? storage.object(forKey: feedTimeCacheKey(account.feedCacheKey)),
|
|
|
|
let string = json.dictionaryValue["date"]?.string,
|
2022-12-11 11:38:57 +00:00
|
|
|
let date = iso8601DateFormatter.date(from: string)
|
2022-12-10 02:01:59 +00:00
|
|
|
{
|
|
|
|
return date
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func clear() {
|
|
|
|
try? storage.removeAll()
|
|
|
|
}
|
|
|
|
|
2022-12-11 11:38:57 +00:00
|
|
|
private func feedTimeCacheKey(_ feedCacheKey: String) -> String {
|
|
|
|
"\(feedCacheKey)-feedTime"
|
|
|
|
}
|
|
|
|
|
|
|
|
private var iso8601DateFormatter: ISO8601DateFormatter {
|
2022-12-10 02:01:59 +00:00
|
|
|
.init()
|
|
|
|
}
|
|
|
|
|
2022-12-11 11:38:57 +00:00
|
|
|
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
|
2022-12-10 02:01:59 +00:00
|
|
|
}
|
|
|
|
}
|