2021-08-02 04:31:24 +05:30
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
2022-11-27 16:12:16 +05:30
|
|
|
struct VerticalCells<Header: View>: View {
|
2021-08-02 04:31:24 +05:30
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.verticalSizeClass) private var verticalSizeClass
|
|
|
|
#endif
|
|
|
|
|
2022-01-05 04:48:01 +05:30
|
|
|
@Environment(\.loadMoreContentHandler) private var loadMoreContentHandler
|
2022-12-12 05:48:29 +05:30
|
|
|
@Environment(\.listingStyle) private var listingStyle
|
2022-01-05 04:48:01 +05:30
|
|
|
|
2021-10-22 04:59:10 +05:30
|
|
|
var items = [ContentItem]()
|
2023-06-08 02:02:54 +05:30
|
|
|
var allowEmpty = false
|
2023-05-21 20:47:56 +05:30
|
|
|
var edgesIgnoringSafeArea = Edge.Set.horizontal
|
2021-08-02 04:31:24 +05:30
|
|
|
|
2022-11-27 16:12:16 +05:30
|
|
|
let header: Header?
|
2023-05-25 17:58:29 +05:30
|
|
|
|
|
|
|
@State private var gridSize = CGSize.zero
|
|
|
|
|
|
|
|
init(
|
|
|
|
items: [ContentItem],
|
2023-06-08 02:02:54 +05:30
|
|
|
allowEmpty: Bool = false,
|
2023-05-25 17:58:29 +05:30
|
|
|
edgesIgnoringSafeArea: Edge.Set = .horizontal,
|
|
|
|
@ViewBuilder header: @escaping () -> Header? = { nil }
|
|
|
|
) {
|
2022-11-27 16:12:16 +05:30
|
|
|
self.items = items
|
2023-06-08 02:02:54 +05:30
|
|
|
self.allowEmpty = allowEmpty
|
2023-05-21 20:47:56 +05:30
|
|
|
self.edgesIgnoringSafeArea = edgesIgnoringSafeArea
|
2022-11-27 16:12:16 +05:30
|
|
|
self.header = header()
|
|
|
|
}
|
|
|
|
|
2023-05-25 17:58:29 +05:30
|
|
|
init(
|
|
|
|
items: [ContentItem],
|
2023-06-08 02:02:54 +05:30
|
|
|
allowEmpty: Bool = false
|
2023-05-25 17:58:29 +05:30
|
|
|
) where Header == EmptyView {
|
2023-06-08 02:02:54 +05:30
|
|
|
self.init(items: items, allowEmpty: allowEmpty) { EmptyView() }
|
2022-11-27 16:12:16 +05:30
|
|
|
}
|
|
|
|
|
2021-08-02 04:31:24 +05:30
|
|
|
var body: some View {
|
2021-09-30 22:23:26 +05:30
|
|
|
ScrollView(.vertical, showsIndicators: scrollViewShowsIndicators) {
|
2023-06-08 02:02:54 +05:30
|
|
|
LazyVGrid(columns: adaptiveItem, alignment: .center) {
|
|
|
|
Section(header: header) {
|
|
|
|
ForEach(contentItems) { item in
|
|
|
|
ContentItemView(item: item)
|
|
|
|
.onAppear { loadMoreContentItemsIfNeeded(current: item) }
|
2022-11-27 16:12:16 +05:30
|
|
|
}
|
2021-09-30 04:59:18 +05:30
|
|
|
}
|
2021-08-02 04:31:24 +05:30
|
|
|
}
|
2021-09-30 22:23:26 +05:30
|
|
|
.padding()
|
2021-08-02 04:31:24 +05:30
|
|
|
}
|
2022-08-08 22:58:02 +05:30
|
|
|
.animation(nil)
|
2023-05-21 20:47:56 +05:30
|
|
|
.edgesIgnoringSafeArea(edgesIgnoringSafeArea)
|
2021-09-27 03:58:42 +05:30
|
|
|
#if os(macOS)
|
2021-12-20 05:06:12 +05:30
|
|
|
.background(Color.secondaryBackground)
|
2024-09-09 19:35:24 +05:30
|
|
|
.frame(minWidth: Constants.contentViewMinWidth)
|
2021-09-27 03:58:42 +05:30
|
|
|
#endif
|
2021-08-02 04:31:24 +05:30
|
|
|
}
|
|
|
|
|
2022-03-27 16:19:57 +05:30
|
|
|
var contentItems: [ContentItem] {
|
2023-06-08 02:02:54 +05:30
|
|
|
items.isEmpty ? (allowEmpty ? items : ContentItem.placeholders) : items.sorted { $0 < $1 }
|
2022-03-27 16:19:57 +05:30
|
|
|
}
|
|
|
|
|
2022-01-05 04:48:01 +05:30
|
|
|
func loadMoreContentItemsIfNeeded(current item: ContentItem) {
|
|
|
|
let thresholdIndex = items.index(items.endIndex, offsetBy: -5)
|
|
|
|
if items.firstIndex(where: { $0.id == item.id }) == thresholdIndex {
|
|
|
|
loadMoreContentHandler()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 02:40:22 +05:30
|
|
|
var adaptiveItem: [GridItem] {
|
2022-12-12 05:48:29 +05:30
|
|
|
if listingStyle == .list {
|
|
|
|
return [.init(.flexible())]
|
|
|
|
}
|
|
|
|
|
|
|
|
return [GridItem(.adaptive(minimum: adaptiveGridItemMinimumSize, maximum: adaptiveGridItemMaximumSize))]
|
2021-08-02 04:31:24 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 02:06:42 +05:30
|
|
|
var adaptiveGridItemMinimumSize: Double {
|
2021-08-02 04:31:24 +05:30
|
|
|
#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-11-12 02:37:13 +05:30
|
|
|
return 600
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-12 02:37:13 +05:30
|
|
|
var adaptiveGridItemMaximumSize: Double {
|
|
|
|
#if os(tvOS)
|
|
|
|
return 600
|
|
|
|
#else
|
|
|
|
return .infinity
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-02 04:31:24 +05:30
|
|
|
var scrollViewShowsIndicators: Bool {
|
|
|
|
#if !os(tvOS)
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 22:19:45 +05:30
|
|
|
struct VeticalCells_Previews: PreviewProvider {
|
2021-08-02 04:31:24 +05:30
|
|
|
static var previews: some View {
|
2023-06-08 02:02:54 +05:30
|
|
|
VerticalCells(items: ContentItem.array(of: Array(repeating: Video.fixture, count: 30)))
|
2021-09-29 17:15:00 +05:30
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-08-02 04:31:24 +05:30
|
|
|
}
|
|
|
|
}
|