2022-11-09 13:34:04 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct HistoryView: View {
|
2024-05-17 22:36:40 +00:00
|
|
|
var limit: Int
|
2022-11-15 11:22:27 +00:00
|
|
|
|
2022-11-09 13:34:04 +00:00
|
|
|
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
|
|
|
|
var watches: FetchedResults<Watch>
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var player = PlayerModel.shared
|
2023-05-23 16:49:14 +00:00
|
|
|
@State private var visibleWatches = [Watch]()
|
|
|
|
|
2022-11-09 13:34:04 +00:00
|
|
|
var body: some View {
|
|
|
|
LazyVStack {
|
2022-11-11 17:50:13 +00:00
|
|
|
if visibleWatches.isEmpty {
|
2022-11-25 18:31:48 +00:00
|
|
|
VStack(spacing: 20) {
|
2022-11-11 17:50:13 +00:00
|
|
|
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 {
|
2023-05-25 12:28:29 +00:00
|
|
|
ListView(items: contentItems, limit: limit)
|
2022-11-09 13:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-22 18:36:35 +00:00
|
|
|
.animation(nil, value: visibleWatches)
|
2023-05-23 16:49:14 +00:00
|
|
|
.onChange(of: player.currentVideo) { _ in reloadVisibleWatches() }
|
2022-11-09 13:34:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 12:28:29 +00:00
|
|
|
var contentItems: [ContentItem] {
|
|
|
|
visibleWatches.map { .init(video: player.historyVideo($0.videoID) ?? $0.video) }
|
|
|
|
}
|
|
|
|
|
2023-05-23 16:49:14 +00:00
|
|
|
func reloadVisibleWatches() {
|
|
|
|
visibleWatches = Array(watches.filter { $0.videoID != player.currentVideo?.videoID }.prefix(limit))
|
2022-11-09 13:34:04 +00:00
|
|
|
}
|
2024-05-17 22:36:40 +00:00
|
|
|
|
|
|
|
init(limit: Int = 10) {
|
|
|
|
self.limit = limit
|
|
|
|
}
|
2022-11-09 13:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct HistoryView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2024-05-17 22:36:40 +00:00
|
|
|
HistoryView(limit: 10)
|
2022-11-09 13:34:04 +00:00
|
|
|
}
|
|
|
|
}
|