2021-08-01 23:01:24 +00:00
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
2022-11-27 10:42:16 +00:00
|
|
|
struct VerticalCells<Header: View>: View {
|
2021-08-01 23:01:24 +00:00
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.verticalSizeClass) private var verticalSizeClass
|
|
|
|
#endif
|
|
|
|
|
2022-02-04 17:38:29 +00:00
|
|
|
@Environment(\.scrollViewBottomPadding) private var scrollViewBottomPadding
|
2022-01-04 23:18:01 +00:00
|
|
|
@Environment(\.loadMoreContentHandler) private var loadMoreContentHandler
|
2022-12-12 00:18:29 +00:00
|
|
|
@Environment(\.listingStyle) private var listingStyle
|
2022-01-04 23:18:01 +00:00
|
|
|
|
2021-10-21 23:29:10 +00:00
|
|
|
var items = [ContentItem]()
|
2022-03-27 10:49:57 +00:00
|
|
|
var allowEmpty = false
|
2021-08-01 23:01:24 +00:00
|
|
|
|
2022-11-27 10:42:16 +00:00
|
|
|
let header: Header?
|
|
|
|
init(items: [ContentItem], allowEmpty: Bool = false, @ViewBuilder header: @escaping () -> Header? = { nil }) {
|
|
|
|
self.items = items
|
|
|
|
self.allowEmpty = allowEmpty
|
|
|
|
self.header = header()
|
|
|
|
}
|
|
|
|
|
|
|
|
init(items: [ContentItem], allowEmpty: Bool = false) where Header == EmptyView {
|
|
|
|
self.init(items: items, allowEmpty: allowEmpty) { EmptyView() }
|
|
|
|
}
|
|
|
|
|
2021-08-01 23:01:24 +00:00
|
|
|
var body: some View {
|
2021-09-30 16:53:26 +00:00
|
|
|
ScrollView(.vertical, showsIndicators: scrollViewShowsIndicators) {
|
2021-10-21 23:29:10 +00:00
|
|
|
LazyVGrid(columns: columns, alignment: .center) {
|
2022-11-27 10:42:16 +00:00
|
|
|
Section(header: header) {
|
|
|
|
ForEach(contentItems) { item in
|
|
|
|
ContentItemView(item: item)
|
|
|
|
.onAppear { loadMoreContentItemsIfNeeded(current: item) }
|
|
|
|
}
|
2021-09-29 23:29:18 +00:00
|
|
|
}
|
2021-08-01 23:01:24 +00:00
|
|
|
}
|
2021-09-30 16:53:26 +00:00
|
|
|
.padding()
|
2022-02-04 17:38:29 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
Color.clear.padding(.bottom, scrollViewBottomPadding)
|
|
|
|
#endif
|
2021-08-01 23:01:24 +00:00
|
|
|
}
|
2022-08-08 17:28:02 +00:00
|
|
|
.animation(nil)
|
2021-08-02 21:10:22 +00:00
|
|
|
.edgesIgnoringSafeArea(.horizontal)
|
2021-09-26 22:28:42 +00:00
|
|
|
#if os(macOS)
|
2021-12-19 23:36:12 +00:00
|
|
|
.background(Color.secondaryBackground)
|
2021-09-26 22:28:42 +00:00
|
|
|
.frame(minWidth: 360)
|
|
|
|
#endif
|
2021-08-01 23:01:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 10:49:57 +00:00
|
|
|
var contentItems: [ContentItem] {
|
|
|
|
items.isEmpty ? (allowEmpty ? items : placeholders) : items.sorted { $0 < $1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
var placeholders: [ContentItem] {
|
|
|
|
(0 ..< 9).map { _ in .init() }
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:18:01 +00:00
|
|
|
func loadMoreContentItemsIfNeeded(current item: ContentItem) {
|
|
|
|
let thresholdIndex = items.index(items.endIndex, offsetBy: -5)
|
|
|
|
if items.firstIndex(where: { $0.id == item.id }) == thresholdIndex {
|
|
|
|
loadMoreContentHandler()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 23:29:10 +00:00
|
|
|
var columns: [GridItem] {
|
2021-08-02 21:10:22 +00:00
|
|
|
#if os(tvOS)
|
2022-03-27 18:31:56 +00:00
|
|
|
contentItems.count < 3 ? Array(repeating: GridItem(.fixed(500)), count: [contentItems.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] {
|
2022-12-12 00:18:29 +00:00
|
|
|
if listingStyle == .list {
|
|
|
|
return [.init(.flexible())]
|
|
|
|
}
|
|
|
|
|
|
|
|
return [GridItem(.adaptive(minimum: adaptiveGridItemMinimumSize, maximum: adaptiveGridItemMaximumSize))]
|
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-11-11 21:07:13 +00:00
|
|
|
return 600
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-11 21:07:13 +00:00
|
|
|
var adaptiveGridItemMaximumSize: Double {
|
|
|
|
#if os(tvOS)
|
|
|
|
return 600
|
|
|
|
#else
|
|
|
|
return .infinity
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-01 23:01:24 +00:00
|
|
|
var scrollViewShowsIndicators: Bool {
|
|
|
|
#if !os(tvOS)
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 16:49:45 +00:00
|
|
|
struct VeticalCells_Previews: PreviewProvider {
|
2021-08-01 23:01:24 +00:00
|
|
|
static var previews: some View {
|
2021-11-11 21:07:13 +00:00
|
|
|
VerticalCells(items: ContentItem.array(of: Array(repeating: Video.fixture, count: 30)))
|
2021-09-29 11:45:00 +00:00
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-08-01 23:01:24 +00:00
|
|
|
}
|
|
|
|
}
|