1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 22:30:32 +05:30
yattee/Shared/Player/Video Details/ChapterView.swift

138 lines
4.4 KiB
Swift
Raw Normal View History

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
var showThumbnail: Bool
2023-11-29 00:35:04 +05:30
var isCurrentChapter: Bool {
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: {
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) {
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)
.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)
}
.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 {
thumbnailWidth / (16 / 9)
2023-11-29 00:35:04 +05:30
}
}
#else
struct ChapterViewTVOS: View {
var chapter: Chapter
var player = PlayerModel.shared
var body: some View {
Button {
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 {
thumbnailWidth / 1.7777
}
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
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
}
}