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

101 lines
3.8 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
let chaptersHaveImages: Bool
let showThumbnails: Bool
2023-04-22 18:06:30 +00:00
var chapters: [Chapter] {
player.videoForDisplay?.chapters ?? []
}
var body: some View {
if !chapters.isEmpty {
if chaptersHaveImages, showThumbnails {
#if os(tvOS)
List {
Section {
2023-04-24 10:08:58 +00:00
ForEach(chapters) { chapter in
2023-11-28 19:05:04 +00:00
ChapterViewTVOS(chapter: chapter)
2023-04-24 10:08:58 +00:00
}
}
.listRowBackground(Color.clear)
2023-04-24 10:08:58 +00:00
}
.listStyle(.plain)
#else
ScrollView(.horizontal) {
ScrollViewReader { scrollViewProxy in
LazyHStack(spacing: 20) {
chapterViews(for: chapters[...], scrollViewProxy: scrollViewProxy)
}
.padding(.horizontal, 15)
.onAppear {
if let currentChapterIndex = player.currentChapterIndex {
scrollViewProxy.scrollTo(currentChapterIndex, anchor: .center)
}
}
.onChange(of: player.currentChapterIndex) { currentChapterIndex in
if let index = currentChapterIndex {
scrollViewProxy.scrollTo(index, anchor: .center)
}
}
}
2023-04-22 18:06:30 +00:00
}
#endif
} else if expand {
2023-11-28 19:05:04 +00:00
#if os(tvOS)
Section {
ForEach(chapters) { chapter in
ChapterViewTVOS(chapter: chapter)
}
}
2023-11-28 19:05:04 +00:00
#else
Section { chapterViews(for: chapters[...]) }.padding(.horizontal)
2023-11-28 19:05:04 +00:00
#endif
} else {
#if os(iOS)
Button(action: {
self.expand.toggle()
}) {
2023-11-28 19:05:04 +00:00
Section {
chapterViews(for: chapters.prefix(3), opacity: 0.3, clickable: false)
2023-11-28 19:05:04 +00:00
}.padding(.horizontal)
}
2023-11-28 19:05:04 +00:00
#elseif os(macOS)
Section {
chapterViews(for: chapters.prefix(3), opacity: 0.3, clickable: false)
2023-11-28 19:05:04 +00:00
}.padding(.horizontal)
#else
2023-11-28 19:05:04 +00:00
Section {
ForEach(chapters) { chapter in
ChapterViewTVOS(chapter: chapter)
}
}
#endif
}
}
}
2023-11-28 19:05:04 +00:00
#if !os(tvOS)
2024-05-16 16:28:32 +00:00
private func chapterViews(for chaptersToShow: ArraySlice<Chapter>, opacity: Double = 1.0, clickable: Bool = true, scrollViewProxy _: ScrollViewProxy? = nil) -> some View {
2023-11-28 19:05:04 +00:00
ForEach(Array(chaptersToShow.indices), id: \.self) { index in
let chapter = chaptersToShow[index]
ChapterView(chapter: chapter, chapterIndex: index, showThumbnail: showThumbnails)
.id(index)
2023-11-28 19:05:04 +00:00
.opacity(index == 0 ? 1.0 : opacity)
.allowsHitTesting(clickable)
}
2022-06-24 23:39:29 +00:00
}
2023-11-28 19:05:04 +00:00
#endif
}
2022-08-20 21:05:40 +00:00
struct ChaptersView_Previews: PreviewProvider {
static var previews: some View {
ChaptersView(expand: .constant(false), chaptersHaveImages: false, showThumbnails: true)
.injectFixtureEnvironmentObjects()
}
}