Network layer (ongoing)

This commit is contained in:
Rodrigo Pedroso 2019-06-27 00:23:14 -04:00
commit e348ad1eb1
5 changed files with 165 additions and 5 deletions

View file

@ -8,7 +8,7 @@
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, RBRNetworkControllerDelegate {
@IBOutlet var txfRepoOwner: UITextField!
@IBOutlet var tblRepoTable: UITableView!
@ -16,6 +16,7 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
let cellIdentifier = "RepoCell"
var dataSource: Array<Dictionary<String, String>> = []
var selectedIndex: Int!
let netController = RBRNetworkController()
override func viewDidLoad() {
super.viewDidLoad()
@ -64,4 +65,62 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
selectedIndex = indexPath.row
performSegue(withIdentifier: "sortedDetailSegue", sender: self)
}
// MARK: - Network calls
/**
Get all repositories of a user
*/
func getRepos() {
if let owner = txfRepoOwner.text {
weak var weakSelf = self
DispatchQueue.global(qos: .userInitiated).async {
weakSelf?.netController.getRepos(owner: owner, completion: { (data, HTTPStatusCode, error) in
if HTTPStatusCode == 200 && error == nil {
do {
let result = try JSONSerialization.jsonObject(with: data!, options: []) as! Array<Dictionary<String, AnyObject>>
print(result)
// weakSelf?.btnConfigUsers.isHidden = self.appChildren.count > 0 ? false : true
// weakSelf?.cltChildrenCollection.reloadData()
}
catch {
print("Post request try/catch error: \(error)")
}
}
else {
print("Error retrieving repo")
}
})
}
}
else {
self.warnMessage(msg: "Please fill repository owner field")
}
}
// MARK: - Action
@IBAction func loadRepo(_ sender: UIButton) {
self.getRepos()
}
// MARK: - Helpers
func warnMessage(msg: String) {
let alert = UIAlertController(title: "Error", message: msg, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
// MARK: - Delegates
func messageServerDown() {
let msg: String = "The server is not responding. Please try again later."
let alert = UIAlertController(title: "Error", message: msg, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}