added refresh

Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
Matt Bruce 2025-03-15 11:39:09 -05:00
parent f72c31b0d3
commit cb69829d26

View File

@ -71,13 +71,6 @@ public struct EmployeeDirectoryList: View {
} }
.tint(.yellow) .tint(.yellow)
} }
.swipeActions {
Button(role: .destructive) {
print("\(employee.firstName) deleted")
} label: {
Label("Delete", systemImage: "trash")
}
}
} }
.onMove(perform: moveEmployee) .onMove(perform: moveEmployee)
.onDelete(perform: deleteEmployees) .onDelete(perform: deleteEmployees)
@ -90,6 +83,9 @@ public struct EmployeeDirectoryList: View {
} }
} }
} }
.refreshable {
await viewModel.refresh()
}
.navigationTitle("Employee Directory") .navigationTitle("Employee Directory")
.navigationDestination(for: Employee.self) { employee in .navigationDestination(for: Employee.self) { employee in
EmployeeDetailView(viewModel: .init(employee: employee)) EmployeeDetailView(viewModel: .init(employee: employee))
@ -106,19 +102,14 @@ public struct EmployeeDirectoryList: View {
} }
} }
// Move Function (Reordering Rows)
private func moveEmployee(from source: IndexSet, to destination: Int) { private func moveEmployee(from source: IndexSet, to destination: Int) {
viewModel.employees.move(fromOffsets: source, toOffset: destination) viewModel.move(employeeFrom: source, to: destination)
print("moveEmployee called")
} }
// Delete Function (Swipe to Delete)
private func deleteEmployees(at offsets: IndexSet) { private func deleteEmployees(at offsets: IndexSet) {
viewModel.employees.remove(atOffsets: offsets) viewModel.delete(employeeAt: offsets)
} print("deleteEmployees called")
// Single Delete Function (For Swipe Actions)
private func deleteEmployee(_ employee: Employee) {
viewModel.employees.removeAll { $0.id == employee.id }
} }
} }
@ -217,23 +208,40 @@ public class EmployeesViewModel {
} }
isLoading = false isLoading = false
} }
public func refresh() async {
currentPage = 1
employees.removeAll()
await loadNextPage()
} }
@Observable public func move(employeeFrom source: IndexSet, to destination: Int) {
public class EmployeeViewModel { employees.move(fromOffsets: source, toOffset: destination)
}
public func delete(employeeAt offsets: IndexSet) {
employees.remove(atOffsets: offsets)
}
public func delete(employee: Employee) {
employees.removeAll { $0.id == employee.id }
}
}
public struct EmployeeViewModel {
// MARK: - Properties // MARK: - Properties
private let employee: Employee private let employee: Employee
public private(set) var uuid: String public let uuid: String
public private(set) var fullName: String public let fullName: String
public private(set) var phoneNumber: String? public let phoneNumber: String?
public private(set) var emailAddress: String public let emailAddress: String
public private(set) var biography: String? public let biography: String?
public private(set) var smallPhoto: String? public let smallPhoto: String?
public private(set) var largePhoto: String? public let largePhoto: String?
public private(set) var emailAddressURL: URL? public let emailAddressURL: URL?
public private(set) var phoneNumberURL: URL? public let phoneNumberURL: URL?
// MARK: - Initializer // MARK: - Initializer
public init(employee: Employee) { public init(employee: Employee) {
@ -248,11 +256,11 @@ public class EmployeeViewModel {
if let url = URL(string: "mailto:\(emailAddress)"), UIApplication.shared.canOpenURL(url) { if let url = URL(string: "mailto:\(emailAddress)"), UIApplication.shared.canOpenURL(url) {
emailAddressURL = url emailAddressURL = url
} } else { emailAddressURL = nil }
if let phoneNumber, let url = URL(string: "tel:\(phoneNumber)"), UIApplication.shared.canOpenURL(url) { if let phoneNumber, let url = URL(string: "tel:\(phoneNumber)"), UIApplication.shared.canOpenURL(url) {
phoneNumberURL = url phoneNumberURL = url
} } else { phoneNumberURL = nil }
} }
} }