yattee/Shared/Home/HistoryView.swift

57 lines
1.7 KiB
Swift
Raw Normal View History

2022-11-09 13:34:04 +00:00
import SwiftUI
struct HistoryView: View {
2022-11-14 17:46:50 +00:00
static let detailsPreloadLimit = 50
2022-11-15 11:22:27 +00:00
var limit = 10
2022-11-09 13:34:04 +00:00
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
var watches: FetchedResults<Watch>
@ObservedObject private var player = PlayerModel.shared
2022-11-09 13:34:04 +00:00
var body: some View {
LazyVStack {
2022-11-11 17:50:13 +00:00
if visibleWatches.isEmpty {
VStack(alignment: .center, spacing: 20) {
HStack {
Image(systemName: "clock")
Text("Playback history is empty")
}.foregroundColor(.secondary)
2022-11-09 13:34:04 +00:00
}
2022-11-11 17:50:13 +00:00
} else {
ForEach(visibleWatches, id: \.videoID) { watch in
PlayerQueueRow(
item: PlayerQueueItem.from(watch, video: player.historyVideo(watch.videoID)),
history: true
)
.contextMenu {
VideoContextMenuView(video: player.historyVideo(watch.videoID) ?? watch.video)
}
2022-11-09 13:34:04 +00:00
}
}
}
2022-11-14 17:46:50 +00:00
.onAppear {
visibleWatches
.prefix(Self.detailsPreloadLimit)
.map(\.videoID)
.forEach(player.loadHistoryVideoDetails)
}
2022-11-11 17:50:13 +00:00
#if os(tvOS)
.padding(.horizontal, 40)
#else
.padding(.horizontal, 15)
#endif
2022-11-09 13:34:04 +00:00
}
private var visibleWatches: [Watch] {
Array(watches.filter { $0.videoID != player.currentVideo?.videoID }.prefix(limit))
}
}
struct HistoryView_Previews: PreviewProvider {
static var previews: some View {
HistoryView()
}
}