ArticlesViewModel
com.bsvillarraga.spaceflightnews.presentation.ui.articles.viewmodel.ArticlesViewModel
ViewModel responsible for managing article list data and search functionality.
Injection: Annotated with @HiltViewModel, uses Dagger Hilt for dependency injection.
Constructor Parameters
articleUseCase: GetArticlesUseCase- Use case for fetching articles from repository
LiveData Properties
articles
Resource.Loading()- Initial state and during data fetchResource.Success(List<Article>)- Successfully loaded articlesResource.Error- Error occurred during fetch
Methods
fetchArticles()
query- Optional search query string. If null, uses current search queryreload- When true, clears current list and shows loading stateloadMore- When true, appends new articles to existing list for pagination
- Skips fetch if data already loaded (unless reload or loadMore is true)
- Updates LiveData with loading state when reloading
- Executes in viewModelScope coroutine
viewmodel/ArticlesViewModel.kt:55
onSearchQueryChanged()
query- New search query entered by user
- Updates internal StateFlow
_searchQuery - Triggers automatic search after 300ms debounce
- Clears current article list before new search
viewmodel/ArticlesViewModel.kt:92
onGetSearchQueryChanged()
viewmodel/ArticlesViewModel.kt:96
Private Methods
observeSearchQuery()
Observes search query changes with debouncing (300ms) and distinctUntilChanged to prevent duplicate queries. Source:viewmodel/ArticlesViewModel.kt:40
handleResult()
viewmodel/ArticlesViewModel.kt:72
ArticleDetailsViewModel
com.bsvillarraga.spaceflightnews.presentation.ui.articles.viewmodel.ArticleDetailsViewModel
ViewModel for managing single article detail data.
Injection: Annotated with @HiltViewModel, uses Dagger Hilt for dependency injection.
Constructor Parameters
articleById: GetArticleByIdUseCase- Use case for fetching article details by ID
LiveData Properties
article
Resource.Loading()- During data fetchResource.Success(ArticleDetail)- Successfully loaded article detailsResource.Error- Error occurred during fetch
Methods
fetchArticleById()
id- Article ID to fetchreload- When true, forces reload even if already loaded (useful for error retry)
- Prevents duplicate loads using internal
hasLoadedflag - Sets loading state before fetch
- Executes in viewModelScope coroutine
- Marks article as loaded on success
viewmodel/ArticleDetailsViewModel.kt:28