diff --git a/Model/Cache/FeedCacheModel.swift b/Model/Cache/FeedCacheModel.swift index fe392b6d..d65a7936 100644 --- a/Model/Cache/FeedCacheModel.swift +++ b/Model/Cache/FeedCacheModel.swift @@ -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" } diff --git a/Shared/Defaults.swift b/Shared/Defaults.swift index b9b769a7..5ad9f3a1 100644 --- a/Shared/Defaults.swift +++ b/Shared/Defaults.swift @@ -202,6 +202,9 @@ extension Defaults.Keys { static let mpvCachePauseWait = Key("mpvCachePauseWait", default: "3") static let mpvEnableLogging = Key("mpvEnableLogging", default: false) + static let showCacheStatus = Key("showCacheStatus", default: false) + static let feedCacheSize = Key("feedCacheSize", default: "50") + static let subscriptionsViewPage = Key("subscriptionsViewPage", default: .feed) static let subscriptionsListingStyle = Key("subscriptionsListingStyle", default: .cells) diff --git a/Shared/Playlists/PlaylistsView.swift b/Shared/Playlists/PlaylistsView.swift index 17b7d965..4cc31420 100644 --- a/Shared/Playlists/PlaylistsView.swift +++ b/Shared/Playlists/PlaylistsView.swift @@ -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) diff --git a/Shared/Settings/AdvancedSettings.swift b/Shared/Settings/AdvancedSettings.swift index 7d27c9d4..7a30c6d4 100644 --- a/Shared/Settings/AdvancedSettings.swift +++ b/Shared/Settings/AdvancedSettings.swift @@ -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( diff --git a/Shared/Subscriptions/ChannelsView.swift b/Shared/Subscriptions/ChannelsView.swift index 658395dd..2452a8a6 100644 --- a/Shared/Subscriptions/ChannelsView.swift +++ b/Shared/Subscriptions/ChannelsView.swift @@ -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: { diff --git a/Shared/Subscriptions/FeedView.swift b/Shared/Subscriptions/FeedView.swift index 61a069e1..2a8b51b8 100644 --- a/Shared/Subscriptions/FeedView.swift +++ b/Shared/Subscriptions/FeedView.swift @@ -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: {