Skip to content

Instantly share code, notes, and snippets.

@hlung
Last active April 29, 2019 10:34
Show Gist options
  • Save hlung/a0f7db1434f673598c1d9d885b84fe7b to your computer and use it in GitHub Desktop.
Save hlung/a0f7db1434f673598c1d9d885b84fe7b to your computer and use it in GitHub Desktop.
A UIViewController invoking PokeApi
import UIKit
import app
class PokeListViewController: UIViewController {
@IBOutlet var pokemonSprite: UIImageView!
@IBOutlet var pokemonInfo: UILabel!
@IBOutlet var pokeListTableView: UITableView!
internal var pokeList: [PokemonEntry] = []
internal var api = PokeApi()
override func viewDidLoad() {
super.viewDidLoad()
pokeListTableView.dataSource = self
pokeListTableView.delegate = self
loadList()
}
private func loadList(){
api.getPokemonList(
success: { data in
self.update(data: data)
return KotlinUnit()
}, failure: {
self.handleError($0?.message)
return KotlinUnit()
})
}
internal func handleError(_ error: String?){
let message = error ?? "An unknown error occurred. Retry?"
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Retry", style: .default, handler: { action in
self.loadList()
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment