initial commit

This commit is contained in:
Matt Bruce 2025-03-04 14:54:57 -06:00
parent 356708f6d7
commit 09bd1740de
17 changed files with 548 additions and 25 deletions

View File

@ -29,9 +29,22 @@
EAC304C22D76693000D9006D /* EmployeeDirectoryUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = EmployeeDirectoryUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */
EA57F4B72D778F140015D7BC /* Exceptions for "EmployeeDirectory" folder in "EmployeeDirectory" target */ = {
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
membershipExceptions = (
Info.plist,
);
target = EAC304A72D76692F00D9006D /* EmployeeDirectory */;
};
/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */
/* Begin PBXFileSystemSynchronizedRootGroup section */
EAC304AA2D76692F00D9006D /* EmployeeDirectory */ = {
isa = PBXFileSystemSynchronizedRootGroup;
exceptions = (
EA57F4B72D778F140015D7BC /* Exceptions for "EmployeeDirectory" folder in "EmployeeDirectory" target */,
);
path = EmployeeDirectory;
sourceTree = "<group>";
};
@ -399,6 +412,7 @@
DEVELOPMENT_TEAM = 6R7KLBPBLZ;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = EmployeeDirectory/Info.plist;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
@ -428,6 +442,7 @@
DEVELOPMENT_TEAM = 6R7KLBPBLZ;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = EmployeeDirectory/Info.plist;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "BCD20A5B-B7B0-4775-A851-C44BE1718BD6"
type = "1"
version = "2.0">
</Bucket>

View File

@ -1,24 +0,0 @@
//
// ContentView.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/3/25.
//
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
}
#Preview {
ContentView()
}

View File

@ -11,7 +11,7 @@ import SwiftUI
struct EmployeeDirectoryApp: App {
var body: some Scene {
WindowGroup {
ContentView()
EmployeeListView()
}
}
}

View File

@ -0,0 +1,26 @@
//
// String.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/4/25.
//
import Foundation
extension String {
/// Formats a string into a US phone number format (XXX-XXX-XXXX).
/// Non-numeric characters are removed, and formatting is applied based on the length of the string.
/// - Returns: A formatted phone number as a string.
internal func formatUSNumber() -> String {
let mask = "XXX-XXX-XXXX"
let digits = filter { $0.isNumber }
var index = digits.startIndex
return mask.reduce(into: "") { result, char in
if char == "X", index < digits.endIndex {
result.append(digits[index])
index = digits.index(after: index)
} else {
result.append(char)
}
}
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>dummyimage.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
</dict>
</plist>

View File

@ -0,0 +1,50 @@
//
// Employee.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/3/25.
//
import Foundation
/// Employee Object
/// JSON Object defintion
/// - https://square.github.io/microsite/mobile-interview-project/
public struct Employee: Identifiable, Hashable, Codable {
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"
}
}

View File

@ -0,0 +1,14 @@
//
// Employees.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/3/25.
//
import Foundation
/// Wrapper JSON Class for the Employees
public struct Employees: Codable {
/// Array of Employees
public var result: [Employee]
public var hasNextPage: Bool = false
}

View File

@ -0,0 +1,10 @@
//
// EmployeeServiceProtocol.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/3/25.
//
public protocol EmployeeServiceProtocol {
func getUsers(page: Int?) async throws -> Employees
}

View File

@ -0,0 +1,76 @@
//
// EmployeeService.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/3/25.
//
import Foundation
public class EmployeeService: EmployeeServiceProtocol {
// MARK: - Properties
public static let shared = EmployeeService() // Default shared instance
// MARK: - Initializer
public init() {}
// MARK: - Public Methods
/// This will get a list of all employees
/// - Returns: An Employees struct
public func getUsers(page: Int? = nil) async throws -> Employees {
var endpoint = "https://my.api.mockaroo.com/users.json?key=f298b840"
if let page {
endpoint += "&page=\(page)"
}
//ensure a valid URL
guard let url = URL(string: endpoint) else {
throw URLError(.badURL)
}
// Perform network request
let (data, response) = try await URLSession.shared.data(from: url)
// Validate HTTP response
guard let httpResponse = response as? HTTPURLResponse,
200..<300 ~= httpResponse.statusCode else {
throw URLError(.badServerResponse)
}
// Decode the response into the specified type
return try JSONDecoder().decode(Employees.self, from: data)
}
}
// Mock Service Implementation for testing & previews.
struct MockEmployeeService: EmployeeServiceProtocol {
static let sample = createUser(page: 1, index: 1)
static func createUser(page: Int?, index: Int) -> Employee {
Employee(
uuid: UUID(),
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://example.com/photo_small.jpg",
photoURLLarge: "https://example.com/photo_large.jpg"
)
}
func getUsers(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)
}
}

View File

@ -0,0 +1,35 @@
//
// EmployeeViewModel.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/3/25.
//
import Foundation
import Combine
@MainActor
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?.formatUSNumber()
emailAddress = employee.emailAddress
biography = employee.biography
smallPhoto = employee.photoURLSmall
largePhoto = employee.photoURLLarge
}
}

View File

@ -0,0 +1,40 @@
//
// EmployeesViewModel.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/3/25.
//
import Foundation
import Combine
/// ViewModel that will be bound to an Employees model and used
/// specifically with the EmployeesViewController.
@MainActor
public class EmployeesViewModel: ObservableObject {
@Published var employees: [Employee] = []
@Published var isLoading = false
@Published var hasNextPage = false
private var currentPage = 1
let service: EmployeeServiceProtocol
init(service: EmployeeServiceProtocol) {
self.service = service
}
/// Loads the next page of employees.
func loadEmployees() async {
guard !isLoading else { return }
isLoading = true
do {
let response = try await service.getUsers(page: currentPage)
employees.append(contentsOf: response.result)
hasNextPage = response.hasNextPage
currentPage += 1
} catch {
print("Error loading employees: \(error)")
}
isLoading = false
}
}

