1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Shared/VideosCellsView.swift

76 lines
2.0 KiB
Swift
Raw Normal View History

2021-08-02 04:31:24 +05:30
import Defaults
import SwiftUI
struct VideosCellsView: View {
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
var videos = [Video]()
var body: some View {
ScrollViewReader { scrollView in
ScrollView(.vertical, showsIndicators: scrollViewShowsIndicators) {
LazyVGrid(columns: items, alignment: .center) {
ForEach(videos) { video in
VideoView(video: video, layout: .cells)
2021-08-03 02:40:22 +05:30
#if os(tvOS)
.padding(.horizontal)
#endif
2021-08-02 04:31:24 +05:30
}
}
.padding()
}
.onChange(of: videos) { [videos] newVideos in
guard !videos.isEmpty, let video = newVideos.first else {
return
}
scrollView.scrollTo(video.id, anchor: .top)
}
2021-08-03 02:40:22 +05:30
#if os(tvOS)
.padding(.horizontal, 10)
#endif
2021-08-02 04:31:24 +05:30
}
2021-08-03 02:40:22 +05:30
.edgesIgnoringSafeArea(.horizontal)
2021-08-02 04:31:24 +05:30
}
var items: [GridItem] {
2021-08-03 02:40:22 +05:30
#if os(tvOS)
videos.count < 3 ? Array(repeating: GridItem(.fixed(540)), count: videos.count) : adaptiveItem
#else
adaptiveItem
#endif
2021-08-02 04:31:24 +05:30
}
2021-08-03 02:40:22 +05:30
var adaptiveItem: [GridItem] {
[GridItem(.adaptive(minimum: adaptiveGridItemMinimumSize))]
2021-08-02 04:31:24 +05:30
}
var adaptiveGridItemMinimumSize: CGFloat {
#if os(iOS)
2021-08-16 19:09:31 +05:30
return verticalSizeClass == .regular ? 320 : 800
2021-08-02 04:31:24 +05:30
#elseif os(tvOS)
2021-08-03 02:40:22 +05:30
return 540
2021-08-02 04:31:24 +05:30
#else
2021-08-16 19:09:31 +05:30
return 320
2021-08-02 04:31:24 +05:30
#endif
}
var scrollViewShowsIndicators: Bool {
#if !os(tvOS)
true
#else
false
#endif
}
}
struct VideoCellsView_Previews: PreviewProvider {
static var previews: some View {
VideosView(videos: Video.allFixtures)
.frame(minWidth: 1000)
.environmentObject(NavigationState())
}
}