first cut of building out a list of employees using the valid API

this is built off of combine to deal with the state changes of the ViewModel.

Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
Matt Bruce 2025-01-20 18:17:10 -06:00
parent 26864403fd
commit 53fc099d6e

View File

@ -6,14 +6,53 @@
// //
import UIKit import UIKit
import Combine
class EmployeesViewController: UIViewController { class EmployeesViewController: UIViewController {
private let tableView = UITableView()
private let viewModel = EmployeesViewModel()
private var cancellables = Set<AnyCancellable>()
override func viewDidLoad() { public override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
// Do any additional setup after loading the view. setupUI()
bindViewModel()
viewModel.fetchEmployees()
} }
private func setupUI() {
view.backgroundColor = .white
// Configure TableView
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.dataSource = self
view.addSubview(tableView)
tableView.frame = view.bounds
}
private func bindViewModel() {
viewModel.$employees
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.tableView.reloadData()
self?.tableView.refreshControl?.endRefreshing()
}
.store(in: &cancellables)
}
} }
extension EmployeesViewController: UITableViewDataSource {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModel.employees.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let employee = viewModel.employees[indexPath.row]
cell.textLabel?.text = employee.fullName
cell.detailTextLabel?.text = employee.emailAddress
return cell
}
}