2022-06-18 12:39:49 +00:00
|
|
|
import Foundation
|
|
|
|
import SDWebImageSwiftUI
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ChaptersView: View {
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var player = PlayerModel.shared
|
2023-11-21 14:25:22 +00:00
|
|
|
@Binding var expand: Bool
|
2024-05-11 18:18:56 +00:00
|
|
|
let chaptersHaveImages: Bool
|
|
|
|
let showThumbnails: Bool
|
2022-06-18 12:39:49 +00:00
|
|
|
|
2023-04-22 18:06:30 +00:00
|
|
|
var chapters: [Chapter] {
|
|
|
|
player.videoForDisplay?.chapters ?? []
|
|
|
|
}
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
var body: some View {
|
2023-11-27 11:13:51 +00:00
|
|
|
if !chapters.isEmpty {
|
2024-05-11 18:18:56 +00:00
|
|
|
if chaptersHaveImages, showThumbnails {
|
2023-11-27 11:13:51 +00:00
|
|
|
#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
|
|
|
}
|
|
|
|
}
|
2023-11-27 11:13:51 +00:00
|
|
|
.listRowBackground(Color.clear)
|
2023-04-24 10:08:58 +00:00
|
|
|
}
|
2023-11-27 11:13:51 +00:00
|
|
|
.listStyle(.plain)
|
|
|
|
#else
|
2023-11-27 13:18:43 +00:00
|
|
|
ScrollView(.horizontal) {
|
2024-05-11 21:24:17 +00:00
|
|
|
ScrollViewReader { scrollViewProxy in
|
|
|
|
LazyHStack(spacing: 20) {
|
2024-08-25 22:46:35 +00:00
|
|
|
chapterViews(for: chapters[...])
|
2024-05-11 21:24:17 +00:00
|
|
|
}
|
|
|
|
.padding(.horizontal, 15)
|
|
|
|
.onAppear {
|
2024-08-25 22:46:35 +00:00
|
|
|
scrollToCurrentChapter(scrollViewProxy)
|
2024-05-11 21:24:17 +00:00
|
|
|
}
|
2024-08-25 22:46:35 +00:00
|
|
|
.onChange(of: player.currentChapterIndex) { _ in
|
|
|
|
scrollToCurrentChapter(scrollViewProxy)
|
2024-05-11 21:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-22 18:06:30 +00:00
|
|
|
}
|
2023-11-27 11:13:51 +00:00
|
|
|
#endif
|
2023-11-27 13:18:43 +00:00
|
|
|
} else if expand {
|
2023-11-28 19:05:04 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
Section {
|
|
|
|
ForEach(chapters) { chapter in
|
|
|
|
ChapterViewTVOS(chapter: chapter)
|
|
|
|
}
|
2023-11-27 13:18:43 +00:00
|
|
|
}
|
2023-11-28 19:05:04 +00:00
|
|
|
#else
|
2024-08-25 22:46:35 +00:00
|
|
|
Section { chapterViews(for: chapters[...]) }
|
|
|
|
.padding(.horizontal)
|
2023-11-28 19:05:04 +00:00
|
|
|
#endif
|
2023-11-27 11:13:51 +00:00
|
|
|
} else {
|
|
|
|
#if os(iOS)
|
|
|
|
Button(action: {
|
|
|
|
self.expand.toggle()
|
|
|
|
}) {
|
2023-11-28 19:05:04 +00:00
|
|
|
Section {
|
2023-12-03 23:39:29 +00:00
|
|
|
chapterViews(for: chapters.prefix(3), opacity: 0.3, clickable: false)
|
2023-11-28 19:05:04 +00:00
|
|
|
}.padding(.horizontal)
|
2023-11-27 11:13:51 +00:00
|
|
|
}
|
2023-11-28 19:05:04 +00:00
|
|
|
#elseif os(macOS)
|
|
|
|
Section {
|
2023-12-03 23:39:29 +00:00
|
|
|
chapterViews(for: chapters.prefix(3), opacity: 0.3, clickable: false)
|
2023-11-28 19:05:04 +00:00
|
|
|
}.padding(.horizontal)
|
2023-11-27 11:13:51 +00:00
|
|
|
#else
|
2023-11-28 19:05:04 +00:00
|
|
|
Section {
|
|
|
|
ForEach(chapters) { chapter in
|
|
|
|
ChapterViewTVOS(chapter: chapter)
|
|
|
|
}
|
|
|
|
}
|
2023-11-27 11:13:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
2023-11-26 14:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 19:05:04 +00:00
|
|
|
#if !os(tvOS)
|
2024-08-25 22:46:35 +00:00
|
|
|
private func chapterViews(for chaptersToShow: ArraySlice<Chapter>, opacity: Double = 1.0, clickable: Bool = true) -> some View {
|
2023-11-28 19:05:04 +00:00
|
|
|
ForEach(Array(chaptersToShow.indices), id: \.self) { index in
|
|
|
|
let chapter = chaptersToShow[index]
|
2024-05-11 18:18:56 +00:00
|
|
|
ChapterView(chapter: chapter, chapterIndex: index, showThumbnail: showThumbnails)
|
2024-05-11 21:24:17 +00:00
|
|
|
.id(index)
|
2023-11-28 19:05:04 +00:00
|
|
|
.opacity(index == 0 ? 1.0 : opacity)
|
2023-12-03 23:39:29 +00:00
|
|
|
.allowsHitTesting(clickable)
|
2023-11-21 14:25:22 +00:00
|
|
|
}
|
2022-06-24 23:39:29 +00:00
|
|
|
}
|
2024-08-25 22:46:35 +00:00
|
|
|
|
|
|
|
private func scrollToCurrentChapter(_ scrollViewProxy: ScrollViewProxy) {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // Slight delay to ensure the view is fully rendered
|
|
|
|
if let currentChapterIndex = player.currentChapterIndex {
|
|
|
|
scrollViewProxy.scrollTo(currentChapterIndex, anchor: .center)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 19:05:04 +00:00
|
|
|
#endif
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 21:05:40 +00:00
|
|
|
struct ChaptersView_Previews: PreviewProvider {
|
2022-06-18 12:39:49 +00:00
|
|
|
static var previews: some View {
|
2024-05-11 18:18:56 +00:00
|
|
|
ChaptersView(expand: .constant(false), chaptersHaveImages: false, showThumbnails: true)
|
2022-06-18 12:39:49 +00:00
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
}
|