yattee/Shared/Player/Video Details/ChaptersView.swift
2023-11-28 20:05:04 +01:00

87 lines
3.0 KiB
Swift

import Foundation
import SDWebImageSwiftUI
import SwiftUI
struct ChaptersView: View {
@ObservedObject private var player = PlayerModel.shared
@Binding var expand: Bool
var chapters: [Chapter] {
player.videoForDisplay?.chapters ?? []
}
var chaptersHaveImages: Bool {
chapters.allSatisfy { $0.image != nil }
}
var body: some View {
if !chapters.isEmpty {
if chaptersHaveImages {
#if os(tvOS)
List {
Section {
ForEach(chapters) { chapter in
ChapterViewTVOS(chapter: chapter)
}
}
.listRowBackground(Color.clear)
}
.listStyle(.plain)
#else
ScrollView(.horizontal) {
LazyHStack(spacing: 20) { chapterViews(for: chapters.prefix(3), opacity: 1.0) }.padding(.horizontal, 15)
}
#endif
} else if expand {
#if os(tvOS)
Section {
ForEach(chapters) { chapter in
ChapterViewTVOS(chapter: chapter)
}
}
#else
Section { chapterViews(for: chapters[...], opacity: 1.0) }.padding(.horizontal)
#endif
} else {
#if os(iOS)
Button(action: {
self.expand.toggle()
}) {
Section {
chapterViews(for: chapters.prefix(3), opacity: 0.3)
}.padding(.horizontal)
}
#elseif os(macOS)
Section {
chapterViews(for: chapters.prefix(3), opacity: 0.3)
}.padding(.horizontal)
#else
Section {
ForEach(chapters) { chapter in
ChapterViewTVOS(chapter: chapter)
}
}
#endif
}
}
}
#if !os(tvOS)
private func chapterViews(for chaptersToShow: ArraySlice<Chapter>, opacity: Double = 1.0) -> some View {
ForEach(Array(chaptersToShow.indices), id: \.self) { index in
let chapter = chaptersToShow[index]
let nextChapterStart: Double? = index < chaptersToShow.count - 1 ? chaptersToShow[index + 1].start : nil
ChapterView(chapter: chapter, nextChapterStart: nextChapterStart, chapterIndex: index)
.opacity(index == 0 ? 1.0 : opacity)
}
}
#endif
}
struct ChaptersView_Previews: PreviewProvider {
static var previews: some View {
ChaptersView(expand: .constant(false))
.injectFixtureEnvironmentObjects()
}
}