Skip to content
This repository was archived by the owner on Jan 17, 2019. It is now read-only.

Commit 9b01f01

Browse files
authored
Merge pull request #100 from JohnSundell/fix-initial-child-component-frame
Calculate parent component’s view size up-front when creating children
2 parents d7eab34 + 9d9254b commit 9b01f01

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

sources/HUBViewControllerImplementation.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ - (void)componentWrapper:(HUBComponentWrapper *)componentWrapper
472472
- (HUBComponentWrapper *)componentWrapper:(HUBComponentWrapper *)componentWrapper
473473
childComponentForModel:(id<HUBComponentModel>)model
474474
{
475-
CGSize const containerViewSize = HUBComponentLoadViewIfNeeded(componentWrapper).frame.size;
475+
CGSize const containerViewSize = [self childComponentContainerViewSizeForParentWrapper:componentWrapper];
476476

477477
HUBComponentWrapper * const childComponentWrapper = [self.childComponentReusePool componentWrapperForModel:model
478478
delegate:self
@@ -863,6 +863,16 @@ - (void)configureComponentWrapper:(HUBComponentWrapper *)wrapper withModel:(id<H
863863
self.componentWrappersByModelIdentifier[model.identifier] = wrapper;
864864
}
865865

866+
- (CGSize)childComponentContainerViewSizeForParentWrapper:(HUBComponentWrapper *)parentWrapper
867+
{
868+
if (parentWrapper.isRootComponent && parentWrapper.model.type == HUBComponentTypeBody) {
869+
NSIndexPath * const indexPath = [NSIndexPath indexPathForItem:(NSInteger)parentWrapper.model.index inSection:0];
870+
return [self.collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath].frame.size;
871+
}
872+
873+
return HUBComponentLoadViewIfNeeded(parentWrapper).frame.size;
874+
}
875+
866876
- (nullable HUBComponentWrapper *)componentWrapperFromCell:(HUBComponentCollectionViewCell *)cell
867877
{
868878
return self.componentWrappersByCellIdentifier[cell.identifier];

tests/HUBViewControllerTests.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,12 +1385,13 @@ - (void)testContainerViewSizeForReusedRootComponentsAreSameAsCollectionViewSize
13851385
XCTAssertTrue(CGSizeEqualToSize(self.component.currentContainerViewSize, self.collectionView.bounds.size));
13861386
}
13871387

1388-
- (void)testContainerViewSizeForChildComponentsAreParerentComponentsViewSize
1388+
- (void)testContainerViewSizeForChildComponentsAreParentComponentsViewSize
13891389
{
13901390
NSString * const componentNamespace = @"childComponentSelection";
13911391
NSString * const componentName = @"component";
13921392
NSString * const childComponentName = @"componentB";
13931393
HUBComponentMock * const component = [HUBComponentMock new];
1394+
component.preferredViewSize = CGSizeMake(200, 200);
13941395
HUBComponentMock * const childComponent = [HUBComponentMock new];
13951396

13961397
HUBComponentFactoryMock * const componentFactory = [[HUBComponentFactoryMock alloc] initWithComponents:@{
@@ -1417,17 +1418,16 @@ - (void)testContainerViewSizeForChildComponentsAreParerentComponentsViewSize
14171418
NSIndexPath * const indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
14181419
[self.collectionView.dataSource collectionView:self.collectionView cellForItemAtIndexPath:indexPath];
14191420

1420-
const CGRect expectedParentFrame = CGRectMake(0, 0, 88, 88);
1421-
component.view.frame = expectedParentFrame;
1422-
14231421
id<HUBComponentChildDelegate> const childDelegate = component.childDelegate;
14241422

14251423
id<HUBComponentModel> const childComponentModelA = [component.model childAtIndex:0];
14261424
XCTAssertNotNil(childComponentModelA);
14271425

14281426
[childDelegate component:component childComponentForModel:childComponentModelA];
14291427

1430-
XCTAssertTrue(CGSizeEqualToSize(childComponent.currentContainerViewSize, expectedParentFrame.size));
1428+
CGSize const expectedContainerViewSize = [self.collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath].frame.size;
1429+
XCTAssertTrue(CGSizeEqualToSize(expectedContainerViewSize, CGSizeMake(200, 200)));
1430+
XCTAssertTrue(CGSizeEqualToSize(childComponent.currentContainerViewSize, expectedContainerViewSize));
14311431
}
14321432

14331433
- (void)testCollectionViewNotAddedOnTopOfInitialOverlayComponent

0 commit comments

Comments
 (0)