Compare commits

...

2 Commits

Author SHA1 Message Date
12ff7b0e8a added code for a network service
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
2025-03-14 16:50:37 -05:00
b2ce994cea fixeds 2025-03-06 14:24:33 -06:00
3 changed files with 26 additions and 3 deletions

View File

@ -43,4 +43,24 @@ public class EmployeeService: EmployeeServiceProtocol {
}
}
public class Networkservice {
public static let shared = Networkservice()
public init(){}
public func fetch<T: Decodable>(endpoint: String, type: T.Type) async throws -> T {
guard let url = URL(string: endpoint) else {
throw URLError(.badURL)
}
let (data, response) = try await URLSession.shared.data(from: url)
guard let httpResponse = response as? HTTPURLResponse,
200..<300 ~= httpResponse.statusCode else {
throw URLError(.badServerResponse)
}
return try JSONDecoder().decode(T.self, from: data)
}
}

View File

@ -19,12 +19,16 @@ struct EmployeeListView: View {
var body: some View {
NavigationView {
List {
ForEach(viewModel.employees) { employee in
//build out dynamic rows first
ForEach(viewModel.employees, id: \.id) { employee in
NavigationLink(destination: EmployeeDetailsView(viewModel: .init(employee: employee))) {
EmployeeRowView(viewModel: .init(employee: employee))
}
.listRowInsets(.none)
.listRowSeparator(.hidden)
}
//add static row if there is a next page
if viewModel.hasNextPage {
ProgressView()
.frame(maxWidth: .infinity, alignment: .center)
@ -34,7 +38,7 @@ struct EmployeeListView: View {
}
}
.navigationTitle("Employees")
.listStyle(.plain)
.listStyle(.insetGrouped)
}
.task {
if viewModel.employees.isEmpty {

View File

@ -37,7 +37,6 @@ struct EmployeeRowView: View {
Spacer() // Pushes everything to the left
}
.padding(.vertical, 8)
}
}