2022-08-20 21:05:40 +00:00
|
|
|
import Foundation
|
|
|
|
import SDWebImageSwiftUI
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ChapterView: View {
|
|
|
|
var chapter: Chapter
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
var player = PlayerModel.shared
|
2022-08-20 21:05:40 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Button {
|
2022-08-28 17:18:49 +00:00
|
|
|
player.backend.seek(to: chapter.start, seekType: .userInteracted)
|
2022-08-20 21:05:40 +00:00
|
|
|
} label: {
|
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-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)
|
2023-11-27 13:18:43 +00:00
|
|
|
.lineLimit(3)
|
2023-04-22 20:44:59 +00:00
|
|
|
.multilineTextAlignment(.leading)
|
|
|
|
.font(.headline)
|
|
|
|
Text(chapter.start.formattedAsPlaybackTime(allowZero: true) ?? "")
|
|
|
|
.font(.system(.subheadline).monospacedDigit())
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
2023-11-27 13:18:43 +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
|
|
|
#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 {
|
|
|
|
ChapterView(chapter: .init(title: "Chapter", start: 30))
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
}
|