View File

@ -0,0 +1,80 @@
//
// ContactButtonView.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/4/25.
//
import SwiftUI
enum ContactType {
case phone, email
}
struct ContactButtonView: View {
let contactType: ContactType
let contactValue: String
let enabled: Bool
init(contactType: ContactType, contactValue: String, enabled: Bool = true) {
self.contactType = contactType
self.contactValue = contactValue
self.enabled = enabled
}
var body: some View {
if !enabled {
Label(labelText(for: contactValue), systemImage: iconName)
.foregroundColor(.gray)
.font(.caption)
} else {
Button(action: {
if let url = URL(string: urlString(for: contactValue)) {
UIApplication.shared.open(url)
}
}) {
Label(labelText(for: contactValue), systemImage: iconName)
.foregroundColor(.blue)
.font(.caption)
}
}
}
// MARK: - Helpers
private var iconName: String {
switch contactType {
case .phone:
return "phone.fill"
case .email:
return "envelope.fill"
}
}
private func labelText(for value: String) -> String {
switch contactType {
case .phone:
return "Call \(value)"
case .email:
return "Email \(value)"
}
}
private func urlString(for value: String) -> String {
switch contactType {
case .phone:
return "tel://\(value)"
case .email:
return "mailto:\(value)"
}
}
}
#Preview {
VStack (alignment: .leading) {
Text("Phone Number:")
ContactButtonView(contactType: .phone, contactValue: MockEmployeeService.sample.phoneNumber!.formatUSNumber()).padding()
Text("Email Address:")
ContactButtonView(contactType: .email, contactValue: MockEmployeeService.sample.emailAddress).padding()
}
}

View File

@ -0,0 +1,43 @@
//
// EmployeeDetailsView.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/4/25.
//
import SwiftUI
struct EmployeeDetailsView: View {
let viewModel: EmployeeViewModel
let defaultImage = "person.circle.fill" // SF Symbol as fallback
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)
}
ContactButtonView(contactType: .email, contactValue: viewModel.emailAddress)
if let phone = viewModel.phoneNumber {
ContactButtonView(contactType: .phone, contactValue: phone)
}
Spacer()
}
.padding(.all)
}
}
#Preview {
EmployeeDetailsView(viewModel: .init(employee: MockEmployeeService.sample)).padding(.all)
}

View File

@ -0,0 +1,49 @@
//
// ContentView.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/3/25.
//
import SwiftUI
@MainActor
struct EmployeeListView: View {
@StateObject public var viewModel: EmployeesViewModel
// Dependency injection via the initializer.
init(viewModel: EmployeesViewModel? = nil) {
_viewModel = StateObject(wrappedValue: viewModel ?? .init(service: EmployeeService()))
}
var body: some View {
NavigationView {
List {
ForEach(viewModel.employees) { employee in
NavigationLink(destination: EmployeeDetailsView(viewModel: .init(employee: employee))) {
EmployeeRowView(viewModel: .init(employee: employee))
}
.listRowSeparator(.hidden)
}
if viewModel.hasNextPage {
ProgressView()
.frame(maxWidth: .infinity, alignment: .center)
.task {
await viewModel.loadEmployees()
}
}
}
.navigationTitle("Employees")
.listStyle(.plain)
}
.task {
if viewModel.employees.isEmpty {
await viewModel.loadEmployees()
}
}
}
}
#Preview {
EmployeeListView(viewModel: .init(service: MockEmployeeService()))
}

View File

@ -0,0 +1,47 @@
//
// EmployeeRowView.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/3/25.
//
import SwiftUI
struct EmployeeRowView: View {
let viewModel: EmployeeViewModel
let defaultImage = "person.circle.fill" // SF Symbol as fallback
var body: some View {
HStack(alignment: .top, spacing: 12) {
// Profile Image with Default Placeholder
ProfileImageView(urlString: viewModel.smallPhoto, size: 50) // Uses reusable component
// Employee Info Stack
VStack(alignment: .leading, spacing: 4) {
Text(viewModel.fullName)
.font(.headline)
if let bio = viewModel.biography {
Text(bio)
.font(.footnote)
.foregroundColor(.gray)
.lineLimit(2)
}
ContactButtonView(contactType: .email, contactValue: viewModel.emailAddress, enabled: false)
if let phone = viewModel.phoneNumber {
ContactButtonView(contactType: .phone, contactValue: phone, enabled: false)
}
}
Spacer() // Pushes everything to the left
}
.padding(.vertical, 8)
}
}
#Preview {
EmployeeRowView(viewModel: .init(employee: MockEmployeeService.sample)).padding()
}

View File

@ -0,0 +1,37 @@
//
// ProfileImageView.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/3/25.
//
import SwiftUI
struct ProfileImageView: View {
let urlString: String?
let size: CGFloat // Allows us to customize the image size
private let defaultImage = "person.circle.fill" // SF Symbol
var body: some View {
AsyncImage(url: URL(string: urlString ?? "")) { phase in
if let image = phase.image {
image.resizable()
} else if phase.error != nil || urlString == nil {
Image(systemName: defaultImage)
.resizable()
.foregroundColor(.gray)
} else {
ProgressView() // Show loader while fetching
}
}
.scaledToFill()
.frame(width: size, height: size)
.clipShape(Circle()) // Rounded shape
.overlay(Circle().stroke(Color.gray, lineWidth: 1)) // Border
}
}
#Preview {
ProfileImageView(urlString: MockEmployeeService.sample.photoURLSmall, size: 100).padding()
}