From 3773a051ab319a0457796c63824584987795fcbf Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Thu, 7 Dec 2023 16:45:42 -0600 Subject: [PATCH] Changes NewsCollectionViewCell image aspect ratio to 3:2 Also updates fonts to use dynamic text styles. --- AltStore/News/NewsCollectionViewCell.swift | 3 +++ AltStore/News/NewsCollectionViewCell.xib | 29 +++++++++++----------- AltStore/News/NewsViewController.swift | 27 +++++++++++++------- AltStoreCore/Model/NewsItem.swift | 10 ++++++++ 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/AltStore/News/NewsCollectionViewCell.swift b/AltStore/News/NewsCollectionViewCell.swift index 3fae6638..eb7535e7 100644 --- a/AltStore/News/NewsCollectionViewCell.swift +++ b/AltStore/News/NewsCollectionViewCell.swift @@ -19,6 +19,9 @@ final class NewsCollectionViewCell: UICollectionViewCell { super.awakeFromNib() + let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .title2).bolded() + self.titleLabel.font = UIFont(descriptor: descriptor, size: 0.0) + self.contentView.preservesSuperviewLayoutMargins = true self.contentBackgroundView.layer.cornerRadius = 30 diff --git a/AltStore/News/NewsCollectionViewCell.xib b/AltStore/News/NewsCollectionViewCell.xib index 42ec35d6..48e982fc 100644 --- a/AltStore/News/NewsCollectionViewCell.xib +++ b/AltStore/News/NewsCollectionViewCell.xib @@ -1,9 +1,9 @@ - + - + @@ -22,31 +22,31 @@ - - + + - - + + - - + + - + @@ -56,12 +56,11 @@ - + - diff --git a/AltStore/News/NewsViewController.swift b/AltStore/News/NewsViewController.swift index 6ad135ab..64fc43a2 100644 --- a/AltStore/News/NewsViewController.swift +++ b/AltStore/News/NewsViewController.swift @@ -90,6 +90,16 @@ class NewsViewController: UICollectionViewController, PeekPopPreviewing self.prototypeCell = NewsCollectionViewCell.instantiate(with: NewsCollectionViewCell.nib!) self.prototypeCell.contentView.translatesAutoresizingMaskIntoConstraints = false + // Need to add dummy constraint + layout subviews before we can remove Interface Builder's width constraint. + self.prototypeCell.widthAnchor.constraint(greaterThanOrEqualToConstant: 0).isActive = true + self.prototypeCell.layoutIfNeeded() + + let constraints = self.prototypeCell.constraintsAffectingLayout(for: .horizontal) + for constraint in constraints where constraint.identifier?.contains("Encapsulated-Layout-Width") == true + { + self.prototypeCell.removeConstraint(constraint) + } + self.collectionView.dataSource = self.dataSource self.collectionView.prefetchDataSource = self.dataSource @@ -149,10 +159,12 @@ private extension NewsViewController let dataSource = RSTFetchedResultsCollectionViewPrefetchingDataSource(fetchedResultsController: fetchedResultsController) dataSource.proxy = self - dataSource.cellConfigurationHandler = { (cell, newsItem, indexPath) in + dataSource.cellConfigurationHandler = { [weak self] (cell, newsItem, indexPath) in + guard let self else { return } + let cell = cell as! NewsCollectionViewCell - cell.layoutMargins.left = self.view.layoutMargins.left - cell.layoutMargins.right = self.view.layoutMargins.right + cell.contentView.layoutMargins.left = self.view.layoutMargins.left + cell.contentView.layoutMargins.right = self.view.layoutMargins.right cell.titleLabel.text = newsItem.title cell.captionLabel.text = newsItem.caption @@ -452,16 +464,13 @@ extension NewsViewController: UICollectionViewDelegateFlowLayout func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let item = self.dataSource.item(at: indexPath) + let globallyUniqueID = item.globallyUniqueID ?? item.identifier - if let previousSize = self.cachedCellSizes[item.identifier] + if let previousSize = self.cachedCellSizes[globallyUniqueID] { return previousSize } - // Take layout margins into account. - self.prototypeCell.layoutMargins.left = self.view.layoutMargins.left - self.prototypeCell.layoutMargins.right = self.view.layoutMargins.right - let widthConstraint = self.prototypeCell.contentView.widthAnchor.constraint(equalToConstant: collectionView.bounds.width) NSLayoutConstraint.activate([widthConstraint]) defer { NSLayoutConstraint.deactivate([widthConstraint]) } @@ -469,7 +478,7 @@ extension NewsViewController: UICollectionViewDelegateFlowLayout self.dataSource.cellConfigurationHandler(self.prototypeCell, item, indexPath) let size = self.prototypeCell.contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize) - self.cachedCellSizes[item.identifier] = size + self.cachedCellSizes[globallyUniqueID] = size return size } diff --git a/AltStoreCore/Model/NewsItem.swift b/AltStoreCore/Model/NewsItem.swift index f3c98377..2e143105 100644 --- a/AltStoreCore/Model/NewsItem.swift +++ b/AltStoreCore/Model/NewsItem.swift @@ -82,6 +82,16 @@ public class NewsItem: NSManagedObject, Decodable, Fetchable } } +public extension NewsItem +{ + var globallyUniqueID: String? { + guard let sourceIdentifier = self.sourceIdentifier else { return nil } + + let globallyUniqueID = self.identifier + "|" + sourceIdentifier + return globallyUniqueID + } +} + public extension NewsItem { @nonobjc class func fetchRequest() -> NSFetchRequest