yattee/Shared/Videos/HorizontalCells.swift

65 lines
1.9 KiB
Swift
Raw Normal View History

2021-09-18 20:36:42 +00:00
import Defaults
import SwiftUI
struct HorizontalCells: View {
var items = [ContentItem]()
2021-09-18 20:36:42 +00:00
@Environment(\.loadMoreContentHandler) private var loadMoreContentHandler
2021-11-07 22:27:09 +00:00
@Default(.channelOnThumbnail) private var channelOnThumbnail
2021-09-18 20:36:42 +00:00
var body: some View {
2021-09-30 16:53:26 +00:00
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 20) {
2022-03-27 10:49:57 +00:00
ForEach(contentItems) { item in
ContentItemView(item: item)
2021-09-30 16:53:26 +00:00
.environment(\.horizontalCells, true)
.onAppear { loadMoreContentItemsIfNeeded(current: item) }
2021-09-30 16:53:26 +00:00
#if os(tvOS)
.frame(width: 580)
.padding(.trailing, 20)
.padding(.bottom, 40)
#else
2021-11-05 20:53:43 +00:00
.frame(width: 295)
2021-09-30 16:53:26 +00:00
#endif
2021-09-29 23:29:18 +00:00
}
2021-09-18 20:36:42 +00:00
}
2021-09-30 16:53:26 +00:00
#if os(tvOS)
2021-11-08 16:29:35 +00:00
.padding(.horizontal, 40)
.padding(.vertical, 30)
2021-09-30 16:53:26 +00:00
#else
2021-11-08 16:29:35 +00:00
.padding(.horizontal, 15)
.padding(.vertical, 10)
2021-09-30 16:53:26 +00:00
#endif
2021-09-18 20:36:42 +00:00
}
2021-11-07 22:27:09 +00:00
.frame(height: cellHeight)
.edgesIgnoringSafeArea(.horizontal)
}
2022-03-27 10:49:57 +00:00
var contentItems: [ContentItem] {
2022-12-14 16:20:24 +00:00
items.isEmpty ? ContentItem.placeholders : items
2022-03-27 10:49:57 +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-11-07 22:27:09 +00:00
var cellHeight: Double {
2021-09-18 20:36:42 +00:00
#if os(tvOS)
2022-12-15 22:59:35 +00:00
600
2021-09-18 20:36:42 +00:00
#else
2021-11-07 22:27:09 +00:00
290 - (channelOnThumbnail ? 23 : 0)
2021-09-18 20:36:42 +00:00
#endif
}
}
struct HorizontalCells_Previews: PreviewProvider {
2021-09-18 20:36:42 +00:00
static var previews: some View {
HorizontalCells(items: ContentItem.array(of: Video.allFixtures))
2021-09-29 11:45:00 +00:00
.injectFixtureEnvironmentObjects()
2021-09-18 20:36:42 +00:00
}
}