Rx in the real world. 1 Rob Ciolli

Similar documents
Chapter 22 TableView TableView. TableView ios. ViewController. Cell TableViewCell TableView

App. Chapter 19 App. App (ViewController) App. Single View Application Single View Application View. (View Controller)

Cocoa Touch Best Practices

Tables. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

Collection Views. Dr. Sarah Abraham

} override func didreceivememorywarning() { 26 super.didreceivememorywarning() 27 } 28 } Pause Stop

Implementing UI Designs in Interface Builder

Generic Programming with Protocol in Swift. ios

UI Design and Storyboarding

Lecture 8 Demo Code: Cassini Multithreading

INTRODUCTION TO ARCHITECTING YOUR IOS APP

Mobile Development - Lab 2

Introductory ios Development

ITP 342 Mobile App Dev. Collection View

A Mad Libs app that you will navigate through 3 UIViewControllers to add text that will be shown in a story on the fourth UIViewController.

Document Version Date: 1st March, 2015

Building the App - Part 5 - Adding a Link

Building Mapping Apps for ios With Swift

Enhancing your apps for the next dimension of touch

Stream iphone Sensor Data to Adafruit IO

News- ipad: ios(swift) Application

ITP 342 Mobile App Dev. Connections

iphone Application Programming Lab 3: Swift Types and Custom Operator + A02 discussion

CSE 438: Mobile Application Development Lab 2: Virtual Pet App

Social Pinboard: ios(swift) Application

BE PROACTIVE USE REACTIVE

Advanced Notifications

A Better MVC. 300 line view controllers or bust. Dave A guy who thinks too deeply about stuff

Gerontechnology II. Collecting Smart Phone Sensor Data for Gerontechnology. Using ios

ios Tic Tac Toe Game John Robinson at Rowan University

ios Application Development Hello World App Rubric

Miscellaneous Topics

ios Development - Xcode IDE

COMP327 Mobile Computing Session: Lecture Set 1a - Swift Introduction and the Foundation Framework Part 2

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Announcements. Today s Topics

CSC 581: Mobile App Development Spring 2018

Mastering Drag and Drop

Patterns & practices for unit-testing Swift-ly. Jakub Turek 18th June, 2018

Media and Gaming Accessibility

ITP 342 Mobile App Dev. Web View

Mastering UIKit on tvos

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

1 Build Your First App. The way to get started is to quit talking and begin doing. Walt Disney

Mobile Development Lab 3

LESSONS LEARNED. SWIFT. Dagna Bieda, 7th April 2016

ARCHETYPE MODERN ANDROID ARCHITECTURE STEPAN GONCHAROV / DENIS NEKLIUDOV

CSC 581: Mobile App Development Spring 2019

Lecture 5 Demo Code: FaceIt MVC and Gestures

What s New in tvos #WWDC16. App Frameworks. Session 206. Hans Kim tvos Engineer

Building Faster in Xcode

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

lecture 10 UI/UX and Programmatic Design cs : spring 2018

ITP 342 Mobile App Dev. Delegates

COMPLETE TUTORIAL COURSE. Learn to make tvos LE. apps with real-worldam S F

Gestures. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

A Type is Worth a Thousand Tests

HPE AppPulse Mobile. Software Version: 2.1. Adding AppPulse Mobile to Your ios App

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

ios Application Development Lecture 3: Unit 2

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

What s New in tvos 12

User Interfaces. Lecture 15. Application Programming on Mac OS. Hamza Bennani September 4, 2018

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

Objectives. Submission. Register for an Apple account. Notes on Saving Projects. Xcode Shortcuts. CprE 388 Lab 1: Introduction to Xcode

This book contains code samples available under the MIT License, printed below:

Mobile application development using the ReactiveX framework

Data Storage. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

Reactive Programming in Java. Copyright - Syncogni Consulting Pvt Ltd. All rights reserved.

Appendix B: Master Code

Learn to make desktop LE

Data Storage. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

ITP 342 Mobile App Dev. Table Views

Assignment I: Concentration

ITP 342 Mobile App Dev. Table Views

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

ios 9 SDK Development

04 Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio. Ben Riga

Learn to make ios apps

Linkify Documentation

UI-Testing, Reactive Programming and some Kotlin.

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

LEARNING ios APP DEVELOPMENT With Swift 3 & ios 10

Reactive Programming in Java. Copyright - Syncogni Consulting Pvt Ltd. All rights reserved.

Tip Calculator App Introducing Swift, Text Fields, Sliders, Outlets, Actions, View Controllers, Event Handling, NSDecimalNumber,

Apple Watch Docs. Release 0.1. Michael Hahn

