ContactsAppWithJSONPlaceholder is an iOS application demonstrating the use of modern Swift architecture, clean coding practices, and robust UI/Unit testing. The app fetches user data from the JSONPlaceholder API and displays it in a user-friendly interface.
- User List: Displays a list of users fetched from an API.
- Search Functionality: Enables users to search for contacts dynamically.
- User Details: Provides detailed information about each user, including email, phone, and website.
- Custom Transitions: Implements animated transitions for navigation.
- Robust Testing: Includes comprehensive Unit and UI test cases for reliability.
- Swift: The primary programming language for iOS development.
- UIKit: Used for building the appβs user interface.
- SnapKit: Simplifies Auto Layout constraints using a declarative syntax.
- Then: Provides syntactic sugar for initializing objects.
- SwiftUI: Used for building reusable components, integrated into UIKit via
UIHostingController
. - XCTest: Framework for writing Unit and UI tests.
The app follows the Coordinator Pattern for navigation, ensuring a modular and testable structure. This approach centralizes navigation logic, making it easier to manage transitions between screens and maintain a clean separation of concerns. It also employs a ViewModel layer to separate business logic from the view.
ContactsAppWithJSONPlaceholder/
βββ MainProject/
β βββ AppConfig/ # Application configuration files
β βββ Coordinator/ # Coordinator classes for navigation
β βββ Extensions/ # Extensions for utilities and reusability
β βββ Models/ # Data models (User, Address, Geo, Company)
β βββ Network/ # Networking classes (EndPoint, UserRepository)
β βββ Resources/ # Assets and configurations
β βββ Screens/ # UIKit screens (User List, User Details)
β βββ UIComponents/ # Reusable UI components
β βββ Utils/ # General utility classes and helpers
β βββ ViewModels/ # ViewModel classes for business logic
βββ UnitTests/
β βββ Mocks/ # Mock data and repositories for testing
β βββ Tests/ # Unit test cases
βββ UITests/ # UI test cases
Why: Simplifies Auto Layout constraints with a declarative syntax, making UI code cleaner and more maintainable.
containerView.snp.makeConstraints {
$0.top.equalToSuperview().offset(16)
$0.leading.trailing.equalToSuperview().inset(16)
}
Why: Reduces boilerplate code when initializing objects by allowing property configuration in a single block.
private let nameLabel = UILabel().then {
$0.font = UIFont.systemFont(ofSize: 18, weight: .medium)
$0.textColor = .label
}
Why: Enables the creation of reusable, declarative components, which can be seamlessly integrated into UIKit.
struct LoadingIndicator: View {
var body: some View {
ProgressView("Loading...")
}
}
The project includes comprehensive test coverage for both Unit and UI tests.
Tests cover the following:
- ViewModel Logic: Validates state transitions and business logic.
- Repository: Ensures correct data fetching and error handling.
Example:
func testFetchUsersSuccess() {
mockRepository.shouldReturnError = false
let expectation = self.expectation(description: "Fetch users")
mockRepository.fetchUsers { result in
switch result {
case .success(let users):
XCTAssertEqual(users.count, 1)
case .failure:
XCTFail("Expected success, got failure")
}
expectation.fulfill()
}
waitForExpectations(timeout: 1.0)
}
Tests cover:
- User List Screen: Verifies elements like the search bar and table view are displayed correctly.
- Search Functionality: Ensures accurate filtering of user results.
- User Detail Navigation: Validates navigation to and content of the detail screen.
Example:
func testUserDetailNavigation() {
let cell = app.tables.cells.staticTexts["Leanne Graham"]
XCTAssertTrue(cell.exists, "User cell should exist")
cell.tap()
let detailScreenTitle = app.staticTexts["User Details"]
XCTAssertTrue(detailScreenTitle.exists, "User Details screen should be displayed")
}
- Xcode 14.0+
- iOS 15.0+
- Clone the repository:
git clone https://github.com/fatihemres/ContactsAppWithJSONPlaceholder.git
- Open the workspace:
cd ContactsAppWithJSONPlaceholder open ContactsAppWithJSONPlaceholder.xcworkspace
Note: Ensure you open the project using the workspace file. The project includes CocoaPods dependencies, and opening it directly through the
.xcodeproj
file may result in build errors. - Run the app on the simulator or a physical device.
This project is licensed under the MIT License.
- JSONPlaceholder for providing the mock API.
- The contributors who made this project possible.
For questions or collaboration, please reach out at [email protected].