Last active
April 1, 2020 14:32
-
-
Save lucasromanomr/00c37c846a6d03f4f1e92b1e24e89cfb 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 | |
class Contact: Codable { | |
var name = "" | |
var photoURL: String = "" | |
var id = "" | |
init(name: String, photoURL: String, id: String) { | |
self.name = name | |
self.photoURL = photoURL | |
self.id = id | |
} | |
enum CodingKeys: String, CodingKey { | |
case name = "name" | |
case photoURL = "photoURL" | |
case id = "id" | |
} | |
} | |
class ListContactsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
lazy var tableView: UITableView = { | |
let tableView = UITableView(frame: .zero, style: UITableView.Style.plain) | |
tableView.translatesAutoresizingMaskIntoConstraints = false | |
tableView.dataSource = self | |
return tableView | |
}() | |
var contacts: [Contact]! | |
var viewModel: ListContactsViewModel! | |
init() { | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
viewModel = ListContactsViewModel() | |
addComponents() | |
navigationController?.navigationController?.title = "Lista de contatos" | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
loadData() | |
} | |
func addComponents() { | |
view.backgroundColor = .red | |
view.addSubview(tableView) | |
NSLayoutConstraint.activate([ | |
tableView.topAnchor.constraint(equalTo: view.topAnchor), | |
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor), | |
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor), | |
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor) | |
]) | |
} | |
func numberOfSections(in tableView: UITableView) -> Int { | |
return 0 | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return contacts.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let reuseCell = "reuseCellContact" | |
let cell: UITableViewCell | |
if let cellDeque = tableView.dequeueReusableCell(withIdentifier: reuseCell) { | |
cell = cellDeque | |
} else { | |
cell = UITableViewCell(style: .default, reuseIdentifier: reuseCell) | |
} | |
let contact = contacts[indexPath.row] | |
cell.textLabel?.text = contact.name | |
let urlPhoto = URL(string: contact.photoURL)! | |
let data = try! Data(contentsOf: urlPhoto) | |
let image = UIImage(data: data) | |
cell.imageView?.image = image | |
return cell | |
} | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
let contato = contacts[indexPath.row - 1] | |
guard isLegacy(contact: contato) else { | |
let alert = UIAlertController(title: "Você tocou em", message: "\(contato.name)", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) | |
self.present(alert, animated: true) | |
return | |
} | |
let alert = UIAlertController(title: "Atenção", message:"Você tocou no contato sorteado", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) | |
self.present(alert, animated: true) | |
} | |
func loadData() { | |
viewModel.loadContacts { (contacts, error) in | |
DispatchQueue.global().async { | |
if let error = error { | |
print(error) | |
let alert = UIAlertController(title: "Ops, ocorreu um erro", message: error.localizedDescription, preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) | |
self.present(alert, animated: true) | |
return | |
} | |
self.contacts = contacts! | |
self.tableView.reloadData() | |
} | |
} | |
} | |
func isLegacy(contact: Contact) -> Bool { | |
return UserIdsLegacy.isLegacy(id: contact.id) | |
} | |
} | |
class UserIdsLegacy { | |
static let legacyIds = ["123", "4321", "9900"] | |
static func isLegacy(id: String) -> Bool { | |
return legacyIds.contains(id) | |
} | |
} | |
class ListContactsViewModel { | |
func loadContacts(_ completion: @escaping ([Contact]?, Error?) -> Void) { | |
/// considerrar implementação que retorna o request | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment