Enum Factories And Generic Data Sources

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Enum Factories And Generic Data Sources

Presented at CocoaHeads Montreal on November 16th, 2017.

Olivier Collet

November 16, 2017

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

12 slides
  1. IT STARTS WITH A PAGED REQUEST I’ll spare you the

    details. The most important thing is this: struct Cursor { let page: Int let perPage: Int let parameters: [String: Any]? }
  2. LET’S MOVE TO THE STUFF THE DATA SOURCE • It

    uses generics to handle photos and collections • It uses a factory to generate cursors and requests protocol PagedDataSourceFactory { func initialCursor() -> Cursor func request(with cursor: Cursor) -> PagedRequest }
  3. SO WHAT ABOUT THE FACTORY? THAT’S WHERE IT GETS REALLY

    enum PhotosDataSourceFactory: PagedDataSourceFactory { case photos(order: GetPhotosRequest.Order) case trending case search(query: String) case collection(identifier: String, curated: Bool) }
  4. WHAT ABOUT THAT PROTOCOL? IT’S GETTING func initialCursor() -> PagedRequest.Cursor

    { switch self { case .photos(let order): return GetPhotosRequest.cursor(with: order, page: 1, perPage: 20) case .trending: return GetHomePhotosRequest.cursor(with: nil) case .search(let query): return SearchPhotosRequest.cursor(with: query, page: 1, perPage: 20) ... } func request(with cursor: PagedRequest.Cursor) -> PagedRequest { switch self { case .photos(let order): return GetPhotosRequest(orderBy: order, page: cursor.page, perPage: cursor.perPage) case .trending: return GetHomePhotosRequest(with: cursor) case .search(let query): return SearchPhotosRequest(with: query, page: cursor.page, perPage: cursor.perPage) ... }
  5. OK, BUT HOW TO USE THIS? The PhotosViewController uses a

    data source: var dataSource: PagedDataSource<Photo> Which is set as easily as this: photosViewController.dataSource = PhotosDataSourceFactory.search(query:”summer").dataSource

More Decks by Olivier Collet

Other Decks in Programming