Skip to content

Instantly share code, notes, and snippets.

@fnazarios
Last active January 20, 2020 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnazarios/af890268ecea80f4e197789e46fe9a88 to your computer and use it in GitHub Desktop.
Save fnazarios/af890268ecea80f4e197789e46fe9a88 to your computer and use it in GitHub Desktop.
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: .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 = tableView.dequeueReusableCell(withIdentifier: reuseCell, for: indexPath)
let contact = contacts[indexPath.row]
cell.textLabel?.text = contact.name
let urlPhoto = URL(string: contact.photoURL)!
do {
let data = try Data(contentsOf: urlPhoto)
let image = UIImage(data: data)
cell.imageView?.image = image
} catch _ {}
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 {
private let service = Service()
private var completion: ([Contact]?, Error?) -> Void
func loadContacts(_ completion: @escaping ([Contact]?, Error?) -> Void) {
self.completion = completion
service.fetchContacts { contacts, err in
//... aplica a regra de negocio
self.handle(contacts, err)
}
}
private func handle(_ contacts: [Contact]?, _ error: Error?) {
completion(contacts, err)
}
}
class Service {
func fetchContacts(completion: @escaping ([Contact]?, Error?) -> Void) {
/// Usa URLSession para acessar a API e retorna
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment