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