Architecture Overview
ViewModels
UI state management and business logic coordination
Fragments
UI rendering and user interaction
Adapters
RecyclerView data binding
ViewModels
ViewModels manage UI state and coordinate with use cases. They survive configuration changes and expose data through LiveData or StateFlow.ArticlesViewModel
Manages the article list screen with search and pagination:presentation/ui/articles/viewmodel/ArticlesViewModel.kt
Search Debouncing
Search Debouncing
The ViewModel uses Flow operators to optimize search:
debounce(300): Waits 300ms after the user stops typingdistinctUntilChanged(): Only triggers when the query actually changescollectLatest: Cancels previous searches if a new one starts
Pagination Support
Pagination Support
The
fetchArticles method supports three modes:- Initial load:
reload = false, loadMore = false - Pull to refresh:
reload = true - Load more:
loadMore = true
_currentList maintains the accumulated articles for infinite scrolling.State Management
State Management
Uses both LiveData and StateFlow:
- LiveData for UI state that Fragments observe
- StateFlow for internal state like search query
- Exposes immutable versions to prevent external modification
ArticleDetailsViewModel
Manages the article detail screen:presentation/ui/articles/viewmodel/ArticleDetailsViewModel.kt
The
hasLoaded flag prevents unnecessary re-fetching when the screen is revisited during the same session, improving performance and reducing API calls.Fragments
Fragments render the UI and observe ViewModel state changes.ArticlesFragment
Displays a list of articles with search and infinite scrolling:presentation/ui/articles/ArticlesFragment.kt
Key Fragment Features
Voice Search
Integrates Android’s speech recognition for hands-free search
Pull to Refresh
Swipe gesture to reload the article list
Infinite Scroll
Automatically loads more articles when reaching the bottom
State Views
Shows loading, error, and empty state screens
RecyclerView Adapters
Adapters bind data to RecyclerView items and handle user interactions.ArticleAdapter
presentation/ui/articles/adapter/ArticleAdapter.kt
ListAdapter Benefits
ListAdapter Benefits
Extends
ListAdapter instead of RecyclerView.Adapter:- Automatic diff calculation on background thread
- Smooth animations when items change
- Better performance for large lists
- Built-in support for immutable data
Multiple View Types
Multiple View Types
Supports two view types:
- Item view: Regular article cards
- Loading view: Progress indicator at the bottom during pagination
setLoading() method dynamically adds/removes the loading indicator.Click Handling
Click Handling
Uses a lambda callback pattern:This keeps navigation logic in the Fragment while maintaining adapter reusability.
MVVM Data Flow
Lifecycle Awareness
The presentation layer is fully lifecycle-aware:ViewModel Scope
Coroutines launched in
viewModelScope are automatically cancelled when ViewModel is cleared:State Management Patterns
Loading State
Shows progress indicator while fetching data
Success State
Displays data in RecyclerView
Error State
Shows error message with retry button
Empty State
Displays message when no results found
Dependency Injection
All presentation components use Hilt for dependency injection:@HiltViewModelenables automatic ViewModel injection@AndroidEntryPointenables field injection in Android components- Dependencies are provided by Hilt modules in the DI package
Best Practices
Separation of Concerns
Separation of Concerns
- ViewModel: UI logic, state management, use case coordination
- Fragment: View rendering, user input handling, navigation
- Adapter: Data binding to RecyclerView items
Reactive UI
Reactive UI
UI updates automatically when state changes:No manual state synchronization needed.
Configuration Changes
Configuration Changes
ViewModels survive configuration changes (rotation, etc.):
- No need to save/restore UI state manually
- Ongoing operations continue without interruption
- No duplicate API calls after rotation
Resource Management
Resource Management
Proper cleanup prevents memory leaks:
Advanced Features
Voice Search Integration
The app supports voice search using Android’s speech recognition:Permission Handling
Custom permission chain manager for requesting audio recording permission:Testing ViewModels
ViewModels are easy to unit test:Related Components
Domain Layer
Learn about use cases that ViewModels invoke
Data Layer
Understand where the data comes from