Skip to content

Commit

Permalink
final attempt fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odaridavid committed May 20, 2020
1 parent 3f32eed commit 5716b66
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.k0d4black.theforce.mappers.toPresentation
import com.k0d4black.theforce.models.FilmPresentation
import com.k0d4black.theforce.models.PlanetPresentation
import com.k0d4black.theforce.models.SpeciePresentation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -41,33 +40,33 @@ class CharacterDetailViewModel @Inject constructor(
private var _species = MutableLiveData<List<SpeciePresentation>>()

fun getCharacterDetails(characterUrl: String) {
viewModelScope.launch(Dispatchers.IO + handler) {
_uiState.postValue(Loading)
viewModelScope.launch(handler) {
_uiState.value = Loading
loadPlanet(characterUrl)
loadFilms(characterUrl)
loadSpecies(characterUrl)
_uiState.postValue(Success(Unit))
_uiState.value = Success(Unit)
}
}

private suspend fun loadPlanet(characterUrl: String) {
getPlanetUseCase(characterUrl).collect { planet ->
val planetPresentation = planet.toPresentation()
_planet.postValue(planetPresentation)
_planet.value = planetPresentation
}
}

private suspend fun loadFilms(characterUrl: String) {
getFilmsUseCase(characterUrl).collect { films ->
val filmsPresentation = films.map { eachFilm -> eachFilm.toPresentation() }
_films.postValue(filmsPresentation)
_films.value = filmsPresentation
}
}

private suspend fun loadSpecies(characterUrl: String) {
getSpeciesUseCase(characterUrl).collect { species ->
val speciesPresentation = species.map { eachSpecie -> eachSpecie.toPresentation() }
_species.postValue(speciesPresentation)
_species.value = speciesPresentation
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class CharacterSearchViewModel @Inject constructor(
) : UiStateViewModel() {

fun executeCharacterSearch(characterName: String) {
viewModelScope.launch(Dispatchers.IO + handler) {
_uiState.postValue(Loading)
viewModelScope.launch(handler) {
_uiState.value = Loading
searchCharactersUseCase(characterName).collect { results ->
val characters = results.map { character -> character.toPresentation() }
_uiState.postValue(Success(characters))
_uiState.value = Success(characters)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class CharacterDetailViewModelTest : BaseViewModelTest() {
Truth.assertThat(filmPresentation)
.isEqualTo(SampleData.films.map { it.toPresentation() })
}
characterDetailViewModel.planet1.observeOnce { planetPresentation->
characterDetailViewModel.planet.observeOnce { planetPresentation->
Truth.assertThat(planetPresentation).isEqualTo(SampleData.planet.toPresentation())
}
}
Expand Down

0 comments on commit 5716b66

Please sign in to comment.