Adapting UICollectionViewCompositionalLayout for iPhoneDuo

I have a question about how to adapt UICollectionViewCompositionalLayout for different size classes. Initially I adapted both the diffableDataSource snapshot and the UICollectionViewCompositionalLayout based on the horizontalSizeClass. I wasn't very happy that my datasource (snapshot construction code) needs to know about sizeClasses, it feels like it should only know about data. So I thought why not just return an empty section in the UICollectionViewCompositionalLayout because it get's passed NSCollectionLayoutEnvironment. It works fine but I'm not sure if it's supported or the right way to reason about this. Here's my empty section code.

static var hiddenSection: NSCollectionLayoutSection {
        // Note: .zero is regarded by the system as an error, it might crash in the future
        let dimension = NSCollectionLayoutDimension.absolute(0.1)
        let size = NSCollectionLayoutSize(widthDimension: dimension, heightDimension: dimension)
        return NSCollectionLayoutSection(group: NSCollectionLayoutGroup.horizontal(layoutSize: size, subitems: [.init(layoutSize: size), .init(layoutSize: size), .init(layoutSize: size)] ))
        
    }
```  Thank you for any insights!

This approach doesn't really hide the section. It will likely generate a bunch of items 0.1pt wide.

Can you share more details on why your data source needs to update in response to the size class change?

Hi thanks for the reply,

starting with the assumption that it's simpler if the view controller hierarchy does not change based on changes in size class... as vertical space is shorter on the iPhone Duo vs a standard iPhone, I think it's reasonable to hide one of the vertical sections in the layout (where the infomation is supplementary to the main section) on the iPhone Duo, if the user want's to see it they can unfold to the large display. I suppose the other approach is to return a different layout rather than to adapt in in the sectionProvider based on sizeClass, but then it would probably require manual invalidation (sorry just thinking out loud).

Adapting UICollectionViewCompositionalLayout for iPhoneDuo
 
 
Q