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 storyboard support #39 #92

Closed
wants to merge 17 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class StoryboardLabelSectionController: IGListSectionController, IGListSectionTy
}

func cellForItem(at index: Int) -> UICollectionViewCell {
//let cell = collectionContext!.dequeueReusableCell(of: LabelCell.self, for: self, at: index) as! LabelCell
let cell = collectionContext!.dequeueReusableCellFromStoryboard(withIdentifier: "cell", for: self, at: index) as! StoryboardCell
cell.textLabel.text = object
return cell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class StoryboardLoadMoreViewController: UIViewController, IGListAdapterDataSourc
adapter.scrollViewDelegate = self
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
collectionView.frame = view.bounds
}

//MARK: IGListAdapterDataSource
func objects(for listAdapter: IGListAdapter) -> [IGListDiffable] {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: space between //MARK: and function

var items: [IGListDiffable] = words as [IGListDiffable]
Expand Down
1 change: 0 additions & 1 deletion Example/IGListKitExamples/Views/StoryboardCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ import UIKit

class StoryboardCell: UICollectionViewCell {
@IBOutlet weak var textLabel: UILabel!

}
6 changes: 6 additions & 0 deletions IGListKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
821BC4C41DB8CEF800172ED0 /* IGTestStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 821BC4C21DB8CAE900172ED0 /* IGTestStoryboard.storyboard */; };
821BC4CB1DB8D60100172ED0 /* IGTestSingleStoryboardViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 821BC4C81DB8D5B200172ED0 /* IGTestSingleStoryboardViewController.m */; };
821BC4D01DB8D90900172ED0 /* IGTestStoryboardCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 821BC4CD1DB8D8C500172ED0 /* IGTestStoryboardCell.m */; };
821BC4D31DB981AB00172ED0 /* IGTestSingleStoryboardItemDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 821BC4D21DB981AB00172ED0 /* IGTestSingleStoryboardItemDataSource.m */; };
88144F071D870EDC007C7F66 /* IGListAdapterE2ETests.m in Sources */ = {isa = PBXBuildFile; fileRef = 88144EE21D870EDC007C7F66 /* IGListAdapterE2ETests.m */; };
88144F081D870EDC007C7F66 /* IGListAdapterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 88144EE31D870EDC007C7F66 /* IGListAdapterTests.m */; };
88144F091D870EDC007C7F66 /* IGListAdapterUpdaterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 88144EE41D870EDC007C7F66 /* IGListAdapterUpdaterTests.m */; };
Expand Down Expand Up @@ -138,6 +139,8 @@
821BC4C81DB8D5B200172ED0 /* IGTestSingleStoryboardViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IGTestSingleStoryboardViewController.m; sourceTree = "<group>"; };
821BC4CC1DB8D8C500172ED0 /* IGTestStoryboardCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IGTestStoryboardCell.h; sourceTree = "<group>"; };
821BC4CD1DB8D8C500172ED0 /* IGTestStoryboardCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IGTestStoryboardCell.m; sourceTree = "<group>"; };
821BC4D11DB9816E00172ED0 /* IGTestSingleStoryboardItemDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IGTestSingleStoryboardItemDataSource.h; sourceTree = "<group>"; };
821BC4D21DB981AB00172ED0 /* IGTestSingleStoryboardItemDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IGTestSingleStoryboardItemDataSource.m; sourceTree = "<group>"; };
88144EE21D870EDC007C7F66 /* IGListAdapterE2ETests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IGListAdapterE2ETests.m; sourceTree = "<group>"; };
88144EE31D870EDC007C7F66 /* IGListAdapterTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IGListAdapterTests.m; sourceTree = "<group>"; };
88144EE41D870EDC007C7F66 /* IGListAdapterUpdaterTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IGListAdapterUpdaterTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -295,6 +298,8 @@
88144F021D870EDC007C7F66 /* IGTestSingleItemDataSource.m */,
26271C881DAE94E40073E116 /* IGTestSingleNibItemDataSource.h */,
26271C891DAE94E40073E116 /* IGTestSingleNibItemDataSource.m */,
821BC4D11DB9816E00172ED0 /* IGTestSingleStoryboardItemDataSource.h */,
821BC4D21DB981AB00172ED0 /* IGTestSingleStoryboardItemDataSource.m */,
821BC4C71DB8D5B200172ED0 /* IGTestSingleStoryboardViewController.h */,
821BC4C81DB8D5B200172ED0 /* IGTestSingleStoryboardViewController.m */,
88144F031D870EDC007C7F66 /* IGTestStackedDataSource.h */,
Expand Down Expand Up @@ -690,6 +695,7 @@
26271C8C1DAE96740073E116 /* IGListSingleNibItemControllerTests.m in Sources */,
88144F101D870EDC007C7F66 /* IGListSingleItemControllerTests.m in Sources */,
88144F121D870EDC007C7F66 /* IGListWorkingRangeHandlerTests.m in Sources */,
821BC4D31DB981AB00172ED0 /* IGTestSingleStoryboardItemDataSource.m in Sources */,
88144F151D870EDC007C7F66 /* IGListTestSection.m in Sources */,
88144F1D1D870EDC007C7F66 /* IGTestSupplementarySource.m in Sources */,
88144F081D870EDC007C7F66 /* IGListAdapterTests.m in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions Source/IGListSingleSectionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ IGLK_SUBCLASSING_RESTRICTED
(usually "self") or the IGListAdapter. Pass in locally scoped objects or use weak references!
*/
- (instancetype)initWithStoryboardCellIdentifier:(NSString *)identifier
configureBlock:(IGListSingleSectionCellConfigureBlock)configureBlock
sizeBlock:(IGListSingleSectionCellSizeBlock)sizeBlock;
configureBlock:(IGListSingleSectionCellConfigureBlock)configureBlock
sizeBlock:(IGListSingleSectionCellSizeBlock)sizeBlock;

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

