Consider better name for ViewDistributionItem.collapsing(_:)
The current name is kind of confusing, and this is a weird place for it. Some ideas:
ViewDistributionItem.compacting(:) ViewDistributionItem.compactingHidden(:) Array<ViewDistributionItem>.compacted()
Personally I think it would be cleanest to make this a method extending Array<ViewDistributionItem>, so you could do something like:
applySubviewDistribution(
[ firstView, secondView, ].compacted()
)
The method name is a bit complicated though... it's not exactly hidden views, since it checks more than the isHidden property. I'm having trouble thinking of a good description of it right now, but I agree that "compacting" is too vague.
Today it's collapsing, which implies that all of the views are collapsed, so compacting is at least an improvement. We could use something like compactingInvisible, or removingInvisible(). There's some ambiguity there as to whether it removes all spacers, so could also do removingInvisibleViews(). Some adjacent spacers will also be removed, but that's sort of just a side effect so I think it might be fine.
I wonder if it's worth being more explicit about what it does, either by breaking it into separate methods or adding parameters to control the behavior? That's probably unnecessary, since you'll likely want all or nothing, but it would at least make the API clearer.
In that vein I could see something like items.filterViews { $0.isVisible } where filterViews only calls the block for view items, and isVisible is whatever logic we want for whether or not to show a view item.
What if we did something like this:
public struct ViewVisibilityFactor: OptionSet {
public static let viewNotInHierarchy = VisibilityFactor(rawValue: 1 << 0)
public static let viewIsHidden = VisibilityFactor(rawValue: 1 << 1)
public static let viewHasZeroAlpha = VisibilityFactor(rawValue: 1 << 2)
public static let labelHasEmptyText = ...
public static let imageViewHasNilImage = ...
public static let all = [.viewNotInHierarchy, ...]
}
func filterViews(removing: VisibilityFactor) {}
public enum SpacerCollapsingBehavior {
case summingAdjacentSpacers
case selectingLargerOfAdjacentSpacers
case selectingSmallerOfAdjacentSpacers
}
func collapseSpacers(by behavior: SpacerCollapsingBehavior, removingFixedSpacersAdjacentToFlexibleSpacers: Bool) {}
Since this hasn't made any progress in a while, I propose we remove the API for now from the 1.0 release. We can then add in more specific functionality (such as filtering view and collapsing spacers) as we establish clear use cases.