Created
March 21, 2017 14:56
-
-
Save vgonda/11ffa295da6e320b172267616de7013e to your computer and use it in GitHub Desktop.
Example Donuts MVP architecture
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
class DonutsViewController: UIViewController, DonutsView { | |
lazy let presenter = { DonutsPresenter(view: self) }() | |
override func showLoading() { | |
} | |
override func showNoBringers() { | |
} | |
override func showBringers(users: [User]) { | |
} | |
} | |
class DonutsPresenter { | |
let view: DonutsView | |
let donutsRepo: DonutsRepo | |
init(view: DonutsView, repo: DonutsRepo = DonutsRepo.shared) { | |
self.view = view | |
} | |
func onBringingDonutsSelected() { | |
view.showLoading(true) | |
donutsRepo.postBringingDonuts() | |
.subscribe( | |
onNext: { users in | |
self.updateView(users) | |
}, | |
onError: { error in | |
updateView([], error: error) | |
} | |
) | |
} | |
private func updateView(users: [User], error: Error? = nil) { | |
view.showLoading(false) | |
if users.isEmpty { | |
view.showNoBringers() | |
} else { | |
view.showBringers(users) | |
} | |
if let error = error { | |
view.showError(error) | |
} | |
} | |
} | |
protocol DonutsView { | |
func showLoading(_ isLoading: Bool) | |
func showBringers(bringers: [User]) | |
func showNoBringers() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment