SlideShare a Scribd company logo
1 of 41
Download to read offline
Delegateless
Coordinators
Ou ainda… Event Based Coordinators
SUMÁRIO
Conteúdo
●Problemas do MVC
●Introdução aos Coordinators
●Delegation
●Solução Base
●Chain of Responsibility
●Delegateless Coordinators
Problemas do
MVC
Problemas do MVC
Responsabilidades
Dependency Injection
Navigation Flow
• Layout
• Model-View binding
• Subview management
• User Input/Interaction
• Data
• Fetching
• Transformation
• Persistency
• Error Handling
• Navigation Flow
Problemas do MVC
Responsabilidades
Dependency Injection
Navigation Flow
• Core Data/Realm
• Network
• Cache
• SomethingManager
• Frameworks de DI
• Typhon
• Kraken
• Perform
• SwiftDependencyInjection
• Swinject
Problemas do MVC
Responsabilidades
Dependency Injection
Navigation Flow
Problemas do MVC
Responsabilidades
Dependency Injection
Navigation Flow
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier !== "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = fetchedResultsController.object(at: indexPath)
let controller = (segue.destination as! UINavigationController)
.topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem
.leftBarButtonItem = splitViewController!?.displayModeButtonItem
controller.navigationItem
.leftItemsSupplementBackButton = true
}
}
}
Introdução aos
Coordinators
So what is a coordinator?
The Coordinator is a PONSO, like all great objects. For
something like Instagram’s photo creation flow, we could
have a PhotoCreationCoordinator. The app
coordinator could spawn a new one, and pass it the
root view controller so that it could present the first
view controller in the flow.
The Coordinator

