Compare commits
No commits in common. "bugfix/afterSubmittal" and "develop" have entirely different histories.
bugfix/aft
...
develop
@ -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: Hashable, Codable {
|
public struct Employee: 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
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
public protocol EmployeeServiceProtocol {
|
public protocol EmployeeServiceProtocol {
|
||||||
|
|
||||||
/// This will get a list of all employees
|
/// This will get a list of all employees
|
||||||
|
/// - Parameter serviceMode: Mode in which to hit.
|
||||||
/// - Returns: An Employees struct
|
/// - Returns: An Employees struct
|
||||||
func getEmployees() async throws -> Employees
|
func getEmployees(_ serviceMode: EmployeeServiceMode) async throws -> Employees
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,22 +7,11 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
/// These are the testing URL Endpoints for different states
|
/// These are the testing URL Endpoints for different states
|
||||||
internal enum EmployeeServiceMode: String, CaseIterable {
|
public enum EmployeeServiceMode: String, CaseIterable {
|
||||||
case production
|
case production
|
||||||
case malformed
|
case malformed
|
||||||
case empty
|
case empty
|
||||||
|
|
||||||
public var service: EmployeeServiceProtocol {
|
|
||||||
switch self {
|
|
||||||
case .production:
|
|
||||||
return EmployeeService.shared
|
|
||||||
case .malformed:
|
|
||||||
return EmployeeMalformedService.shared
|
|
||||||
case .empty:
|
|
||||||
return EmployeeEmptyService.shared
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Enpoint in which to grabe employees from.
|
/// Enpoint in which to grabe employees from.
|
||||||
public var endpoint: String {
|
public var endpoint: String {
|
||||||
switch self {
|
switch self {
|
||||||
@ -50,45 +39,7 @@ public class EmployeeService: EmployeeServiceProtocol {
|
|||||||
/// This will get a list of all employees
|
/// This will get a list of all employees
|
||||||
/// - Parameter serviceMode: Mode in which to hit.
|
/// - Parameter serviceMode: Mode in which to hit.
|
||||||
/// - Returns: An Employees struct
|
/// - Returns: An Employees struct
|
||||||
public func getEmployees() async throws -> Employees {
|
public func getEmployees(_ serviceMode: EmployeeServiceMode = .production) async throws -> Employees {
|
||||||
return try await NetworkService.shared.fetchData(from: EmployeeServiceMode.production.endpoint, as: Employees.self)
|
return try await NetworkService.shared.fetchData(from: serviceMode.endpoint, as: Employees.self)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Service Layer for Employees
|
|
||||||
public class EmployeeMalformedService: EmployeeServiceProtocol {
|
|
||||||
// MARK: - Properties
|
|
||||||
public static let shared = EmployeeMalformedService() // Default shared instance
|
|
||||||
|
|
||||||
// MARK: - Initializer
|
|
||||||
|
|
||||||
public init() {}
|
|
||||||
|
|
||||||
// MARK: - Public Methods
|
|
||||||
|
|
||||||
/// This will get a list of all employees
|
|
||||||
/// - Parameter serviceMode: Mode in which to hit.
|
|
||||||
/// - Returns: An Employees struct
|
|
||||||
public func getEmployees() async throws -> Employees {
|
|
||||||
return try await NetworkService.shared.fetchData(from: EmployeeServiceMode.malformed.endpoint, as: Employees.self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Service Layer for Employees
|
|
||||||
public class EmployeeEmptyService: EmployeeServiceProtocol {
|
|
||||||
// MARK: - Properties
|
|
||||||
public static let shared = EmployeeEmptyService() // Default shared instance
|
|
||||||
|
|
||||||
// MARK: - Initializer
|
|
||||||
|
|
||||||
public init() {}
|
|
||||||
|
|
||||||
// MARK: - Public Methods
|
|
||||||
|
|
||||||
/// This will get a list of all employees
|
|
||||||
/// - Parameter serviceMode: Mode in which to hit.
|
|
||||||
/// - Returns: An Employees struct
|
|
||||||
public func getEmployees() async throws -> Employees {
|
|
||||||
return try await NetworkService.shared.fetchData(from: EmployeeServiceMode.empty.endpoint, as: Employees.self)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,46 +31,24 @@ class EmployeesViewController: UIViewController {
|
|||||||
/// Will show specific state of the viewModel
|
/// Will show specific state of the viewModel
|
||||||
private var footerView: TableFooterView?
|
private var footerView: TableFooterView?
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
/// Setup TableView dataSource
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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
|
||||||
view.addSubview(tableView)
|
view.addSubview(tableView)
|
||||||
tableView.frame = view.bounds
|
tableView.frame = view.bounds
|
||||||
|
|
||||||
@ -92,8 +70,10 @@ class EmployeesViewController: UIViewController {
|
|||||||
private func bindViewModel() {
|
private func bindViewModel() {
|
||||||
viewModel.$employees
|
viewModel.$employees
|
||||||
.receive(on: RunLoop.main)
|
.receive(on: RunLoop.main)
|
||||||
.sink { [weak self] employees in
|
.sink { [weak self] _ in
|
||||||
self?.applySnapshot(employees: employees)
|
self?.updateFooter()
|
||||||
|
self?.tableView.reloadData()
|
||||||
|
self?.tableView.refreshControl?.endRefreshing()
|
||||||
}
|
}
|
||||||
.store(in: &cancellables)
|
.store(in: &cancellables)
|
||||||
|
|
||||||
@ -104,8 +84,6 @@ class EmployeesViewController: UIViewController {
|
|||||||
self?.activityIndicator.startAnimating()
|
self?.activityIndicator.startAnimating()
|
||||||
} else {
|
} else {
|
||||||
self?.activityIndicator.stopAnimating()
|
self?.activityIndicator.stopAnimating()
|
||||||
self?.tableView.refreshControl?.endRefreshing()
|
|
||||||
self?.updateFooter()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.store(in: &cancellables)
|
.store(in: &cancellables)
|
||||||
@ -158,6 +136,22 @@ extension EmployeesViewController {
|
|||||||
case 2: selectedMode = .empty
|
case 2: selectedMode = .empty
|
||||||
default: return
|
default: return
|
||||||
}
|
}
|
||||||
viewModel.changeService(to: selectedMode.service)
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import Foundation
|
|||||||
/// specifically with the EmployeesViewController.
|
/// specifically with the EmployeesViewController.
|
||||||
@MainActor
|
@MainActor
|
||||||
public class EmployeesViewModel: ObservableObject {
|
public class EmployeesViewModel: ObservableObject {
|
||||||
private var employeeService: EmployeeServiceProtocol = EmployeeService()
|
private var serviceMode: EmployeeServiceMode = .production
|
||||||
|
|
||||||
@Published public private(set) var employees: [Employee] = []
|
@Published public private(set) var employees: [Employee] = []
|
||||||
@Published public private(set) var errorMessage: String? = nil
|
@Published public private(set) var errorMessage: String? = nil
|
||||||
@ -27,7 +27,7 @@ public class EmployeesViewModel: ObservableObject {
|
|||||||
Task {
|
Task {
|
||||||
do {
|
do {
|
||||||
// Fetch employees using the async method
|
// Fetch employees using the async method
|
||||||
let wrapper = try await employeeService.getEmployees()
|
let wrapper = try await EmployeeService.shared.getEmployees(serviceMode)
|
||||||
|
|
||||||
// Update published properties
|
// Update published properties
|
||||||
employees = wrapper.employees
|
employees = wrapper.employees
|
||||||
@ -43,8 +43,8 @@ public class EmployeesViewModel: ObservableObject {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func changeService(to employeeService: EmployeeServiceProtocol) {
|
public func changeMode(to mode: EmployeeServiceMode) {
|
||||||
self.employeeService = employeeService
|
serviceMode = mode
|
||||||
fetchEmployees()
|
fetchEmployees()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -128,7 +128,6 @@ public class EmployeeTableViewCell: UITableViewCell {
|
|||||||
|
|
||||||
// Bind the image to the photoImageView
|
// Bind the image to the photoImageView
|
||||||
smallPhotoSubscriber = viewModel.$smallPhoto
|
smallPhotoSubscriber = viewModel.$smallPhoto
|
||||||
.compactMap { $0 }
|
|
||||||
.receive(on: DispatchQueue.main)
|
.receive(on: DispatchQueue.main)
|
||||||
.sink { [weak self] image in
|
.sink { [weak self] image in
|
||||||
self?.photoImageView.image = image
|
self?.photoImageView.image = image
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user