yattee/Shared/Player/Video Details/ChaptersView.swift

85 lines
3.0 KiB
Swift
Raw Normal View History

import Foundation
import SDWebImageSwiftUI
import SwiftUI
struct ChaptersView: View {
@ObservedObject private var player = PlayerModel.shared
@Binding var expand: Bool
2023-04-22 18:06:30 +00:00
var chapters: [Chapter] {
player.videoForDisplay?.chapters ?? []
}
2023-04-24 10:08:58 +00:00
var chaptersHaveImages: Bool {
chapters.allSatisfy { $0.image != nil }
}
var body: some View {
if !chapters.isEmpty {
if chaptersHaveImages {
#if os(tvOS)
List {
Section {
2023-04-24 10:08:58 +00:00
ForEach(chapters) { chapter in
ChapterView(chapter: chapter)
}
}
.listRowBackground(Color.clear)
2023-04-24 10:08:58 +00:00
}
.listStyle(.plain)
#else
ScrollView(.horizontal) {
LazyHStack(spacing: 20) {
2023-11-28 15:45:36 +00:00
ForEach(Array(chapters.indices), id: \.self) { index in
let chapter = chapters[index]
let nextChapterStart: Double? = index < chapters.count - 1 ? chapters[index + 1].start : nil
ChapterView(chapter: chapter, nextChapterStart: nextChapterStart, chapterIndex: index)
}
2023-04-22 18:06:30 +00:00
}
.padding(.horizontal, 15)
2023-04-22 18:06:30 +00:00
}
#endif
} else if expand {
Section {
2023-11-28 15:45:36 +00:00
ForEach(Array(chapters.indices), id: \.self) { index in
let chapter = chapters[index]
let nextChapterStart: Double? = index < chapters.count - 1 ? chapters[index + 1].start : nil
ChapterView(chapter: chapter, nextChapterStart: nextChapterStart, chapterIndex: index)
}
}
.padding(.horizontal)
} else {
#if os(iOS)
Button(action: {
self.expand.toggle()
}) {
contents
}
#else
contents
#endif
}
}
}
var contents: some View {
Section {
2023-11-28 15:45:36 +00:00
ForEach(Array(chapters.prefix(3).indices), id: \.self) { index in
let chapter = chapters[index]
let nextChapterStart: Double? = index < chapters.count - 1 ? chapters[index + 1].start : nil
ChapterView(chapter: chapter, nextChapterStart: nextChapterStart, chapterIndex: index)
.allowsHitTesting(expand)
.opacity(index == 0 ? 1.0 : 0.3)
}
2022-06-24 23:39:29 +00:00
}
.padding(.horizontal)
}
}
2022-08-20 21:05:40 +00:00
struct ChaptersView_Previews: PreviewProvider {
static var previews: some View {
ChaptersView(expand: .constant(false))
.injectFixtureEnvironmentObjects()
}
}