From 5559e78bc051945d7bb31b0f24d4fe94c2624631 Mon Sep 17 00:00:00 2001 From: Ricky Kresslein Date: Tue, 27 Feb 2024 19:01:11 +0100 Subject: [PATCH] Add setting to show/hide recents and limit number of recents shown --- Shared/Defaults.swift | 3 + Shared/Navigation/AppSidebarRecents.swift | 4 +- Shared/Navigation/Sidebar.swift | 7 +- Shared/Settings/HistorySettings.swift | 88 +++++++++++++++++++++++ 4 files changed, 99 insertions(+), 3 deletions(-) diff --git a/Shared/Defaults.swift b/Shared/Defaults.swift index 8b3d6b80..7df4c47d 100644 --- a/Shared/Defaults.swift +++ b/Shared/Defaults.swift @@ -225,6 +225,9 @@ extension Defaults.Keys { static let saveRecents = Key("saveRecents", default: true) static let saveHistory = Key("saveHistory", default: true) + static let showRecents = Key("showRecents", default: true) + static let limitRecents = Key("limitRecents", default: false) + static let limitRecentsAmount = Key("limitRecentsAmount", default: 10) static let showWatchingProgress = Key("showWatchingProgress", default: true) static let saveLastPlayed = Key("saveLastPlayed", default: false) diff --git a/Shared/Navigation/AppSidebarRecents.swift b/Shared/Navigation/AppSidebarRecents.swift index d20d40ed..a6e5d882 100644 --- a/Shared/Navigation/AppSidebarRecents.swift +++ b/Shared/Navigation/AppSidebarRecents.swift @@ -5,12 +5,14 @@ struct AppSidebarRecents: View { var recents = RecentsModel.shared @Default(.recentlyOpened) private var recentItems + @Default(.limitRecents) private var limitRecents + @Default(.limitRecentsAmount) private var limitRecentsAmount var body: some View { Group { if !recentItems.isEmpty { Section(header: Text("Recents")) { - ForEach(recentItems.reversed()) { recent in + ForEach(recentItems.reversed().prefix(limitRecents ? limitRecentsAmount : recentItems.count)) { recent in Group { switch recent.type { case .channel: diff --git a/Shared/Navigation/Sidebar.swift b/Shared/Navigation/Sidebar.swift index 8d3f8607..8131055d 100644 --- a/Shared/Navigation/Sidebar.swift +++ b/Shared/Navigation/Sidebar.swift @@ -13,6 +13,7 @@ struct Sidebar: View { @Default(.showDocuments) private var showDocuments #endif @Default(.showUnwatchedFeedBadges) private var showUnwatchedFeedBadges + @Default(.showRecents) private var showRecents var body: some View { ScrollViewReader { scrollView in @@ -20,8 +21,10 @@ struct Sidebar: View { mainNavigationLinks if !accounts.isEmpty { - AppSidebarRecents() - .id("recentlyOpened") + if showRecents { + AppSidebarRecents() + .id("recentlyOpened") + } if accounts.api.signedIn { if visibleSections.contains(.subscriptions), accounts.app.supportsSubscriptions { diff --git a/Shared/Settings/HistorySettings.swift b/Shared/Settings/HistorySettings.swift index a999240d..082cf267 100644 --- a/Shared/Settings/HistorySettings.swift +++ b/Shared/Settings/HistorySettings.swift @@ -10,6 +10,9 @@ struct HistorySettings: View { @Default(.saveRecents) private var saveRecents @Default(.saveLastPlayed) private var saveLastPlayed @Default(.saveHistory) private var saveHistory + @Default(.showRecents) private var showRecents + @Default(.limitRecents) private var limitRecents + @Default(.limitRecentsAmount) private var limitRecentsAmount @Default(.showWatchingProgress) private var showWatchingProgress @Default(.watchedThreshold) private var watchedThreshold @Default(.watchedVideoStyle) private var watchedVideoStyle @@ -56,6 +59,26 @@ struct HistorySettings: View { Section(header: SettingsHeader(text: "History".localized())) { Toggle("Save history of searches, channels and playlists", isOn: $saveRecents) Toggle("Save history of played videos", isOn: $saveHistory) + Toggle("Show recents in sidebar", isOn: $showRecents) + #if os(macOS) + HStack { + Toggle("Limit recents shown", isOn: $limitRecents) + .frame(minWidth: 140, alignment: .leading) + .disabled(!showRecents) + Spacer() + counterButtons(for: $limitRecentsAmount) + .disabled(!limitRecents) + } + #else + Toggle("Limit recents shown", isOn: $limitRecents) + .disabled(!showRecents) + HStack { + Text("Recents shown") + Spacer() + counterButtons(for: $limitRecentsAmount) + .disabled(!limitRecents) + } + #endif Toggle("Show progress of watching on thumbnails", isOn: $showWatchingProgress) .disabled(!saveHistory) Toggle("Keep last played video in the queue after restart", isOn: $saveLastPlayed) @@ -169,6 +192,71 @@ struct HistorySettings: View { .foregroundColor(.red) } } + + private func counterButtons(for _value: Binding) -> some View { + var value: Binding { + Binding( + get: { return _value.wrappedValue }, + set: { + if $0 < 1 { + _value.wrappedValue = 1 + } else { + _value.wrappedValue = $0 + } + } + ) + } + + return HStack { + #if !os(tvOS) + Label("Minus", systemImage: "minus") + .imageScale(.large) + .labelStyle(.iconOnly) + .padding(7) + .foregroundColor(limitRecents ? .accentColor : .gray) + .accessibilityAddTraits(.isButton) + #if os(iOS) + .frame(minHeight: 35) + .background(RoundedRectangle(cornerRadius: 4).strokeBorder(lineWidth: 1).foregroundColor(.accentColor)) + #endif + .contentShape(Rectangle()) + .onTapGesture { + value.wrappedValue -= 1 + } + #endif + + #if os(tvOS) + let textFieldWidth = 100.00 + #else + let textFieldWidth = 30.00 + #endif + + TextField("Duration", value: value, formatter: NumberFormatter()) + .frame(width: textFieldWidth, alignment: .trailing) + .multilineTextAlignment(.center) + .labelsHidden() + .foregroundColor(limitRecents ? .accentColor : .gray) + #if !os(macOS) + .keyboardType(.numberPad) + #endif + + #if !os(tvOS) + Label("Plus", systemImage: "plus") + .imageScale(.large) + .labelStyle(.iconOnly) + .padding(7) + .foregroundColor(limitRecents ? .accentColor : .gray) + .accessibilityAddTraits(.isButton) + #if os(iOS) + .background(RoundedRectangle(cornerRadius: 4).strokeBorder(lineWidth: 1).foregroundColor(.accentColor)) + #endif + .contentShape(Rectangle()) + .onTapGesture { + value.wrappedValue += 1 + } + #endif + } + } } struct HistorySettings_Previews: PreviewProvider {