Skip to content

Commit

Permalink
Add new iOS 11 api adjustedContentInset support
Browse files Browse the repository at this point in the history
Summary:
adopt adjustedContentInset instead of contentInset on iOS 11

- [x] All tests pass. Demo project builds and runs.
- [x] I added tests, an experiment, or detailed why my change isn't tested.
- [x] I added an entry to the `CHANGELOG.md` for any breaking changes, enhancements, or bug fixes.
- [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/.github/CONTRIBUTING.md)
Closes #1020

Reviewed By: manicakes

Differential Revision: D6513703

Pulled By: rnystrom

fbshipit-source-id: 5d718f2cb30361959ca5ba8238746427223cb5fd
  • Loading branch information
yiplee authored and facebook-github-bot committed Dec 18, 2017
1 parent 74c5fb3 commit e63a0e7
Show file tree
Hide file tree
Showing 12 changed files with 1,146 additions and 967 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The changelog for `IGListKit`. Also see the [releases](https://github.com/instag

- Added experiment to make `-[IGListAdapter visibleSectionControllers:]` a bit faster. [Maxime Ollivier](https://github.com/maxoll) (tbd)

- Added support `-[UIScrollView adjustedContentInset]` for iOS 11. [Guoyin Li](https://github.com/yiplee) [(#1020)](https://github.com/Instagram/IGListKit/pull/1020)

### Fixes

- Duplicate objects for initial data source setup filtered out. [Mikhail Vashlyaev](https://github.com/yemodin) [(#993](https://github.com/Instagram/IGListKit/pull/993)
Expand Down
1,006 changes: 507 additions & 499 deletions Examples/Examples-iOS/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

926 changes: 467 additions & 459 deletions Examples/Examples-tvOS/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions IGListKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@
DAA83ACF1E8ECE06000F6810 /* IGListAdapter+UICollectionView.m in Sources */ = {isa = PBXBuildFile; fileRef = DA5F484A1E8E9D7000DAE6DA /* IGListAdapter+UICollectionView.m */; };
DAD4A40D1E8E9E1E00DACC36 /* IGListAdapter+UICollectionView.h in Headers */ = {isa = PBXBuildFile; fileRef = DA5F48491E8E9D7000DAE6DA /* IGListAdapter+UICollectionView.h */; settings = {ATTRIBUTES = (Private, ); }; };
DD468D380BBF350ACE7EA28B /* Pods_IGListKitTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AB7195278D0BBB5DA88D36F /* Pods_IGListKitTests.framework */; };
E8B5C73B1FC431D400C26914 /* UIScrollView+IGListKit.h in Headers */ = {isa = PBXBuildFile; fileRef = E8B5C7391FC431D400C26914 /* UIScrollView+IGListKit.h */; };
E8B5C73C1FC431D400C26914 /* UIScrollView+IGListKit.m in Sources */ = {isa = PBXBuildFile; fileRef = E8B5C73A1FC431D400C26914 /* UIScrollView+IGListKit.m */; };
E8D312E01FC472A60009FA2F /* IGListContentInsetTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E8D312DF1FC472A60009FA2F /* IGListContentInsetTests.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -587,6 +590,9 @@
DA5F48491E8E9D7000DAE6DA /* IGListAdapter+UICollectionView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "IGListAdapter+UICollectionView.h"; sourceTree = "<group>"; };
DA5F484A1E8E9D7000DAE6DA /* IGListAdapter+UICollectionView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "IGListAdapter+UICollectionView.m"; sourceTree = "<group>"; };
DD3152AC1DE227FA00AC9D2C /* IGListKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = IGListKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E8B5C7391FC431D400C26914 /* UIScrollView+IGListKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIScrollView+IGListKit.h"; sourceTree = "<group>"; };
E8B5C73A1FC431D400C26914 /* UIScrollView+IGListKit.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIScrollView+IGListKit.m"; sourceTree = "<group>"; };
E8D312DF1FC472A60009FA2F /* IGListContentInsetTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IGListContentInsetTests.m; sourceTree = "<group>"; };
E980179F5E885E417EB20D55 /* Pods_IGListKit_tvOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_IGListKit_tvOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -768,6 +774,8 @@
290DF3571E930CE2009FE456 /* UICollectionView+DebugDescription.m */,
0B3B92C21E08D7F5008390ED /* UICollectionView+IGListBatchUpdateData.h */,
0B3B92C31E08D7F5008390ED /* UICollectionView+IGListBatchUpdateData.m */,
E8B5C7391FC431D400C26914 /* UIScrollView+IGListKit.h */,
E8B5C73A1FC431D400C26914 /* UIScrollView+IGListKit.m */,
);
path = Internal;
sourceTree = "<group>";
Expand Down Expand Up @@ -929,6 +937,7 @@
29DA5CAA1EA7D3FF00113926 /* IGListTestHelpers.h */,
88144EEF1D870EDC007C7F66 /* IGListWorkingRangeHandlerTests.m */,
2997D4961DF5FC0B005A5DD2 /* IGReloadDataUpdaterTests.m */,
E8D312DF1FC472A60009FA2F /* IGListContentInsetTests.m */,
887D0B571D870E1E009E01F7 /* Info.plist */,
88144EF01D870EDC007C7F66 /* Objects */,
);
Expand Down Expand Up @@ -1030,6 +1039,7 @@
290DF3741E931B57009FE456 /* IGListDebuggingUtilities.h in Headers */,
0B3B92EC1E08D7F5008390ED /* IGListMoveIndexPathInternal.h in Headers */,
0B3B93341E08D7F5008390ED /* IGListDisplayHandler.h in Headers */,
E8B5C73B1FC431D400C26914 /* UIScrollView+IGListKit.h in Headers */,
0B3B92D21E08D7F5008390ED /* IGListIndexPathResult.h in Headers */,
2926586C1E75E01A0041B56D /* IGListBindingSectionControllerDataSource.h in Headers */,
0B3B93241E08D7F5008390ED /* IGListSupplementaryViewSource.h in Headers */,
Expand Down Expand Up @@ -1554,6 +1564,7 @@
0B3B93421E08D7F5008390ED /* IGListWorkingRangeHandler.mm in Sources */,
0B3B92F01E08D7F5008390ED /* NSNumber+IGListDiffable.m in Sources */,
298DDA211E3B0DC800F76F50 /* IGListCollectionViewLayout.mm in Sources */,
E8B5C73C1FC431D400C26914 /* UIScrollView+IGListKit.m in Sources */,
0B3B93301E08D7F5008390ED /* IGListAdapterProxy.m in Sources */,
0B3B92CC1E08D7F5008390ED /* IGListDiff.mm in Sources */,
0D52F0B61F1AE8BD00460F79 /* IGListBindingSectionController+DebugDescription.m in Sources */,
Expand Down Expand Up @@ -1628,6 +1639,7 @@
82914C5B1E6E2DEC0066C2F8 /* IGListTestContainerSizeSection.m in Sources */,
29DA5CA71EA7D37000113926 /* IGListTestCase.m in Sources */,
88144F1D1D870EDC007C7F66 /* IGTestSupplementarySource.m in Sources */,
E8D312E01FC472A60009FA2F /* IGListContentInsetTests.m in Sources */,
298DDA071E3AE2B100F76F50 /* IGTestStringBindableCell.m in Sources */,
88144F081D870EDC007C7F66 /* IGListAdapterTests.m in Sources */,
8240C7F21DC284C300B3AAE7 /* IGListAdapterStoryboardTests.m in Sources */,
Expand Down
17 changes: 11 additions & 6 deletions Source/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "IGListSectionControllerInternal.h"
#import "IGListDebugger.h"
#import "IGListArrayUtilsInternal.h"
#import "UIScrollView+IGListKit.h"

@implementation IGListAdapter {
NSMapTable<UICollectionReusableView *, IGListSectionController *> *_viewSectionControllerMap;
Expand Down Expand Up @@ -249,7 +250,7 @@ - (void)scrollToObject:(id)object
const CGFloat offsetMid = (offsetMin + offsetMax) / 2.0;
const CGFloat collectionViewWidth = collectionView.bounds.size.width;
const CGFloat collectionViewHeight = collectionView.bounds.size.height;
const UIEdgeInsets contentInset = collectionView.contentInset;
const UIEdgeInsets contentInset = collectionView.ig_contentInset;
CGPoint contentOffset = collectionView.contentOffset;
switch (scrollDirection) {
case UICollectionViewScrollDirectionHorizontal: {
Expand All @@ -270,8 +271,8 @@ - (void)scrollToObject:(id)object
contentOffset.x = offsetMin - contentInset.left;
break;
}
const CGFloat maxOffsetX = collectionView.contentSize.width - collectionView.frame.size.width + collectionView.contentInset.right;
const CGFloat minOffsetX = -collectionView.contentInset.left;
const CGFloat maxOffsetX = collectionView.contentSize.width - collectionView.frame.size.width + contentInset.right;
const CGFloat minOffsetX = -contentInset.left;
contentOffset.x = MIN(contentOffset.x, maxOffsetX);
contentOffset.x = MAX(contentOffset.x, minOffsetX);
break;
Expand All @@ -294,8 +295,8 @@ - (void)scrollToObject:(id)object
contentOffset.y = offsetMin - contentInset.top;
break;
}
const CGFloat maxOffsetY = collectionView.contentSize.height - collectionView.frame.size.height + collectionView.contentInset.bottom;
const CGFloat minOffsetY = -collectionView.contentInset.top;
const CGFloat maxOffsetY = collectionView.contentSize.height - collectionView.frame.size.height + contentInset.bottom;
const CGFloat minOffsetY = -contentInset.top;
contentOffset.y = MIN(contentOffset.y, maxOffsetY);
contentOffset.y = MAX(contentOffset.y, minOffsetY);
break;
Expand Down Expand Up @@ -830,9 +831,13 @@ - (UIEdgeInsets)containerInset {
return self.collectionView.contentInset;
}

- (UIEdgeInsets)adjustedContainerInset {
return self.collectionView.ig_contentInset;
}

- (CGSize)insetContainerSize {
UICollectionView *collectionView = self.collectionView;
return UIEdgeInsetsInsetRect(collectionView.bounds, collectionView.contentInset).size;
return UIEdgeInsetsInsetRect(collectionView.bounds, collectionView.ig_contentInset).size;
}

- (CGSize)containerSizeForSectionController:(IGListSectionController *)sectionController {
Expand Down
5 changes: 5 additions & 0 deletions Source/IGListCollectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ NS_SWIFT_NAME(ListCollectionContext)
*/
@property (nonatomic, readonly) UIEdgeInsets containerInset;

/**
The adjusted content insets of the collection view. Equivalent to containerInset under iOS 11.
*/
@property (nonatomic, readonly) UIEdgeInsets adjustedContainerInset;

/**
The size of the collection view with content insets applied.
*/
Expand Down
6 changes: 3 additions & 3 deletions Source/IGListCollectionViewLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#import "IGListCollectionViewLayout.h"
#import "IGListCollectionViewLayoutInternal.h"
#import "UIScrollView+IGListKit.h"

#import <vector>

Expand Down Expand Up @@ -335,8 +336,7 @@ - (CGSize)collectionViewContentSize {

const IGListSectionEntry section = _sectionData[sectionCount - 1];
UICollectionView *collectionView = self.collectionView;
const UIEdgeInsets contentInset = collectionView.contentInset;

const UIEdgeInsets contentInset = collectionView.ig_contentInset;
switch (self.scrollDirection) {
case UICollectionViewScrollDirectionVertical: {
const CGFloat height = CGRectGetMaxY(section.bounds) + section.insets.bottom;
Expand Down Expand Up @@ -435,7 +435,7 @@ - (void)calculateLayoutIfNeeded {
id<UICollectionViewDelegateFlowLayout> delegate = (id<UICollectionViewDelegateFlowLayout>)collectionView.delegate;

const NSInteger sectionCount = [dataSource numberOfSectionsInCollectionView:collectionView];
const UIEdgeInsets contentInset = collectionView.contentInset;
const UIEdgeInsets contentInset = collectionView.ig_contentInset;
const CGRect contentInsetAdjustedCollectionViewBounds = UIEdgeInsetsInsetRect(collectionView.bounds, contentInset);

_sectionData.resize(sectionCount);
Expand Down
4 changes: 4 additions & 0 deletions Source/IGListStackedSectionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ - (UIEdgeInsets)containerInset {
return [self.collectionContext containerInset];
}

- (UIEdgeInsets)adjustedContainerInset {
return [self.collectionContext adjustedContainerInset];
}

- (CGSize)insetContainerSize {
return [self.collectionContext insetContainerSize];
}
Expand Down
16 changes: 16 additions & 0 deletions Source/Internal/UIScrollView+IGListKit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#import <UIKit/UIKit.h>

@interface UIScrollView (IGListKit)

- (UIEdgeInsets) ig_contentInset;

@end
27 changes: 27 additions & 0 deletions Source/Internal/UIScrollView+IGListKit.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#import "UIScrollView+IGListKit.h"

@implementation UIScrollView (IGListKit)

- (UIEdgeInsets) ig_contentInset
{
#ifdef __IPHONE_11_0
if (@available(iOS 11,*)) {
return self.adjustedContentInset;
} else {
return self.contentInset;
}
#else
return self.contentInset;
#endif
}

@end
84 changes: 84 additions & 0 deletions Tests/IGListContentInsetTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//

#import <XCTest/XCTest.h>

#import <IGListKit/IGListKit.h>
#import "UIScrollView+IGListKit.h"
#import "IGListTestHelpers.h"
#import "IGListAdapterInternal.h"

static const CGRect kStackTestFrame = (CGRect){{0.0, 0.0}, {320.0, 480.0}};

@interface IGListContentInsetTests : XCTestCase<IGListAdapterDataSource>

@property (nonatomic, strong) IGListAdapter *adapter;

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UIViewController *viewController;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation IGListContentInsetTests

- (void)setUp {
[super setUp];

self.viewController = [UIViewController new];

IGListCollectionViewLayout *layout = [[IGListCollectionViewLayout alloc] initWithStickyHeaders:NO
topContentInset:0
stretchToEdge:YES];
self.collectionView = [[UICollectionView alloc] initWithFrame:kStackTestFrame
collectionViewLayout:layout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.viewController.view addSubview:self.collectionView];

self.adapter = [[IGListAdapter alloc] initWithUpdater:[IGListAdapterUpdater new]
viewController:self.viewController];
self.adapter.dataSource = self;
self.adapter.collectionView = self.collectionView;

self.window = [[UIWindow alloc] initWithFrame:kStackTestFrame];
self.window.rootViewController = self.viewController;
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];

self.adapter = nil;
self.viewController = nil;
self.collectionView = nil;
}

- (void) testCollectionViewContentInset {
const UIEdgeInsets inset = UIEdgeInsetsMake(10, 0, 10, 0);
self.collectionView.contentInset = inset;
IGAssertEqualInsets(self.collectionView.ig_contentInset, inset.top, inset.left, inset.bottom, inset.right);
id<IGListCollectionContext> context = self.adapter;
IGAssertEqualInsets(context.adjustedContainerInset, inset.top, inset.left, inset.bottom, inset.right);
}

#pragma mark - IGListAdapterDataSource

- (NSArray<id <IGListDiffable>> *)objectsForListAdapter:(IGListAdapter *)listAdapter {
return @[];
}

- (IGListSectionController *) listAdapter:(IGListAdapter *)listAdapter sectionControllerForObject:(id)object {
return nil;
}

- (UIView *) emptyViewForListAdapter:(IGListAdapter *)listAdapter {
return nil;
}

@end
8 changes: 8 additions & 0 deletions Tests/IGListTestHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ XCTAssertEqual(CGRectGetWidth(expected), CGRectGetWidth(frame)); \
XCTAssertEqual(CGRectGetHeight(expected), CGRectGetHeight(frame)); \
} while(0)

#define IGAssertEqualInsets(insets,t,l,b,r, ...) \
do { \
XCTAssertEqual(insets.top,t); \
XCTAssertEqual(insets.left,l); \
XCTAssertEqual(insets.bottom,b); \
XCTAssertEqual(insets.right,r); \
} while(0)

#define IGAssertContains(collection, object) do {\
id haystack = collection; id needle = object; \
XCTAssertTrue([haystack containsObject:needle], @"%@ does not contain %@", haystack, needle); \
Expand Down

0 comments on commit e63a0e7

Please sign in to comment.