converted to navigationStack

This commit is contained in:
Matt Bruce 2025-03-15 10:30:22 -05:00
parent aed9caabdb
commit ab5593d773

View File

@ -23,47 +23,43 @@ struct ContentView: View {
//MARK: - Views
public struct EmployeeDirectoryList: View {
@State public var viewModel: EmployeesViewModel
@State public var path = NavigationPath()
init(viewModel: EmployeesViewModel? = nil) {
_viewModel = .init(wrappedValue: viewModel ?? .init(service: EmployeeService()))
}
public var body: some View {
NavigationStack {
NavigationStack(path: $path) {
List {
ForEach(viewModel.employees, id: \.id) { employee in
let employeeViewModel = EmployeeViewModel(employee: employee)
NavigationLink(destination: EmployeeDetailView(viewModel: .init(employee: employee))) {
HStack (spacing: 10) {
HStack (spacing: 10) {
ProfileImageView(urlString: employeeViewModel.smallPhoto, size: 51)
VStack(alignment: .leading) {
Text(employeeViewModel.fullName)
.font(.headline)
ProfileImageView(urlString: employeeViewModel.smallPhoto, size: 51)
if let bio = employeeViewModel.biography {
Text(bio)
.font(.footnote)
.foregroundColor(.gray)
.lineLimit(2)
}
VStack(alignment: .leading) {
Label("Email: \(employeeViewModel.emailAddress)", systemImage: "envelope.fill")
.foregroundColor(.gray)
.font(.caption)
Text(employeeViewModel.fullName)
.font(.headline)
if let bio = employeeViewModel.biography {
Text(bio)
.font(.footnote)
.foregroundColor(.gray)
.lineLimit(2)
}
Label("Email: \(employeeViewModel.emailAddress)", systemImage: "envelope.fill")
if let phoneNumber = employeeViewModel.phoneNumber {
Label("Call: \(phoneNumber)", systemImage: "phone.fill")
.foregroundColor(.gray)
.font(.caption)
if let phoneNumber = employeeViewModel.phoneNumber {
Label("Call: \(phoneNumber)", systemImage: "phone.fill")
.foregroundColor(.gray)
.font(.caption)
}
}
}
}.onTapGesture {
path.append(employee)
}
}
if viewModel.hasNextPage {
ProgressView()
.frame(maxWidth: .infinity, alignment: .center)
@ -73,6 +69,9 @@ public struct EmployeeDirectoryList: View {
}
}
.navigationTitle("Employee Directory")
.navigationDestination(for: Employee.self) { employee in
EmployeeDetailView(viewModel: .init(employee: employee))
}
.listStyle(.insetGrouped)
.task {
if viewModel.employees.isEmpty {
@ -112,7 +111,9 @@ public struct EmployeeDetailView: View {
}
Spacer()
}.padding(.all)
}
.navigationTitle("Employee Details")
.padding(.all)
}
}
@ -266,7 +267,7 @@ public class NetworkService {
}
//MARK: - Models
public struct Employee: Codable, Identifiable, CustomStringConvertible {
public struct Employee: Codable, Identifiable, CustomStringConvertible, Hashable {
public var id: UUID { uuid }
/// The unique identifier for the employee. Represented as a UUID.