yattee/Shared/Videos/VideosCellsVertical.swift

82 lines
2.2 KiB
Swift
Raw Normal View History

2021-08-01 23:01:24 +00:00
import Defaults
import SwiftUI
2021-09-18 20:36:42 +00:00
struct VideosCellsVertical: View {
2021-08-01 23:01:24 +00:00
#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
2021-09-26 22:28:42 +00:00
VideoView(video: video)
2021-08-02 21:10:22 +00:00
#if os(tvOS)
.padding(.horizontal)
#endif
2021-08-01 23:01:24 +00:00
}
}
.padding()
}
.onChange(of: videos) { [videos] newVideos in
#if !os(tvOS)
guard !videos.isEmpty, let video = newVideos.first else {
return
}
2021-08-01 23:01:24 +00:00
scrollView.scrollTo(video.id, anchor: .top)
#endif
2021-08-01 23:01:24 +00:00
}
2021-08-02 21:10:22 +00:00
#if os(tvOS)
.padding(.horizontal, 10)
#endif
2021-08-01 23:01:24 +00:00
}
2021-08-02 21:10:22 +00:00
.edgesIgnoringSafeArea(.horizontal)
2021-09-26 22:28:42 +00:00
#if os(macOS)
.background()
.frame(minWidth: 360)
#endif
2021-08-01 23:01:24 +00:00
}
var items: [GridItem] {
2021-08-02 21:10:22 +00:00
#if os(tvOS)
videos.count < 3 ? Array(repeating: GridItem(.fixed(540)), count: [videos.count, 1].max()!) : adaptiveItem
2021-08-02 21:10:22 +00:00
#else
adaptiveItem
#endif
2021-08-01 23:01:24 +00:00
}
2021-08-02 21:10:22 +00:00
var adaptiveItem: [GridItem] {
[GridItem(.adaptive(minimum: adaptiveGridItemMinimumSize))]
2021-08-01 23:01:24 +00:00
}
2021-09-18 20:36:42 +00:00
var adaptiveGridItemMinimumSize: Double {
2021-08-01 23:01:24 +00:00
#if os(iOS)
2021-08-16 13:39:31 +00:00
return verticalSizeClass == .regular ? 320 : 800
2021-08-01 23:01:24 +00:00
#elseif os(tvOS)
2021-08-02 21:10:22 +00:00
return 540
2021-08-01 23:01:24 +00:00
#else
2021-08-16 13:39:31 +00:00
return 320
2021-08-01 23:01:24 +00:00
#endif
}
var scrollViewShowsIndicators: Bool {
#if !os(tvOS)
true
#else
false
#endif
}
}
2021-09-26 22:28:42 +00:00
struct VideoCellsVertical_Previews: PreviewProvider {
2021-08-01 23:01:24 +00:00
static var previews: some View {
2021-09-26 22:28:42 +00:00
VideosCellsVertical(videos: Video.allFixtures)
2021-08-01 23:01:24 +00:00
.frame(minWidth: 1000)
2021-09-25 08:18:22 +00:00
.environmentObject(NavigationModel())
2021-08-01 23:01:24 +00:00
}
}