diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift index 9681ec5..6410a46 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift @@ -25,6 +25,8 @@ class LocationsListViewController: BaseViewController { private lazy var table = { let table = UITableView(frame: .zero, style: .plain) + table.dataSource = self + table.delegate = self table.translatesAutoresizingMaskIntoConstraints = false return table @@ -50,10 +52,47 @@ class LocationsListViewController: BaseViewController { setupBar() setupView() 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 private extension LocationsListViewController { @@ -76,7 +115,13 @@ private extension LocationsListViewController { view.addSubview(table) view.addSubview(error) view.addSubview(loading) - error.onRetry = { print("RETRY BUTTON PRESSED!") } + + error.onRetry = { + self.viewModel.loadLocations() + } + + table.register(UITableViewCell.self, forCellReuseIdentifier: "cellID") + NSLayoutConstraint.activate([ error.widthAnchor.constraint(equalToConstant: 300), view.centerXAnchor.constraint(equalTo: error.centerXAnchor), @@ -99,6 +144,10 @@ private extension LocationsListViewController { self.error.isHidden = viewStatus != .error self.loading.isHidden = viewStatus != .loading self.table.isHidden = viewStatus != .loaded + + if viewStatus == .loaded { + self.table.reloadData() + } } .store(in: &cancellables) }