1
0
mirror of https://github.com/yattee/yattee.git synced 2025-04-28 07:50:33 +05:30

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 Cache
import Defaults
import Foundation import Foundation
import Logging import Logging
import SwiftyJSON import SwiftyJSON
struct FeedCacheModel: CacheModel { struct FeedCacheModel: CacheModel {
static let shared = FeedCacheModel() static let shared = FeedCacheModel()
static let limit = 30
let logger = Logger(label: "stream.yattee.cache.feed") let logger = Logger(label: "stream.yattee.cache.feed")
static let diskConfig = DiskConfig(name: "feed") static let diskConfig = DiskConfig(name: "feed")
@ -21,7 +21,7 @@ struct FeedCacheModel: CacheModel {
let date = iso8601DateFormatter.string(from: Date()) let date = iso8601DateFormatter.string(from: Date())
logger.info("caching feed \(account.feedCacheKey) -- \(date)") logger.info("caching feed \(account.feedCacheKey) -- \(date)")
let feedTimeObject: JSON = ["date": 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(feedTimeObject, forKey: feedTimeCacheKey(account.feedCacheKey))
try? storage?.setObject(videosObject, forKey: account.feedCacheKey) try? storage?.setObject(videosObject, forKey: account.feedCacheKey)
} }
@ -49,6 +49,15 @@ struct FeedCacheModel: CacheModel {
return nil 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 { private func feedTimeCacheKey(_ feedCacheKey: String) -> String {
"\(feedCacheKey)-feedTime" "\(feedCacheKey)-feedTime"
} }

View File

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

View File

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

View File

@ -6,8 +6,8 @@ struct AdvancedSettings: View {
@Default(.mpvCacheSecs) private var mpvCacheSecs @Default(.mpvCacheSecs) private var mpvCacheSecs
@Default(.mpvCachePauseWait) private var mpvCachePauseWait @Default(.mpvCachePauseWait) private var mpvCachePauseWait
@Default(.mpvEnableLogging) private var mpvEnableLogging @Default(.mpvEnableLogging) private var mpvEnableLogging
@Default(.countryOfPublicInstances) private var countryOfPublicInstances @Default(.showCacheStatus) private var showCacheStatus
@Default(.instances) private var instances @Default(.feedCacheSize) private var feedCacheSize
@State private var countries = [String]() @State private var countries = [String]()
@State private var filesToShare = [MPVClient.logFile] @State private var filesToShare = [MPVClient.logFile]
@ -33,9 +33,6 @@ struct AdvancedSettings: View {
#endif #endif
#endif #endif
} }
.onChange(of: countryOfPublicInstances) { newCountry in
InstancesManifest.shared.setPublicAccount(newCountry, asCurrent: AccountsModel.shared.current?.isPublic ?? true)
}
#if os(tvOS) #if os(tvOS)
.frame(maxWidth: 1000) .frame(maxWidth: 1000)
#endif #endif
@ -85,9 +82,10 @@ struct AdvancedSettings: View {
} }
} }
Section(header: SettingsHeader(text: "Cache")) { Section(header: SettingsHeader(text: "Cache"), footer: cacheSize) {
showCacheStatusToggle
feedCacheSizeTextField
clearCacheButton clearCacheButton
cacheSize
} }
} }
@ -130,6 +128,22 @@ struct AdvancedSettings: View {
} }
#endif #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 { private var clearCacheButton: some View {
Button { Button {
settings.presentAlert( settings.presentAlert(

View File

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

View File

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