Architecture Overview
The data layer consists of three main components:- Remote Data Sources: API clients using Retrofit
- Local Data Sources: Room database for caching and pagination
- Repositories: Implementations that coordinate between remote and local sources
API Clients
Retrofit interfaces for network calls
Local Database
Room database for offline support
Repositories
Data coordination and mapping
Remote Data Sources
API clients are defined as Retrofit interfaces that describe the endpoints and HTTP methods.ArticlesApiClient
The main API client for fetching space flight news articles:data/remote/articles/ArticlesApiClient.kt
All API calls are suspend functions, making them compatible with Kotlin coroutines for asynchronous operations.
ApiHelper
TheApiHelper class provides safe API call execution with automatic error handling:
data/remote/ApiHelper.kt
Data Transfer Objects (DTOs)
DTOs represent the API response structure and include mapping functions to domain models.ArticleDto
data/model/ArticleDto.kt
DTOs include mapping functions (
toArticleDomain(), toArticleDetailDomain()) to convert API responses into domain models, separating external data structures from internal business logic.Local Data Sources
The app uses Room for local data persistence, primarily for pagination management.Database Configuration
data/local/SpaceFlightNewsDb.kt
PaginationDao
data/local/dao/PaginationDao.kt
PaginationManager
Manages pagination state across app sessions:data/utils/PaginationManager.kt
Repository Implementation
Repositories coordinate between remote and local data sources while mapping data to domain models.ArticleRepositoryImpl
data/repository/ArticleRepositoryImpl.kt
Data Flow
Key Features
Error Handling
Error Handling
The
ApiHelper class provides centralized error handling for:- Network connectivity issues
- HTTP errors and status codes
- Timeouts and IO exceptions
- Generic exception fallbacks
Pagination Management
Pagination Management
The
PaginationManager stores pagination state locally to:- Track the current offset for API requests
- Enable seamless infinite scrolling
- Persist pagination across app sessions
Data Mapping
Data Mapping
DTOs include mapping functions that:
- Convert API models to domain models
- Keep external data structures separate from business logic
- Support different levels of detail (Article vs ArticleDetail)
Dependency Injection
Dependency Injection
All components use constructor injection with Hilt:
@Injectannotations for automatic dependency provision- Configured in
NetworkModuleandRoomModule - Enables easy testing with mock implementations
Related Components
Domain Layer
Learn about domain models and use cases
Presentation Layer
Explore ViewModels and UI components