-
-
Save tungpt247/03038b8b5a747efea852 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
struct Person { // Entity (usually more complex e.g. NSManagedObject) | |
let firstName: String | |
let lastName: String | |
} | |
struct GreetingData { // Transport data structure (not Entity) | |
let greeting: String | |
let subject: String | |
} | |
protocol GreetingProvider { | |
func provideGreetingData() | |
} | |
protocol GreetingOutput: class { | |
func receiveGreetingData(greetingData: GreetingData) | |
} | |
class GreetingInteractor : GreetingProvider { | |
weak var output: GreetingOutput! | |
func provideGreetingData() { | |
let person = Person(firstName: "David", lastName: "Blaine") // usually comes from data access layer | |
let subject = person.firstName + " " + person.lastName | |
let greeting = GreetingData(greeting: "Hello", subject: subject) | |
self.output.receiveGreetingData(greeting) | |
} | |
} | |
protocol GreetingViewEventHandler { | |
func didTapShowGreetingButton() | |
} | |
protocol GreetingView: class { | |
func setGreeting(greeting: String) | |
} | |
class GreetingPresenter : GreetingOutput, GreetingViewEventHandler { | |
weak var view: GreetingView! | |
var greetingProvider: GreetingProvider! | |
func didTapShowGreetingButton() { | |
self.greetingProvider.provideGreetingData() | |
} | |
func receiveGreetingData(greetingData: GreetingData) { | |
let greeting = greetingData.greeting + " " + greetingData.subject | |
self.view.setGreeting(greeting) | |
} | |
} | |
class GreetingViewController : UIViewController, GreetingView { | |
var eventHandler: GreetingViewEventHandler! | |
let showGreetingButton = UIButton() | |
let greetingLabel = UILabel() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.showGreetingButton.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside) | |
} | |
func didTapButton(button: UIButton) { | |
self.eventHandler.didTapShowGreetingButton() | |
} | |
func setGreeting(greeting: String) { | |
self.greetingLabel.text = greeting | |
} | |
// layout code goes here | |
} | |
// Assembling of VIPER module, without Router | |
let view = GreetingViewController() | |
let presenter = GreetingPresenter() | |
let interactor = GreetingInteractor() | |
view.eventHandler = presenter | |
presenter.view = view | |
presenter.greetingProvider = interactor | |
interactor.output = presenter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment