REDI-94: GetCharactersPageUseCase + inject into list ViewModel
Add a domain UseCase (operator invoke) in :feature:characters:domain delegating to CharacterRepository, and have CharacterListViewModel depend on it instead of the repository directly. The UseCase is a deliberate thin pass-through that documents the 'when to add a UseCase' convention (real logic / multi-source composition vs. a single forwarded call).
This commit is contained in:
@@ -8,7 +8,7 @@ import com.example.architecture.core.domain.onFailure
|
||||
import com.example.architecture.core.domain.onSuccess
|
||||
import com.example.architecture.core.presentation.UiText
|
||||
import com.example.architecture.core.presentation.toUiText
|
||||
import com.example.architecture.feature.characters.domain.CharacterRepository
|
||||
import com.example.architecture.feature.characters.domain.usecase.GetCharactersPageUseCase
|
||||
import com.example.architecture.feature.characters.presentation.model.CharacterUi
|
||||
import com.example.architecture.feature.characters.presentation.model.toCharacterUi
|
||||
import kotlinx.collections.immutable.toImmutableList
|
||||
@@ -25,7 +25,7 @@ import kotlinx.coroutines.launch
|
||||
* via a [Channel], maps failures to [UiText], and persists the loaded page in [SavedStateHandle].
|
||||
*/
|
||||
class CharacterListViewModel(
|
||||
private val characterRepository: CharacterRepository,
|
||||
private val getCharactersPage: GetCharactersPageUseCase,
|
||||
private val savedStateHandle: SavedStateHandle,
|
||||
) : ViewModel() {
|
||||
|
||||
@@ -62,7 +62,7 @@ class CharacterListViewModel(
|
||||
|
||||
var page = 1
|
||||
while (page <= targetPage) {
|
||||
when (val result = characterRepository.getCharacters(page)) {
|
||||
when (val result = getCharactersPage(page)) {
|
||||
is Result.Success -> {
|
||||
accumulated += result.data.characters.map { it.toCharacterUi() }
|
||||
lastLoadedPage = page
|
||||
@@ -123,7 +123,7 @@ class CharacterListViewModel(
|
||||
// get appended twice.
|
||||
_state.update { it.copy(isLoadingNextPage = true, error = null) }
|
||||
viewModelScope.launch {
|
||||
characterRepository.getCharacters(page)
|
||||
getCharactersPage(page)
|
||||
.onSuccess { pageData ->
|
||||
_state.update { state ->
|
||||
state.copy(
|
||||
|
||||
Reference in New Issue
Block a user