now using diffable datasource
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
parent
3238defe84
commit
7501ce5936
@ -27,7 +27,7 @@ public enum EmployeeType: String, Codable, CustomStringConvertible {
|
||||
/// Employee Object
|
||||
/// JSON Object defintion
|
||||
/// - https://square.github.io/microsite/mobile-interview-project/
|
||||
public struct Employee: Codable {
|
||||
public struct Employee: Hashable, Codable {
|
||||
|
||||
/// The unique identifier for the employee. Represented as a UUID.
|
||||
public let uuid: UUID
|
||||
|
||||
@ -34,24 +34,45 @@ class EmployeesViewController: UIViewController {
|
||||
// Prevents multiple calls during rapid scrolling
|
||||
private var isFetchingNextPage: Bool = false
|
||||
|
||||
private var dataSource: UITableViewDiffableDataSource<Int, Employee>!
|
||||
|
||||
// MARK: - Public Methods
|
||||
|
||||
public override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
setupUI()
|
||||
setupDataSource()
|
||||
bindViewModel()
|
||||
viewModel.fetchEmployees()
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
|
||||
private func setupDataSource() {
|
||||
dataSource = UITableViewDiffableDataSource<Int, Employee>(tableView: tableView) { tableView, indexPath, employee in
|
||||
guard let cell = tableView.dequeueReusableCell(withIdentifier: EmployeeTableViewCell.identifier,for: indexPath) as? EmployeeTableViewCell else {
|
||||
return UITableViewCell()
|
||||
}
|
||||
cell.configure(with: EmployeeCellViewModel(employee: employee))
|
||||
return cell
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Snapshot Handling
|
||||
|
||||
private func applySnapshot(employees: [Employee]) {
|
||||
var snapshot = NSDiffableDataSourceSnapshot<Int, Employee>()
|
||||
snapshot.appendSections([0])
|
||||
snapshot.appendItems(employees, toSection: 0)
|
||||
dataSource.apply(snapshot, animatingDifferences: true)
|
||||
}
|
||||
|
||||
/// Setup the UI by adding the views to the main view
|
||||
private func setupUI() {
|
||||
view.backgroundColor = .white
|
||||
|
||||
// Configure TableView
|
||||
tableView.register(EmployeeTableViewCell.self, forCellReuseIdentifier: EmployeeTableViewCell.identifier)
|
||||
tableView.dataSource = self
|
||||
tableView.delegate = self
|
||||
view.addSubview(tableView)
|
||||
tableView.frame = view.bounds
|
||||
@ -108,13 +129,9 @@ class EmployeesViewController: UIViewController {
|
||||
/// Using the ViewModel setup combine handlers
|
||||
private func bindViewModel() {
|
||||
viewModel.$employees
|
||||
.scan(([], [])) { previous, current in
|
||||
(previous.1, current) // (oldValue, newValue)
|
||||
}
|
||||
.receive(on: RunLoop.main)
|
||||
.sink { [weak self] oldValue, newValue in
|
||||
self?.animateEmployeeChanges(from: oldValue, to: newValue)
|
||||
self?.updateFooter()
|
||||
.sink { [weak self] employees in
|
||||
self?.applySnapshot(employees: employees)
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
@ -235,19 +252,3 @@ extension EmployeesViewController {
|
||||
viewModel.changeMode(to: selectedMode)
|
||||
}
|
||||
}
|
||||
|
||||
/// Mark: - UITableViewDataSource
|
||||
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 {
|
||||
guard let cell = tableView.dequeueReusableCell(withIdentifier: EmployeeTableViewCell.identifier, for: indexPath) as? EmployeeTableViewCell else {
|
||||
return UITableViewCell()
|
||||
}
|
||||
let employee = viewModel.employees[indexPath.row]
|
||||
cell.configure(with: EmployeeCellViewModel(employee: employee))
|
||||
return cell
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user