2022-11-09 19:04:04 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct HistoryView: View {
|
2024-05-18 04:06:40 +05:30
|
|
|
var limit: Int
|
2022-11-15 16:52:27 +05:30
|
|
|
|
2022-11-09 19:04:04 +05:30
|
|
|
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
|
|
|
|
var watches: FetchedResults<Watch>
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
@ObservedObject private var player = PlayerModel.shared
|
2023-05-23 22:19:14 +05:30
|
|
|
@State private var visibleWatches = [Watch]()
|
|
|
|
|
2022-11-09 19:04:04 +05:30
|
|
|
var body: some View {
|
|
|
|
LazyVStack {
|
2022-11-11 23:20:13 +05:30
|
|
|
if visibleWatches.isEmpty {
|
2022-11-26 00:01:48 +05:30
|
|
|
VStack(spacing: 20) {
|
2022-11-11 23:20:13 +05:30
|
|
|
HStack {
|
|
|
|
Image(systemName: "clock")
|
|
|
|
Text("Playback history is empty")
|
|
|
|
}.foregroundColor(.secondary)
|
2022-11-09 19:04:04 +05:30
|
|
|
}
|
2022-11-11 23:20:13 +05:30
|
|
|
} else {
|
2023-05-25 17:58:29 +05:30
|
|
|
ListView(items: contentItems, limit: limit)
|
2022-11-09 19:04:04 +05:30
|
|
|
}
|
|
|
|
}
|
2022-12-23 00:06:35 +05:30
|
|
|
.animation(nil, value: visibleWatches)
|
2023-05-23 22:19:14 +05:30
|
|
|
.onChange(of: player.currentVideo) { _ in reloadVisibleWatches() }
|
2022-11-09 19:04:04 +05:30
|
|
|
}
|
|
|
|
|
2023-05-25 17:58:29 +05:30
|
|
|
var contentItems: [ContentItem] {
|
|
|
|
visibleWatches.map { .init(video: player.historyVideo($0.videoID) ?? $0.video) }
|
|
|
|
}
|
|
|
|
|
2023-05-23 22:19:14 +05:30
|
|
|
func reloadVisibleWatches() {
|
|
|
|
visibleWatches = Array(watches.filter { $0.videoID != player.currentVideo?.videoID }.prefix(limit))
|
2022-11-09 19:04:04 +05:30
|
|
|
}
|
2024-05-18 04:06:40 +05:30
|
|
|
|
|
|
|
init(limit: Int = 10) {
|
|
|
|
self.limit = limit
|
|
|
|
}
|
2022-11-09 19:04:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
struct HistoryView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2024-05-18 04:06:40 +05:30
|
|
|
HistoryView(limit: 10)
|
2022-11-09 19:04:04 +05:30
|
|
|
}
|
|
|
|
}
|