This is great!

An optional delegate that handles selection and deselection.
Expand Down
10 changes: 5 additions & 5 deletions Source/IGListSingleSectionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ - (instancetype)initWithNibName:(NSString *)nibName
}

- (instancetype)initWithStoryboardCellIdentifier:(NSString *)identifier
configureBlock:(IGListSingleSectionCellConfigureBlock)configureBlock
sizeBlock:(IGListSingleSectionCellSizeBlock)sizeBlock {
configureBlock:(IGListSingleSectionCellConfigureBlock)configureBlock
sizeBlock:(IGListSingleSectionCellSizeBlock)sizeBlock {
IGParameterAssert(identifier != nil);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's assert identifier.length > 0

Copy link
Contributor

Choose a reason for hiding this comment

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

identifier.length > 0

IGParameterAssert(configureBlock != nil);
Copy link
Contributor

Choose a reason for hiding this comment

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

Need an assert for sizeBlock too

Copy link
Author

@zhubofei zhubofei Oct 20, 2016

Choose a reason for hiding this comment

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

@rnystrom What about the other two init funcions initWithCellClass: & initWithNibName:? Do you want me to add assertions for sizeBlock to them as well?

if (self = [super init]) {
_identifier = identifier;
_identifier = [identifier copy];
_configureBlock = [configureBlock copy];
_sizeBlock = [sizeBlock copy];
}
Expand Down Expand Up @@ -89,8 +89,8 @@ - (UICollectionViewCell *)cellForItemAtIndex:(NSInteger)index {
atIndex:index];
} else if ([self.identifier length] > 0) {
cell = [collectionContext dequeueReusableCellFromStoryboardWithIdentifier:self.identifier
forSectionController:self
atIndex:index];
forSectionController:self
atIndex:index];
} else {
cell = [collectionContext dequeueReusableCellOfClass:self.cellClass forSectionController:self atIndex:index];
}
Expand Down
12 changes: 7 additions & 5 deletions Tests/IGListSingleStoryboardItemControllerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <XCTest/XCTest.h>

#import "IGTestStoryboardCell.h"
#import "IGTestSingleStoryboardItemDataSource.h"
#import "IGTestSingleStoryboardViewController.h"

#define genTestObject(k, v) [[IGTestObject alloc] initWithKey:k value:v]
Expand All @@ -21,6 +22,7 @@ @interface IGListSingleStoryboardItemControllerTests : XCTestCase
@property (nonatomic, strong) IGListCollectionView *collectionView;
@property (nonatomic, strong) IGListAdapter *adapter;
@property (nonatomic, strong) IGListAdapterUpdater *updater;
@property (nonatomic, strong) IGTestSingleStoryboardItemDataSource *dataSource;
@property (nonatomic, strong) IGTestSingleStoryboardViewController *viewController;

@end
Expand All @@ -32,8 +34,8 @@ - (void)setUp {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"IGTestStoryboard" bundle:[NSBundle bundleForClass:self.class]];
self.viewController = [storyboard instantiateViewControllerWithIdentifier:@"testVC"];
[self.viewController performSelectorOnMainThread:@selector(loadView) withObject:nil waitUntilDone:YES];
XCTAssertNotNil(self.viewController.collectionView, @"collectionView should be connected");
self.collectionView = self.viewController.collectionView;
self.dataSource = [[IGTestSingleStoryboardItemDataSource alloc] init];
self.updater = [[IGListAdapterUpdater alloc] init];
self.adapter = [[IGListAdapter alloc] initWithUpdater:self.updater viewController:self.viewController workingRangeSize:2];
}
Expand All @@ -46,9 +48,9 @@ - (void)tearDown {
}

- (void)setupWithObjects:(NSArray *)objects {
self.viewController.objects = objects;
self.adapter.collectionView = self.collectionView;
self.adapter.dataSource = self.viewController;
self.dataSource.objects = objects;
self.adapter.collectionView = self.viewController.collectionView;
self.adapter.dataSource = self.dataSource;
[self.collectionView layoutIfNeeded];
}

Expand Down Expand Up @@ -101,7 +103,7 @@ - (void)test_whenItemUpdated_thatCellIsConfigured {
genTestObject(@2, @"Bar"),
genTestObject(@3, @"Baz"),
]];
self.viewController.objects = @[
self.dataSource.objects = @[
genTestObject(@1, @"Foo"),
genTestObject(@2, @"Qux"), // new value
genTestObject(@3, @"Baz"),
Expand Down
20 changes: 20 additions & 0 deletions Tests/Objects/IGTestSingleStoryboardItemDataSource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 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 <Foundation/Foundation.h>

#import <IGListKit/IGListAdapterDataSource.h>

#import "IGTestObject.h"

@interface IGTestSingleStoryboardItemDataSource : NSObject <IGListAdapterDataSource>

@property (nonatomic, strong) NSArray <IGTestObject *> *objects;

@end
39 changes: 39 additions & 0 deletions Tests/Objects/IGTestSingleStoryboardItemDataSource.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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 "IGTestSingleStoryboardItemDataSource.h"

#import <IGListKit/IGListSingleSectionController.h>

#import "IGTestStoryboardCell.h"

@implementation IGTestSingleStoryboardItemDataSource

- (NSArray<id<IGListDiffable>> *)objectsForListAdapter:(IGListAdapter *)listAdapter {
return self.objects;
}

- (IGListSectionController<IGListSectionType> *)listAdapter:(IGListAdapter *)listAdapter sectionControllerForObject:(id)object
{
void (^configureBlock)(id, __kindof UICollectionViewCell *) = ^(IGTestObject *item, IGTestStoryboardCell *cell) {
cell.label.text = [item.value description];
};
CGSize (^sizeBlock)(id<IGListCollectionContext>) = ^CGSize(id<IGListCollectionContext> collectionContext) {
return CGSizeMake([collectionContext containerSize].width, 44);
};
return [[IGListSingleSectionController alloc] initWithStoryboardCellIdentifier:@"IGTestStoryboardCell"
configureBlock:configureBlock
sizeBlock:sizeBlock];
}

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

@end
18 changes: 9 additions & 9 deletions Tests/Objects/IGTestSingleStoryboardViewController.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//
// IGTestSingleStoryboardViewController.h
// IGListKit
//
// Created by Bofei Zhu on 10/20/16.
// Copyright © 2016 Instagram. All rights reserved.
//
/**
* 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>

Expand All @@ -14,9 +15,8 @@

#import "IGTestObject.h"

@interface IGTestSingleStoryboardViewController : UIViewController <IGListAdapterDataSource>
@interface IGTestSingleStoryboardViewController : UIViewController

@property (nonatomic, strong) NSArray <IGTestObject *> *objects;
@property (weak, nonatomic) IBOutlet IGListCollectionView *collectionView;

@end
22 changes: 0 additions & 22 deletions Tests/Objects/IGTestSingleStoryboardViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,4 @@ @interface IGTestSingleStoryboardViewController ()

@implementation IGTestSingleStoryboardViewController


- (NSArray<id<IGListDiffable>> *)objectsForListAdapter:(IGListAdapter *)listAdapter {
return self.objects;
}

- (IGListSectionController<IGListSectionType> *)listAdapter:(IGListAdapter *)listAdapter sectionControllerForObject:(id)object
{
void (^configureBlock)(id, __kindof UICollectionViewCell *) = ^(IGTestObject *item, IGTestStoryboardCell *cell) {
cell.label.text = [item.value description];
};
CGSize (^sizeBlock)(id<IGListCollectionContext>) = ^CGSize(id<IGListCollectionContext> collectionContext) {
return CGSizeMake([collectionContext containerSize].width, 44);
};
return [[IGListSingleSectionController alloc] initWithStoryboardCellIdentifier:@"IGTestStoryboardCell"
configureBlock:configureBlock
sizeBlock:sizeBlock];
}

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

@end
15 changes: 8 additions & 7 deletions Tests/Objects/IGTestStoryboardCell.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//
// IGTestStoryboardCell.h
// IGListKit
//
// Created by Bofei Zhu on 10/20/16.
// Copyright © 2016 Instagram. All rights reserved.
//
/**
* 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>

Expand Down
15 changes: 8 additions & 7 deletions Tests/Objects/IGTestStoryboardCell.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//
// IGTestStoryboardCell.m
// IGListKit
//
// Created by Bofei Zhu on 10/20/16.
// Copyright © 2016 Instagram. All rights reserved.
//
/**
* 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 "IGTestStoryboardCell.h"

Expand Down