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
|
/// Employee Object
|
||||||
/// JSON Object defintion
|
/// JSON Object defintion
|
||||||
/// - https://square.github.io/microsite/mobile-interview-project/
|
/// - 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.
|
/// The unique identifier for the employee. Represented as a UUID.
|
||||||
public let uuid: UUID
|
public let uuid: UUID
|
||||||
|
|||||||
@ -34,24 +34,45 @@ class EmployeesViewController: UIViewController {
|
|||||||
// Prevents multiple calls during rapid scrolling
|
// Prevents multiple calls during rapid scrolling
|
||||||
private var isFetchingNextPage: Bool = false
|
private var isFetchingNextPage: Bool = false
|
||||||
|
|
||||||
|
private var dataSource: UITableViewDiffableDataSource<Int, Employee>!
|
||||||
|
|
||||||
// MARK: - Public Methods
|
// MARK: - Public Methods
|
||||||
|
|
||||||
public override func viewDidLoad() {
|
public override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
setupUI()
|
setupUI()
|
||||||
|
setupDataSource()
|
||||||
bindViewModel()
|
bindViewModel()
|
||||||
viewModel.fetchEmployees()
|
viewModel.fetchEmployees()
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Private Methods
|
// 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
|
/// Setup the UI by adding the views to the main view
|
||||||
private func setupUI() {
|
private func setupUI() {
|
||||||
view.backgroundColor = .white
|
view.backgroundColor = .white
|
||||||
|
|
||||||
// Configure TableView
|
// Configure TableView
|
||||||
tableView.register(EmployeeTableViewCell.self, forCellReuseIdentifier: EmployeeTableViewCell.identifier)
|
tableView.register(EmployeeTableViewCell.self, forCellReuseIdentifier: EmployeeTableViewCell.identifier)
|
||||||
tableView.dataSource = self
|
|
||||||
tableView.delegate = self
|
tableView.delegate = self
|
||||||
view.addSubview(tableView)
|
view.addSubview(tableView)
|
||||||
tableView.frame = view.bounds
|
tableView.frame = view.bounds
|
||||||
@ -108,13 +129,9 @@ class EmployeesViewController: UIViewController {
|
|||||||
/// Using the ViewModel setup combine handlers
|
/// Using the ViewModel setup combine handlers
|
||||||
private func bindViewModel() {
|
private func bindViewModel() {
|
||||||
viewModel.$employees
|
viewModel.$employees
|
||||||
.scan(([], [])) { previous, current in
|
|
||||||
(previous.1, current) // (oldValue, newValue)
|
|
||||||
}
|
|
||||||
.receive(on: RunLoop.main)
|
.receive(on: RunLoop.main)
|
||||||
.sink { [weak self] oldValue, newValue in
|
.sink { [weak self] employees in
|
||||||
self?.animateEmployeeChanges(from: oldValue, to: newValue)
|
self?.applySnapshot(employees: employees)
|
||||||
self?.updateFooter()
|
|
||||||
}
|
}
|
||||||
.store(in: &cancellables)
|
.store(in: &cancellables)
|
||||||
|
|
||||||
@ -235,19 +252,3 @@ extension EmployeesViewController {
|
|||||||
viewModel.changeMode(to: selectedMode)
|
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