27
@JOHNSUNDELL BACKEND-DRIVEN UIS John Sundell Lead iOS Developer, Spotify

Backend-driven UIs - #Pragma Conference 2016

Embed Size (px)

Citation preview

Page 1: Backend-driven UIs - #Pragma Conference 2016

@JOHNSUNDELL

BACKEND-DRIVEN UIS

John Sundell Lead iOS Developer, Spotify

Page 2: Backend-driven UIs - #Pragma Conference 2016

! "

Page 3: Backend-driven UIs - #Pragma Conference 2016

! "#

CocoaPods/Carthage

Fastlane

Swift

Protocol oriented programming

Model-View-ViewModel

Promises / Operations / Rx

Page 4: Backend-driven UIs - #Pragma Conference 2016

UI

Page 5: Backend-driven UIs - #Pragma Conference 2016
Page 6: Backend-driven UIs - #Pragma Conference 2016

// MARK: - UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { return 2 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section == 0 { return model.unreadMessages.count } return model.readMessages.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "message", for: indexPath) let message: Message if indexPath.section == 0 { message = model.unreadMessages[indexPath.row] } else { message = model.readMessages[indexPath.row] } cell.textLabel?.text = message.subject cell.detailTextLabel?.text = "From: \(message.sender)" return cell }

Page 7: Backend-driven UIs - #Pragma Conference 2016

UITableViewDelegate

UITableViewDataSource

Caching

UIViewController subclass

JSON parsing

Integrate view controller with the rest of the app

Setup UITableView

Register UITableViewCell classUITableViewCell subclass

Handle network errors

Handle slow connections

OfflinePerform HTTP request

View Model

City Model

BACKEND

Page 8: Backend-driven UIs - #Pragma Conference 2016
Page 9: Backend-driven UIs - #Pragma Conference 2016
Page 10: Backend-driven UIs - #Pragma Conference 2016

Backend response

[ Image(“Tokyo”), Image(“Gothenburg”), Row(“Berlin”, “Germany”), Row(“Beijing”, “China”), Row(“Paris”, “France”), Row(“San Francisco”, “USA”), User(“Julia”), User(“Mathew”), User(“Caroline”), User(“David”), Row(“Athens”, “Greece”), Row(“Oslo”, “Norway”), Row(“Stockholm”, “Sweden”)

]

Page 11: Backend-driven UIs - #Pragma Conference 2016
Page 12: Backend-driven UIs - #Pragma Conference 2016

1. SHARED DATA MODEL(FULLY SERIALIZABLE)

Page 13: Backend-driven UIs - #Pragma Conference 2016

struct Image { var url: URL }

struct City { var name: String var country: String }struct User {

var name: String var imageUrl: URL }

Page 14: Backend-driven UIs - #Pragma Conference 2016

ComponentModel

ComponentModel

ComponentModel

ViewModel

Page 15: Backend-driven UIs - #Pragma Conference 2016

struct { var title: String var subtitle: String var imageUrl: URL ... }

ComponentModel

Page 16: Backend-driven UIs - #Pragma Conference 2016

2. PROTOCOL-ORIENTED VIEWS

Page 17: Backend-driven UIs - #Pragma Conference 2016

Components

UICollectionView

Page 18: Backend-driven UIs - #Pragma Conference 2016

Components

UICollectionView

protocol Component {var view: UIView? { get }

func loadView()

}func configure(withModel: ComponentModel)

var preferredViewSize: CGSize { get }var layoutTraits: [LayoutTrait] { get }

UICollectionViewLayout

Page 19: Backend-driven UIs - #Pragma Conference 2016

3. A DECLARATIVE API

Page 20: Backend-driven UIs - #Pragma Conference 2016

class ViewController: UIViewController { init(json: Data) }

Page 21: Backend-driven UIs - #Pragma Conference 2016

class ViewController: UIViewController { init(json: Data) }

$

Reloads Actions Local dataInteractions

Page 22: Backend-driven UIs - #Pragma Conference 2016

ContentOperationsViewModelBuilder Content

BACKEND

LOCAL CODE

Page 23: Backend-driven UIs - #Pragma Conference 2016

1. SHARED DATA MODEL

2. PROTOCOL-ORIENTED VIEWS

3. A DECLARATIVE API

Page 24: Backend-driven UIs - #Pragma Conference 2016
Page 25: Backend-driven UIs - #Pragma Conference 2016

DEMO

Page 26: Backend-driven UIs - #Pragma Conference 2016

OPEN SOURCE! %

Page 27: Backend-driven UIs - #Pragma Conference 2016

GITHUB.COM/JOHNSUNDELL

@JOHNSUNDELL