-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial layout work for Home content
https://getpocket.atlassian.net/browse/IN-188 Co-authored-by: David Skuza <dskuza@getpocket.com>
- Loading branch information
1 parent
691fa39
commit c5405e3
Showing
67 changed files
with
2,448 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
157 changes: 157 additions & 0 deletions
157
PocketKit/Sources/PocketKit/Home/HomeCollectionViewSectionProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import UIKit | ||
import Sync | ||
|
||
|
||
class HomeViewControllerSectionProvider { | ||
func section(for slate: Slate?, width: CGFloat) -> NSCollectionLayoutSection { | ||
let dividerHeight: CGFloat = 17 | ||
let margin: CGFloat = 8 | ||
let spacing: CGFloat = margin * 2 | ||
|
||
guard let slate = slate, !slate.recommendations.isEmpty else { | ||
return NSCollectionLayoutSection( | ||
group: .vertical( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .absolute(0), | ||
heightDimension: .absolute(0) | ||
), | ||
subitems: [] | ||
) | ||
) | ||
} | ||
|
||
let hero = RecommendationPresenter(recommendation: slate.recommendations[0]) | ||
let heroHeight = RecommendationCell.fullHeight(width: width - spacing, recommendation: hero) | ||
let heroItem = NSCollectionLayoutItem( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .fractionalWidth(1), | ||
heightDimension: .absolute(heroHeight) | ||
) | ||
) | ||
|
||
let heroGroup = NSCollectionLayoutGroup.vertical( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .fractionalWidth(1), | ||
heightDimension: .absolute(heroHeight + dividerHeight) | ||
), | ||
subitems: [heroItem] | ||
) | ||
|
||
heroGroup.supplementaryItems = [ | ||
NSCollectionLayoutSupplementaryItem( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .fractionalWidth(1), | ||
heightDimension: .absolute(dividerHeight) | ||
), | ||
elementKind: "divider", | ||
containerAnchor: NSCollectionLayoutAnchor(edges: .bottom) | ||
) | ||
] | ||
|
||
let twoUp = twoUpGroup(slate: slate, width: width, spacing: spacing, dividerHeight: dividerHeight) | ||
|
||
let topLevelGroup = NSCollectionLayoutGroup.vertical( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .fractionalWidth(1), | ||
heightDimension: .absolute(heroHeight + dividerHeight + (twoUp.height + dividerHeight) * 2) | ||
), | ||
subitems: [heroGroup, twoUp.group] | ||
) | ||
|
||
let section = NSCollectionLayoutSection(group: topLevelGroup) | ||
let slatePresenter = SlateHeaderPresenter(slate: slate) | ||
|
||
section.boundarySupplementaryItems = [ | ||
NSCollectionLayoutBoundarySupplementaryItem( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .fractionalWidth(1.0), | ||
heightDimension: .absolute(SlateHeaderView.height(width: width, slate: slatePresenter)) | ||
), | ||
elementKind: SlateHeaderView.kind, | ||
alignment: .top | ||
) | ||
] | ||
|
||
section.contentInsets = NSDirectionalEdgeInsets( | ||
top: 0, | ||
leading: margin, | ||
bottom: 0, | ||
trailing: margin | ||
) | ||
|
||
return section | ||
} | ||
|
||
func twoUpGroup( | ||
slate: Slate, | ||
width: CGFloat, | ||
spacing: CGFloat, | ||
dividerHeight: CGFloat | ||
) -> (group: NSCollectionLayoutGroup, height: CGFloat) { | ||
guard slate.recommendations.count > 1 else { | ||
return ( | ||
group: NSCollectionLayoutGroup.vertical( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .absolute(0), | ||
heightDimension: .absolute(0) | ||
), | ||
subitems: [] | ||
), | ||
height: 0 | ||
) | ||
} | ||
|
||
let endIndex = slate.recommendations.index( | ||
1, | ||
offsetBy: 3, | ||
limitedBy: slate.recommendations.endIndex - 1 | ||
) ?? slate.recommendations.endIndex - 1 | ||
let recommendationsToShow = slate.recommendations[1...endIndex] | ||
|
||
let miniCardHeight = recommendationsToShow.map { | ||
RecommendationCell.miniHeight( | ||
width: width - spacing, | ||
recommendation: RecommendationPresenter(recommendation: $0) | ||
) | ||
}.max() ?? 0 | ||
|
||
let twoUpInner = NSCollectionLayoutGroup.horizontal( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .fractionalWidth(1), | ||
heightDimension: .absolute(miniCardHeight) | ||
), | ||
subitem: NSCollectionLayoutItem( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .fractionalWidth(0.5), | ||
heightDimension: .absolute(miniCardHeight) | ||
) | ||
), | ||
count: 2 | ||
) | ||
|
||
twoUpInner.supplementaryItems = [ | ||
NSCollectionLayoutSupplementaryItem( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .fractionalWidth(1), | ||
heightDimension: .absolute(dividerHeight) | ||
), | ||
elementKind: "twoup-divider", | ||
containerAnchor: NSCollectionLayoutAnchor(edges: .bottom), | ||
itemAnchor: NSCollectionLayoutAnchor(edges: .bottom) | ||
) | ||
] | ||
|
||
let numberOfRows = Int((Float(recommendationsToShow.count) / 2).rounded(.up)) | ||
return ( | ||
group: NSCollectionLayoutGroup.vertical( | ||
layoutSize: NSCollectionLayoutSize( | ||
widthDimension: .fractionalWidth(1), | ||
heightDimension: .absolute((miniCardHeight + dividerHeight) * 2) | ||
), | ||
subitem: twoUpInner, | ||
count: numberOfRows | ||
), | ||
height: miniCardHeight | ||
) | ||
} | ||
} |
Oops, something went wrong.