(Soroush Khanlou)
So what is a coordinator?
A coordinator is an object that bosses one or more view
controllers around. Taking all of the driving logic out of
your view controllers, and moving that stuff one layer
up is gonna make your life a lot more awesome.
Coordinators Redux
(Soroush Khanlou)
Estrutr
Delegation
Delegation
Delegation is a way to make composition as powerful for reuse as inheritance.
In delegation, two objects are involved in handling a request: a receiving object
delegates operations to its delegate. This is analogous to subclasses deferring
requests to parent classes. But with inheritance, an inherited operation can
always refer to the receiving object through the this member variable in C++
and self in Smalltalk. To achieve the same effect with delegation, the receiver
passes itself to the delegate to let the delegated operation refer to the receiver.
Delegation: Super Máquina e RoboCop
Let’s start with a story: Once upon a time, there was a man with no name.
Knight Industries decided that if this man were given guns and wheels and
booster rockets, he would be the perfect crime-fighting tool. First they
thought, “Let’s subclass him and override everything we need to add the
guns and wheels and booster rockets.” The problem was that to subclass
Michael Knight, they needed to wire his insides to the guns, wheels, and
booster rockets – a time-consuming task requiring lots of specialized
knowledge. So instead, Knight Industries created a helper object, the
Knight Industries 2000, or “KITT,” a well-equipped car designed to assist
Michael Knight in a variety of crime- fighting situations.
Delegation: Super Máquina e RoboCop
While approaching the perimeter of an arms dealer’s compound, Michael
Knight would say, “KITT, I need to get to the other side of that wall.” KITT
would then blast a big hole in the wall with a small rocket. After destroying
the wall, KITT would return control to Michael, who would charge through
the rubble and capture the arms dealer.
Note how creating a helper object is different from the RoboCop
approach. RoboCop was a man subclassed and extended. The RoboCop
project involved dozens of surgeons who extended the man into a fighting
machine. This is the approach taken by many object-oriented frameworks.
Delegation: Super Máquina e RoboCop
In the Cocoa framework, many objects are extended in the Knight
Industries way – by supplying them with helper objects. In this section, you
are going to provide the speech synthesizer with a type of helper object
called a delegate.
From Cocoa Programming for OS X: The Big Nerd Ranch Guide
MVC-C · Injecting
Coordinator pattern in
UIKit
Taking the first step towards clean and minimal
app architecture in iOS app means freeing your
view controllers from the burden of dealing with
other controllers.
Implementação extension UIViewController {
private struct AssociatedKeys {
static var ParentCoordinator = "ParentCoordinator"
}
public weak var parentCoordinator: Coordinating? {
get {
return objc_getAssociatedObject(self,
&AssociatedKeys.ParentCoordinator) as? Coordinating
}
set {
objc_setAssociatedObject(self,
&AssociatedKeys.ParentCoordinator,
newValue,
.OBJC_ASSOCIATION_ASSIGN)
}
}
}
View Controller
UIResponder
UIEvent
Filhos de UIResponder
Documentação
Implementação extension UIResponder {
public var coordinatingResponder: UIResponder? {
return next
}
}
extension UIResponder {
func messageTemplate(args: Whatever, sender: Any?) {
coordinatingResponder!?.messageTemplate(args: args, sender: sender)
}
}
extension UIResponder {
func cartBuyNow(_ product: Product, sender: Any?) { … }
func cartAdd(product: Product, color: ColorBox,
sender: Any?, completion: @escaping (Bool, Int) !-> Void) { … }
}
View Controller
UIResponder
UIEvent
Filhos de UIResponder
Documentação
Implementação
Responder objects—that is, instances of UIResponder—
constitute the event-handling backbone of a UIKit app.
Many key objects are also responders, including the
UIApplication object, UIViewController objects, and all
UIView objects (which includes UIWindow). As events
occur, UIKit dispatches them to your app's responder
objects for handling.
View Controller
UIResponder
UIEvent
Filhos de UIResponder
Documentação
Implementação
In addition to handling events, UIKit responders also
manage the forwarding of unhandled events to other
parts of your app. If a given responder does not
handle an event, it forwards that event to the next
event in the responder chain. UIKit manages the
responder chain dynamically, using predefined rules to
determine which object should be next to receive an
event. For example, a view forwards events to its
superview, and the root view of a hierarchy forwards
events to its view controller.
View Controller
UIResponder
UIEvent
Filhos de UIResponder
Documentação
Implementação
View Controller
UIResponder
UIEvent
Filhos de UIResponder
Documentação
Implementação
View Controller
UIResponder
UIEvent
Filhos de UIResponder
Documentação
Implementação
View Controller
UIResponder
UIEvent
Filhos de UIResponder
Documentação
Implementação
View Controller
UIResponder
UIEvent
Filhos de UIResponder
Documentação
Implementação
View Controller
UIResponder
UIEvent
Filhos de UIResponder
Documentação
Implementação
•open class UIView : UIResponder
•open class UIViewController : UIResponder
•open class UIWindow : UIView
•class AppDelegate: UIResponder
•open class UIApplication : UIResponder
View Controller
UIResponder
UIEvent
Filhos de
UIResponder
Documentação
Chain of
Responsibility
Chain-of-Responsibility
In object-oriented design, the chain-of-responsibility pattern is a design pattern
consisting of a source of command objects and a series of processing objects.
Each processing object contains logic that defines the types of command
objects that it can handle; the rest are passed to the next processing object in
the chain. A mechanism also exists for adding new processing objects to the end
of this chain. Thus, the chain of responsibility is an object oriented version of
the if ... else if ... else if ....... else ... endif idiom, with the benefit that the
condition–action blocks can be dynamically rearranged and reconfigured at
runtime.
Delegateless
Coordinators
Nossa solução
There are several kinds of events, including touch
events, motion events, remote-control events, and
press events. To handle a specific type of event, a
responder must override the corresponding
methods. For example, to handle touch events, a
responder implements the touchesBegan(_:with:),
touchesMoved(_:with:), touchesEnded(_:with:), and
touchesCancelled(_:with:) methods. In the case of
touches, the responder uses the event information
provided by UIKit to track changes to those touches
and to update the app's interface appropriately.
Remembering Events
Initial Concept
Defining Events
Protocol
Base implementation
Child management
Event handling
protocol ActionType {}
struct ResponseAction {
var type: ActionType
}
class Action {
private var target: NSObject?
private var selector: Selector?
func addTarget(_ target: NSObject?, action: Selector) {
self.target = target
self.selector = action
}
func send(_ event: Any) {
target!?.perform(selector, with: event)
}
}
Remembering Events
Initial Concept
Defining Events
Protocol
Base implementation
Child management
Event handling
class MyCoodinator: NSObject {
let action: Action
override init() {
action = Action()
super.init()
action.addTarget(self, action: #selector(self.navigate(to:)))
}
@objc func navigate(to destination: Any) {
guard let value = destination as? ResponseAction,
let type = value.type as? HomeType else {
return
}
print(type)
}
}
Remembering Events
Initial Concept
Defining Events
Protocol
Base implementation
Child management
Event handling
protocol AppEventType { }
protocol AppEventSubType { }
protocol AppEvent {
var type: AppEventType { get }
var subtype: AppEventSubType? { get }
}
Remembering Events
Initial Concept
Defining Events
Protocol
Base implementation
Child management
Event handling
protocol CoordinatorProtocol: class {
var identifier: String { get }
var parent: CoordinatorProtocol? { get set }
var childCoordinators: [String: CoordinatorProtocol] { get }
func canProcessEvent(_ event: AppEvent,
withSender sender: Any?) !-> Bool
func target(forEvent event: AppEvent,
withSender sender: Any?) !-> CoordinatorProtocol?
func handleEvent(_ event: AppEvent, withSender: Any?)
func start(with completion: @escaping () !-> Void)
func stop(with completion: @escaping () !-> Void)
func startChild(coordinator: CoordinatorProtocol,
completion: @escaping () !-> Void)
func stopChild(coordinator: CoordinatorProtocol,
completion: @escaping () !-> Void)
}
Remembering Events
Initial Concept
Defining Events
Protocol
Base implementation
Child management
Event handling
class Coordinator: NSObject, CoordinatorProtocol {
weak var parent: CoordinatorProtocol?
var childCoordinators: [String: CoordinatorProtocol] = [:]
weak var rootViewController: UIViewController?
init(rootViewController: UIViewController) {
self.rootViewController = rootViewController
}
func start(with completion: @escaping () !-> Void = {}) {
self.rootViewController!?.parentCoordinator = self
completion()
}
func stop(with completion: @escaping () !-> Void = {}) {
rootViewController!?.parentCoordinator = nil
completion()
}
Remembering Events
Initial Concept
Defining Events
Protocol
Base implementation
Child management
Event handling
func startChild(coordinator: CoordinatorProtocol,
completion: @escaping () !-> Void) {
childCoordinators[coordinator.identifier] = coordinator
coordinator.parent = self
}
func stopChild(coordinator: CoordinatorProtocol,
completion: @escaping () !-> Void = {}) {
coordinator.parent = nil
coordinator.stop { [unowned self] in
self.childCoordinators
.removeValue(forKey: coordinator.identifier)
completion()
}
}
Remembering Events
Initial Concept
Defining Events
Protocol
Base implementation
Child management
Event handling
func canProcessEvent(_ event: AppEvent,
withSender sender: Any?) !-> Bool {
return false
}
func target(forEvent event: AppEvent,
withSender sender: Any?) !-> CoordinatorProtocol? {
guard self.canProcessEvent(event,
withSender: sender) !!= true else {
return self
}
var next = self.parent
while next!?.canProcessEvent(event,
withSender: sender) !!= true {
next = next!?.parent
}
return next
}
func handleEvent(_ event: AppEvent, withSender sender: Any?) {
if self.canProcessEvent(event, withSender: sender) {
!//do something
}
else {
let handler = self.target(forEvent: event,
withSender: sender)
handler!?.handleEvent(event, withSender: sender)
}
}
Remembering Events
Initial Concept
Defining Events
Protocol
Base implementation
Child management
Event handling
DEMO TIME
See here:
https://github.com/talesp/DelegatelessCoordinators
Centro
Av. Presidente Wilson,
231 - 29º andar
(21) 2240-2030
Cidade Monções
Av. Nações Unidas,
11.541 - 3º andar
(11) 4119-0449
Savassi
Av. Getúlio Vargas, 671
Sala 800 - 8º andar
(31) 3360-8900
www.concrete.com.br

More Related Content

Similar to Delegateless Coordinator

From User Action to Framework Reaction
From User Action to Framework ReactionFrom User Action to Framework Reaction
From User Action to Framework Reactionjbandi
 
Ios development 2
Ios development 2Ios development 2
Ios development 2elnaqah
 
What 100M downloads taught us about iOS architectures
What 100M downloads taught us about iOS architecturesWhat 100M downloads taught us about iOS architectures
What 100M downloads taught us about iOS architecturesFrancesco Di Lorenzo
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptWalid Ashraf
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom componentsJeremy Grancher
 
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
MVC Design Pattern in JavaScript by ADMEC Multimedia InstituteMVC Design Pattern in JavaScript by ADMEC Multimedia Institute
MVC Design Pattern in JavaScript by ADMEC Multimedia InstituteRavi Bhadauria
 
Developing maintainable Cordova applications
Developing maintainable Cordova applicationsDeveloping maintainable Cordova applications
Developing maintainable Cordova applicationsIvano Malavolta
 
Overview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyOverview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyPeter R. Egli
 
Tools and practices for rapid application development
Tools and practices for rapid application developmentTools and practices for rapid application development
Tools and practices for rapid application developmentZoltán Váradi
 
Keynote: Trends in Modern Application Development - Gilly Dekel, IBM
Keynote: Trends in Modern Application Development - Gilly Dekel, IBMKeynote: Trends in Modern Application Development - Gilly Dekel, IBM
Keynote: Trends in Modern Application Development - Gilly Dekel, IBMCodemotion Tel Aviv
 
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...MongoDB
 
"Universal programming recipes", Kateryna Trofimenko
"Universal programming recipes", Kateryna Trofimenko"Universal programming recipes", Kateryna Trofimenko
"Universal programming recipes", Kateryna TrofimenkoBadoo Development
 
Universal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Universal programming recipes​ - Ekaterina Trofimenko - Women In TechnologyUniversal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Universal programming recipes​ - Ekaterina Trofimenko - Women In TechnologyBadoo
 
Hackatron - UIKit Dynamics
Hackatron - UIKit DynamicsHackatron - UIKit Dynamics
Hackatron - UIKit DynamicsRenzo G. Pretto
 
Building Rich User Experiences Without JavaScript Spaghetti
Building Rich User Experiences Without JavaScript SpaghettiBuilding Rich User Experiences Without JavaScript Spaghetti
Building Rich User Experiences Without JavaScript SpaghettiJared Faris
 
MVVM Design Pattern NDC2009
MVVM Design Pattern NDC2009MVVM Design Pattern NDC2009
MVVM Design Pattern NDC2009Jonas Follesø
 
Architecting Microservices in .Net
Architecting Microservices in .NetArchitecting Microservices in .Net
Architecting Microservices in .NetRichard Banks
 
Managed Extensibility Framework (MEF)
Managed Extensibility Framework (MEF)Managed Extensibility Framework (MEF)
Managed Extensibility Framework (MEF)Mohamed Meligy
 

Similar to Delegateless Coordinator (20)

From User Action to Framework Reaction
From User Action to Framework ReactionFrom User Action to Framework Reaction
From User Action to Framework Reaction
 
Ios development 2
Ios development 2Ios development 2
Ios development 2
 
What 100M downloads taught us about iOS architectures
What 100M downloads taught us about iOS architecturesWhat 100M downloads taught us about iOS architectures
What 100M downloads taught us about iOS architectures
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom components
 
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
MVC Design Pattern in JavaScript by ADMEC Multimedia InstituteMVC Design Pattern in JavaScript by ADMEC Multimedia Institute
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
 
Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
Developing maintainable Cordova applications
Developing maintainable Cordova applicationsDeveloping maintainable Cordova applications
Developing maintainable Cordova applications
 
Overview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyOverview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technology
 
Tools and practices for rapid application development
Tools and practices for rapid application developmentTools and practices for rapid application development
Tools and practices for rapid application development
 
Keynote: Trends in Modern Application Development - Gilly Dekel, IBM
Keynote: Trends in Modern Application Development - Gilly Dekel, IBMKeynote: Trends in Modern Application Development - Gilly Dekel, IBM
Keynote: Trends in Modern Application Development - Gilly Dekel, IBM
 
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...
 
"Universal programming recipes", Kateryna Trofimenko
"Universal programming recipes", Kateryna Trofimenko"Universal programming recipes", Kateryna Trofimenko
"Universal programming recipes", Kateryna Trofimenko
 
Universal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Universal programming recipes​ - Ekaterina Trofimenko - Women In TechnologyUniversal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Universal programming recipes​ - Ekaterina Trofimenko - Women In Technology
 
Hackatron - UIKit Dynamics
Hackatron - UIKit DynamicsHackatron - UIKit Dynamics
Hackatron - UIKit Dynamics
 
Building Rich User Experiences Without JavaScript Spaghetti
Building Rich User Experiences Without JavaScript SpaghettiBuilding Rich User Experiences Without JavaScript Spaghetti
Building Rich User Experiences Without JavaScript Spaghetti
 
MVVM Design Pattern NDC2009
MVVM Design Pattern NDC2009MVVM Design Pattern NDC2009
MVVM Design Pattern NDC2009
 
02 objective-c session 2
02  objective-c session 202  objective-c session 2
02 objective-c session 2
 
Architecting Microservices in .Net
Architecting Microservices in .NetArchitecting Microservices in .Net
Architecting Microservices in .Net
 
Managed Extensibility Framework (MEF)
Managed Extensibility Framework (MEF)Managed Extensibility Framework (MEF)
Managed Extensibility Framework (MEF)
 

More from Tales Andrade

Debugging tips and tricks - coders on beers Santiago
Debugging tips and tricks -  coders on beers SantiagoDebugging tips and tricks -  coders on beers Santiago
Debugging tips and tricks - coders on beers SantiagoTales Andrade
 
Swift na linha de comando
Swift na linha de comandoSwift na linha de comando
Swift na linha de comandoTales Andrade
 
Debugging tips and tricks
Debugging tips and tricksDebugging tips and tricks
Debugging tips and tricksTales Andrade
 
Usando POP com Programação Funcional
Usando POP com Programação FuncionalUsando POP com Programação Funcional
Usando POP com Programação FuncionalTales Andrade
 
Swift!.opcionais.oh!.my()?!?
Swift!.opcionais.oh!.my()?!?Swift!.opcionais.oh!.my()?!?
Swift!.opcionais.oh!.my()?!?Tales Andrade
 
Debugging fast track
Debugging fast trackDebugging fast track
Debugging fast trackTales Andrade
 

More from Tales Andrade (7)

Debugging tips and tricks - coders on beers Santiago
Debugging tips and tricks -  coders on beers SantiagoDebugging tips and tricks -  coders on beers Santiago
Debugging tips and tricks - coders on beers Santiago
 
Swift na linha de comando
Swift na linha de comandoSwift na linha de comando
Swift na linha de comando
 
Debugging tips and tricks
Debugging tips and tricksDebugging tips and tricks
Debugging tips and tricks
 
Usando POP com Programação Funcional
Usando POP com Programação FuncionalUsando POP com Programação Funcional
Usando POP com Programação Funcional
 
Swift!.opcionais.oh!.my()?!?
Swift!.opcionais.oh!.my()?!?Swift!.opcionais.oh!.my()?!?
Swift!.opcionais.oh!.my()?!?
 
Debugging fast track
Debugging fast trackDebugging fast track
Debugging fast track
 
Tales@tdc
Tales@tdcTales@tdc
Tales@tdc
 

Recently uploaded

Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

Delegateless Coordinator

