mirror of
https://github.com/yattee/yattee.git
synced 2025-08-04 01:34:10 +00:00
.github
Backports
Extensions
Fixtures
Model
Accounts
Applications
Cache
BaseCacheModel.swift
BookmarksCacheModel.swift
CacheModel.swift
ChannelPlaylistsCacheModel.swift
ChannelsCacheModel.swift
FeedCacheModel.swift
PlaylistsCacheModel.swift
SubscribedChannelsModel.swift
VideosCacheModel.swift
Favorites
Import Export Settings
Player
ReturnYouTubeDislike
Search
SponsorBlock
Yattee.xcdatamodeld
Captions.swift
Channel.swift
ChannelPage.swift
ChannelPlaylist.swift
Chapter.swift
Comment.swift
CommentsModel.swift
CommentsPage.swift
ContentItem.swift
Country.swift
DocumentsModel.swift
FeedModel.swift
HistoryModel.swift
InstancesManifest.swift
KeychainModel.swift
ManifestedInstance.swift
MenuModel.swift
NavigationModel.swift
NetworkStateModel.swift
OpenVideosModel.swift
PersistenceController.swift
Playlist.swift
PlaylistsModel.swift
QualityProfile.swift
QualityProfilesModel.swift
RecentsModel.swift
SeekModel.swift
SeekType.swift
Segment.swift
SettingsModel.swift
SingleAssetStream.swift
Store.swift
Stream.swift
Thumbnail.swift
ThumbnailsModel.swift
TrendingCategory.swift
URLBookmarkModel.swift
UnwatchedFeedCountModel.swift
Video.swift
Watch.swift
WatchModel.swift
Open in Yattee
Shared
Shared Tests
Tests Apple TV
Tests iOS
Tests macOS
Vendor
Xcode-config
Yattee.xcodeproj
fastlane
iOS
macOS
tvOS
.gitignore
.swift-version
.swiftformat
.swiftlint.yml
CHANGELOG.md
Gemfile
Gemfile.lock
LICENSE
README.md

Add settings: Show channel avatars in channels lists Show channel avatars in videos lists Fix #508
67 lines
2.0 KiB
Swift
67 lines
2.0 KiB
Swift
import Cache
|
|
import Defaults
|
|
import Foundation
|
|
import Logging
|
|
import SwiftyJSON
|
|
|
|
struct FeedCacheModel: CacheModel {
|
|
static let shared = Self()
|
|
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: BaseCacheModel.jsonTransformer
|
|
)
|
|
|
|
func storeFeed(account: Account, videos: [Video]) {
|
|
DispatchQueue.global(qos: .background).async {
|
|
let date = iso8601DateFormatter.string(from: Date())
|
|
logger.info("caching feed \(account.feedCacheKey) -- \(date)")
|
|
let feedTimeObject: JSON = ["date": date]
|
|
let videosObject: JSON = ["videos": videos.prefix(cacheLimit).map { $0.json.object }]
|
|
try? storage?.setObject(feedTimeObject, forKey: feedTimeCacheKey(account.feedCacheKey))
|
|
try? storage?.setObject(videosObject, forKey: account.feedCacheKey)
|
|
}
|
|
}
|
|
|
|
func retrieveFeed(account: Account) -> [Video] {
|
|
logger.debug("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,
|
|
let date = iso8601DateFormatter.date(from: string)
|
|
{
|
|
return date
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
private var cacheLimit: Int {
|
|
let setting = Int(Defaults[.feedCacheSize]) ?? 0
|
|
if setting > 0 {
|
|
return setting
|
|
}
|
|
|
|
return 50
|
|
}
|
|
|
|
private func feedTimeCacheKey(_ feedCacheKey: String) -> String {
|
|
"\(feedCacheKey)-feedTime"
|
|
}
|
|
}
|