Leveraging Touch Input on ios

ADVANCED M A. Learn SiriKit, imessage apps, rich notifications, and more. with real-world projects HACKING WITH SWIFT COMPLETE TUTORIAL COURSE

Assignment II: Calculator Brain

Multimedia. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

Data Delivery with Drag and Drop

Mobile Applications Development. Swift, Cocoa and Xcode

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

Step 1: Open Xcode and select Create a new Xcode Project from the Welcome to Xcode menu.

IOS - TEXT FIELD. Use of Text Field. Important Properties of Text Field. Updating Properties in xib

Mobile Application Programming. Swift Classes

Assignment IV: Smashtag Mentions

iphone Application Programming Lecture 3: Swift Part 2

Transcription:

Rx in the real world 1 Rob Ciolli

2 Rob Ciolli

3 Rob Ciolli

The App 4 Rob Ciolli

Quick architecture overview 5 Rob Ciolli

MV - WTF 6 Rob Ciolli

Model Simple, immutable data struct returned from DB or APIs 7 Rob Ciolli

View UI layout as defined in storyboard file 8 Rob Ciolli

Controller Regular Cocoa ViewController protocol ViewControllerProtocol: class {... associatedtype ViewModelType func recieve(viewmodel: ViewModelType) 9 Rob Ciolli

Presenter import UIKit protocol PresenterProtocol { associatedtype ViewControllerType: UIViewController func makeviewcontroller() -> ViewControllerType 10 Rob Ciolli

ViewModel protocol ViewModelDelegateProtocol { protocol ViewModelProtocol { associatedtype DelegateType //: ViewModelDelegateProtocol var delegate: DelegateType { get init(delegate: DelegateType) class BaseViewModel<T> : ViewModelProtocol { let delegate: T required init(delegate: T) { self.delegate = delegate 11 Rob Ciolli

Example 1 Beer Detail Page 12 Rob Ciolli

Beer Model struct Beer { let id: Int let name: String let style: String let brewry: String let abv: Double let ibu: Double let description: String let image: String 13 Rob Ciolli

Observable next complete error 14 Rob Ciolli

Observer 'Listens' to an Observable implements onnext, oncompleted, onerror subscribing or binding returns a Disposable DisposeBag pattern 15 Rob Ciolli

Observable example let observable = Observable<String>.create { observer in o.onnext("beer") o.onnext("is") o.onnext("good") o.oncompleted() return Disposables.create() 16 Rob Ciolli

Observer example let disposebag = DisposeBag() observable.subscribe( onnext: { s in print(s), onerror: { _ in print("wtf"), oncompleted: { _ in print("done") ).disposed(by: disposebag) 17 Rob Ciolli

Mutating the streams Observable Operators Transform => Map & FlatMap... Filter => Filter / Debounce / Skip / Take Combine => Zip / CombineLatest Error Handling => Retry / Catch 18 Rob Ciolli

Data Layer import RxSwift protocol Datalayer { func requestallbeers() -> Observable<[Beer]> 19 Rob Ciolli

DetailPresenter struct DetailPresenter { let beer: Beer init(beer: Beer) { self.beer = beer extension DetailPresenter: PresenterProtocol {... extension DetailPresenter: DetailViewModelDelegateProtocol { 20 Rob Ciolli

DetailViewModel protocol DetailViewModelDelegateProtocol: ViewModelDelegateProtocol { var beer: Beer { get class DetailViewModel: BaseViewModel<DetailViewModelDelegateProtocol> { let name = Variable<String?>("")... required init(delegate: DetailViewModelDelegateProtocol) { super.init(delegate: delegate) name.value = beer.name... 21 Rob Ciolli

Map 22 Rob Ciolli

DetailViewController class DetailViewController: UIViewController, ViewControllerProtocol {... let disposebag = DisposeBag() @IBOutlet weak var namelabel: UILabel! @IBOutlet weak var imageview: UIImageView! override func viewdidload() { super.viewdidload() viewmodel.name.asobservable().bindto(namelabel.rx.text).disposed(by: disposebag) viewmodel.image.asobservable().map { UIImage(named: $0)!.bindTo(imageView.rx.image).disposed(by: disposebag) 23 Rob Ciolli

Show me the code Example 1 - Beer Detail Page 24 Rob Ciolli

! " Binding is too verbose Only displays one piece of data Not really reactive 25 Rob Ciolli

Example 1b Beer Detail Page (again...) heaps more reactive 26 Rob Ciolli

Binding 27 Rob Ciolli

what if... viewmodel.name.asobservable().bindto(namelabel.rx.text) looked like namelabel.rx.text <- viewmodel.name 28 Rob Ciolli

... and you could add all disposables to disposebag in one call... disposebag.dispose([ namelabel.rx.text <- viewmodel.name, stylelabel.rx.text <- viewmodel.style, brewrylabel.rx.text <- viewmodel.brewry, ]) 29 Rob Ciolli

<- operator infix operator <- func <- <T>(property: ControlProperty<T>, variable: Variable<T>) -> Disposable { return variable.asobservable().bind(to: property) 30 Rob Ciolli

DisposeBag extension DisposeBag { func dispose(_ disposables: [Disposable]) { disposables.foreach { [unowned self] disposable in self.insert(disposable) 31 Rob Ciolli

ViewModel Requirements Page through [Beer] protocol DetailViewModelDelegateProtocol: ViewModelDelegateProtocol { var beers: Observable<[Beer]> { get... 32 Rob Ciolli

ViewModel Requirements React to Next/Prev let next = Variable<()>() let index: Variable<Int>... next.asobservable().subscribe(onnext: onnext)... private func onnext() { index.value += 1 33 Rob Ciolli

Combine Latest 34 Rob Ciolli

ViewModel Requirements Be heaps more reactive Observable.combineLatest(delegate.beers, index.asobservable(), resultselector: selectbeer) 35 Rob Ciolli

ViewModel Requirements Control enabled state of UIButton extension UIButton { var rx_driveenable: AnyObserver<Bool> { return UIBindingObserver(UIElement: self) { button, enabled in button.isuserinteractionenabled = enabled.asobserver() 36 Rob Ciolli

Show me the code (... and tests) Example 1b - Beer Detail Page 37 Rob Ciolli

Example 2 Sign in Page capture user input validate input call api and handle response 38 Rob Ciolli

Sign in ViewModel initialise protocol SigninViewModelDelegateProtocol: ViewModelDelegateProtocol { func signin(username: String, password: String) -> Observable<Void> class SigninViewModel: BaseViewModel<SigninViewModelDelegateProtocol> {... let username = Variable<String?>(nil) let password = Variable<String?>(nil) let signintapped = Variable<()>() let error = Variable<String?>(nil) let signedin = Variable<()>() 39 Rob Ciolli

Sign in ViewModel call api...... signintapped.asobservable().subscribe(onnext: signin).disposed(by: disposebag) private func signin() { guard let username = username.value, let password = password.value else { return delegate.signin(username: username, password: password).subscribe(onnext: onsignedin, onerror: onsigninerror).disposed(by: disposebag) private func onsignedin() { signedin.value = () private func onsigninerror(_: Error) { error.value = "Error signing in" 40 Rob Ciolli

Sign in ViewModel input validation var signinenabled: Observable<Bool> { return Observable.combineLatest(username.asObservable(), password.asobservable(), resultselector: inputisvalid) private func inputisvalid(username: String?, password:string?) -> Bool { guard let username = username, let password = password else { return false return!username.isempty &&!password.isempty 41 Rob Ciolli

Show me the code Example 2 - Beer Sign in 42 Rob Ciolli

Example 3 Beer ListView filter list show detail view 43 Rob Ciolli

List ViewModel let searchvariable = Variable<String?>("") let itemtapped = Variable<Int>(-1) required init(delegate: ListViewModelDelegateProtocol) {... itemtapped.asobservable().skip(1).subscribe(onnext: onitemtapped).disposed(by: disposebag) 44 Rob Ciolli

Filter 45 Rob Ciolli

List ViewModel func filteredbeers() -> Observable<[Beer]> { let search = searchvariable.asobservable() return Observable.combineLatest(delegate.beers, search, resultselector: filterbeers) private func filterbeers(beers: [Beer], search: String?) -> [Beer] { guard let search = search else { return beers return beers.filter { search.isempty $0.name.lowercased().contains(search.lowercased()) 46 Rob Ciolli

List ViewModel private func onitemtapped(index: Int) { delegate.showdetail(beers: filteredbeers(), index: index) 47 Rob Ciolli

List ViewController viewmodel.filteredbeers().debounce(0.3, scheduler: MainScheduler.instance).bind(to: tableview.rx.items( cellidentifier: "Cell", celltype: UITableViewCell.self), curriedargument: initialisecell) 48 Rob Ciolli

Show me the code Example 3 - Beer List 49 Rob Ciolli

Wash up single responsibility is good Rx makes us think about data flow infix operators and swift extensions are cool 50 Rob Ciolli

Resources http: /reactivex.io/ http: /rxmarbles.com/ https: /github.com/reactivex/rxswift 51 Rob Ciolli

Thank you @boblaroc <- (, ) 52 Rob Ciolli