Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
Matt Bruce 2025-03-14 16:56:38 -05:00
commit aed9caabdb
8 changed files with 781 additions and 0 deletions

61
.gitignore vendored Normal file
View File

@ -0,0 +1,61 @@
# macOS system files
.DS_Store
.DS_Store?
# Xcode project files
*.pbxuser
!default.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
xcuserdata/
*.xcscmblueprint
# Build folders and products
DerivedData/
build/
*.ipa
*.dSYM.zip
*.dSYM
# Swift Package Manager
.swiftpm/
Package.resolved
.build/
# Xcode's Package Dependencies
xcshareddata/swiftpm/
# CocoaPods
Pods/
Podfile.lock
# Carthage
Carthage/Build/
# Fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/
fastlane/test_output/
# Xcode Playgrounds
timeline.xctimeline
playground.xcworkspace
# Swift Package Index
.idea/
# xcresult (Xcode test results)
*.xcresult
# SPM Xcode project (if manually generated)
*.xcodeproj/project.xcworkspace/
# Any environment settings
.env
# Firebase
GoogleService-Info.plist
# Secrets or Configuration files (optional)
*.xcconfig

View File

@ -0,0 +1,329 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 77;
objects = {
/* Begin PBXFileReference section */
EA57F6AA2D84C8BA0015D7BC /* EmployeeDirectory-Observed.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "EmployeeDirectory-Observed.app"; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
/* Begin PBXFileSystemSynchronizedRootGroup section */
EA57F6AC2D84C8BA0015D7BC /* EmployeeDirectory-Observed */ = {
isa = PBXFileSystemSynchronizedRootGroup;
path = "EmployeeDirectory-Observed";
sourceTree = "<group>";
};
/* End PBXFileSystemSynchronizedRootGroup section */
/* Begin PBXFrameworksBuildPhase section */
EA57F6A72D84C8BA0015D7BC /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
EA57F6A12D84C8BA0015D7BC = {
isa = PBXGroup;
children = (
EA57F6AC2D84C8BA0015D7BC /* EmployeeDirectory-Observed */,
EA57F6AB2D84C8BA0015D7BC /* Products */,
);
sourceTree = "<group>";
};
EA57F6AB2D84C8BA0015D7BC /* Products */ = {
isa = PBXGroup;
children = (
EA57F6AA2D84C8BA0015D7BC /* EmployeeDirectory-Observed.app */,
);
name = Products;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
EA57F6A92D84C8BA0015D7BC /* EmployeeDirectory-Observed */ = {
isa = PBXNativeTarget;
buildConfigurationList = EA57F6B82D84C8BA0015D7BC /* Build configuration list for PBXNativeTarget "EmployeeDirectory-Observed" */;
buildPhases = (
EA57F6A62D84C8BA0015D7BC /* Sources */,
EA57F6A72D84C8BA0015D7BC /* Frameworks */,
EA57F6A82D84C8BA0015D7BC /* Resources */,
);
buildRules = (
);
dependencies = (
);
fileSystemSynchronizedGroups = (
EA57F6AC2D84C8BA0015D7BC /* EmployeeDirectory-Observed */,
);
name = "EmployeeDirectory-Observed";
packageProductDependencies = (
);
productName = "EmployeeDirectory-Observed";
productReference = EA57F6AA2D84C8BA0015D7BC /* EmployeeDirectory-Observed.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
EA57F6A22D84C8BA0015D7BC /* Project object */ = {
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = 1;
LastSwiftUpdateCheck = 1620;
LastUpgradeCheck = 1620;
TargetAttributes = {
EA57F6A92D84C8BA0015D7BC = {
CreatedOnToolsVersion = 16.2;
};
};
};
buildConfigurationList = EA57F6A52D84C8BA0015D7BC /* Build configuration list for PBXProject "EmployeeDirectory-Observed" */;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = EA57F6A12D84C8BA0015D7BC;
minimizedProjectReferenceProxies = 1;
preferredProjectObjectVersion = 77;
productRefGroup = EA57F6AB2D84C8BA0015D7BC /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
EA57F6A92D84C8BA0015D7BC /* EmployeeDirectory-Observed */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
EA57F6A82D84C8BA0015D7BC /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
EA57F6A62D84C8BA0015D7BC /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
EA57F6B62D84C8BA0015D7BC /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu17;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 18.2;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
name = Debug;
};
EA57F6B72D84C8BA0015D7BC /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu17;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 18.2;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
VALIDATE_PRODUCT = YES;
};
name = Release;
};
EA57F6B92D84C8BA0015D7BC /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"EmployeeDirectory-Observed/Preview Content\"";
DEVELOPMENT_TEAM = 6R7KLBPBLZ;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.mbrucedogs.EmployeeDirectory-Observed";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
EA57F6BA2D84C8BA0015D7BC /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"EmployeeDirectory-Observed/Preview Content\"";
DEVELOPMENT_TEAM = 6R7KLBPBLZ;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.mbrucedogs.EmployeeDirectory-Observed";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
EA57F6A52D84C8BA0015D7BC /* Build configuration list for PBXProject "EmployeeDirectory-Observed" */ = {
isa = XCConfigurationList;
buildConfigurations = (
EA57F6B62D84C8BA0015D7BC /* Debug */,
EA57F6B72D84C8BA0015D7BC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
EA57F6B82D84C8BA0015D7BC /* Build configuration list for PBXNativeTarget "EmployeeDirectory-Observed" */ = {
isa = XCConfigurationList;
buildConfigurations = (
EA57F6B92D84C8BA0015D7BC /* Debug */,
EA57F6BA2D84C8BA0015D7BC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = EA57F6A22D84C8BA0015D7BC /* Project object */;
}

View File

@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@ -0,0 +1,35 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "tinted"
}
],
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@ -0,0 +1,316 @@
//
// ContentView.swift
// EmployeeDirectory-Observed
//
// Created by Matt Bruce on 3/14/25.
//
import SwiftUI
struct ContentView: View {
let service: EmployeeDirectoryService
var body: some View {
EmployeeDirectoryList(viewModel: .init(service: service))
}
}
#Preview {
ContentView(service: MockEmployeeService())
}
//MARK: - Views
public struct EmployeeDirectoryList: View {
@State public var viewModel: EmployeesViewModel
init(viewModel: EmployeesViewModel? = nil) {
_viewModel = .init(wrappedValue: viewModel ?? .init(service: EmployeeService()))
}
public var body: some View {
NavigationStack {
List {
ForEach(viewModel.employees, id: \.id) { employee in
let employeeViewModel = EmployeeViewModel(employee: employee)
NavigationLink(destination: EmployeeDetailView(viewModel: .init(employee: employee))) {
HStack (spacing: 10) {
ProfileImageView(urlString: employeeViewModel.smallPhoto, size: 51)
VStack(alignment: .leading) {
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")
.foregroundColor(.gray)
.font(.caption)
if let phoneNumber = employeeViewModel.phoneNumber {
Label("Call: \(phoneNumber)", systemImage: "phone.fill")
.foregroundColor(.gray)
.font(.caption)
}
}
}
}
}
if viewModel.hasNextPage {
ProgressView()
.frame(maxWidth: .infinity, alignment: .center)
.task {
await viewModel.loadNextPage()
}
}
}
.navigationTitle("Employee Directory")
.listStyle(.insetGrouped)
.task {
if viewModel.employees.isEmpty {
await viewModel.loadNextPage()
}
}
}
}
}
public struct EmployeeDetailView: View {
public let viewModel: EmployeeViewModel
public var body: some View {
VStack(alignment: .leading, spacing: 12) {
// Profile Image with Default Placeholder
ProfileImageView(urlString: viewModel.largePhoto, size: 250) // Uses reusable component
.padding(.bottom)
Text(viewModel.fullName)
.font(.headline)
if let bio = viewModel.biography {
Text(bio)
.font(.footnote)
.foregroundColor(.gray)
.lineLimit(2)
}
Label("Email: \(viewModel.emailAddress)", systemImage: "envelope.fill")
.foregroundColor(.gray)
.font(.caption)
if let phoneNumber = viewModel.phoneNumber {
Label("Call: \(phoneNumber)", systemImage: "phone.fill")
.foregroundColor(.gray)
.font(.caption)
}
Spacer()
}.padding(.all)
}
}
public struct ProfileImageView: View {
public let urlString: String?
public let size: CGFloat
public var body: some View {
return AsyncImage(url: URL(string: urlString ?? "")) { phase in
if let image = phase.image {
image.resizable()
} else if phase.error != nil || urlString == nil {
Image(systemName: "person.circle.fill")
.resizable()
.foregroundColor(.gray)
} else {
ProgressView()
.scaleEffect(min(size / 50, 2)) // Dynamically adjust based on `size`
}
}
.scaledToFill()
.frame(width: size, height: size)
.clipShape(.circle)
.overlay(Circle().stroke(Color.gray, lineWidth: 1))
}
}
//MARK: - ViewModels
@Observable
public class EmployeesViewModel {
public var employees: [Employee] = []
public var isLoading: Bool = false
public var hasNextPage: Bool = true
private var currentPage: Int = 1
private let service: EmployeeDirectoryService
public init(service: EmployeeDirectoryService = EmployeeService()) {
self.service = service
}
public func loadNextPage() async {
guard !isLoading else { return }
isLoading = true
do {
let response = try await service.fetchEmployees(page: currentPage)
employees.append(contentsOf: response.result)
hasNextPage = response.hasNextPage
currentPage += 1
} catch {
print("Error loading employees: \(error)")
}
isLoading = false
}
}
@Observable
public class 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?
// MARK: - Initializer
public init(employee: Employee) {
self.employee = employee
uuid = employee.uuid.uuidString
fullName = "\(employee.firstName) \(employee.lastName)"
phoneNumber = employee.phoneNumber
emailAddress = employee.emailAddress
biography = employee.biography
smallPhoto = employee.photoURLSmall
largePhoto = employee.photoURLLarge
}
}
//MARK: - Protocols
public protocol EmployeeDirectoryService {
func fetchEmployees(page: Int?) async throws -> Employees
}
//MARK: - Services
public class EmployeeService: EmployeeDirectoryService {
public init() {}
public func fetchEmployees(page: Int? = nil) async throws -> Employees {
var endpoint = "https://my.api.mockaroo.com/users.json?key=f298b840"
if let page {
endpoint += "&page=\(page)"
}
return try await NetworkService.shared.fetch(endpoint, type: Employees.self)
}
}
struct MockEmployeeService: EmployeeDirectoryService {
static let sample = createUser(page: 1, index: 1)
static func createUser(page: Int?, index: Int) -> Employee {
let id = UUID()
return Employee(
uuid: id,
firstName: "First \(index + ((page ?? 1) - 1) * 20)",
lastName: "Last \(index + ((page ?? 1) - 1) * 20)",
phoneNumber: "555555\(1000 + index)",
emailAddress: "user\(index)@example.com",
biography: "Biography for employee \(index)",
photoURLSmall: "https://robohash.org/\(id.uuidString).png?size=100x100&set=set1",
photoURLLarge: "https://robohash.org/\(id.uuidString).png?size=400x400&set=set1"
)
}
func fetchEmployees(page: Int?) async throws -> Employees {
// Simulate network delay.
try await Task.sleep(nanoseconds: 500_000_000)
// Create dummy data (20 employees per page).
let dummyEmployees: [Employee] = (0..<20).map { i in
Self.createUser(page: page, index: i)
}
// Simulate that there are more pages if page is less than 5.
let hasNext = (page ?? 1) < 5
return Employees(result: dummyEmployees, hasNextPage: hasNext)
}
}
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)
}
}
//MARK: - Models
public struct Employee: Codable, Identifiable, CustomStringConvertible {
public var id: UUID { uuid }
/// The unique identifier for the employee. Represented as a UUID.
public let uuid: UUID
/// The first name of the employee.
public let firstName: String
/// The last name of the employee.
public let lastName: String
/// The phone number of the employee, sent as an unformatted string (eg, 5556661234).
public let phoneNumber: String?
/// The email address of the employee.
public let emailAddress: String
/// A short, tweet-length (~300 chars) string that the employee provided to describe themselves.
public let biography: String?
/// The URL of the employees small photo. Useful for list view.
public let photoURLSmall: String?
/// The URL of the employees full-size photo.
public let photoURLLarge: String?
private enum CodingKeys: String, CodingKey {
case uuid
case firstName = "first_name"
case lastName = "last_name"
case phoneNumber = "phone_number"
case emailAddress = "email_address"
case biography
case photoURLSmall = "photo_url_small"
case photoURLLarge = "photo_url_large"
}
public var description: String {
"\(firstName) \(lastName)"
}
}
public struct Employees: Codable {
/// Array of Employees
public var result: [Employee]
public var hasNextPage: Bool = false
}

View File

@ -0,0 +1,17 @@
//
// EmployeeDirectory_ObservedApp.swift
// EmployeeDirectory-Observed
//
// Created by Matt Bruce on 3/14/25.
//
import SwiftUI
@main
struct EmployeeDirectory_ObservedApp: App {
var body: some Scene {
WindowGroup {
ContentView(service: EmployeeService())
}
}
}

View File

@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}