Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add horizontal scrolling support to IGListCollectionViewLayout #857

Closed

Conversation

edmonston
Copy link
Contributor

Changes in this pull request

Issue fixed: #752

Checklist

  • All tests pass. Demo project builds and runs.
  • I added tests, an experiment, or detailed why my change isn't tested.
  • I added an entry to the CHANGELOG.md for any breaking changes, enhancements, or bug fixes.
  • I have reviewed the contributing guide

This PR generalizes the layout logic in IGListCollectionViewLayout.mm to handle horizontally scrolling layouts, mainly by generalizing references to width, height, x and y to take scrolling direction into account. This changes the signature of IGListCollectionViewLayout.init as well as the names of a few properties, so it would be a breaking change.

I added a couple of unit tests specifically for horizontal layouts -- but held off from adding a horizontal version of every unit test for this class, as it would basically double the number of tests. But if you want that, just let me know and I'm happy to do it.

Also let me know if you want me to add a demo VC to the Examples project that uses this new horizontal flow layout -- I have some demo code handy (I used it for testing), but didn't want to clutter up the PR if you didn't want/need it.

@facebook-github-bot
Copy link
Contributor

@edmonston updated the pull request - view changes

@rnystrom
Copy link
Contributor

rnystrom commented Jul 18, 2017

@edmonston in order to land this earlier than the 4.0 release, what about keeping the existing init but just call the new initializer with UICollectionViewScrollDirectionVertical (and topContentInset can stay). We can file a follow-up for 4.0 to remove it in favor of a single initializer (which I prefer).

Just want to get this in for 3.1 or 3.2 and not have to hold it. We're trying to avoid breaking changes in minor releases.

Thoughts?

edit: This is AMAZING btw!

Copy link
Contributor

@rnystrom rnystrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind also adding a test demonstrating:

  • Items moving to the next column while still in the same section
  • Sections that don't break to new columns (so they stack vertically)

rect.origin.y + insets.top,
rect.size.width - insets.left - insets.right,
rect.size.height - insets.top - insets.bottom);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this exactly what UIEdgeInsetsInsetRect does already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh, I did not even know this function already existed...

}

static CGFloat CGPointGetCoordinateInDirection(CGPoint point, UICollectionViewScrollDirection direction) {
return direction == UICollectionViewScrollDirectionVertical ? point.y : point.x;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on using a switch(...) instead so the compiler will error if something ever changes? It likely wont, just good for longterm maintenance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 will do


static CGFloat CGSizeGetLengthInDirection(CGSize size, UICollectionViewScrollDirection direction) {
return direction == UICollectionViewScrollDirectionVertical ? size.height : size.width;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 love these methods 👌

@"%@ of item %zi in section %zi must be less than container %.0f accounting for section insets %@",
self.scrollDirection == UICollectionViewScrollDirectionVertical ? @"Width" : @"Height",
item, section, CGRectGetLengthInDirection(contentInsetAdjustedCollectionViewBounds, fixedDirection),
NSStringFromUIEdgeInsets(insets));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it!


// find the lowest point in the section and add the bottom inset to find the next row's Y
nextRowY = MAX(nextRowY, CGRectGetMaxY(rollingSectionBounds) + insets.bottom);
// find the farthest point in the section and add the trailing inset to find the next row's coord
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even these comment updates are great!

IGAssertEqualFrame([self cellForSection:0 item:0].frame, 20, 10, 45, 10);
IGAssertEqualFrame([self cellForSection:0 item:1].frame, 20, 20, 45, 20);
IGAssertEqualFrame([self headerForSection:1].frame, 80, 10, 10, 85);
IGAssertEqualFrame([self cellForSection:1 item:0].frame, 90, 10, 45, 30);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test, can see the items stacking vertically

@edmonston
Copy link
Contributor Author

Definitely @rnystrom! Makes total sense to preserve the old initializer and just add a new one for now, so it isn't breaking and doesn't have to wait to 4.0.

When I update the CHANGELOG.md which release should I target this PR for?

I'll make this change, add the unit tests you suggested and make the other tweaks from the comments. Stay tuned.

@rnystrom
Copy link
Contributor

@edmonston we can make this a 3.1 enhancement 😁

@facebook-github-bot
Copy link
Contributor

@edmonston updated the pull request - view changes

@facebook-github-bot
Copy link
Contributor

@edmonston updated the pull request - view changes

@facebook-github-bot
Copy link
Contributor

@edmonston updated the pull request - view changes

@edmonston
Copy link
Contributor Author

So, I did those changes to make this PR non-breaking (in the semver sense), and everything seemed fine -- except I just realized that I had altered one other element of the public-facing IGListCollectionViewLayout API as well: I had changed the property name stickyHeaderOriginYAdjustment to the more general stickyHeaderOriginOffset. So technically it's still a breaking update.

Should I just revert to the original name (referencing the 'Y') for compatibility's sake, even though it's not quite accurate for a horizontal layout, and then we fix it later in 4.0?

Even nit-pickier: As it stands now, the NS_DESIGNATED_INITIALIZER will become the new init method, while the old one transitions to a convenience initializer. I'm hoping this is not considered a breaking change...

@weyert
Copy link
Contributor

weyert commented Jul 21, 2017

@edmonston Maybe consider adding both properties and just return the value of the new property for stickyHeaderOriginYAdjustment and then remove stickyHeaderOriginYAdjustment-property in 4.0?

@facebook-github-bot
Copy link
Contributor

@edmonston updated the pull request - view changes

@edmonston
Copy link
Contributor Author

Hey @weyert that's a good suggestion. I've updated the PR so stickyHeadersYOffset is just a passthrough to the new property. So this should be thoroughly non-breaking, API-wise.

(I also changed two occurrences of @discussion to @note in the header comments because it seems like we made that change globally in #182 to make Jazzy docs happy.)

@rnystrom lemme know if those new unit tests (I actually added 4) seem ok.

@rnystrom
Copy link
Contributor

@edmonston awesome! Don't worry about changing the DI, that shouldn't break anything. Even though the naming is inconsistent, let's keep the Y for now so that people can upgrade w/out worry while 4.0 is WIP.

I'm out of the country this week and will land this when I'm back, just in case this breaks something I don't want to make people scramble while I'm gone!

@facebook-github-bot
Copy link
Contributor

@edmonston updated the pull request - view changes

@facebook-github-bot
Copy link
Contributor

@rnystrom has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Horizontal list support in IGListCollectionViewLayout
4 participants