From 57059967c61026d3d5e808392eb055d0a611c258 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Thu, 12 Oct 2023 15:47:09 -0500 Subject: [PATCH] Improves paging screenshots with different aspect ratios We now page by the smallest screenshot width to ensure we never overshoot an item. --- .../AppScreenshotsViewController.swift | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/AltStore/App Detail/Screenshots/AppScreenshotsViewController.swift b/AltStore/App Detail/Screenshots/AppScreenshotsViewController.swift index 56e3976a..4a88e31a 100644 --- a/AltStore/App Detail/Screenshots/AppScreenshotsViewController.swift +++ b/AltStore/App Detail/Screenshots/AppScreenshotsViewController.swift @@ -65,12 +65,25 @@ private extension AppScreenshotsViewController let preferredHeight = 400.0 let estimatedWidth = preferredHeight * (AppScreenshot.defaultAspectRatio.width / AppScreenshot.defaultAspectRatio.height) - let layout = UICollectionViewCompositionalLayout(sectionProvider: { (sectionIndex, layoutEnvironment) -> NSCollectionLayoutSection? in + let layout = UICollectionViewCompositionalLayout(sectionProvider: { [dataSource] (sectionIndex, layoutEnvironment) -> NSCollectionLayoutSection? in + let screenshotWidths = dataSource.items.map { screenshot in + var aspectRatio = screenshot.size ?? AppScreenshot.defaultAspectRatio + if aspectRatio.width > aspectRatio.height + { + aspectRatio = CGSize(width: aspectRatio.height, height: aspectRatio.width) + } + + let screenshotWidth = (preferredHeight * (aspectRatio.width / aspectRatio.height)).rounded() + return screenshotWidth + } - let itemSize = NSCollectionLayoutSize(widthDimension: .estimated(estimatedWidth), heightDimension: .fractionalHeight(1.0)) + let smallestWidth = screenshotWidths.sorted().first + let itemWidth = smallestWidth ?? estimatedWidth // Use smallestWidth to ensure we never overshoot an item when paging. + + let itemSize = NSCollectionLayoutSize(widthDimension: .estimated(itemWidth), heightDimension: .fractionalHeight(1.0)) let item = NSCollectionLayoutItem(layoutSize: itemSize) - let groupSize = NSCollectionLayoutSize(widthDimension: .estimated(estimatedWidth), heightDimension: .absolute(preferredHeight)) + let groupSize = NSCollectionLayoutSize(widthDimension: .estimated(itemWidth), heightDimension: .absolute(preferredHeight)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let layoutSection = NSCollectionLayoutSection(group: group)