| 1 | // |
| 2 | // TVHeroSection.swift |
| 3 | // JomloCV tvOS Example |
| 4 | // |
| 5 | // Created by Seto Elkahfi on 2026-01-27. |
| 6 | // Copyright © 2026 Seto Elkahfi. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import JomloCollectionView |
| 10 | import UIKit |
| 11 | |
| 12 | /// A full-width hero section for featured content at the top of the TV screen. |
| 13 | /// Uses group paging for a cinematic browsing feel with the Siri Remote. |
| 14 | @MainActor |
| 15 | final class TVHeroSection: JomloSection { |
| 16 | |
| 17 | let identifier = JomloSectionID("tv-hero") |
| 18 | var onReloadNeeded: (@MainActor @Sendable () -> Void)? |
| 19 | |
| 20 | private let heroItems: [TVHeroItem] |
| 21 | |
| 22 | init(items: [TVHeroItem]) { |
| 23 | self.heroItems = items |
| 24 | } |
| 25 | |
| 26 | func items() -> [any JomloItem] { |
| 27 | heroItems |
| 28 | } |
| 29 | |
| 30 | func layoutSection( |
| 31 | environment: NSCollectionLayoutEnvironment, |
| 32 | sectionIndex: Int |
| 33 | ) -> NSCollectionLayoutSection { |
| 34 | // Full-width hero sized for 1080p TV (16:9 ratio, padded) |
| 35 | let itemSize = NSCollectionLayoutSize( |
| 36 | widthDimension: .fractionalWidth(1.0), |
| 37 | heightDimension: .fractionalHeight(1.0) |
| 38 | ) |
| 39 | let item = NSCollectionLayoutItem(layoutSize: itemSize) |
| 40 | |
| 41 | let groupSize = NSCollectionLayoutSize( |
| 42 | widthDimension: .fractionalWidth(0.85), |
| 43 | heightDimension: .absolute(500) |
| 44 | ) |
| 45 | let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) |
| 46 | |
| 47 | let section = NSCollectionLayoutSection(group: group) |
| 48 | section.orthogonalScrollingBehavior = .groupPagingCentered |
| 49 | section.interGroupSpacing = 40 |
| 50 | section.contentInsets = NSDirectionalEdgeInsets( |
| 51 | top: 20, leading: 0, bottom: 50, trailing: 0) |
| 52 | return section |
| 53 | } |
| 54 | |
| 55 | func cell( |
| 56 | for item: any JomloItem, |
| 57 | in collectionView: UICollectionView, |
| 58 | at indexPath: IndexPath |
| 59 | ) -> UICollectionViewCell { |
| 60 | guard let heroItem = item as? TVHeroItem else { return UICollectionViewCell() } |
| 61 | return collectionView.dequeueJomloCell(TVHeroCell.self, for: indexPath, item: heroItem) |
| 62 | } |
| 63 | } |