  • 1.
  • 3. SUMÁRIO Conteúdo ●Problemas do MVC ●Introdução aos Coordinators ●Delegation ●Solução Base ●Chain of Responsibility ●Delegateless Coordinators
  • 5. Problemas do MVC Responsabilidades Dependency Injection Navigation Flow • Layout • Model-View binding • Subview management • User Input/Interaction • Data • Fetching • Transformation • Persistency • Error Handling • Navigation Flow
  • 6. Problemas do MVC Responsabilidades Dependency Injection Navigation Flow • Core Data/Realm • Network • Cache • SomethingManager • Frameworks de DI • Typhon • Kraken • Perform • SwiftDependencyInjection • Swinject
  • 8. Problemas do MVC Responsabilidades Dependency Injection Navigation Flow override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier !== "showDetail" { if let indexPath = tableView.indexPathForSelectedRow { let object = fetchedResultsController.object(at: indexPath) let controller = (segue.destination as! UINavigationController) .topViewController as! DetailViewController controller.detailItem = object controller.navigationItem .leftBarButtonItem = splitViewController!?.displayModeButtonItem controller.navigationItem .leftItemsSupplementBackButton = true } } }
  • 10. So what is a coordinator? The Coordinator is a PONSO, like all great objects. For something like Instagram’s photo creation flow, we could have a PhotoCreationCoordinator. The app coordinator could spawn a new one, and pass it the root view controller so that it could present the first view controller in the flow. The Coordinator
 (Soroush Khanlou)
  • 11. So what is a coordinator? A coordinator is an object that bosses one or more view controllers around. Taking all of the driving logic out of your view controllers, and moving that stuff one layer up is gonna make your life a lot more awesome. Coordinators Redux (Soroush Khanlou)
  • 14. Delegation Delegation is a way to make composition as powerful for reuse as inheritance. In delegation, two objects are involved in handling a request: a receiving object delegates operations to its delegate. This is analogous to subclasses deferring requests to parent classes. But with inheritance, an inherited operation can always refer to the receiving object through the this member variable in C++ and self in Smalltalk. To achieve the same effect with delegation, the receiver passes itself to the delegate to let the delegated operation refer to the receiver.
  • 15. Delegation: Super Máquina e RoboCop Let’s start with a story: Once upon a time, there was a man with no name. Knight Industries decided that if this man were given guns and wheels and booster rockets, he would be the perfect crime-fighting tool. First they thought, “Let’s subclass him and override everything we need to add the guns and wheels and booster rockets.” The problem was that to subclass Michael Knight, they needed to wire his insides to the guns, wheels, and booster rockets – a time-consuming task requiring lots of specialized knowledge. So instead, Knight Industries created a helper object, the Knight Industries 2000, or “KITT,” a well-equipped car designed to assist Michael Knight in a variety of crime- fighting situations.
  • 16. Delegation: Super Máquina e RoboCop While approaching the perimeter of an arms dealer’s compound, Michael Knight would say, “KITT, I need to get to the other side of that wall.” KITT would then blast a big hole in the wall with a small rocket. After destroying the wall, KITT would return control to Michael, who would charge through the rubble and capture the arms dealer. Note how creating a helper object is different from the RoboCop approach. RoboCop was a man subclassed and extended. The RoboCop project involved dozens of surgeons who extended the man into a fighting machine. This is the approach taken by many object-oriented frameworks.
  • 17. Delegation: Super Máquina e RoboCop In the Cocoa framework, many objects are extended in the Knight Industries way – by supplying them with helper objects. In this section, you are going to provide the speech synthesizer with a type of helper object called a delegate. From Cocoa Programming for OS X: The Big Nerd Ranch Guide
  • 18. MVC-C · Injecting Coordinator pattern in UIKit Taking the first step towards clean and minimal app architecture in iOS app means freeing your view controllers from the burden of dealing with other controllers.
  • 19. Implementação extension UIViewController { private struct AssociatedKeys { static var ParentCoordinator = "ParentCoordinator" } public weak var parentCoordinator: Coordinating? { get { return objc_getAssociatedObject(self, &AssociatedKeys.ParentCoordinator) as? Coordinating } set { objc_setAssociatedObject(self, &AssociatedKeys.ParentCoordinator, newValue, .OBJC_ASSOCIATION_ASSIGN) } } } View Controller UIResponder UIEvent Filhos de UIResponder Documentação
  • 20. Implementação extension UIResponder { public var coordinatingResponder: UIResponder? { return next } } extension UIResponder { func messageTemplate(args: Whatever, sender: Any?) { coordinatingResponder!?.messageTemplate(args: args, sender: sender) } } extension UIResponder { func cartBuyNow(_ product: Product, sender: Any?) { … } func cartAdd(product: Product, color: ColorBox, sender: Any?, completion: @escaping (Bool, Int) !-> Void) { … } } View Controller UIResponder UIEvent Filhos de UIResponder Documentação
  • 21. Implementação Responder objects—that is, instances of UIResponder— constitute the event-handling backbone of a UIKit app. Many key objects are also responders, including the UIApplication object, UIViewController objects, and all UIView objects (which includes UIWindow). As events occur, UIKit dispatches them to your app's responder objects for handling. View Controller UIResponder UIEvent Filhos de UIResponder Documentação
  • 22. Implementação In addition to handling events, UIKit responders also manage the forwarding of unhandled events to other parts of your app. If a given responder does not handle an event, it forwards that event to the next event in the responder chain. UIKit manages the responder chain dynamically, using predefined rules to determine which object should be next to receive an event. For example, a view forwards events to its superview, and the root view of a hierarchy forwards events to its view controller. View Controller UIResponder UIEvent Filhos de UIResponder Documentação
  • 28. Implementação •open class UIView : UIResponder •open class UIViewController : UIResponder •open class UIWindow : UIView •class AppDelegate: UIResponder •open class UIApplication : UIResponder View Controller UIResponder UIEvent Filhos de UIResponder Documentação
  • 30. Chain-of-Responsibility In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain. Thus, the chain of responsibility is an object oriented version of the if ... else if ... else if ....... else ... endif idiom, with the benefit that the condition–action blocks can be dynamically rearranged and reconfigured at runtime.
  • 32. There are several kinds of events, including touch events, motion events, remote-control events, and press events. To handle a specific type of event, a responder must override the corresponding methods. For example, to handle touch events, a responder implements the touchesBegan(_:with:), touchesMoved(_:with:), touchesEnded(_:with:), and touchesCancelled(_:with:) methods. In the case of touches, the responder uses the event information provided by UIKit to track changes to those touches and to update the app's interface appropriately. Remembering Events Initial Concept Defining Events Protocol Base implementation Child management Event handling
  • 33. protocol ActionType {} struct ResponseAction { var type: ActionType } class Action { private var target: NSObject? private var selector: Selector? func addTarget(_ target: NSObject?, action: Selector) { self.target = target self.selector = action } func send(_ event: Any) { target!?.perform(selector, with: event) } } Remembering Events Initial Concept Defining Events Protocol Base implementation Child management Event handling
  • 34. class MyCoodinator: NSObject { let action: Action override init() { action = Action() super.init() action.addTarget(self, action: #selector(self.navigate(to:))) } @objc func navigate(to destination: Any) { guard let value = destination as? ResponseAction, let type = value.type as? HomeType else { return } print(type) } } Remembering Events Initial Concept Defining Events Protocol Base implementation Child management Event handling
  • 35. protocol AppEventType { } protocol AppEventSubType { } protocol AppEvent { var type: AppEventType { get } var subtype: AppEventSubType? { get } } Remembering Events Initial Concept Defining Events Protocol Base implementation Child management Event handling
  • 36. protocol CoordinatorProtocol: class { var identifier: String { get } var parent: CoordinatorProtocol? { get set } var childCoordinators: [String: CoordinatorProtocol] { get } func canProcessEvent(_ event: AppEvent, withSender sender: Any?) !-> Bool func target(forEvent event: AppEvent, withSender sender: Any?) !-> CoordinatorProtocol? func handleEvent(_ event: AppEvent, withSender: Any?) func start(with completion: @escaping () !-> Void) func stop(with completion: @escaping () !-> Void) func startChild(coordinator: CoordinatorProtocol, completion: @escaping () !-> Void) func stopChild(coordinator: CoordinatorProtocol, completion: @escaping () !-> Void) } Remembering Events Initial Concept Defining Events Protocol Base implementation Child management Event handling
  • 37. class Coordinator: NSObject, CoordinatorProtocol { weak var parent: CoordinatorProtocol? var childCoordinators: [String: CoordinatorProtocol] = [:] weak var rootViewController: UIViewController? init(rootViewController: UIViewController) { self.rootViewController = rootViewController } func start(with completion: @escaping () !-> Void = {}) { self.rootViewController!?.parentCoordinator = self completion() } func stop(with completion: @escaping () !-> Void = {}) { rootViewController!?.parentCoordinator = nil completion() } Remembering Events Initial Concept Defining Events Protocol Base implementation Child management Event handling
  • 38. func startChild(coordinator: CoordinatorProtocol, completion: @escaping () !-> Void) { childCoordinators[coordinator.identifier] = coordinator coordinator.parent = self } func stopChild(coordinator: CoordinatorProtocol, completion: @escaping () !-> Void = {}) { coordinator.parent = nil coordinator.stop { [unowned self] in self.childCoordinators .removeValue(forKey: coordinator.identifier) completion() } } Remembering Events Initial Concept Defining Events Protocol Base implementation Child management Event handling
  • 39. func canProcessEvent(_ event: AppEvent, withSender sender: Any?) !-> Bool { return false } func target(forEvent event: AppEvent, withSender sender: Any?) !-> CoordinatorProtocol? { guard self.canProcessEvent(event, withSender: sender) !!= true else { return self } var next = self.parent while next!?.canProcessEvent(event, withSender: sender) !!= true { next = next!?.parent } return next } func handleEvent(_ event: AppEvent, withSender sender: Any?) { if self.canProcessEvent(event, withSender: sender) { !//do something } else { let handler = self.target(forEvent: event, withSender: sender) handler!?.handleEvent(event, withSender: sender) } } Remembering Events Initial Concept Defining Events Protocol Base implementation Child management Event handling
  • 41. Centro Av. Presidente Wilson, 231 - 29º andar (21) 2240-2030 Cidade Monções Av. Nações Unidas, 11.541 - 3º andar (11) 4119-0449 Savassi Av. Getúlio Vargas, 671 Sala 800 - 8º andar (31) 3360-8900 www.concrete.com.br