Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #93: add pokemon id to pokemon list #145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/src/main/res/layout/item_pokemon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@
app:layout_constraintTop_toBottomOf="@id/image"
tools:text="Bulbasaur" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/pokemonId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:elevation="4dp"
android:gravity="center"
android:maxLines="1"
android:padding="12dp"
android:text="@{String.format(`#%03d`, pokemon.pokemonId)}"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@+id/image"
tools:layout_editor_absoluteX="-16dp"
tools:text="1" />

</androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ class MainRepositoryImpl @Inject constructor(
val response = pokedexClient.fetchPokemonList(page = page)
response.suspendOnSuccess {
pokemons = data.results
pokemons.forEach { pokemon -> pokemon.page = page }
pokemons.forEach { pokemon ->
val pokemonInfoResponse = pokedexClient.fetchPokemonInfo(pokemon.name)
pokemonInfoResponse.suspendOnSuccess {
pokemon.pokemonId = data.id
}.onFailure {
onError(message())
}
pokemon.page = page
}
pokemonDao.insertPokemonList(pokemons.asEntity())
emit(pokemonDao.getAllPokemonList(page).asDomain())
}.onFailure { // handles the all error cases from the API request fails.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "3e4fc349c7e47ef58902f587531caab0",
"identityHash": "363dfa1073e55464f02e3d247d8f7e98",
"entities": [
{
"tableName": "PokemonEntity",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`page` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, PRIMARY KEY(`name`))",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`page` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, `pokemonId` INTEGER NOT NULL, PRIMARY KEY(`name`))",
"fields": [
{
"fieldPath": "page",
Expand All @@ -25,13 +25,19 @@
"columnName": "url",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "pokemonId",
"columnName": "pokemonId",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"name"
],
"autoGenerate": false
]
},
"indices": [],
"foreignKeys": []
Expand Down Expand Up @@ -108,10 +114,10 @@
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
],
"autoGenerate": false
]
},
"indices": [],
"foreignKeys": []
Expand All @@ -120,7 +126,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '3e4fc349c7e47ef58902f587531caab0')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '363dfa1073e55464f02e3d247d8f7e98')"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"formatVersion": 1,
"database": {
"version": 3,
"identityHash": "363dfa1073e55464f02e3d247d8f7e98",
"entities": [
{
"tableName": "PokemonEntity",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`page` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, `pokemonId` INTEGER NOT NULL, PRIMARY KEY(`name`))",
"fields": [
{
"fieldPath": "page",
"columnName": "page",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "url",
"columnName": "url",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "pokemonId",
"columnName": "pokemonId",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"name"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "PokemonInfoEntity",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `height` INTEGER NOT NULL, `weight` INTEGER NOT NULL, `experience` INTEGER NOT NULL, `types` TEXT NOT NULL, `hp` INTEGER NOT NULL, `attack` INTEGER NOT NULL, `defense` INTEGER NOT NULL, `speed` INTEGER NOT NULL, `exp` INTEGER NOT NULL, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "height",
"columnName": "height",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "weight",
"columnName": "weight",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "experience",
"columnName": "experience",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "types",
"columnName": "types",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "hp",
"columnName": "hp",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "attack",
"columnName": "attack",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "defense",
"columnName": "defense",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "speed",
"columnName": "speed",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "exp",
"columnName": "exp",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '363dfa1073e55464f02e3d247d8f7e98')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.skydoves.pokedex.core.database.entitiy.PokemonInfoEntity

@Database(
entities = [PokemonEntity::class, PokemonInfoEntity::class],
version = 2,
version = 3,
exportSchema = true,
)
@TypeConverters(value = [TypeResponseConverter::class])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ data class PokemonEntity(
var page: Int = 0,
@PrimaryKey val name: String,
val url: String,
val pokemonId: Int = 0,
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object PokemonEntityMapper : EntityMapper<List<Pokemon>, List<PokemonEntity>> {
page = pokemon.page,
name = pokemon.name,
url = pokemon.url,
pokemonId = pokemon.pokemonId,
)
}
}
Expand All @@ -37,6 +38,7 @@ object PokemonEntityMapper : EntityMapper<List<Pokemon>, List<PokemonEntity>> {
page = pokemonEntity.page,
name = pokemonEntity.name,
url = pokemonEntity.url,
pokemonId = pokemonEntity.pokemonId,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ data class Pokemon(
@field:Json(name = "name")
val name: String,
@field:Json(name = "url") val url: String,
var pokemonId: Int = 0,
) : Parcelable {

fun getImageUrl(): String {
Expand Down