Skip to content

Commit

Permalink
Second iteration of challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
emeruvia committed Sep 6, 2023
1 parent 4bf4891 commit 30733b2
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 47 deletions.
123 changes: 123 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ The app consists of 3 activites.
- XmlListActivity, this will load a horizontal and vertical recyclerviews using XML. The data will be provided downstream from Retrofit and Room
- ComposeActivity, this screen is purposely left in blank so that candidates can show their compose abilities and build UI around it.

## API Endpoint
- https://pokeapi.co/api/v2/pokedex/1/

## Possible Challenges
- Fix the broken code, currently there are 4 places where code is broken
- Fix the broken code
- Reimplement the XML layout in Compose
- Create a error interceptor for the network layer
- Fix a UI bug. There is a UI bug in the PokedexAdapter class. Whenever an item in the list is selected, the UI is not refreshed to reflect the `isFavorite` state of the item; you will need to scroll away of the item and back to it to see the UI changes reflected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlinx.coroutines.flow.Flow
interface FavoritePokemonDao {

@Query("Select * FROM ${Pokemon.TABLE_NAME}")
suspend fun getAllFavoritePokemon(): Flow<List<Pokemon>>
fun getAllFavoritePokemon(): Flow<List<Pokemon>>

@Query("Select * FROM ${Pokemon.TABLE_NAME}")
suspend fun getAllFavoritePokemonAsync(): List<Pokemon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import javax.inject.Singleton
@Component(
modules = [
ViewModelModule::class,
DatabaseModule::class
DatabaseModule::class,
NetworkModule::class
]
)
interface AppComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xplor.android.challenge.di.modules

import androidx.viewbinding.BuildConfig
import com.xplor.android.challenge.network.ApiService
import dagger.Binds
import dagger.Module
import dagger.Provides
import okhttp3.OkHttpClient
Expand All @@ -14,7 +15,7 @@ import javax.inject.Singleton
class NetworkModule {

@Singleton
@Provides
@Binds
fun provideMovieDbService(okHttpClient: OkHttpClient): ApiService {
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
Expand All @@ -34,7 +35,7 @@ class NetworkModule {

// TODO: Challenge create an interceptor for custom error codes

@Provides
@Binds
fun provideLoggingInterceptor(): HttpLoggingInterceptor {
val loggingInterceptor = HttpLoggingInterceptor()
if (BuildConfig.DEBUG) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.xplor.android.challenge.repository.models.Pokemon
data class PokedexResponse(
val id: Long,
val name: String,
@SerializedName("pokemon_entries") val pokemonEntries: List<PokemonEntryResponse>
@SerializedName("pokemon_entris") val pokemonEntries: List<PokemonEntryResponse>
)

data class PokemonEntryResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,9 @@ class MainRepository @Inject constructor(
private val database: XplorDatabase
) {

fun fetchApiList() = channelFlow<ApiState<List<Pokemon>>> {
val apiResults = apiService.fetchNationalPokedex()
database.favoritePokemonDao().getAllFavoritePokemon().collect { list ->
if (list.isEmpty()) {
send(ApiState.Success(apiResults.toPokedex().pokemonEntries))
} else {
val pokemonEntries: List<Pokemon> = apiResults.toPokedex().pokemonEntries.toMutableList()
.map {
if (list.contains(it)) {
it.copy(isFavorite = true)
} else {
it
}
}
send(ApiState.Success(pokemonEntries))
}
}
fun fetchApiList() = flow<ApiState<List<Pokemon>>> {
// TODO: Get all pokemon data from API and the favorite pokemon from DB, merge both sources
// and emit the result back upstream.

}.catch { throwable ->
emit(ApiState.Error(throwable, throwable.message))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,23 @@ class XmlListActivity : AppCompatActivity(), PokedexAdapter.OnClickListener {
}

lifecycleScope.launchWhenStarted {
withContext(Dispatchers.IO ) {
viewModel.favoritePokemon.collect {
when (it) {
is ApiState.Success -> {
Timber.d("Success Favorite Pokemon: ${it.data}")
if (it.data.isEmpty()) {
binding.favoriteRecyclerview.visibility = View.GONE
} else {
binding.favoriteRecyclerview.visibility = View.VISIBLE
favoriteAdapter.submitList(it.data)
}
}

is ApiState.Loading -> {
Timber.d("Favorite Pokemon loading")
}

is ApiState.Error -> {
Timber.e("Favorite Pokemon error")
viewModel.favoritePokemon.collect {
when (it) {
is ApiState.Success -> {
Timber.d("Success Favorite Pokemon: ${it.data}")
if (it.data.isEmpty()) {
binding.favoriteRecyclerview.visibility = View.GONE
} else {
binding.favoriteRecyclerview.visibility = View.VISIBLE
favoriteAdapter.submitList(it.data)
}
}
is ApiState.Loading -> {
Timber.d("Favorite Pokemon loading")
}
is ApiState.Error -> {
Timber.e("Favorite Pokemon error")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.xplor.android.challenge.utils

fun Long.toImageUrlById(): String {
// return "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/$this.png"
return ""
return "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/$this.png"
}

fun Long.toEntryNumber(): String {
Expand Down

0 comments on commit 30733b2

Please sign in to comment.