Skip to content

Commit 166732c

Browse files
committed
Feat(AddressListView): create
1 parent e3bafff commit 166732c

File tree

3 files changed

+110
-6
lines changed

3 files changed

+110
-6
lines changed

solutions/devsprint-vinicius-carvalho-3/DeliveryApp/Screens/AddressSearch/AddressListView.swift

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,105 @@
88
import UIKit
99

1010
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)
1343

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
14111
}
15112
}

solutions/devsprint-vinicius-carvalho-3/DeliveryApp/Screens/AddressSearch/AddressSearchViewController.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import UIKit
99

1010
class AddressSearchViewController: UIViewController {
11-
11+
12+
1213
private let deliveryApi = DeliveryApi()
1314

1415
private let addressListView: AddressListView = {
@@ -19,7 +20,6 @@ class AddressSearchViewController: UIViewController {
1920

2021
init() {
2122
super.init(nibName: nil, bundle: nil)
22-
2323
}
2424

2525
required init?(coder: NSCoder) {
@@ -31,7 +31,14 @@ class AddressSearchViewController: UIViewController {
3131
}
3232

3333
override func viewDidLoad() {
34-
34+
view.backgroundColor = .white
35+
36+
navigationController?.navigationBar.prefersLargeTitles = true
37+
navigationItem.title = "Address"
38+
navigationController?.navigationBar.backgroundColor = .secondarySystemBackground
39+
40+
tabBarController?.tabBar.tintColor = .secondarySystemFill
41+
3542
deliveryApi.searchAddresses { addresses in
3643

3744
guard let addresses = addresses else {

solutions/devsprint-vinicius-carvalho-3/DeliveryApp/Screens/Components/AdressCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extension AddressCell: ViewCode {
6161
NSLayoutConstraint.activate([
6262
container.topAnchor.constraint(equalTo: topAnchor, constant: 12),
6363
container.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
64-
container.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
64+
container.trailingAnchor.constraint(equalTo: trailingAnchor),
6565
container.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12)
6666
])
6767
}

0 commit comments

Comments
 (0)