SlideShare a Scribd company logo
November 2017
Coming Soon…
Declarative presentations UIKonf
Declarative presentations UIKonf
Declarative presentations UIKonf
Declarative
presentations
Imperative
vs
Declarative
Instructions
Facts
Understand how the system works
and based on its state and state
transitions decide what instructions to
execute.
Instructions
Facts
Understand how the system works
and based on its state and state
transitions decide what instructions to
execute.
State how your code should work if
certain conditions are met. You don’t
need to keep track of the current
state of the system, it calls you.
Example
———————————— Imperative
button.frame = CGRect(
x: inset,
y: view.bounds.size.height - buttonSize - inset,
width: view.bounds.size.width - 2 * inset,
height: buttonSize
)
Example
———————————— Imperative
button.frame = CGRect(
x: inset,
y: view.bounds.size.height - buttonSize - inset,
width: view.bounds.size.width - 2 * inset,
height: buttonSize
)
Example
———————————— Imperative
button.frame = CGRect(
x: inset,
y: view.bounds.size.height - buttonSize - inset,
width: view.bounds.size.width - 2 * inset,
height: buttonSize
) // <— Needs to be done every time view.bounds change
Example
———————————— Imperative
button.frame = CGRect(
x: inset,
y: view.bounds.size.height - buttonSize - inset,
width: view.bounds.size.width - 2 * inset,
height: buttonSize
)
———————————— (More) Declarative
NSLayoutConstraint.activate(
button.heightAnchor == buttonSize,
button.centerXAnchor == view.centerXAnchor,
button.bottomAnchor == view.bottomAnchor - inset,
button.leadingAnchor == view.leadingAnchor + inset
)
Example
———————————— Imperative
button.frame = CGRect(
x: inset,
y: view.bounds.size.height - buttonSize - inset,
width: view.bounds.size.width - 2 * inset,
height: buttonSize
)
———————————— (More) Declarative
NSLayoutConstraint.activate(
button.heightAnchor == buttonSize,
button.centerXAnchor == view.centerXAnchor,
button.bottomAnchor == view.bottomAnchor - inset,
button.leadingAnchor == view.leadingAnchor + inset
)
Google Facebook
Apple? (maybe)
UIKit encourages
imperative
programming style
UIKit encourages
imperative
programming style
but we can wrap it!
The concept 📚
See it in practice "
Further opportunities 🚀
The plan
The concept
We show things on the screen for a
reason. When presenting a view, we
need to wait for a user to take some
kind of action and then our app
responds to that action.
💡
Presenting a view == asynchronous
operation.
The concept
If view presentations
are async functions
they can have a declarative interface
Let’s build a small app!
Declarative presentations UIKonf
Pay now
Shopping cart
Receipt
Check out
Declarative presentations UIKonf
Taking it further
Presentable as we defined it
protocol Presentable {
associatedtype Value
func start() -> (UIViewController, FutureResult<Value>)
}
Presentable in iZettle
public protocol Presentable {
associatedtype Matter
associatedtype Result
/// Constructs a matter from `self` and returns it
together with the result of presenting it.
func materialize() -> (Matter, Result)
}
Presentable wrapper
public struct Presentation<P>
where P : Presentable, P.Matter : UIViewController {
// The presentable wrapped by `self`.
public var presentable: P
// The presentation style to use when presenting `self`.
public var style: PresentationStyle
// The presentation options to use when presenting `self`.
public var options: PresentationOptions
// The configuration to apply just before presenting `self`.
public var configure: (P.Matter, DisposeBag) -> ()
// A transformation to apply on the result of `materialize()`.
public var transform: (P.Result) -> P.Result
// A callback that will be called once presentation is done,
either with `nil` if normally dismissed, or with an error if not.
public var onDismiss: (Error?) -> ()
}
Example
let playVideo: (Video) -> Presentation<MoviePlayer> = { video in
return Presentation(MoviePlayer(video: video),
style: .flipPush,
configure: installDismiss(.close))
}
9:55:53 AM: TestScreen will 'flipPush' present: MoviePlayer
9:55:55 AM: TestScreen did end presentation of: MoviePlayer
9:55:55 AM: MoviePlayer was deallocated after presentation from
TestScreen
(lldb)
Utilising Swift type system
You can create helpers that make it
easier to write declarative code, keep
track of presentations and even detect
memory leaks!
Testing
Test failure
func testRegister_fails_whenPresentReturnsError() {
let pointOfSale = PointOfSale.dummy
let expectedError = TestError()
let result = pointOfSale.pay { _ in
return FutureResult(error: expectedError)
}
let flowCompletion = expectation(description: "Flow completed")
result.observe { flowResult in
guard case .failure(let error) = flowResult else {
XCTFail("Expected flow failure but got (flowResult)")
return
}
XCTAssertEqual(error as? TestError, expectedError)
flowCompletion.fulfill()
}
wait(for: [flowCompletion], timeout: 0.1)
}
Snapshot testing
func configureAndTakeSnapshots<T: Presentable, R>(
of presentable: T,
configuredAs configuration: SnapshotWindow.Configuration,
identifier: String,
file: StaticString = #file,
line: UInt = #line
) where T.Matter: UIViewController, T.Result == Future<R> {
let (window, result) = presentable.materializeAndAddToWindow(
configuredAs: configuration
)
FBSnapshotVerifyView(
window,
identifier: identifier,
file: file,
line: line
)
result.disposable.dispose()
}
Hiding complexity
Choose
Payment
More realistic checkout
Checkout
Payment
Specific
Screens
Send
Receipt
Register
Choose
Payment
More realistic checkout
Checkout
Receipt
Payment
Specific
Register
Field tested
Take-aways
•We show UI for a purpose - to drive
data flows in our apps and we can
express presentations as async
functions that return a Result
•Keeping the UI flow logic decoupled
makes it easy to follow and test
•Declarative programming for
presentations makes the possible user
journeys explicit.
Further reading
1. Live coding playground
2. iZettle - Introducing Presentation [oss]
3. Chris Eidhof - Functional View Controllers
4. Benjamin Encz - Turning UIKit Inside Out
5. iZettle - Functional Views
6. Google I/O - Declarative UI patterns
7. Matt Galagher - Declarative views
8. Less verbose constraints [oss]
…
@nataliya_bg
Thank you!

