From 78ff495927be4dfc0c761a67d5427e22644ae9fa Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Tue, 15 Nov 2022 12:22:27 +0100 Subject: [PATCH] Add clear history button to home --- Model/HistoryModel.swift | 5 ++++ Shared/Home/HistoryView.swift | 4 +-- Shared/Home/HomeView.swift | 40 +++++++++++++++++++++------ Shared/Player/PlayerQueueRow.swift | 5 +++- Shared/Settings/HistorySettings.swift | 5 +--- 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/Model/HistoryModel.swift b/Model/HistoryModel.swift index a2365091..1195534e 100644 --- a/Model/HistoryModel.swift +++ b/Model/HistoryModel.swift @@ -94,6 +94,11 @@ extension PlayerModel { } } + func removeHistory() { + removeAllWatches() + CacheModel.shared.removeAll() + } + func removeWatch(_ watch: Watch) { context.delete(watch) try? context.save() diff --git a/Shared/Home/HistoryView.swift b/Shared/Home/HistoryView.swift index ef969242..ea5443b8 100644 --- a/Shared/Home/HistoryView.swift +++ b/Shared/Home/HistoryView.swift @@ -3,13 +3,13 @@ import SwiftUI struct HistoryView: View { static let detailsPreloadLimit = 50 + var limit = 10 + @FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)]) var watches: FetchedResults @EnvironmentObject private var player - var limit = 10 - var body: some View { LazyVStack { if visibleWatches.isEmpty { diff --git a/Shared/Home/HomeView.swift b/Shared/Home/HomeView.swift index 66957fce..5ff19e0a 100644 --- a/Shared/Home/HomeView.swift +++ b/Shared/Home/HomeView.swift @@ -12,6 +12,10 @@ struct HomeView: View { @State private var favoritesChanged = false + @FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)]) + var watches: FetchedResults + @State private var historyID = UUID() + var favoritesObserver: Any? #if !os(tvOS) @@ -79,27 +83,45 @@ struct HomeView: View { #if os(macOS) .workaroundForVerticalScrollingBug() #endif - #if os(iOS) - .padding(.top, item == first && RefreshControl.navigationBarTitleDisplayMode == .inline ? 10 : 0) - #endif } #endif } if homeHistoryItems > 0 { VStack { - Text("History") + HStack { + Text("History") + Spacer() + Button { + navigation.presentAlert( + Alert( + title: Text("Are you sure you want to clear history of watched videos?"), + message: Text("It cannot be reverted"), + primaryButton: .destructive(Text("Clear All")) { + PlayerModel.shared.removeHistory() + historyID = UUID() + }, + secondaryButton: .cancel() + ) + ) + } label: { + Label("Clear History", systemImage: "trash") + .font(.headline) + .labelStyle(.iconOnly) + } + } #if os(tvOS) - .padding(.horizontal, 40) + .padding(.horizontal, 40) #else - .padding(.horizontal, 15) + .padding(.horizontal, 15) #endif - .font(.title3.bold()) - .frame(maxWidth: .infinity, alignment: .leading) - .foregroundColor(.secondary) + .font(.title3.bold()) + .frame(maxWidth: .infinity, alignment: .leading) + .foregroundColor(.secondary) HistoryView(limit: homeHistoryItems) + .id(historyID) } } diff --git a/Shared/Player/PlayerQueueRow.swift b/Shared/Player/PlayerQueueRow.swift index d277c425..f68e1759 100644 --- a/Shared/Player/PlayerQueueRow.swift +++ b/Shared/Player/PlayerQueueRow.swift @@ -29,9 +29,12 @@ struct PlayerQueueRow: View { var body: some View { Button { - guard let video = item.video else { + guard let video = item.video else { return } + guard video != player.currentVideo else { + player.show() return } + #if os(iOS) guard !video.localStreamIsDirectory else { if let url = video.localStream?.localURL { diff --git a/Shared/Settings/HistorySettings.swift b/Shared/Settings/HistorySettings.swift index a7673e9c..fae004a9 100644 --- a/Shared/Settings/HistorySettings.swift +++ b/Shared/Settings/HistorySettings.swift @@ -151,10 +151,7 @@ struct HistorySettings: View { message: Text( "This cannot be reverted. You might need to switch between views or restart the app to see changes." ), - primaryButton: .destructive(Text("Clear All")) { - player.removeAllWatches() - CacheModel.shared.removeAll() - }, + primaryButton: .destructive(Text("Clear All"), action: player.removeHistory), secondaryButton: .cancel() ) )