59 lines
1.8 KiB
Swift
59 lines
1.8 KiB
Swift
//
|
|
// ContentView.swift
|
|
// EmployeeDirectory
|
|
//
|
|
// Created by Matt Bruce on 3/3/25.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
@MainActor
|
|
struct EmployeeListView: View {
|
|
@StateObject public var viewModel: EmployeesViewModel
|
|
@State private var path = NavigationPath()
|
|
|
|
// Dependency injection via the initializer.
|
|
init(viewModel: EmployeesViewModel? = nil) {
|
|
_viewModel = StateObject(wrappedValue: viewModel ?? .init(service: EmployeeService()))
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack(path: $path) {
|
|
List {
|
|
//build out dynamic rows first
|
|
ForEach(viewModel.employees, id: \.id) { employee in
|
|
EmployeeRowView(viewModel: .init(employee: employee))
|
|
.onTapGesture {
|
|
path.append(employee)
|
|
}
|
|
.listRowInsets(.none)
|
|
.listRowSeparator(.hidden)
|
|
}
|
|
|
|
//add static row if there is a next page
|
|
if viewModel.hasNextPage {
|
|
ProgressView()
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
.task {
|
|
await viewModel.loadEmployees()
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Employees")
|
|
.navigationDestination(for: Employee.self, destination: { employee in
|
|
EmployeeDetailsView(viewModel: .init(employee: employee))
|
|
})
|
|
.listStyle(.insetGrouped)
|
|
}
|
|
.task {
|
|
if viewModel.employees.isEmpty {
|
|
await viewModel.loadEmployees()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
EmployeeListView(viewModel: .init(service: MockEmployeeService()))
|
|
}
|