Add clear history button to home

This commit is contained in:
Arkadiusz Fal 2022-11-15 12:22:27 +01:00
parent 8727fb1e30
commit 78ff495927
5 changed files with 43 additions and 16 deletions

View File

@ -94,6 +94,11 @@ extension PlayerModel {
}
}
func removeHistory() {
removeAllWatches()
CacheModel.shared.removeAll()
}
func removeWatch(_ watch: Watch) {
context.delete(watch)
try? context.save()

View File

@ -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<Watch>
@EnvironmentObject<PlayerModel> private var player
var limit = 10
var body: some View {
LazyVStack {
if visibleWatches.isEmpty {

View File

@ -12,6 +12,10 @@ struct HomeView: View {
@State private var favoritesChanged = false
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
var watches: FetchedResults<Watch>
@State private var historyID = UUID()
var favoritesObserver: Any?
#if !os(tvOS)
@ -79,16 +83,33 @@ 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 {
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)
@ -100,6 +121,7 @@ struct HomeView: View {
.foregroundColor(.secondary)
HistoryView(limit: homeHistoryItems)
.id(historyID)
}
}

View File

@ -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 {

View File

@ -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()
)
)