1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Shared/Player/Video Details/ChaptersView.swift

87 lines
2.9 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 23:36:30 +05:30
var chapters: [Chapter] {
player.videoForDisplay?.chapters ?? []
}
2023-04-24 15:38:58 +05:30
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 15:38:58 +05:30
ForEach(chapters) { chapter in
2023-11-29 00:35:04 +05:30
ChapterViewTVOS(chapter: chapter)
2023-04-24 15:38:58 +05:30
}
}
.listRowBackground(Color.clear)
2023-04-24 15:38:58 +05:30
}
.listStyle(.plain)
#else
ScrollView(.horizontal) {
LazyHStack(spacing: 20) { chapterViews(for: chapters[...]) }.padding(.horizontal, 15)
2023-04-22 23:36:30 +05:30
}
#endif
} else if expand {
2023-11-29 00:35:04 +05:30
#if os(tvOS)
Section {
ForEach(chapters) { chapter in
ChapterViewTVOS(chapter: chapter)
}
}
2023-11-29 00:35:04 +05:30
#else
Section { chapterViews(for: chapters[...]) }.padding(.horizontal)
2023-11-29 00:35:04 +05:30
#endif
} else {
#if os(iOS)
Button(action: {
self.expand.toggle()
}) {
2023-11-29 00:35:04 +05:30
Section {
chapterViews(for: chapters.prefix(3), opacity: 0.3, clickable: false)
2023-11-29 00:35:04 +05:30
}.padding(.horizontal)
}
2023-11-29 00:35:04 +05:30
#elseif os(macOS)
Section {
chapterViews(for: chapters.prefix(3), opacity: 0.3, clickable: false)
2023-11-29 00:35:04 +05:30
}.padding(.horizontal)
#else
2023-11-29 00:35:04 +05:30
Section {
ForEach(chapters) { chapter in
ChapterViewTVOS(chapter: chapter)
}
}
#endif
}
}
}
2023-11-29 00:35:04 +05:30
#if !os(tvOS)
private func chapterViews(for chaptersToShow: ArraySlice<Chapter>, opacity: Double = 1.0, clickable: Bool = true) -> some View {
2023-11-29 00:35:04 +05:30
ForEach(Array(chaptersToShow.indices), id: \.self) { index in
let chapter = chaptersToShow[index]
ChapterView(chapter: chapter, chapterIndex: index)
2023-11-29 00:35:04 +05:30
.opacity(index == 0 ? 1.0 : opacity)
.allowsHitTesting(clickable)
}
2022-06-25 05:09:29 +05:30
}
2023-11-29 00:35:04 +05:30
#endif
}
2022-08-21 02:35:40 +05:30
struct ChaptersView_Previews: PreviewProvider {
static var previews: some View {
ChaptersView(expand: .constant(false))
.injectFixtureEnvironmentObjects()
}
}