fixed issues in refresh

Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
Matt Bruce 2025-01-22 14:09:09 -06:00
parent 67b2163134
commit 4e59fa6f6a
2 changed files with 9 additions and 6 deletions

View File

@ -139,12 +139,13 @@ class EmployeesViewController: UIViewController {
viewModel.$isLoading viewModel.$isLoading
.receive(on: RunLoop.main) .receive(on: RunLoop.main)
.sink { [weak self] isLoading in .sink { [weak self] isLoading in
guard let self, let refreshControl = self.tableView.refreshControl else { return }
if isLoading { if isLoading {
self?.activityIndicator.startAnimating() activityIndicator.startAnimating()
} else { } else {
self?.activityIndicator.stopAnimating() activityIndicator.stopAnimating()
self?.tableView.refreshControl?.endRefreshing() refreshControl.endRefreshing()
self?.updateFooter() updateFooter()
} }
} }
.store(in: &cancellables) .store(in: &cancellables)
@ -182,7 +183,7 @@ class EmployeesViewController: UIViewController {
var footerMessage: String? var footerMessage: String?
// Check for error messages or empty state first // Check for error messages or empty state first
if let message = viewModel.errorMessage ?? (viewModel.employees.isEmpty && !viewModel.isLoading ? "No employees found, please try to refresh." : nil) { if let message = viewModel.errorMessage ?? (viewModel.employees.isEmpty && viewModel.isLoading == false ? "No employees found, please try to refresh." : nil) {
footerMessage = message footerMessage = message
} }

View File

@ -46,10 +46,11 @@ public class EmployeesViewModel: ObservableObject {
/// Fetch employees for the given page /// Fetch employees for the given page
public func fetchEmployees(page: Int = 1) { public func fetchEmployees(page: Int = 1) {
// Prevent duplicate calls // Prevent duplicate calls
guard !isLoading else { return } guard !isLoading else { return }
errorMessage = nil
isLoading = true isLoading = true
Task { Task {
do { do {
// Fetch employees using the paginated API // Fetch employees using the paginated API
@ -68,6 +69,7 @@ public class EmployeesViewModel: ObservableObject {
} else { } else {
employees.append(contentsOf: wrapper.employees) // Append for subsequent pages employees.append(contentsOf: wrapper.employees) // Append for subsequent pages
} }
} catch { } catch {
// Handle errors // Handle errors
errorMessage = "An unexpected error occurred, please try to refresh." errorMessage = "An unexpected error occurred, please try to refresh."