Skip to content

Instantly share code, notes, and snippets.

@vgonda
Created March 21, 2017 14:56
Show Gist options
  • Save vgonda/11ffa295da6e320b172267616de7013e to your computer and use it in GitHub Desktop.
Save vgonda/11ffa295da6e320b172267616de7013e to your computer and use it in GitHub Desktop.
Example Donuts MVP architecture
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