Skip to content

Commit

Permalink
Remove IGListSectionType
Browse files Browse the repository at this point in the history
Summary:
Removing the `IGListSectionType` protocol and adding default implementations into `IGListSectionController`.

- `numberOfItems` returns 1
- `cellForItemAtIndex:` asserts (have to return a cell)
- `didUpdateToObject:` no-ops
- `didSelectItemAtIndex:` no-ops

Fixes #168

Reviewed By: jessesquires

Differential Revision: D4909585

fbshipit-source-id: 8816702504e3fc0683868914ff4dd20e4af7c166
  • Loading branch information
Ryan Nystrom authored and facebook-github-bot committed Apr 19, 2017
1 parent 4441bd8 commit 3102852
Show file tree
Hide file tree
Showing 76 changed files with 1,390 additions and 1,525 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ This release closes the [3.0.0 milestone](https://github.com/Instagram/IGListKit
- Fix a crash when inserting or deleting from the same index within the same batch-update application. [Ryan Nystrom](https://github.com/rnystrom) [(#616)](https://github.com/Instagram/IGListKit/pull/616)
- `IGListSectionType` protocol was removed and its methods were absorted into the `IGListSectionController` base class with default implementations. [Ryan Nystrom](https://github.com/rnystrom) (tbd)
2.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,25 @@ extension DemoItem: IGListDiffable {

}

final class DemoSectionController: IGListSectionController, IGListSectionType {
final class DemoSectionController: IGListSectionController {

private var object: DemoItem?

func numberOfItems() -> Int {
return 1
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 55)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: LabelCell.self, for: self, at: index) as! LabelCell
cell.text = object?.name
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
self.object = object as? DemoItem
}

func didSelectItem(at index: Int) {
override func didSelectItem(at index: Int) {
if let identifier = object?.controllerIdentifier {
let storyboard = UIStoryboard(name: "Demo", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: identifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,29 @@
import UIKit
import IGListKit

final class DisplaySectionController: IGListSectionController, IGListSectionType, IGListDisplayDelegate {
final class DisplaySectionController: IGListSectionController, IGListDisplayDelegate {

override init() {
super.init()
displayDelegate = self
inset = UIEdgeInsets(top: 0, left: 0, bottom: 30, right: 0)
}

func numberOfItems() -> Int {
override func numberOfItems() -> Int {
return 4
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 55)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: LabelCell.self, for: self, at: index) as! LabelCell
let section = collectionContext!.section(for: self)
cell.text = "Section \(section), cell \(index)"
return cell
}

func didUpdate(to object: Any) {}

func didSelectItem(at index: Int) {}

// MARK: IGListDisplayDelegate

func listAdapter(_ listAdapter: IGListAdapter, willDisplay sectionController: IGListSectionController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import UIKit
import IGListKit

final class EmbeddedSectionController: IGListSectionController, IGListSectionType {
final class EmbeddedSectionController: IGListSectionController {

private var number: Int?

Expand All @@ -24,27 +24,21 @@ final class EmbeddedSectionController: IGListSectionController, IGListSectionTyp
self.inset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
}

func numberOfItems() -> Int {
return 1
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
let height = collectionContext?.containerSize.height ?? 0
return CGSize(width: height, height: height)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: CenterLabelCell.self, for: self, at: index) as! CenterLabelCell
let value = number ?? 0
cell.text = "\(value + 1)"
cell.backgroundColor = UIColor(red: 237/255.0, green: 73/255.0, blue: 86/255.0, alpha: 1)
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
number = object as? Int
}

func didSelectItem(at index: Int) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,28 @@
import UIKit
import IGListKit

final class ExpandableSectionController: IGListSectionController, IGListSectionType {
final class ExpandableSectionController: IGListSectionController {

private var expanded = false
private var object: String?

func numberOfItems() -> Int {
return 1
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
let width = collectionContext!.containerSize.width
let height = expanded ? LabelCell.textHeight(object ?? "", width: width) : LabelCell.singleLineHeight
return CGSize(width: width, height: height)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: LabelCell.self, for: self, at: index) as! LabelCell
cell.text = object
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
self.object = object as? String
}

func didSelectItem(at index: Int) {
override func didSelectItem(at index: Int) {
expanded = !expanded
UIView.animate(withDuration: 0.5,
delay: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import IGListKit

final class FeedItemSectionController: IGListSectionController, IGListSectionType, IGListSupplementaryViewSource {
final class FeedItemSectionController: IGListSectionController, IGListSupplementaryViewSource {

private var feedItem: FeedItem!

Expand All @@ -23,28 +23,26 @@ final class FeedItemSectionController: IGListSectionController, IGListSectionTyp
supplementaryViewSource = self
}

// MARK: IGlistSectionType
// MARK: IGListSectionController Overrides

func numberOfItems() -> Int {
override func numberOfItems() -> Int {
return feedItem.comments.count
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 55)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext?.dequeueReusableCell(of: LabelCell.self, for: self, at: index) as! LabelCell
cell.text = feedItem.comments[index]
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
feedItem = object as? FeedItem
}

func didSelectItem(at index: Int) {}

// MARK: IGListSupplementaryViewSource

func supportedElementKinds() -> [String] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension GridItem: IGListDiffable {

}

final class GridSectionController: IGListSectionController, IGListSectionType {
final class GridSectionController: IGListSectionController {

private var object: GridItem?

Expand All @@ -49,27 +49,25 @@ final class GridSectionController: IGListSectionController, IGListSectionType {
self.minimumLineSpacing = 1
}

func numberOfItems() -> Int {
override func numberOfItems() -> Int {
return object?.itemCount ?? 0
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
let width = collectionContext?.containerSize.width ?? 0
let itemSize = floor(width / 4)
return CGSize(width: itemSize, height: itemSize)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: CenterLabelCell.self, for: self, at: index) as! CenterLabelCell
cell.text = "\(index + 1)"
cell.backgroundColor = object?.color
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
self.object = object as? GridItem
}

func didSelectItem(at index: Int) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import UIKit
import IGListKit

final class HorizontalSectionController: IGListSectionController, IGListSectionType, IGListAdapterDataSource {
final class HorizontalSectionController: IGListSectionController, IGListAdapterDataSource {

private var number: Int?

Expand All @@ -27,26 +27,20 @@ final class HorizontalSectionController: IGListSectionController, IGListSectionT
return adapter
}()

func numberOfItems() -> Int {
return 1
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 100)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: EmbeddedCollectionViewCell.self, for: self, at: index) as! EmbeddedCollectionViewCell
adapter.collectionView = cell.collectionView
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
number = object as? Int
}

func didSelectItem(at index: Int) {}

//MARK: IGListAdapterDataSource

func objects(for listAdapter: IGListAdapter) -> [IGListDiffable] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,22 @@
import UIKit
import IGListKit

final class LabelSectionController: IGListSectionController, IGListSectionType {
final class LabelSectionController: IGListSectionController {

private var object: String?

func numberOfItems() -> Int {
return 1
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 55)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: LabelCell.self, for: self, at: index) as! LabelCell
cell.text = object
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
self.object = String(describing: object)
}

func didSelectItem(at index: Int) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import IGListKit

final class ListeningSectionController: IGListSectionController, IGListSectionType, IncrementListener {
final class ListeningSectionController: IGListSectionController, IncrementListener {

private var value: Int = 0

Expand All @@ -28,25 +28,18 @@ final class ListeningSectionController: IGListSectionController, IGListSectionTy
cell.text = "Section: \(section), value: \(value)"
}

// MARK: IGListSectionType
// MARK: IGListSectionController Overrides

func numberOfItems() -> Int {
return 1
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 55)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: LabelCell.self, for: self, at: index) as! LabelCell
configureCell(cell: cell)
return cell
}

func didUpdate(to object: Any) {}
func didSelectItem(at index: Int) {}

// MARK: IncrementListener

func didIncrement(announcer: IncrementAnnouncer, value: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

#import <IGListKit/IGListKit.h>

@interface PostSectionController : IGListSectionController <IGListSectionType>
@interface PostSectionController : IGListSectionController

@end
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ @implementation PostSectionController {
Post *_post;
}

#pragma mark - IGListSectionType
#pragma mark - IGListSectionController Overrides

- (NSInteger)numberOfItems {
return cellsBeforeComments + _post.comments.count;
Expand Down Expand Up @@ -69,6 +69,4 @@ - (void)didUpdateToObject:(id)object {
_post = object;
}

- (void)didSelectItemAtIndex:(NSInteger)index {}

@end
Loading

0 comments on commit 3102852

Please sign in to comment.