Skip to content

Commit

Permalink
Merge pull request #71 from odaridavid/fix-flow
Browse files Browse the repository at this point in the history
Refactor Naming and Style Improvements
  • Loading branch information
odaridavid authored May 21, 2020
2 parents 0bdecc8 + 5716b66 commit 174c881
Show file tree
Hide file tree
Showing 86 changed files with 656 additions and 658 deletions.
14 changes: 0 additions & 14 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@ android {
targetCompatibility 1.8
}

bundle {
density {
enableSplit true
}
abi {
enableSplit true
}
language {
enableSplit false
}
}
}

dependencies {
Expand Down Expand Up @@ -110,9 +99,6 @@ dependencies {
implementation appDependencies.recyclerviewAnimations

//Memory leaks
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
debugImplementation appDependencies.leakCanary

//About Libraries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import com.k0d4black.theforce.features.character_details.CharacterDetailActivity
import com.k0d4black.theforce.models.StarWarsCharacterUiModel
import com.k0d4black.theforce.models.CharacterPresentation
import com.k0d4black.theforce.commons.CHARACTER_PARCEL_KEY
import org.junit.After
import org.junit.Rule
Expand Down Expand Up @@ -36,12 +36,12 @@ internal class CharacterDetailActivityIntegrationTest : BaseTest() {
fun shouldLoadDataOnLaunchWithValidCharacterId() {
val intent = Intent().putExtra(
CHARACTER_PARCEL_KEY,
StarWarsCharacterUiModel(
CharacterPresentation(
name = "Luke",
birthYear = "12BBY",
heightInCm = "234",
heightInInches = "544",
url = "1"
url = "https://swapi.py4e.com/api/people/1/"
)
)
activityRule.launchActivity(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
internal class SearchActivityIntegrationTest : BaseTest() {
//TODO Use idling resource

@get:Rule
var activityRule: ActivityTestRule<SearchActivity> =
Expand All @@ -52,7 +53,6 @@ internal class SearchActivityIntegrationTest : BaseTest() {
fun shouldDisplayDataOnSearch() {
onView(withId(R.id.action_search)).perform(click())
onView(isAssignableFrom(EditText::class.java)).perform(typeText(EXISTING_SEARCH_PARAMS))
SystemClock.sleep(2000)
onView(withId(R.id.search_results_recycler_view)).check(matches(isDisplayed()))
}

Expand All @@ -68,13 +68,13 @@ internal class SearchActivityIntegrationTest : BaseTest() {
fun shouldNavigateToCharacterDetailOnItemClick() {
onView(withId(R.id.action_search)).perform(click())
onView(isAssignableFrom(EditText::class.java)).perform(typeText(EXISTING_SEARCH_PARAMS))
SystemClock.sleep(2000)
SystemClock.sleep(1500)
onView(withId(R.id.search_results_recycler_view)).perform(
RecyclerViewActions.actionOnItemAtPosition<SearchResultAdapter.SearchedCharacterViewHolder>(
0, ViewAction.clickChildViewWithId(R.id.more_info_arrow_image_button)
)
)
SystemClock.sleep(2000)
SystemClock.sleep(1500)
intended(hasComponent("com.k0d4black.theforce.features.character_details.CharacterDetailActivity"))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.k0d4black.theforce.di.modules.details

import com.k0d4black.theforce.data.repository.StarWarsCharacterDetailsRepository
import com.k0d4black.theforce.data.repository.CharacterDetailsRepository
import com.k0d4black.theforce.domain.repository.ICharacterDetailsRepository
import dagger.Binds
import dagger.Module
Expand All @@ -11,7 +11,7 @@ abstract class CharacterDetailsBindingModule {
@Singleton
@Binds
abstract fun bindCharacterDetailsRepository(
starWarsCharacterDetailsRepository: StarWarsCharacterDetailsRepository
characterDetailsRepository: CharacterDetailsRepository
): ICharacterDetailsRepository

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.k0d4black.theforce.di.modules.details


import com.k0d4black.theforce.data.api.StarWarsApiService
import com.k0d4black.theforce.data.repository.StarWarsCharacterDetailsRepository
import com.k0d4black.theforce.data.source.StarWarsCharacterDetailsDataSource
import com.k0d4black.theforce.domain.usecases.GetStarWarsCharacterFilmsUseCase
import com.k0d4black.theforce.domain.usecases.GetStarWarsCharacterPlanetUseCase
import com.k0d4black.theforce.domain.usecases.GetStarWarsCharacterSpeciesUseCase
import com.k0d4black.theforce.data.repository.CharacterDetailsRepository
import com.k0d4black.theforce.data.source.CharacterDetailsDataSource
import com.k0d4black.theforce.domain.usecases.GetFilmsUseCase
import com.k0d4black.theforce.domain.usecases.GetPlanetUseCase
import com.k0d4black.theforce.domain.usecases.GetSpeciesUseCase
import dagger.Module
import dagger.Provides

Expand All @@ -15,21 +15,21 @@ class CharacterDetailsModule {

@Provides
fun provideCharacterFilmsUseCase(
starWarsCharacterDetailsRepository: StarWarsCharacterDetailsRepository
): GetStarWarsCharacterFilmsUseCase = GetStarWarsCharacterFilmsUseCase(starWarsCharacterDetailsRepository)
characterDetailsRepository: CharacterDetailsRepository
): GetFilmsUseCase = GetFilmsUseCase(characterDetailsRepository)

@Provides
fun provideCharacterPlanetUseCase(
starWarsCharacterDetailsRepository: StarWarsCharacterDetailsRepository
): GetStarWarsCharacterPlanetUseCase = GetStarWarsCharacterPlanetUseCase(starWarsCharacterDetailsRepository)
characterDetailsRepository: CharacterDetailsRepository
): GetPlanetUseCase = GetPlanetUseCase(characterDetailsRepository)

@Provides
fun provideCharacterSpeciesUseCase(
starWarsCharacterDetailsRepository: StarWarsCharacterDetailsRepository
): GetStarWarsCharacterSpeciesUseCase = GetStarWarsCharacterSpeciesUseCase(starWarsCharacterDetailsRepository)
characterDetailsRepository: CharacterDetailsRepository
): GetSpeciesUseCase = GetSpeciesUseCase(characterDetailsRepository)

@Provides
fun provideCharacterDetailsDataSource(apiService: StarWarsApiService): StarWarsCharacterDetailsDataSource =
StarWarsCharacterDetailsDataSource(apiService)
fun provideCharacterDetailsDataSource(apiService: StarWarsApiService): CharacterDetailsDataSource =
CharacterDetailsDataSource(apiService)

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.k0d4black.theforce.di.modules.search

import com.k0d4black.theforce.data.repository.StarWarsCharacterSearchRepository
import com.k0d4black.theforce.data.repository.CharacterSearchRepository
import com.k0d4black.theforce.domain.repository.ICharacterSearchRepository
import dagger.Binds
import dagger.Module
Expand All @@ -12,7 +12,7 @@ abstract class CharacterSearchBindingModule {
@Singleton
@Binds
abstract fun bindCharacterSearchRepository(
starWarsCharacterSearchRepository: StarWarsCharacterSearchRepository
characterSearchRepository: CharacterSearchRepository
): ICharacterSearchRepository

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.k0d4black.theforce.di.modules.search

import com.k0d4black.theforce.data.api.StarWarsApiService
import com.k0d4black.theforce.data.repository.StarWarsCharacterSearchRepository
import com.k0d4black.theforce.data.source.StarWarsCharacterSearchDataSource
import com.k0d4black.theforce.domain.usecases.SearchStarWarsCharacterUseCase
import com.k0d4black.theforce.data.repository.CharacterSearchRepository
import com.k0d4black.theforce.data.source.CharacterSearchDataSource
import com.k0d4black.theforce.domain.usecases.SearchCharactersUseCase
import com.k0d4black.theforce.features.character_search.CharacterSearchViewModel
import com.k0d4black.theforce.features.character_search.SearchQueryListener
import dagger.Module
Expand All @@ -19,12 +19,12 @@ class CharacterSearchModule {

@Provides
fun provideCharacterSearchUseCase(
starWarsCharacterSearchRepository: StarWarsCharacterSearchRepository
): SearchStarWarsCharacterUseCase = SearchStarWarsCharacterUseCase(starWarsCharacterSearchRepository)
characterSearchRepository: CharacterSearchRepository
): SearchCharactersUseCase = SearchCharactersUseCase(characterSearchRepository)

@Provides
fun provideCharacterSearchDataSource(apiService: StarWarsApiService): StarWarsCharacterSearchDataSource =
StarWarsCharacterSearchDataSource(apiService)
fun provideCharacterSearchDataSource(apiService: StarWarsApiService): CharacterSearchDataSource =
CharacterSearchDataSource(apiService)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.k0d4black.theforce.R
import com.k0d4black.theforce.commons.*
import com.k0d4black.theforce.databinding.ActivityCharacterDetailBinding
import com.k0d4black.theforce.models.StarWarsCharacterUiModel
import com.k0d4black.theforce.models.CharacterPresentation
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_character_detail.*
import javax.inject.Inject
Expand All @@ -36,7 +36,7 @@ class CharacterDetailActivity : AppCompatActivity() {

binding = DataBindingUtil.setContentView(this, R.layout.activity_character_detail)

val character = intent.getParcelableExtra<StarWarsCharacterUiModel>(CHARACTER_PARCEL_KEY)
val character = intent.getParcelableExtra<CharacterPresentation>(CHARACTER_PARCEL_KEY)

character?.run {
characterDetailViewModel.getCharacterDetails(this.url)
Expand All @@ -50,19 +50,21 @@ class CharacterDetailActivity : AppCompatActivity() {
}

private fun observeCharacterSpecies() {
characterDetailViewModel.characterStarWarsCharacterSpecies.observe(
characterDetailViewModel.species.observe(
this,
Observer { species ->
binding.characterDetailsSpeciesRecyclerView.apply {
adapter = speciesAdapter.apply { submitList(species) }
initRecyclerViewWithLineDecoration(this@CharacterDetailActivity)
if (species.isNotEmpty()) {
binding.characterDetailsSpeciesRecyclerView.apply {
adapter = speciesAdapter.apply { submitList(species) }
initRecyclerViewWithLineDecoration(this@CharacterDetailActivity)
}
enableGroup(R.id.character_species_group)
}
enableGroup(R.id.character_species_group)
})
}

private fun observeCharacterFilms() {
characterDetailViewModel.starWarsCharacterFilms.observe(this, Observer { films ->
characterDetailViewModel.films.observe(this, Observer { films ->
binding.characterDetailsFilmsRecyclerView.apply {
adapter = filmsAdapter.apply { submitList(films) }
layoutManager =
Expand All @@ -77,13 +79,13 @@ class CharacterDetailActivity : AppCompatActivity() {
}

private fun observeCharacterPlanet() {
characterDetailViewModel.characterStarWarsCharacterPlanet.observe(this, Observer { planet ->
characterDetailViewModel.planet.observe(this, Observer { planet ->
binding.planet = planet
enableGroup(R.id.character_planet_group)
})
}

private fun displayCharacterDetails(character: StarWarsCharacterUiModel) {
private fun displayCharacterDetails(character: CharacterPresentation) {
supportActionBar?.title = character.name
binding.character = character
enableGroup(R.id.character_details_group)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,69 @@ import com.k0d4black.theforce.commons.Error
import com.k0d4black.theforce.commons.Loading
import com.k0d4black.theforce.commons.Success
import com.k0d4black.theforce.commons.UiStateViewModel
import com.k0d4black.theforce.domain.usecases.GetStarWarsCharacterFilmsUseCase
import com.k0d4black.theforce.domain.usecases.GetStarWarsCharacterPlanetUseCase
import com.k0d4black.theforce.domain.usecases.GetStarWarsCharacterSpeciesUseCase
import com.k0d4black.theforce.domain.usecases.GetFilmsUseCase
import com.k0d4black.theforce.domain.usecases.GetPlanetUseCase
import com.k0d4black.theforce.domain.usecases.GetSpeciesUseCase
import com.k0d4black.theforce.mappers.toPresentation
import com.k0d4black.theforce.models.StarWarsCharacterFilmsUiModel
import com.k0d4black.theforce.models.StarWarsCharacterPlanetUiModel
import com.k0d4black.theforce.models.StarWarsCharacterSpeciesUiModel
import com.k0d4black.theforce.models.FilmPresentation
import com.k0d4black.theforce.models.PlanetPresentation
import com.k0d4black.theforce.models.SpeciePresentation
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject

class CharacterDetailViewModel @Inject constructor(
private val getStarWarsCharacterSpeciesUseCase: GetStarWarsCharacterSpeciesUseCase,
private val getStarWarsCharacterPlanetUseCase: GetStarWarsCharacterPlanetUseCase,
private val getStarWarsCharacterFilmsUseCase: GetStarWarsCharacterFilmsUseCase
private val getSpeciesUseCase: GetSpeciesUseCase,
private val getPlanetUseCase: GetPlanetUseCase,
private val getFilmsUseCase: GetFilmsUseCase
) : UiStateViewModel() {

val characterStarWarsCharacterPlanet: LiveData<StarWarsCharacterPlanetUiModel>
get() = _characterPlanet
val planet: LiveData<PlanetPresentation>
get() = _planet

private var _characterPlanet =
MutableLiveData<StarWarsCharacterPlanetUiModel>()
private var _planet = MutableLiveData<PlanetPresentation>()

val starWarsCharacterFilms: LiveData<List<StarWarsCharacterFilmsUiModel>>
get() = _characterFilms
val films: LiveData<List<FilmPresentation>>
get() = _films

private var _characterFilms =
MutableLiveData<List<StarWarsCharacterFilmsUiModel>>()
private var _films = MutableLiveData<List<FilmPresentation>>()

val characterStarWarsCharacterSpecies: LiveData<List<StarWarsCharacterSpeciesUiModel>>
get() = _characterSpecies
val species: LiveData<List<SpeciePresentation>>
get() = _species

private var _characterSpecies =
MutableLiveData<List<StarWarsCharacterSpeciesUiModel>>()
private var _species = MutableLiveData<List<SpeciePresentation>>()

fun getCharacterDetails(characterUrl: String) {
_uiState.value = Loading
viewModelScope.launch(handler) {
getStarWarsCharacterPlanetUseCase(characterUrl).collect {
_characterPlanet.value = it.toPresentation()
}
getStarWarsCharacterFilmsUseCase(characterUrl).collect {
_characterFilms.value = it.map { film -> film.toPresentation() }
}
getStarWarsCharacterSpeciesUseCase(characterUrl).collect {
_characterSpecies.value = it.map { species -> species.toPresentation() }
}
_uiState.value = Loading
loadPlanet(characterUrl)
loadFilms(characterUrl)
loadSpecies(characterUrl)
_uiState.value = Success(Unit)
}
}

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

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

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

fun displayCharacterError() {
_uiState.value = Error(Exception("Error Loading Character"))
}
Expand Down
Loading

0 comments on commit 174c881

Please sign in to comment.