From ad416ecccdab6fb921956e113aa31f02d861cde7 Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Wed, 19 Apr 2017 16:10:37 -0700 Subject: [PATCH] Remove refs to IGListSectionType, fix TV app example Summary: - Clean up docs - Remove `IGListSectionType` and `IGListCollectionView` - Fix TV app example Issue fixed: #675 - [x] All tests pass. Demo project builds and runs. Closes https://github.com/Instagram/IGListKit/pull/676 Differential Revision: D4915634 Pulled By: rnystrom fbshipit-source-id: 60eb1f1e5ece7fe68f6bf44b465bd5379615d716 --- .../CarouselSectionController.swift | 12 ++++++------ .../DemoSectionController.swift | 12 ++++++------ .../HorizontalSectionController.swift | 12 ++++++------ .../LabelSectionController.swift | 12 ++++++------ Guides/Best Practices and FAQ.md | 2 +- Guides/Getting Started.md | 16 +++++++++++----- 6 files changed, 36 insertions(+), 30 deletions(-) diff --git a/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/CarouselSectionController.swift b/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/CarouselSectionController.swift index 1f9b161e..488a1abf 100644 --- a/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/CarouselSectionController.swift +++ b/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/CarouselSectionController.swift @@ -15,7 +15,7 @@ import UIKit import IGListKit -final class CarouselSectionController: IGListSectionController, IGListSectionType { +final class CarouselSectionController: IGListSectionController { var number: Int? @@ -24,11 +24,11 @@ final class CarouselSectionController: IGListSectionController, IGListSectionTyp self.inset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10) } - func numberOfItems() -> Int { + override func numberOfItems() -> Int { return 1 } - func sizeForItem(at index: Int) -> CGSize { + override func sizeForItem(at index: Int) -> CGSize { let height = collectionContext?.containerSize.height ?? 0 let aspectRatio: CGFloat = 0.75 // 3:4 let width = height * aspectRatio @@ -36,17 +36,17 @@ final class CarouselSectionController: IGListSectionController, IGListSectionTyp return CGSize(width: width, height: height) } - func cellForItem(at index: Int) -> UICollectionViewCell { + override func cellForItem(at index: Int) -> UICollectionViewCell { let cell = collectionContext!.dequeueReusableCell(withNibName: "CarouselCell", bundle: nil, for: self, at: index) as! CarouselCell let value = number ?? 0 cell.titleLabel.text = "#\(value + 1)" return cell } - func didUpdate(to object: Any) { + override func didUpdate(to object: Any) { number = object as? Int } - func didSelectItem(at index: Int) {} + override func didSelectItem(at index: Int) {} } diff --git a/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/DemoSectionController.swift b/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/DemoSectionController.swift index 8ba7c6b6..a27047a7 100644 --- a/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/DemoSectionController.swift +++ b/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/DemoSectionController.swift @@ -31,7 +31,7 @@ final class DemoItem: NSObject { } -final class DemoSectionController: IGListSectionController, IGListSectionType { +final class DemoSectionController: IGListSectionController { var object: DemoItem? @@ -40,26 +40,26 @@ final class DemoSectionController: IGListSectionController, IGListSectionType { inset = UIEdgeInsets(top: 0, left: 50, bottom: 10, right: 0) } - func numberOfItems() -> Int { + override func numberOfItems() -> Int { return 1 } - func sizeForItem(at index: Int) -> CGSize { + override func sizeForItem(at index: Int) -> CGSize { let itemWidth = (collectionContext!.containerSize.width / 2) - inset.left return CGSize(width: itemWidth, height: 100) } - func cellForItem(at index: Int) -> UICollectionViewCell { + override func cellForItem(at index: Int) -> UICollectionViewCell { let cell = collectionContext!.dequeueReusableCell(of: DemoCell.self, for: self, at: index) as! DemoCell cell.label.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) diff --git a/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/HorizontalSectionController.swift b/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/HorizontalSectionController.swift index b519bf3e..a5a982a6 100644 --- a/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/HorizontalSectionController.swift +++ b/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/HorizontalSectionController.swift @@ -15,7 +15,7 @@ import UIKit import IGListKit -final class HorizontalSectionController: IGListSectionController, IGListSectionType, IGListAdapterDataSource { +final class HorizontalSectionController: IGListSectionController, IGListAdapterDataSource { var number: Int? @@ -32,25 +32,25 @@ final class HorizontalSectionController: IGListSectionController, IGListSectionT self.inset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0) } - func numberOfItems() -> Int { + override 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: 340) } - 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) {} + override func didSelectItem(at index: Int) {} // MARK: IGListAdapterDataSource diff --git a/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/LabelSectionController.swift b/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/LabelSectionController.swift index ed3bfd2c..46a866a1 100644 --- a/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/LabelSectionController.swift +++ b/Examples/Examples-tvOS/IGListKitExamples/SectionControllers/LabelSectionController.swift @@ -15,28 +15,28 @@ import UIKit import IGListKit -final class LabelSectionController: IGListSectionController, IGListSectionType { +final class LabelSectionController: IGListSectionController { var object: String? - func numberOfItems() -> Int { + override 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.label.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) {} + override func didSelectItem(at index: Int) {} } diff --git a/Guides/Best Practices and FAQ.md b/Guides/Best Practices and FAQ.md index 10c262e8..88d75f1e 100644 --- a/Guides/Best Practices and FAQ.md +++ b/Guides/Best Practices and FAQ.md @@ -4,7 +4,7 @@ This guide provides notes and details on best practices in using `IGListKit`, ge ## Best Practices -- We recommend adding an assert to check [`-isKindOfClass:`](https://developer.apple.com/reference/objectivec/1418956-nsobject/1418511-iskindofclass) on the object you receive in [`-didUpdateToObject:`](https://instagram.github.io/IGListKit/Protocols/IGListSectionType.html#/c:objc(pl)IGListSectionType(im)didUpdateToObject:) in your section controllers. +- We recommend adding an assert to check [`-isKindOfClass:`](https://developer.apple.com/reference/objectivec/1418956-nsobject/1418511-iskindofclass) on the object you receive in [`-didUpdateToObject:`](https://github.com/Instagram/IGListKit/blob/master/Source/IGListSectionController.h#L63-L72) in your section controllers. This makes it easy to track down easily-overlooked mistakes in your [`IGListAdapaterDataSource`](https://instagram.github.io/IGListKit/Protocols/IGListAdapterDataSource.html#/c:objc(pl)IGListAdapterDataSource(im)listAdapter:sectionControllerForObject:) implementation. If this assert is ever hit, that means `IGListKit` has sent your section controller the incorrect type of object. This would only happen if your objects provide *non-unique* diff identifiers. diff --git a/Guides/Getting Started.md b/Guides/Getting Started.md index efd6c774..a1e41d8f 100644 --- a/Guides/Getting Started.md +++ b/Guides/Getting Started.md @@ -8,23 +8,29 @@ After installing `IGListKit`, creating a new list is easy. ### Creating a section controller -Creating a new section controller is simple. You subclass `IGListSectionController` and conform to the `IGListSectionType` protocol. Once you conform to `IGListSectionType`, the compiler will make sure you implement all of the required methods. +Creating a new section controller is simple. Subclass `IGListSectionController` and override at least `cellForItemAtIndex:` and `sizeForItemAtIndex:`. Take a look at [LabelSectionController](https://raw.githubusercontent.com/Instagram/IGListKit/master/Examples/Examples-iOS/IGListKitExamples/SectionControllers/LabelSectionController.swift) for an example section controller that handles a `String` and configures a single cell with a `UILabel`. ```swift -class LabelSectionController: IGListSectionController, IGListSectionType { - // ... +class LabelSectionController: IGListSectionController { + override func sizeForItem(at index: Int) -> CGSize { + return CGSize(width: collectionContext!.containerSize.width, height: 55) + } + + override func cellForItem(at index: Int) -> UICollectionViewCell { + return collectionContext!.dequeueReusableCell(of: MyCell.self, for: self, at: index) + } } ``` ### Creating the UI -After creating at least one section controller, you must create an `IGListCollectionView` and `IGListAdapter`. +After creating at least one section controller, you must create a `UICollectionView` and `IGListAdapter`. ```swift let layout = UICollectionViewFlowLayout() -let collectionView = IGListCollectionView(frame: .zero, collectionViewLayout: layout) +let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) let updater = IGListAdapterUpdater() let adapter = IGListAdapter(updater: updater, viewController: self, workingRangeSize: 0)