41 lines
1.1 KiB
Swift
41 lines
1.1 KiB
Swift
//
|
|
// EmployeesViewModel.swift
|
|
// EmployeeDirectory
|
|
//
|
|
// Created by Matt Bruce on 3/3/25.
|
|
//
|
|
import Foundation
|
|
import Combine
|
|
|
|
/// ViewModel that will be bound to an Employees model and used
|
|
/// specifically with the EmployeesViewController.
|
|
@MainActor
|
|
public class EmployeesViewModel: ObservableObject {
|
|
@Published var employees: [Employee] = []
|
|
@Published var isLoading = false
|
|
@Published var hasNextPage = false
|
|
|
|
private var currentPage = 1
|
|
let service: EmployeeServiceProtocol
|
|
|
|
init(service: EmployeeServiceProtocol) {
|
|
self.service = service
|
|
}
|
|
|
|
/// Loads the next page of employees.
|
|
func loadEmployees() async {
|
|
guard !isLoading else { return }
|
|
isLoading = true
|
|
do {
|
|
let response = try await service.getUsers(page: currentPage)
|
|
employees.append(contentsOf: response.result)
|
|
hasNextPage = response.hasNextPage
|
|
currentPage += 1
|
|
} catch {
|
|
print("Error loading employees: \(error)")
|
|
}
|
|
isLoading = false
|
|
}
|
|
}
|
|
|