Cache settings

This commit is contained in:
Arkadiusz Fal 2022-12-13 12:09:20 +01:00
parent 8f2b570163
commit e4d583a263
6 changed files with 69 additions and 22 deletions

View File

@ -1,11 +1,11 @@
import Cache
import Defaults
import Foundation
import Logging
import SwiftyJSON
struct FeedCacheModel: CacheModel {
static let shared = FeedCacheModel()
static let limit = 30
let logger = Logger(label: "stream.yattee.cache.feed")
static let diskConfig = DiskConfig(name: "feed")
@ -21,7 +21,7 @@ struct FeedCacheModel: CacheModel {
let date = 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 }]
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)
}
@ -49,6 +49,15 @@ struct FeedCacheModel: CacheModel {
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"
}

View File

@ -202,6 +202,9 @@ extension Defaults.Keys {
static let mpvCachePauseWait = Key<String>("mpvCachePauseWait", default: "3")
static let mpvEnableLogging = Key<Bool>("mpvEnableLogging", default: false)
static let showCacheStatus = Key<Bool>("showCacheStatus", default: false)
static let feedCacheSize = Key<String>("feedCacheSize", default: "50")
static let subscriptionsViewPage = Key<SubscriptionsView.Page>("subscriptionsViewPage", default: .feed)
static let subscriptionsListingStyle = Key<ListingStyle>("subscriptionsListingStyle", default: .cells)

View File

@ -22,6 +22,7 @@ struct PlaylistsView: View {
@Namespace private var focusNamespace
@Default(.playlistListingStyle) private var playlistListingStyle
@Default(.showCacheStatus) private var showCacheStatus
var items: [ContentItem] {
var videos = currentPlaylist?.videos ?? []
@ -77,13 +78,15 @@ struct PlaylistsView: View {
Spacer()
#else
VerticalCells(items: items) {
HStack {
Spacer()
if showCacheStatus {
HStack {
Spacer()
CacheStatusHeader(
refreshTime: cache.getFormattedPlaylistTime(account: accounts.current),
isLoading: model.isLoading
)
CacheStatusHeader(
refreshTime: cache.getFormattedPlaylistTime(account: accounts.current),
isLoading: model.isLoading
)
}
}
}
.environment(\.scrollViewBottomPadding, 70)

View File

@ -6,8 +6,8 @@ struct AdvancedSettings: View {
@Default(.mpvCacheSecs) private var mpvCacheSecs
@Default(.mpvCachePauseWait) private var mpvCachePauseWait
@Default(.mpvEnableLogging) private var mpvEnableLogging
@Default(.countryOfPublicInstances) private var countryOfPublicInstances
@Default(.instances) private var instances
@Default(.showCacheStatus) private var showCacheStatus
@Default(.feedCacheSize) private var feedCacheSize
@State private var countries = [String]()
@State private var filesToShare = [MPVClient.logFile]
@ -33,9 +33,6 @@ struct AdvancedSettings: View {
#endif
#endif
}
.onChange(of: countryOfPublicInstances) { newCountry in
InstancesManifest.shared.setPublicAccount(newCountry, asCurrent: AccountsModel.shared.current?.isPublic ?? true)
}
#if os(tvOS)
.frame(maxWidth: 1000)
#endif
@ -85,9 +82,10 @@ struct AdvancedSettings: View {
}
}
Section(header: SettingsHeader(text: "Cache")) {
Section(header: SettingsHeader(text: "Cache"), footer: cacheSize) {
showCacheStatusToggle
feedCacheSizeTextField
clearCacheButton
cacheSize
}
}
@ -130,6 +128,22 @@ struct AdvancedSettings: View {
}
#endif
private var feedCacheSizeTextField: some View {
HStack {
Text("Maximum feed items")
.frame(minWidth: 200, alignment: .leading)
TextField("Limit", text: $feedCacheSize)
#if !os(macOS)
.keyboardType(.numberPad)
#endif
}
.multilineTextAlignment(.trailing)
}
private var showCacheStatusToggle: some View {
Toggle("Show cache status", isOn: $showCacheStatus)
}
private var clearCacheButton: some View {
Button {
settings.presentAlert(

View File

@ -1,3 +1,4 @@
import Defaults
import SDWebImageSwiftUI
import SwiftUI
@ -5,6 +6,8 @@ struct ChannelsView: View {
@ObservedObject private var subscriptions = SubscribedChannelsModel.shared
@ObservedObject private var accounts = AccountsModel.shared
@Default(.showCacheStatus) private var showCacheStatus
var body: some View {
List {
Section(header: header) {
@ -81,14 +84,19 @@ struct ChannelsView: View {
SubscriptionsPageButton()
#endif
Spacer()
if showCacheStatus {
Spacer()
CacheStatusHeader(
refreshTime: subscriptions.formattedCacheTime,
isLoading: subscriptions.isLoading
)
CacheStatusHeader(
refreshTime: subscriptions.formattedCacheTime,
isLoading: subscriptions.isLoading
)
}
#if os(tvOS)
if !showCacheStatus {
Spacer()
}
Button {
subscriptions.load(force: true)
} label: {

View File

@ -6,6 +6,8 @@ struct FeedView: View {
@ObservedObject private var feed = FeedModel.shared
@ObservedObject private var accounts = AccountsModel.shared
@Default(.showCacheStatus) private var showCacheStatus
#if os(tvOS)
@Default(.subscriptionsListingStyle) private var subscriptionsListingStyle
#endif
@ -22,11 +24,19 @@ struct FeedView: View {
ListingStyleButtons(listingStyle: $subscriptionsListingStyle)
#endif
Spacer()
if showCacheStatus {
Spacer()
CacheStatusHeader(refreshTime: feed.formattedFeedTime, isLoading: feed.isLoading)
CacheStatusHeader(
refreshTime: feed.formattedFeedTime,
isLoading: feed.isLoading
)
}
#if os(tvOS)
if !showCacheStatus {
Spacer()
}
Button {
feed.loadResources(force: true)
} label: {