2023-11-28 15:45:36 +00:00
|
|
|
import CoreMedia
|
2022-08-20 21:05:40 +00:00
|
|
|
import Foundation
|
|
|
|
import SDWebImageSwiftUI
|
|
|
|
import SwiftUI
|
|
|
|
|
2023-11-28 19:05:04 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
struct ChapterView: View {
|
|
|
|
var chapter: Chapter
|
|
|
|
var nextChapterStart: Double?
|
2022-08-20 21:05:40 +00:00
|
|
|
|
2023-11-28 19:05:04 +00:00
|
|
|
var chapterIndex: Int
|
|
|
|
@ObservedObject private var player = PlayerModel.shared
|
2023-11-28 15:45:36 +00:00
|
|
|
|
2023-11-28 19:05:04 +00:00
|
|
|
var isCurrentChapter: Bool {
|
2023-12-03 19:04:57 +00:00
|
|
|
player.currentChapter == chapterIndex
|
2023-11-28 19:05:04 +00:00
|
|
|
}
|
2022-08-20 21:05:40 +00:00
|
|
|
|
2023-11-28 19:05:04 +00:00
|
|
|
var body: some View {
|
|
|
|
Button(action: {
|
|
|
|
player.backend.seek(to: chapter.start, seekType: .userInteracted)
|
2023-11-28 23:31:53 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // Introducing a delay to give the player a chance to skip to the chapter
|
|
|
|
PlayerTimeModel.shared.currentTime = CMTime(seconds: chapter.start, preferredTimescale: 1)
|
|
|
|
handleTimeUpdate(PlayerTimeModel.shared.currentTime)
|
|
|
|
}
|
2023-11-28 19:05:04 +00:00
|
|
|
}) {
|
|
|
|
Group {
|
2023-04-22 18:06:30 +00:00
|
|
|
verticalChapter
|
2023-11-28 19:05:04 +00:00
|
|
|
}
|
|
|
|
.contentShape(Rectangle())
|
2022-08-20 21:05:40 +00:00
|
|
|
}
|
2023-11-28 19:05:04 +00:00
|
|
|
.buttonStyle(.plain)
|
|
|
|
.onReceive(PlayerTimeModel.shared.$currentTime) { cmTime in
|
2023-11-28 23:31:53 +00:00
|
|
|
self.handleTimeUpdate(cmTime)
|
2023-12-03 19:04:57 +00:00
|
|
|
print("currentChapterIndex:", player.currentChapter ?? 0)
|
2023-11-28 15:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-22 18:06:30 +00:00
|
|
|
|
2023-11-28 19:05:04 +00:00
|
|
|
var verticalChapter: some View {
|
|
|
|
VStack(spacing: 12) {
|
2023-04-22 20:44:59 +00:00
|
|
|
if !chapter.image.isNil {
|
|
|
|
smallImage(chapter)
|
|
|
|
}
|
|
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
|
|
Text(chapter.title)
|
2023-11-28 19:05:04 +00:00
|
|
|
.lineLimit(3)
|
|
|
|
.multilineTextAlignment(.leading)
|
2023-04-22 20:44:59 +00:00
|
|
|
.font(.headline)
|
2023-11-28 23:31:53 +00:00
|
|
|
.foregroundColor(isCurrentChapter ? Color("AppRedColor") : .primary)
|
2023-04-22 20:44:59 +00:00
|
|
|
Text(chapter.start.formattedAsPlaybackTime(allowZero: true) ?? "")
|
|
|
|
.font(.system(.subheadline).monospacedDigit())
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
2023-11-28 19:05:04 +00:00
|
|
|
.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
|
|
|
}
|
2023-11-28 19:05:04 +00:00
|
|
|
|
|
|
|
@ViewBuilder func smallImage(_ chapter: Chapter) -> some View {
|
|
|
|
WebImage(url: chapter.image, options: [.lowPriority])
|
|
|
|
.resizable()
|
|
|
|
.placeholder {
|
|
|
|
ProgressView()
|
|
|
|
}
|
|
|
|
.indicator(.activity)
|
|
|
|
.frame(width: Self.thumbnailWidth, height: Self.thumbnailHeight)
|
|
|
|
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 6))
|
|
|
|
}
|
|
|
|
|
|
|
|
static var thumbnailWidth: Double {
|
|
|
|
250
|
|
|
|
}
|
|
|
|
|
|
|
|
static var thumbnailHeight: Double {
|
|
|
|
thumbnailWidth / 1.7777
|
|
|
|
}
|
2023-11-28 23:31:53 +00:00
|
|
|
|
|
|
|
private func handleTimeUpdate(_ cmTime: CMTime) {
|
|
|
|
let time = CMTimeGetSeconds(cmTime)
|
|
|
|
if time >= chapter.start, nextChapterStart == nil || time < nextChapterStart! {
|
2023-12-03 19:04:57 +00:00
|
|
|
player.currentChapter = chapterIndex
|
2023-11-28 23:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-28 19:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
struct ChapterViewTVOS: View {
|
|
|
|
var chapter: Chapter
|
|
|
|
var player = PlayerModel.shared
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Button {
|
|
|
|
player.backend.seek(to: chapter.start, seekType: .userInteracted)
|
|
|
|
} label: {
|
|
|
|
Group {
|
|
|
|
horizontalChapter
|
|
|
|
}
|
|
|
|
.contentShape(Rectangle())
|
|
|
|
}
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
}
|
|
|
|
|
|
|
|
var horizontalChapter: some View {
|
|
|
|
HStack(spacing: 12) {
|
2023-04-22 20:44:59 +00:00
|
|
|
if !chapter.image.isNil {
|
|
|
|
smallImage(chapter)
|
|
|
|
}
|
2023-11-28 19:05:04 +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-11-28 19:05:04 +00:00
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
2023-04-22 18:06:30 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 19:05:04 +00:00
|
|
|
@ViewBuilder func smallImage(_ chapter: Chapter) -> some View {
|
|
|
|
WebImage(url: chapter.image, options: [.lowPriority])
|
|
|
|
.resizable()
|
|
|
|
.placeholder {
|
|
|
|
ProgressView()
|
|
|
|
}
|
|
|
|
.indicator(.activity)
|
|
|
|
.frame(width: Self.thumbnailWidth, height: Self.thumbnailHeight)
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 12))
|
|
|
|
}
|
2022-08-20 21:05:40 +00:00
|
|
|
|
2023-11-28 19:05:04 +00:00
|
|
|
static var thumbnailWidth: Double {
|
|
|
|
250
|
|
|
|
}
|
2023-04-22 18:06:30 +00:00
|
|
|
|
2023-11-28 19:05:04 +00:00
|
|
|
static var thumbnailHeight: Double {
|
|
|
|
thumbnailWidth / 1.7777
|
|
|
|
}
|
2022-08-20 21:05:40 +00:00
|
|
|
}
|
2023-11-28 19:05:04 +00:00
|
|
|
#endif
|
2022-08-20 21:05:40 +00:00
|
|
|
|
|
|
|
struct ChapterView_Preview: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2023-11-28 19:05:04 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
ChapterViewTVOS(chapter: .init(title: "Chapter", start: 30))
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
#else
|
|
|
|
ChapterView(chapter: .init(title: "Chapter", start: 30), nextChapterStart: nil, chapterIndex: 0)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
#endif
|
2022-08-20 21:05:40 +00:00
|
|
|
}
|
|
|
|
}
|