More Related Content

PDF
Deep Dive into React Hooks
PDF
React new features and intro to Hooks
PPTX
Promises, promises, and then observables
PDF
Asynchronous JS in Odoo
PDF
Workshop 23: ReactJS, React & Redux testing
PDF
What's up with Prototype and script.aculo.us?
PDF
Angular js 2.0, ng poznań 20.11
PDF
Anonymous functions in JavaScript
Deep Dive into React Hooks
React new features and intro to Hooks
Promises, promises, and then observables
Asynchronous JS in Odoo
Workshop 23: ReactJS, React & Redux testing
What's up with Prototype and script.aculo.us?
Angular js 2.0, ng poznań 20.11
Anonymous functions in JavaScript

What's hot (20)

PDF
Workshop 5: JavaScript testing
PDF
Funcitonal Swift Conference: The Functional Way
PDF
Protocol Oriented MVVM - Auckland iOS Meetup
PPTX
Workshop 1: Good practices in JavaScript
PDF
Angular2: Quick overview with 2do app example
PPTX
Java Script Promise
PDF
N Things You Don't Want to Repeat in React Native
PDF
Protocol-Oriented MVVM
PPTX
PPTX
Javascript And J Query
PPT
Javascript Design Patterns
PDF
Protocol-Oriented MVVM (extended edition)
PDF
Human Talks - StencilJS
PDF
The evolution of asynchronous javascript
PPT
AngularJS Testing Strategies
PDF
Func up your code
PDF
Backday Xebia : Akka, the reactive toolkit
PPTX
AngularJs
PDF
Intro to JavaScript
PDF
JavaScript promise
Workshop 5: JavaScript testing
Funcitonal Swift Conference: The Functional Way
Protocol Oriented MVVM - Auckland iOS Meetup
Workshop 1: Good practices in JavaScript
Angular2: Quick overview with 2do app example
Java Script Promise
N Things You Don't Want to Repeat in React Native
Protocol-Oriented MVVM
Javascript And J Query
Javascript Design Patterns
Protocol-Oriented MVVM (extended edition)
Human Talks - StencilJS
The evolution of asynchronous javascript
AngularJS Testing Strategies
Func up your code
Backday Xebia : Akka, the reactive toolkit
AngularJs
Intro to JavaScript
JavaScript promise
Ad

Similar to Declarative presentations UIKonf (20)

PPTX
iOS Beginners Lesson 2
PPTX
Basic iOS Training with SWIFT - Part 3
PDF
Presenting things in Swift
PPTX
Automating the Gaps of Unit Testing Mobile Apps
PDF
Introduction of Xcode
PPTX
iOS app dev Training - Session1
PPT
Programming iOS in C#
PPTX
iOS for C# Developers - DevConnections Talk
PDF
Intro to ios - init by SLOHacks
PPTX
iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...
PDF
SV-ios-objc-to-swift
PDF
TDD by Controlling Dependencies
PDF
Swift
PDF
Actor based approach in practice for Swift developers
PDF
Distributing information on iOS
PPTX
iOS Development (Part 2)
PDF
IBDesignable - CocoaConf Seattle 2014
PDF
Why the Dark Side should use Swift and a SOLID Architecture
PDF
Swift - One step forward from Obj-C
PDF
iOS viper presentation
iOS Beginners Lesson 2
Basic iOS Training with SWIFT - Part 3
Presenting things in Swift
Automating the Gaps of Unit Testing Mobile Apps
Introduction of Xcode
iOS app dev Training - Session1
Programming iOS in C#
iOS for C# Developers - DevConnections Talk
Intro to ios - init by SLOHacks
iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...
SV-ios-objc-to-swift
TDD by Controlling Dependencies
Swift
Actor based approach in practice for Swift developers
Distributing information on iOS
iOS Development (Part 2)
IBDesignable - CocoaConf Seattle 2014
Why the Dark Side should use Swift and a SOLID Architecture
Swift - One step forward from Obj-C
iOS viper presentation
Ad

Recently uploaded (9)

PDF
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
DOC
NIU毕业证学历认证,阿比林基督大学毕业证留学生学历
DOC
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
DOC
SIUE毕业证学历认证,阿祖萨太平洋大学毕业证学位证书复制
PPTX
Introduction to Packet Tracer Course Overview - Aug 21 (1).pptx
PPTX
ASMS Telecommunication company Profile
PDF
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
PDF
2025 Guide to Buy Verified Cash App Accounts You Can Trust.pdf
PDF
Best 4 Sites for Buy Verified Cash App Accounts – BTC Only.pdf
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
NIU毕业证学历认证,阿比林基督大学毕业证留学生学历
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
SIUE毕业证学历认证,阿祖萨太平洋大学毕业证学位证书复制
Introduction to Packet Tracer Course Overview - Aug 21 (1).pptx
ASMS Telecommunication company Profile
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
2025 Guide to Buy Verified Cash App Accounts You Can Trust.pdf
Best 4 Sites for Buy Verified Cash App Accounts – BTC Only.pdf

Declarative presentations UIKonf