Skip to content

Commit

Permalink
Swift name annotations
Browse files Browse the repository at this point in the history
Summary:
This adds `NS_SWIFT_NAME` annotations to all public API's to provide cleaner integration into Swift:

- Removes the need to prefix classes in Swift code, instead rely on Swift module name spacing
- Adds more argument labels to C function API's like `IGListDiff([], [], .equality)` => `ListDiff(oldArray: [], newArray: [], option: .equality)`

While this is a large API change it should be as easy as:

- Find and replace `(IGList)([^K])` to `List$2` in Xcode with a scope set to Swift
- Build and follow compiler's auto fix corrections for C API's or any missed renames

I have not updated the documentation to reflect this yet, I am totally willing to do so but before I sink that amount of time into it I wanted to see if the Instagram team is even open to this change!

- [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)
- [ ] I have updated the documentation
Closes #593

Reviewed By: jessesquires

Differential Revision: D5028039

Pulled By: rnystrom

fbshipit-source-id: b473d874a1f9574e56b2ebaabd5b73d1b57d4bab
  • Loading branch information
Robert Payne authored and facebook-github-bot committed May 9, 2017
1 parent a4dfe96 commit 40625f8
Show file tree
Hide file tree
Showing 88 changed files with 626 additions and 602 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ This release closes the [3.0.0 milestone](https://github.com/Instagram/IGListKit

### Breaking Changes

- Added Swift annotation names which remove `IG` prefixes from class names, C functions, and other APIs. Note, this only affects Swift clients. [Robert Payne](https://github.com/robertjpayne) [(#593)](https://github.com/Instagram/IGListKit/pull/593)

Example:

```swift
// OLD
class MySectionController : IGListSectionController { ... }

// NEW
class MySectionController : ListSectionController { ... }

// OLD
IGListDiff([], [], .equality)

// NEW
ListDiff(oldArray: [], newArray: [], .equality)

```

- Updated `didSelect` delegate call in `IGListSingleSectionControllerDelegate` to include object. [Sherlouk](https://github.com/Sherlouk) [(#397)](https://github.com/Instagram/IGListKit/pull/397)

```objc
Expand Down
6 changes: 3 additions & 3 deletions Examples/Examples-iOS/IGListKitExamples/Models/FeedItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import IGListKit

final class FeedItem: IGListDiffable {
final class FeedItem: ListDiffable {

let pk: Int
let user: User
Expand All @@ -26,13 +26,13 @@ final class FeedItem: IGListDiffable {
self.comments = comments
}

//MARK: IGListDiffable
//MARK: ListDiffable

func diffIdentifier() -> NSObjectProtocol {
return pk as NSObjectProtocol
}

func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
guard self !== object else { return true }
guard let object = object as? FeedItem else { return false }
return user.isEqual(toDiffableObject: object.user) && comments == object.comments
Expand Down
4 changes: 2 additions & 2 deletions Examples/Examples-iOS/IGListKitExamples/Models/Month.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ final class Month {

}

extension Month: IGListDiffable {
extension Month: ListDiffable {

func diffIdentifier() -> NSObjectProtocol {
return name as NSObjectProtocol
}

func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ final class SelectionModel: NSObject {

}

extension SelectionModel: IGListDiffable {
extension SelectionModel: ListDiffable {

func diffIdentifier() -> NSObjectProtocol {
return self
}

func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
return isEqual(object)
}

Expand Down
6 changes: 3 additions & 3 deletions Examples/Examples-iOS/IGListKitExamples/Models/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import IGListKit

final class User: IGListDiffable {
final class User: ListDiffable {

let pk: Int
let name: String
Expand All @@ -26,13 +26,13 @@ final class User: IGListDiffable {
self.handle = handle
}

//MARK: IGListDiffable
//MARK: ListDiffable

func diffIdentifier() -> NSObjectProtocol {
return pk as NSObjectProtocol
}

func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
guard self !== object else { return true }
guard let object = object as? User else { return false }
return name == object.name && handle == object.handle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ final class DayViewModel {

}

extension DayViewModel: IGListDiffable {
extension DayViewModel: ListDiffable {

func diffIdentifier() -> NSObjectProtocol {
return day as NSObjectProtocol
}

func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
if self === object { return true }
guard let object = object as? DayViewModel else { return false }
return today == object.today && selected == object.selected && appointments == object.appointments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ final class MonthTitleViewModel {

}

extension MonthTitleViewModel: IGListDiffable {
extension MonthTitleViewModel: ListDiffable {

func diffIdentifier() -> NSObjectProtocol {
return name as NSObjectProtocol
}

func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
if self === object { return true }
guard object is MonthTitleViewModel else { return false }
// name is checked in the diffidentifier, so we can assume its equal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ final class DemoItem: NSObject {

}

extension DemoItem: IGListDiffable {
extension DemoItem: ListDiffable {

func diffIdentifier() -> NSObjectProtocol {
return name as NSObjectProtocol
}

func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
if self === object { return true }
guard let object = object as? DemoItem else { return false }
return controllerClass == object.controllerClass && controllerIdentifier == object.controllerIdentifier
}

}

final class DemoSectionController: IGListSectionController {
final class DemoSectionController: ListSectionController {

private var object: DemoItem?

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

final class DisplaySectionController: IGListSectionController, IGListDisplayDelegate {
final class DisplaySectionController: ListSectionController, ListDisplayDelegate {

override init() {
super.init()
Expand All @@ -37,21 +37,21 @@ final class DisplaySectionController: IGListSectionController, IGListDisplayDele
return cell
}

// MARK: IGListDisplayDelegate
// MARK: ListDisplayDelegate

func listAdapter(_ listAdapter: IGListAdapter, willDisplay sectionController: IGListSectionController) {
func listAdapter(_ listAdapter: ListAdapter, willDisplay sectionController: ListSectionController) {
print("Will display section \(self.sectionIndex)")
}

func listAdapter(_ listAdapter: IGListAdapter, willDisplay sectionController: IGListSectionController, cell: UICollectionViewCell, at index: Int) {
func listAdapter(_ listAdapter: ListAdapter, willDisplay sectionController: ListSectionController, cell: UICollectionViewCell, at index: Int) {
print("Did will display cell \(index) in section \(self.sectionIndex)")
}

func listAdapter(_ listAdapter: IGListAdapter, didEndDisplaying sectionController: IGListSectionController) {
func listAdapter(_ listAdapter: ListAdapter, didEndDisplaying sectionController: ListSectionController) {
print("Did end displaying section \(self.sectionIndex)")
}

func listAdapter(_ listAdapter: IGListAdapter, didEndDisplaying sectionController: IGListSectionController, cell: UICollectionViewCell, at index: Int) {
func listAdapter(_ listAdapter: ListAdapter, didEndDisplaying sectionController: ListSectionController, cell: UICollectionViewCell, at index: Int) {
print("Did end displaying cell \(index) in section \(self.sectionIndex)")
}

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 {
final class EmbeddedSectionController: ListSectionController {

private var number: Int?

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

final class ExpandableSectionController: IGListSectionController {
final class ExpandableSectionController: ListSectionController {

private var expanded = false
private var object: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import IGListKit

final class FeedItemSectionController: IGListSectionController, IGListSupplementaryViewSource {
final class FeedItemSectionController: ListSectionController, ListSupplementaryViewSource {

private var feedItem: FeedItem!

Expand Down Expand Up @@ -43,7 +43,7 @@ final class FeedItemSectionController: IGListSectionController, IGListSupplement
feedItem = object as? FeedItem
}

// MARK: IGListSupplementaryViewSource
// MARK: ListSupplementaryViewSource

func supportedElementKinds() -> [String] {
return [UICollectionElementKindSectionHeader]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ final class GridItem: NSObject {

}

extension GridItem: IGListDiffable {
extension GridItem: ListDiffable {

func diffIdentifier() -> NSObjectProtocol {
return self
}

func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
return self === object ? true : self.isEqual(object)
}

}

final class GridSectionController: IGListSectionController {
final class GridSectionController: ListSectionController {

private var object: GridItem?

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

final class HorizontalSectionController: IGListSectionController, IGListAdapterDataSource {
final class HorizontalSectionController: ListSectionController, ListAdapterDataSource {

private var number: Int?

lazy var adapter: IGListAdapter = {
let adapter = IGListAdapter(updater: IGListAdapterUpdater(),
lazy var adapter: ListAdapter = {
let adapter = ListAdapter(updater: ListAdapterUpdater(),
viewController: self.viewController)
adapter.dataSource = self
return adapter
Expand All @@ -40,18 +40,18 @@ final class HorizontalSectionController: IGListSectionController, IGListAdapterD
number = object as? Int
}

//MARK: IGListAdapterDataSource
//MARK: ListAdapterDataSource

func objects(for listAdapter: IGListAdapter) -> [IGListDiffable] {
func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
guard let number = number else { return [] }
return (0..<number).map { $0 as IGListDiffable }
return (0..<number).map { $0 as ListDiffable }
}

func listAdapter(_ listAdapter: IGListAdapter, sectionControllerFor object: Any) -> IGListSectionController {
func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
return EmbeddedSectionController()
}

func emptyView(for listAdapter: IGListAdapter) -> UIView? {
func emptyView(for listAdapter: ListAdapter) -> UIView? {
return nil
}

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

final class LabelSectionController: IGListSectionController {
final class LabelSectionController: ListSectionController {

private var object: String?

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

import IGListKit

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

private var value: Int = 0

Expand All @@ -27,7 +27,7 @@ final class ListeningSectionController: IGListSectionController, IncrementListen
cell.text = "Section: \(self.sectionIndex), value: \(value)"
}

// MARK: IGListSectionController Overrides
// MARK: ListSectionController Overrides

override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 55)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import UIKit
import IGListKit

final class MonthSectionController: IGListBindingSectionController<IGListDiffable>, IGListBindingSectionControllerDataSource, IGListBindingSectionControllerSelectionDelegate {
final class MonthSectionController: ListBindingSectionController<ListDiffable>, ListBindingSectionControllerDataSource, ListBindingSectionControllerSelectionDelegate {

private var selectedDay: Int = -1

Expand All @@ -25,15 +25,15 @@ final class MonthSectionController: IGListBindingSectionController<IGListDiffabl
selectionDelegate = self
}

// MARK: IGListBindingSectionControllerDataSource
// MARK: ListBindingSectionControllerDataSource

func sectionController(_ sectionController: IGListBindingSectionController<IGListDiffable>, viewModelsFor object: Any) -> [IGListDiffable] {
func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, viewModelsFor object: Any) -> [ListDiffable] {
guard let month = object as? Month else { return [] }

let date = Date()
let today = Calendar.current.component(.day, from: date)

var viewModels = [IGListDiffable]()
var viewModels = [ListDiffable]()

viewModels.append(MonthTitleViewModel(name: month.name))

Expand All @@ -54,7 +54,7 @@ final class MonthSectionController: IGListBindingSectionController<IGListDiffabl
return viewModels
}

func sectionController(_ sectionController: IGListBindingSectionController<IGListDiffable>, cellForViewModel viewModel: Any, at index: Int) -> UICollectionViewCell {
func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, cellForViewModel viewModel: Any, at index: Int) -> UICollectionViewCell {
let cellClass: AnyClass
if viewModel is DayViewModel {
cellClass = CalendarDayCell.self
Expand All @@ -66,7 +66,7 @@ final class MonthSectionController: IGListBindingSectionController<IGListDiffabl
return collectionContext?.dequeueReusableCell(of: cellClass, for: self, at: index) ?? UICollectionViewCell()
}

func sectionController(_ sectionController: IGListBindingSectionController<IGListDiffable>, sizeForViewModel viewModel: Any, at index: Int) -> CGSize {
func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, sizeForViewModel viewModel: Any, at index: Int) -> CGSize {
guard let width = collectionContext?.containerSize.width else { return .zero }
if viewModel is DayViewModel {
let square = width / 7.0
Expand All @@ -78,9 +78,9 @@ final class MonthSectionController: IGListBindingSectionController<IGListDiffabl
}
}

// MARK: IGListBindingSectionControllerSelectionDelegate
// MARK: ListBindingSectionControllerSelectionDelegate

func sectionController(_ sectionController: IGListBindingSectionController<IGListDiffable>, didSelectItemAt index: Int, viewModel: Any) {
func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, didSelectItemAt index: Int, viewModel: Any) {
guard let dayViewModel = viewModel as? DayViewModel else { return }
if dayViewModel.day == selectedDay {
selectedDay = -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protocol RemoveSectionControllerDelegate: class {
func removeSectionControllerWantsRemoved(_ sectionController: RemoveSectionController)
}

final class RemoveSectionController: IGListSectionController, RemoveCellDelegate {
final class RemoveSectionController: ListSectionController, RemoveCellDelegate {

weak var delegate: RemoveSectionControllerDelegate?
private var number: Int?
Expand Down
Loading

0 comments on commit 40625f8

Please sign in to comment.