[Feature] Locations list #10

Merged
javier merged 13 commits from feature/locations-list into main 2023-04-12 16:58:28 +00:00
Showing only changes of commit 51e4c64a11 - Show all commits

View File

@ -25,6 +25,8 @@ class LocationsListViewController: BaseViewController {
private lazy var table = { private lazy var table = {
let table = UITableView(frame: .zero, style: .plain) let table = UITableView(frame: .zero, style: .plain)
table.dataSource = self
table.delegate = self
table.translatesAutoresizingMaskIntoConstraints = false table.translatesAutoresizingMaskIntoConstraints = false
return table return table
@ -50,10 +52,47 @@ class LocationsListViewController: BaseViewController {
setupBar() setupBar()
setupView() setupView()
bindViewModel() bindViewModel()
viewModel.loadLocations()
} }
} }
// MARK: - UITableViewDataSource
extension LocationsListViewController: UITableViewDataSource {
// MARK: Functions
func numberOfSections(in tableView: UITableView) -> Int {
viewModel.numberOfSectionsInData
}
func tableView(
_ tableView: UITableView,
numberOfRowsInSection section: Int
) -> Int {
viewModel.numberOfDataItems(in: section)
}
func tableView(
_ tableView: UITableView,
cellForRowAt indexPath: IndexPath
) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)
let entity = viewModel.dataItem(at: indexPath)
cell.textLabel?.text = entity.name
return cell
}
}
// MARK: - UITableViewDelegate
extension LocationsListViewController: UITableViewDelegate {}
// MARK: - Helpers // MARK: - Helpers
private extension LocationsListViewController { private extension LocationsListViewController {
@ -76,7 +115,13 @@ private extension LocationsListViewController {
view.addSubview(table) view.addSubview(table)
view.addSubview(error) view.addSubview(error)
view.addSubview(loading) view.addSubview(loading)
error.onRetry = { print("RETRY BUTTON PRESSED!") }
error.onRetry = {
self.viewModel.loadLocations()
}
table.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
error.widthAnchor.constraint(equalToConstant: 300), error.widthAnchor.constraint(equalToConstant: 300),
view.centerXAnchor.constraint(equalTo: error.centerXAnchor), view.centerXAnchor.constraint(equalTo: error.centerXAnchor),
@ -99,6 +144,10 @@ private extension LocationsListViewController {
self.error.isHidden = viewStatus != .error self.error.isHidden = viewStatus != .error
self.loading.isHidden = viewStatus != .loading self.loading.isHidden = viewStatus != .loading
self.table.isHidden = viewStatus != .loaded self.table.isHidden = viewStatus != .loaded
if viewStatus == .loaded {
self.table.reloadData()
}
} }
.store(in: &cancellables) .store(in: &cancellables)
} }