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