added refresh
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
parent
f72c31b0d3
commit
cb69829d26
@ -71,13 +71,6 @@ public struct EmployeeDirectoryList: View {
|
||||
}
|
||||
.tint(.yellow)
|
||||
}
|
||||
.swipeActions {
|
||||
Button(role: .destructive) {
|
||||
print("\(employee.firstName) deleted")
|
||||
} label: {
|
||||
Label("Delete", systemImage: "trash")
|
||||
}
|
||||
}
|
||||
}
|
||||
.onMove(perform: moveEmployee)
|
||||
.onDelete(perform: deleteEmployees)
|
||||
@ -90,6 +83,9 @@ public struct EmployeeDirectoryList: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.refreshable {
|
||||
await viewModel.refresh()
|
||||
}
|
||||
.navigationTitle("Employee Directory")
|
||||
.navigationDestination(for: Employee.self) { employee in
|
||||
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) {
|
||||
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) {
|
||||
viewModel.employees.remove(atOffsets: offsets)
|
||||
}
|
||||
|
||||
// Single Delete Function (For Swipe Actions)
|
||||
private func deleteEmployee(_ employee: Employee) {
|
||||
viewModel.employees.removeAll { $0.id == employee.id }
|
||||
viewModel.delete(employeeAt: offsets)
|
||||
print("deleteEmployees called")
|
||||
}
|
||||
}
|
||||
|
||||
@ -217,23 +208,40 @@ public class EmployeesViewModel {
|
||||
}
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
public func refresh() async {
|
||||
currentPage = 1
|
||||
employees.removeAll()
|
||||
await loadNextPage()
|
||||
}
|
||||
|
||||
public func move(employeeFrom source: IndexSet, to destination: Int) {
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
||||
@Observable
|
||||
public class EmployeeViewModel {
|
||||
public struct EmployeeViewModel {
|
||||
// MARK: - Properties
|
||||
|
||||
private let employee: Employee
|
||||
|
||||
public private(set) var uuid: String
|
||||
public private(set) var fullName: String
|
||||
public private(set) var phoneNumber: String?
|
||||
public private(set) var emailAddress: String
|
||||
public private(set) var biography: String?
|
||||
public private(set) var smallPhoto: String?
|
||||
public private(set) var largePhoto: String?
|
||||
public private(set) var emailAddressURL: URL?
|
||||
public private(set) var phoneNumberURL: URL?
|
||||
public let uuid: String
|
||||
public let fullName: String
|
||||
public let phoneNumber: String?
|
||||
public let emailAddress: String
|
||||
public let biography: String?
|
||||
public let smallPhoto: String?
|
||||
public let largePhoto: String?
|
||||
public let emailAddressURL: URL?
|
||||
public let phoneNumberURL: URL?
|
||||
|
||||
// MARK: - Initializer
|
||||
public init(employee: Employee) {
|
||||
@ -248,11 +256,11 @@ public class EmployeeViewModel {
|
||||
|
||||
if let url = URL(string: "mailto:\(emailAddress)"), UIApplication.shared.canOpenURL(url) {
|
||||
emailAddressURL = url
|
||||
}
|
||||
} else { emailAddressURL = nil }
|
||||
|
||||
if let phoneNumber, let url = URL(string: "tel:\(phoneNumber)"), UIApplication.shared.canOpenURL(url) {
|
||||
phoneNumberURL = url
|
||||
}
|
||||
} else { phoneNumberURL = nil }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user