2022-08-21 02:35:40 +05:30
|
|
|
import Foundation
|
|
|
|
import SDWebImageSwiftUI
|
|
|
|
import SwiftUI
|
|
|
|
|
2023-11-29 00:35:04 +05:30
|
|
|
#if !os(tvOS)
|
|
|
|
struct ChapterView: View {
|
|
|
|
var chapter: Chapter
|
2022-08-21 02:35:40 +05:30
|
|
|
|
2023-11-29 00:35:04 +05:30
|
|
|
var chapterIndex: Int
|
|
|
|
@ObservedObject private var player = PlayerModel.shared
|
2023-11-28 21:15:36 +05:30
|
|
|
|
2024-05-11 23:48:56 +05:30
|
|
|
var showThumbnail: Bool
|
|
|
|
|
2023-11-29 00:35:04 +05:30
|
|
|
var isCurrentChapter: Bool {
|
2024-05-12 02:54:17 +05:30
|
|
|
if let currentChapterIndex = player.currentChapterIndex {
|
|
|
|
return currentChapterIndex == chapterIndex
|
|
|
|
}
|
|
|
|
return false
|
2023-11-29 00:35:04 +05:30
|
|
|
}
|
2022-08-21 02:35:40 +05:30
|
|
|
|
2023-11-29 00:35:04 +05:30
|
|
|
var body: some View {
|
|
|
|
Button(action: {
|
2024-05-03 18:50:51 +05:30
|
|
|
player.backend.seek(to: chapter.start, seekType: .chapterSkip(chapter.title))
|
2023-11-29 00:35:04 +05:30
|
|
|
}) {
|
|
|
|
Group {
|
2023-04-22 23:36:30 +05:30
|
|
|
verticalChapter
|
2023-11-29 00:35:04 +05:30
|
|
|
}
|
|
|
|
.contentShape(Rectangle())
|
2022-08-21 02:35:40 +05:30
|
|
|
}
|
2023-11-29 00:35:04 +05:30
|
|
|
.buttonStyle(.plain)
|
2023-11-28 21:15:36 +05:30
|
|
|
}
|
2023-04-22 23:36:30 +05:30
|
|
|
|
2023-11-29 00:35:04 +05:30
|
|
|
var verticalChapter: some View {
|
|
|
|
VStack(spacing: 12) {
|
2024-05-11 23:48:56 +05:30
|
|
|
if !chapter.image.isNil, showThumbnail {
|
2023-04-23 02:14:59 +05:30
|
|
|
smallImage(chapter)
|
|
|
|
}
|
|
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
|
|
Text(chapter.title)
|
2023-11-29 00:35:04 +05:30
|
|
|
.lineLimit(3)
|
|
|
|
.multilineTextAlignment(.leading)
|
2023-04-23 02:14:59 +05:30
|
|
|
.font(.headline)
|
2023-11-29 05:01:53 +05:30
|
|
|
.foregroundColor(isCurrentChapter ? Color("AppRedColor") : .primary)
|
2023-04-23 02:14:59 +05:30
|
|
|
Text(chapter.start.formattedAsPlaybackTime(allowZero: true) ?? "")
|
|
|
|
.font(.system(.subheadline).monospacedDigit())
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
2024-05-11 23:48:56 +05:30
|
|
|
.frame(maxWidth: !chapter.image.isNil && showThumbnail ? Self.thumbnailWidth : nil, alignment: .leading)
|
2023-04-22 23:36:30 +05:30
|
|
|
}
|
2023-04-23 02:14:59 +05:30
|
|
|
}
|
2023-11-29 00:35:04 +05:30
|
|
|
|
|
|
|
@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 {
|
2024-08-19 20:05:29 +05:30
|
|
|
thumbnailWidth / Constants.aspectRatio16x9
|
2023-11-29 00:35:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
struct ChapterViewTVOS: View {
|
|
|
|
var chapter: Chapter
|
|
|
|
var player = PlayerModel.shared
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Button {
|
2024-05-03 18:50:51 +05:30
|
|
|
player.backend.seek(to: chapter.start, seekType: .chapterSkip(chapter.title))
|
2023-11-29 00:35:04 +05:30
|
|
|
} label: {
|
|
|
|
Group {
|
|
|
|
horizontalChapter
|
|
|
|
}
|
|
|
|
.contentShape(Rectangle())
|
|
|
|
}
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
}
|
|
|
|
|
|
|
|
var horizontalChapter: some View {
|
|
|
|
HStack(spacing: 12) {
|
2023-04-23 02:14:59 +05:30
|
|
|
if !chapter.image.isNil {
|
|
|
|
smallImage(chapter)
|
|
|
|
}
|
2023-11-29 00:35:04 +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-11-29 00:35:04 +05:30
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
2023-04-22 23:36:30 +05:30
|
|
|
}
|
|
|
|
|
2023-11-29 00:35:04 +05:30
|
|
|
@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-21 02:35:40 +05:30
|
|
|
|
2023-11-29 00:35:04 +05:30
|
|
|
static var thumbnailWidth: Double {
|
|
|
|
250
|
|
|
|
}
|
2023-04-22 23:36:30 +05:30
|
|
|
|
2023-11-29 00:35:04 +05:30
|
|
|
static var thumbnailHeight: Double {
|
2024-08-19 20:05:29 +05:30
|
|
|
thumbnailWidth / Constants.aspectRatio16x9
|
2023-11-29 00:35:04 +05:30
|
|
|
}
|
2022-08-21 02:35:40 +05:30
|
|
|
}
|
2023-11-29 00:35:04 +05:30
|
|
|
#endif
|
2022-08-21 02:35:40 +05:30
|
|
|
|
|
|
|
struct ChapterView_Preview: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2023-11-29 00:35:04 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
ChapterViewTVOS(chapter: .init(title: "Chapter", start: 30))
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
#else
|
2024-05-11 23:48:56 +05:30
|
|
|
ChapterView(chapter: .init(title: "Chapter", start: 30), chapterIndex: 0, showThumbnail: true)
|
2023-11-29 00:35:04 +05:30
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
#endif
|
2022-08-21 02:35:40 +05:30
|
|
|
}
|
|
|
|
}
|