Skip to main content
Use cases encapsulate business logic and act as intermediaries between the presentation layer and the repository layer.

GetArticlesUseCase

Retrieves a list of articles from the repository, optionally filtered by a search query.

Method: getArticles

Fetches articles with optional search filtering.
suspend fun getArticles(query: String? = null): Resource<List<Article>>

Parameters

query
String?
default:"null"
Optional search query to filter articles. If null, returns all articles.

Returns

Resource<List<Article>>
Resource
A Resource wrapper containing either a successful list of articles or an error state.

Example Usage

class ArticleViewModel @Inject constructor(
    private val getArticlesUseCase: GetArticlesUseCase
) : ViewModel() {
    
    fun loadArticles(searchQuery: String? = null) {
        viewModelScope.launch {
            when (val result = getArticlesUseCase.getArticles(searchQuery)) {
                is Resource.Success -> {
                    // Handle successful response
                    val articles = result.data
                }
                is Resource.Error -> {
                    // Handle error
                }
                is Resource.Loading -> {
                    // Show loading state
                }
            }
        }
    }
}

GetArticleByIdUseCase

Retrieves detailed information about a specific article by its ID.

Method: getArticleById

Fetches the full details of an article using its unique identifier.
suspend fun getArticleById(articleId: Long): Resource<ArticleDetail>

Parameters

articleId
Long
required
The unique identifier of the article to retrieve.

Returns

Resource<ArticleDetail>
Resource
A Resource wrapper containing either the article details or an error state.

Example Usage

class ArticleDetailViewModel @Inject constructor(
    private val getArticleByIdUseCase: GetArticleByIdUseCase
) : ViewModel() {
    
    fun loadArticleDetails(articleId: Long) {
        viewModelScope.launch {
            when (val result = getArticleByIdUseCase.getArticleById(articleId)) {
                is Resource.Success -> {
                    // Handle successful response
                    val articleDetail = result.data
                }
                is Resource.Error -> {
                    // Handle error
                }
                is Resource.Loading -> {
                    // Show loading state
                }
            }
        }
    }
}

Build docs developers (and LLMs) love