|
8 | 8 | import UIKit |
9 | 9 |
|
10 | 10 | class AddressListView: UIView { |
11 | | - |
12 | | - func updateView(with addresses: [Address]) { |
| 11 | + |
| 12 | + private let addressCellIdentifier = "AddressCellIdentifier" |
| 13 | + |
| 14 | + private var addresses: [Address] = [] |
| 15 | + |
| 16 | + private lazy var searchbar: UISearchBar = { |
| 17 | + let input = UISearchBar() |
| 18 | + input.translatesAutoresizingMaskIntoConstraints = false |
| 19 | + input.layer.borderWidth = 10 |
| 20 | + input.layer.borderColor = UIColor.secondarySystemBackground.cgColor |
| 21 | + input.barTintColor = .secondarySystemBackground |
| 22 | + return input |
| 23 | + }() |
| 24 | + |
| 25 | + private lazy var tableView: UITableView = { |
| 26 | + let tableView = UITableView(frame: .zero) |
| 27 | + tableView.translatesAutoresizingMaskIntoConstraints = false |
| 28 | + tableView.backgroundColor = .white |
| 29 | + tableView.register(AddressCell.self, forCellReuseIdentifier: self.addressCellIdentifier) |
| 30 | + tableView.dataSource = self |
| 31 | + return tableView |
| 32 | + }() |
| 33 | + |
| 34 | + private lazy var divider: UIView = { |
| 35 | + let view = UIView() |
| 36 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 37 | + view.backgroundColor = .secondarySystemFill |
| 38 | + return view |
| 39 | + }() |
| 40 | + |
| 41 | + init(){ |
| 42 | + super.init(frame: .zero) |
13 | 43 |
|
| 44 | + setup() |
| 45 | + } |
| 46 | + |
| 47 | + required init?(coder: NSCoder) { |
| 48 | + fatalError("init(coder:) has not been implemented") |
| 49 | + } |
| 50 | + |
| 51 | + func updateView(with addresses: [Address]) { |
| 52 | + self.addresses = addresses |
| 53 | + self.tableView.reloadData() |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +extension AddressListView: ViewCode { |
| 58 | + func setupSubviews() { |
| 59 | + addSubview(searchbar) |
| 60 | + addSubview(divider) |
| 61 | + addSubview(tableView) |
| 62 | + } |
| 63 | + |
| 64 | + func setupConstraints() { |
| 65 | + setupTableViewConstraints() |
| 66 | + setupSearchbarConstaints() |
| 67 | + setupDividerConstraints() |
| 68 | + } |
| 69 | + |
| 70 | + func setupSearchbarConstaints(){ |
| 71 | + NSLayoutConstraint.activate([ |
| 72 | + searchbar.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor), |
| 73 | + searchbar.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 74 | + searchbar.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 75 | + searchbar.bottomAnchor.constraint(equalTo: divider.topAnchor) |
| 76 | + ]) |
| 77 | + } |
| 78 | + |
| 79 | + func setupDividerConstraints(){ |
| 80 | + NSLayoutConstraint.activate([ |
| 81 | + divider.topAnchor.constraint(equalTo: searchbar.bottomAnchor), |
| 82 | + divider.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 83 | + divider.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 84 | + divider.bottomAnchor.constraint(equalTo: tableView.topAnchor), |
| 85 | + divider.heightAnchor.constraint(equalToConstant: 1) |
| 86 | + ]) |
| 87 | + } |
| 88 | + |
| 89 | + func setupTableViewConstraints(){ |
| 90 | + NSLayoutConstraint.activate([ |
| 91 | + tableView.topAnchor.constraint(equalTo: divider.bottomAnchor), |
| 92 | + tableView.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 93 | + tableView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20), |
| 94 | + tableView.bottomAnchor.constraint(equalTo: bottomAnchor) |
| 95 | + ]) |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +extension AddressListView: UITableViewDataSource { |
| 101 | + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 102 | + let cell = tableView.dequeueReusableCell(withIdentifier: self.addressCellIdentifier) as! AddressCell |
| 103 | + let data = self.addresses[indexPath.row] |
| 104 | + cell.updateView(address: "\(data.street), \(data.number)", neighborhood: data.neighborhood) |
| 105 | + return cell |
| 106 | + } |
| 107 | + |
| 108 | + public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 109 | + |
| 110 | + return self.addresses.count |
14 | 111 | } |
15 | 112 | } |
0 commit comments