yattee/Shared/Player/Video Details/ChapterView.swift

115 lines
3.6 KiB
Swift
Raw Normal View History

2023-11-28 15:45:36 +00:00
import CoreMedia
2022-08-20 21:05:40 +00:00
import Foundation
import SDWebImageSwiftUI
import SwiftUI
struct ChapterView: View {
var chapter: Chapter
2023-11-28 15:45:36 +00:00
var nextChapterStart: Double?
2022-08-20 21:05:40 +00:00
2023-11-28 15:45:36 +00:00
var chapterIndex: Int
@ObservedObject private var player = PlayerModel.shared
var isCurrentChapter: Bool {
player.currentChapterIndex == chapterIndex
}
var hasBeenPlayed: Bool {
player.playedChapters.contains(chapterIndex)
}
2022-08-20 21:05:40 +00:00
var body: some View {
2023-11-28 15:45:36 +00:00
Button(action: {
player.backend.seek(to: chapter.start, seekType: .userInteracted)
2023-11-28 15:45:36 +00:00
}) {
2023-04-22 18:06:30 +00:00
Group {
#if os(tvOS)
horizontalChapter
#else
verticalChapter
#endif
2022-08-20 21:05:40 +00:00
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
2023-11-28 15:45:36 +00:00
.onReceive(PlayerTimeModel.shared.$currentTime) { cmTime in
let time = CMTimeGetSeconds(cmTime)
if time >= self.chapter.start, self.nextChapterStart == nil || time < self.nextChapterStart! {
player.currentChapterIndex = self.chapterIndex
if !player.playedChapters.contains(self.chapterIndex) {
player.playedChapters.append(self.chapterIndex)
}
}
}
2022-08-20 21:05:40 +00:00
}
2023-04-22 20:44:59 +00:00
#if os(tvOS)
2023-04-22 18:06:30 +00:00
2023-04-22 20:44:59 +00:00
var horizontalChapter: some View {
HStack(spacing: 12) {
if !chapter.image.isNil {
smallImage(chapter)
}
2023-04-22 18:06:30 +00:00
2023-04-22 20:44:59 +00:00
VStack(alignment: .leading, spacing: 4) {
Text(chapter.title)
.font(.headline)
Text(chapter.start.formattedAsPlaybackTime(allowZero: true) ?? "")
.font(.system(.subheadline).monospacedDigit())
.foregroundColor(.secondary)
}
2023-04-22 18:06:30 +00:00
}
2023-04-22 20:44:59 +00:00
.frame(maxWidth: .infinity, alignment: .leading)
}
#else
var verticalChapter: some View {
VStack(spacing: 12) {
if !chapter.image.isNil {
smallImage(chapter)
}
VStack(alignment: .leading, spacing: 4) {
Text(chapter.title)
.lineLimit(3)
2023-04-22 20:44:59 +00:00
.multilineTextAlignment(.leading)
.font(.headline)
2023-11-28 15:45:36 +00:00
.foregroundColor(isCurrentChapter ? .detailBadgeOutstandingStyleBackground : .primary)
2023-04-22 20:44:59 +00:00
Text(chapter.start.formattedAsPlaybackTime(allowZero: true) ?? "")
.font(.system(.subheadline).monospacedDigit())
.foregroundColor(.secondary)
}
.frame(maxWidth: !chapter.image.isNil ? Self.thumbnailWidth : nil, alignment: .leading)
2023-04-22 18:06:30 +00:00
}
}
2023-04-22 20:44:59 +00:00
#endif
2023-04-22 18:06:30 +00:00
2022-08-20 21:05:40 +00:00
@ViewBuilder func smallImage(_ chapter: Chapter) -> some View {
2022-11-10 18:11:19 +00:00
WebImage(url: chapter.image, options: [.lowPriority])
2022-09-11 19:33:08 +00:00
.resizable()
.placeholder {
ProgressView()
2022-08-20 21:05:40 +00:00
}
2022-09-11 19:33:08 +00:00
.indicator(.activity)
2023-04-22 18:06:30 +00:00
.frame(width: Self.thumbnailWidth, height: Self.thumbnailHeight)
2022-09-11 19:33:08 +00:00
#if os(tvOS)
.mask(RoundedRectangle(cornerRadius: 12))
#else
.mask(RoundedRectangle(cornerRadius: 6))
#endif
2022-08-20 21:05:40 +00:00
}
2023-04-22 18:06:30 +00:00
static var thumbnailWidth: Double {
250
}
static var thumbnailHeight: Double {
thumbnailWidth / 1.7777
2022-08-20 21:05:40 +00:00
}
}
struct ChapterView_Preview: PreviewProvider {
static var previews: some View {
2023-11-28 15:45:36 +00:00
ChapterView(chapter: .init(title: "Chapter", start: 30), chapterIndex: 0)
2022-08-20 21:05:40 +00:00
.injectFixtureEnvironmentObjects()
}
}