this is built off of combine to deal with the state changes of the ViewModel. Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
59 lines
1.7 KiB
Swift
59 lines
1.7 KiB
Swift
//
|
|
// ViewController.swift
|
|
// EmployeeDirectory
|
|
//
|
|
// Created by Matt Bruce on 1/20/25.
|
|
//
|
|
|
|
import UIKit
|
|
import Combine
|
|
|
|
class EmployeesViewController: UIViewController {
|
|
private let tableView = UITableView()
|
|
private let viewModel = EmployeesViewModel()
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
public override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
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
|
|
}
|
|
}
|