diff --git a/app/build.gradle b/app/build.gradle index 4b34cdc..b084174 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,8 +12,8 @@ android { applicationId "com.benjaminledet.pokedex" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "2.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { @@ -32,6 +32,11 @@ android { kotlinOptions { jvmTarget = '1.8' } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } } dependencies { diff --git a/app/gradle/wrapper/gradle-wrapper.jar b/app/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..f6b961f Binary files /dev/null and b/app/gradle/wrapper/gradle-wrapper.jar differ diff --git a/app/gradle/wrapper/gradle-wrapper.properties b/app/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..bbd91aa --- /dev/null +++ b/app/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Mon Nov 18 08:37:03 CET 2019 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip diff --git a/app/gradlew b/app/gradlew new file mode 100644 index 0000000..cccdd3d --- /dev/null +++ b/app/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/app/gradlew.bat b/app/gradlew.bat new file mode 100644 index 0000000..f955316 --- /dev/null +++ b/app/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/app/schemas/com.benjaminledet.pokedex.data.local.PokedexDatabase/1.json b/app/schemas/com.benjaminledet.pokedex.data.local.PokedexDatabase/1.json index 7c2e71e..9173d26 100644 --- a/app/schemas/com.benjaminledet.pokedex.data.local.PokedexDatabase/1.json +++ b/app/schemas/com.benjaminledet.pokedex.data.local.PokedexDatabase/1.json @@ -2,11 +2,11 @@ "formatVersion": 1, "database": { "version": 1, - "identityHash": "9b2b0d9becf14f0c7303e8256e27d936", + "identityHash": "1412e54ba667f64e305b6ddf826f3745", "entities": [ { "tableName": "Pokemon", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `iconUrl` TEXT, `weight` REAL, `height` REAL, `types` TEXT, PRIMARY KEY(`id`))", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `iconUrl` TEXT, `moves` TEXT, `weight` REAL, `height` REAL, `types` TEXT, PRIMARY KEY(`id`))", "fields": [ { "fieldPath": "id", @@ -26,6 +26,12 @@ "affinity": "TEXT", "notNull": false }, + { + "fieldPath": "detail.moves", + "columnName": "moves", + "affinity": "TEXT", + "notNull": false + }, { "fieldPath": "detail.weight", "columnName": "weight", @@ -185,12 +191,62 @@ ] } ] + }, + { + "tableName": "Move", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `type` TEXT NOT NULL, `accuracy` INTEGER NOT NULL, `power` INTEGER NOT NULL, `pp` INTEGER NOT NULL, PRIMARY KEY(`id`))", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "types", + "columnName": "type", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "accuarcy", + "columnName": "accuracy", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "power", + "columnName": "power", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "pp", + "columnName": "pp", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": false + }, + "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, '9b2b0d9becf14f0c7303e8256e27d936')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '1412e54ba667f64e305b6ddf826f3745')" ] } } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d68d100..bd41143 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,6 +26,7 @@ + diff --git a/app/src/main/java/com/benjaminledet/pokedex/data/DataModule.kt b/app/src/main/java/com/benjaminledet/pokedex/data/DataModule.kt index 52c663c..93d4437 100644 --- a/app/src/main/java/com/benjaminledet/pokedex/data/DataModule.kt +++ b/app/src/main/java/com/benjaminledet/pokedex/data/DataModule.kt @@ -27,6 +27,8 @@ val dataModule = module { single { get().pokemonDao() } + single { get().moveDao() } + //endregion //region remote diff --git a/app/src/main/java/com/benjaminledet/pokedex/data/local/PokedexDatabase.kt b/app/src/main/java/com/benjaminledet/pokedex/data/local/PokedexDatabase.kt index 070ea82..3112bd3 100644 --- a/app/src/main/java/com/benjaminledet/pokedex/data/local/PokedexDatabase.kt +++ b/app/src/main/java/com/benjaminledet/pokedex/data/local/PokedexDatabase.kt @@ -4,18 +4,12 @@ import androidx.room.Database import androidx.room.RoomDatabase import androidx.room.TypeConverters import com.benjaminledet.pokedex.data.local.converter.StringListConverter -import com.benjaminledet.pokedex.data.local.dao.ItemCategoryDao -import com.benjaminledet.pokedex.data.local.dao.ItemDao -import com.benjaminledet.pokedex.data.local.dao.ItemPocketDao -import com.benjaminledet.pokedex.data.local.dao.PokemonDao -import com.benjaminledet.pokedex.data.model.Item -import com.benjaminledet.pokedex.data.model.ItemCategory -import com.benjaminledet.pokedex.data.model.ItemPocket -import com.benjaminledet.pokedex.data.model.Pokemon +import com.benjaminledet.pokedex.data.local.dao.* +import com.benjaminledet.pokedex.data.model.* @Database( entities = [ - Pokemon::class, Item::class, ItemPocket::class, ItemCategory::class + Pokemon::class, Item::class, ItemPocket::class, ItemCategory::class, Move::class ], version = PokedexDatabase.VERSION, exportSchema = true ) @@ -30,6 +24,8 @@ abstract class PokedexDatabase: RoomDatabase() { abstract fun pokemonDao(): PokemonDao + abstract fun moveDao(): MoveDao + companion object { const val DATABASE_NAME = "PokedexDatabase" const val VERSION = 1 diff --git a/app/src/main/java/com/benjaminledet/pokedex/data/local/dao/MoveDao.kt b/app/src/main/java/com/benjaminledet/pokedex/data/local/dao/MoveDao.kt new file mode 100644 index 0000000..c17c9b5 --- /dev/null +++ b/app/src/main/java/com/benjaminledet/pokedex/data/local/dao/MoveDao.kt @@ -0,0 +1,39 @@ +package com.benjaminledet.pokedex.data.local.dao + +import androidx.lifecycle.LiveData +import androidx.paging.DataSource +import androidx.room.* +import com.benjaminledet.pokedex.data.model.Move +import com.benjaminledet.pokedex.data.model.Move.Companion.NAME +import com.benjaminledet.pokedex.data.model.Pokemon +@Dao +abstract class MoveDao { + + + @Insert(onConflict = OnConflictStrategy.REPLACE) + abstract suspend fun insert(data: List): List + + @Delete + abstract suspend fun delete(data: List) + + @Query("DELETE FROM ${Move.TABLE_NAME}") + abstract suspend fun deleteAll() + + @Query("SELECT * FROM ${Move.TABLE_NAME} WHERE ${Move.ID} = :id") + abstract suspend fun getById(id: Int?): Move? + + @Query("SELECT * FROM ${Move.TABLE_NAME} WHERE ${Move.ID} = :id") + abstract fun getByIdObservable(id: Int?): LiveData + + @Query("SELECT * FROM ${Move.TABLE_NAME}") + abstract suspend fun getAll(): List + + @Query("SELECT * FROM ${Move.TABLE_NAME}") + abstract fun getAllObservable(): LiveData> + + @Query("SELECT * FROM ${Move.TABLE_NAME} WHERE $NAME in (:names) ") + abstract fun getAllObservable(names:List): LiveData> + + @Query("SELECT * FROM ${Move.TABLE_NAME}") + abstract fun getAllPaged(): DataSource.Factory +} \ No newline at end of file diff --git a/app/src/main/java/com/benjaminledet/pokedex/data/model/Move.kt b/app/src/main/java/com/benjaminledet/pokedex/data/model/Move.kt new file mode 100644 index 0000000..9fa2e32 --- /dev/null +++ b/app/src/main/java/com/benjaminledet/pokedex/data/model/Move.kt @@ -0,0 +1,46 @@ +package com.benjaminledet.pokedex.data.model + +import androidx.room.ColumnInfo +import androidx.room.Embedded +import androidx.room.Entity +import androidx.room.PrimaryKey +import com.benjaminledet.pokedex.data.remote.response.PokemonTypeResponse +import com.google.gson.annotations.SerializedName + +@Entity(tableName = Move.TABLE_NAME) +data class Move( + + @PrimaryKey + @ColumnInfo(name = ID) + val id: Int, + + @ColumnInfo(name = NAME) + val name: String, + + + @ColumnInfo(name = TYPE) + val types: String, + + @ColumnInfo(name = ACCUARCY) + val accuarcy: Int, + + @ColumnInfo(name = POWER) + val power: Int, + + @ColumnInfo(name = PP) + val pp: Int +) { + + override fun toString(): String = name + + companion object { + + const val TABLE_NAME = "Move" + const val ID = "id" + const val NAME = "name" + const val TYPE = "type" + const val ACCUARCY = "accuracy" + const val POWER = "power" + const val PP = "pp" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/benjaminledet/pokedex/data/model/PokemonDetail.kt b/app/src/main/java/com/benjaminledet/pokedex/data/model/PokemonDetail.kt index 2e6f7dd..687b625 100644 --- a/app/src/main/java/com/benjaminledet/pokedex/data/model/PokemonDetail.kt +++ b/app/src/main/java/com/benjaminledet/pokedex/data/model/PokemonDetail.kt @@ -4,6 +4,9 @@ import androidx.room.ColumnInfo data class PokemonDetail( + @ColumnInfo(name = MOVES) + val moves: List, + @ColumnInfo(name = WEIGHT) val weight: Double, @@ -18,5 +21,6 @@ data class PokemonDetail( const val WEIGHT = "weight" const val HEIGHT = "height" const val TYPES = "types" + const val MOVES = "moves" } } \ No newline at end of file diff --git a/app/src/main/java/com/benjaminledet/pokedex/data/remote/PokeApiClient.kt b/app/src/main/java/com/benjaminledet/pokedex/data/remote/PokeApiClient.kt index b5f005b..05b8e27 100644 --- a/app/src/main/java/com/benjaminledet/pokedex/data/remote/PokeApiClient.kt +++ b/app/src/main/java/com/benjaminledet/pokedex/data/remote/PokeApiClient.kt @@ -52,6 +52,19 @@ class PokeApiClient: KoinComponent { } } + + suspend fun getMoves(moves: List): List { + return coroutineScope { + moves.parallelMap(this){name -> + val response = performRequest { + service.getMovesAsync(name) + } + moveResponseToMove(response) + } + }.toList() + + } + /** * Get the detail of each itemCategory associated to their items from a list of ids */ @@ -84,6 +97,16 @@ class PokeApiClient: KoinComponent { itemCategoryId = itemCategoryId ) + private fun moveResponseToMove(moveResponse: MoveResponse) = Move( + id = moveResponse.id, + name = moveResponse.name, + types = moveResponse.type.name ?: "", + accuarcy = moveResponse.accuarcy, + power = moveResponse.power, + pp = moveResponse.pp + + ) + private fun pokemonResponseToPokemon(pokemonResponse: PokemonResponse) = Pokemon( id = pokemonResponse.id, name = pokemonResponse.name.capitalize(), @@ -91,7 +114,8 @@ class PokeApiClient: KoinComponent { detail = PokemonDetail( weight = pokemonResponse.weight / 10, height = pokemonResponse.height / 10, - types = pokemonResponse.types.mapNotNull { it.type.name } + types = pokemonResponse.types.mapNotNull { it.type.name }, + moves = pokemonResponse.moves.mapNotNull { it.move.name } ) ) @@ -157,6 +181,7 @@ class PokeApiClient: KoinComponent { private fun getItemIconUrl(name: String?) = "$ITEM_ICON_BASE_URL$name$ICON_EXTENSION" + companion object { private const val TAG = "PokeApiClient" private const val ICON_EXTENSION = ".png" diff --git a/app/src/main/java/com/benjaminledet/pokedex/data/remote/PokeApiService.kt b/app/src/main/java/com/benjaminledet/pokedex/data/remote/PokeApiService.kt index d9f703e..bb752b3 100644 --- a/app/src/main/java/com/benjaminledet/pokedex/data/remote/PokeApiService.kt +++ b/app/src/main/java/com/benjaminledet/pokedex/data/remote/PokeApiService.kt @@ -1,5 +1,6 @@ package com.benjaminledet.pokedex.data.remote +import com.benjaminledet.pokedex.data.model.Move import com.benjaminledet.pokedex.data.remote.response.* import com.benjaminledet.pokedex.extensions.createDefaultService import okhttp3.Interceptor @@ -29,6 +30,10 @@ interface PokeApiService { @GET("item-pocket/{name}") suspend fun getItemPocketAsync(@Path("name") name: String): Response + @GET("move/{name}") + suspend fun getMovesAsync(@Path("name") name: String): Response + + companion object { private const val BASE_URL = "https://pokeapi.co/api/v2/" diff --git a/app/src/main/java/com/benjaminledet/pokedex/data/remote/response/MoveResponse.kt b/app/src/main/java/com/benjaminledet/pokedex/data/remote/response/MoveResponse.kt new file mode 100644 index 0000000..2257e73 --- /dev/null +++ b/app/src/main/java/com/benjaminledet/pokedex/data/remote/response/MoveResponse.kt @@ -0,0 +1,24 @@ +package com.benjaminledet.pokedex.data.remote.response + +import com.google.gson.annotations.SerializedName + +data class MoveResponse( + + @SerializedName("id") + val id: Int, + + @SerializedName("name") + val name: String, + + @SerializedName("type") + val type: ApiResourceResponse, + + @SerializedName("accuarcy") + val accuarcy: Int, + + @SerializedName("power") + val power: Int, + + @SerializedName("pp") + val pp: Int +) diff --git a/app/src/main/java/com/benjaminledet/pokedex/data/remote/response/PokemonResponse.kt b/app/src/main/java/com/benjaminledet/pokedex/data/remote/response/PokemonResponse.kt index 9b5ee5f..e575a50 100644 --- a/app/src/main/java/com/benjaminledet/pokedex/data/remote/response/PokemonResponse.kt +++ b/app/src/main/java/com/benjaminledet/pokedex/data/remote/response/PokemonResponse.kt @@ -20,12 +20,12 @@ data class PokemonResponse( val weight: Double, @SerializedName("types") - val types: List + val types: List, + @SerializedName("moves") + val moves: List ) { - companion object { - const val DEFAULT_SPRITE = "front_default" } } @@ -37,4 +37,9 @@ data class PokemonTypeResponse( @SerializedName("type") val type: ApiResourceResponse +) + +data class PokemonMoveResponse ( + @SerializedName("move") + val move: ApiResourceResponse ) \ No newline at end of file diff --git a/app/src/main/java/com/benjaminledet/pokedex/data/repository/PokemonRepository.kt b/app/src/main/java/com/benjaminledet/pokedex/data/repository/PokemonRepository.kt index cf2cf3c..b1c78fd 100644 --- a/app/src/main/java/com/benjaminledet/pokedex/data/repository/PokemonRepository.kt +++ b/app/src/main/java/com/benjaminledet/pokedex/data/repository/PokemonRepository.kt @@ -5,7 +5,9 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Transformations import androidx.paging.toLiveData +import com.benjaminledet.pokedex.data.local.dao.MoveDao import com.benjaminledet.pokedex.data.local.dao.PokemonDao +import com.benjaminledet.pokedex.data.model.Move import com.benjaminledet.pokedex.data.model.Pokemon import com.benjaminledet.pokedex.data.remote.PokeApiClient import com.benjaminledet.pokedex.data.repository.utils.BoundaryCallback @@ -20,6 +22,7 @@ import org.koin.standalone.inject class PokemonRepository: KoinComponent { private val pokemonDao by inject() + private val movesDao by inject() private val pokeApiClient by inject() @@ -27,6 +30,10 @@ class PokemonRepository: KoinComponent { fun getAllPokemonsObservable() = pokemonDao.getAllObservable() + + + fun getMovesObservable(names: List) = movesDao.getAllObservable(names) + fun getAllPokemonsPagedList(scope: CoroutineScope, pageSize: Int): Listing { // create a boundary callback which will observe when the user reaches to the edges of @@ -73,7 +80,16 @@ class PokemonRepository: KoinComponent { scope.launch { try { val pokemon = pokeApiClient.getPokemonDetail(id) + insertPokemons(listOf(pokemon)) + + pokemon.detail?.moves?.let {moves -> + if (moves.isNotEmpty()) + { + val moves = pokeApiClient.getMoves(moves) + insertMoves(moves) + } + } networkState.postValue(NetworkState.LOADED) Log.v(TAG, "refresh pokemon: ${Status.SUCCESS}") @@ -118,6 +134,11 @@ class PokemonRepository: KoinComponent { pokemonDao.insert(pokemons) } + private suspend fun insertMoves(moves: List) { + Log.v(TAG, "insert moves: $moves") + movesDao.insert(moves) + } + companion object { private const val TAG = "PokemonRepository" } diff --git a/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailActivity.kt b/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailActivity.kt index e14fbce..bb7412f 100644 --- a/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailActivity.kt +++ b/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailActivity.kt @@ -1,7 +1,9 @@ package com.benjaminledet.pokedex.ui.pokemon.detail import android.os.Bundle +import android.util.Log import android.view.MenuItem +import android.view.animation.AnimationUtils import androidx.appcompat.app.AppCompatActivity import androidx.core.view.isVisible import androidx.lifecycle.Observer @@ -9,6 +11,12 @@ import com.benjaminledet.pokedex.R import com.benjaminledet.pokedex.data.repository.utils.Status import com.squareup.picasso.Picasso import kotlinx.android.synthetic.main.activity_pokemon_detail.* +import kotlinx.android.synthetic.main.activity_pokemon_detail.content +import kotlinx.android.synthetic.main.activity_pokemon_detail.height +import kotlinx.android.synthetic.main.activity_pokemon_detail.icon +import kotlinx.android.synthetic.main.activity_pokemon_detail.progressBar +import kotlinx.android.synthetic.main.activity_pokemon_detail.weight +import kotlinx.android.synthetic.main.fragment_pokemon_detail.* import org.koin.androidx.viewmodel.ext.android.viewModel class PokemonDetailActivity: AppCompatActivity() { @@ -32,9 +40,10 @@ class PokemonDetailActivity: AppCompatActivity() { title = pokemon?.name weight.text = getString(R.string.pokemon_weight, pokemon?.detail?.weight.toString()) height.text = getString(R.string.pokemon_height, pokemon?.detail?.height.toString()) - Picasso.get().load(pokemon?.iconUrl).into(icon) + }) + } override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) { diff --git a/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailFragment.kt b/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailFragment.kt index 2ec4c28..b7d38b2 100644 --- a/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailFragment.kt +++ b/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailFragment.kt @@ -1,9 +1,13 @@ package com.benjaminledet.pokedex.ui.pokemon.detail +import android.graphics.Color +import android.graphics.drawable.ColorDrawable import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.view.animation.AnimationUtils import androidx.core.view.isVisible import androidx.fragment.app.Fragment import androidx.lifecycle.Observer @@ -13,6 +17,7 @@ import com.squareup.picasso.Picasso import kotlinx.android.synthetic.main.fragment_pokemon_detail.* import org.koin.androidx.viewmodel.ext.android.viewModel + class PokemonDetailFragment: Fragment() { private val viewModel by viewModel() @@ -35,7 +40,84 @@ class PokemonDetailFragment: Fragment() { weight.text = getString(R.string.pokemon_weight, pokemon?.detail?.weight.toString()) height.text = getString(R.string.pokemon_height, pokemon?.detail?.height.toString()) + //affichage types pokemon + + val typesPokemon = pokemon?.detail?.types?.joinToString(" - ") + type.text = getString(R.string.pokemon_type, typesPokemon) + + pokemon?.detail?.let { pokemonDetail -> + var mainTypePokemon = if(pokemon.detail.types.size > 1){ + pokemon.detail.types?.get(1) + } else { + pokemon.detail.types.get(0) + } + + // data HEX COLOR from http://epidemicjohto.forumotion.com/t882-type-colors-hex-colors + + when(mainTypePokemon){ + "poison" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.poison)) + "fire" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.fire)) + "water" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.water)) + "ground" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.ground)) + "rock" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.rock)) + "psychic" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.psychic)) + "dark" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.dark)) + "bug" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.bug)) + "dragon" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.dragon)) + "grass" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.grass)) + "ice" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.ice)) + "ghost" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.ghost)) + "electric" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.electric)) + "normal" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.normal)) + "steal" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.steal)) + "fairy" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.fairy)) + "fighting" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.fighting)) + "flying" -> pokemonContainer.setCardBackgroundColor(requireContext().getColor(R.color.flying)) + else -> pokemonContainer.setCardBackgroundColor(Color.TRANSPARENT) + } + } + Picasso.get().load(pokemon?.iconUrl).into(icon) + val animation = AnimationUtils.loadAnimation(this.context, R.anim.bounce) + icon.startAnimation(animation) + + }) + + viewModel.moves.observe(this, Observer { moves -> + + val color = pokemonContainer.cardBackgroundColor.defaultColor + + attackOne.setBackgroundColor(color) + attackTwo.setBackgroundColor(color) + attackThree.setBackgroundColor(color) + attackFour.setBackgroundColor(color) + + if (moves.isNotEmpty()) + { + var randomOne = 0 + var randomTwo = 0 + var randomThree = 0 + var randomFour = 0 + + val end = moves.size - 1 + + randomOne = (0..end).shuffled().first() + randomTwo = (0..end).shuffled().first() + randomThree= (0..end).shuffled().first() + randomFour = (0..end).shuffled().first() + + attackOne.text = moves[randomOne].toString() + attackTwo.text = moves[randomTwo].toString() + attackThree.text = moves[randomThree].toString() + attackFour.text = moves[randomFour].toString() + } + else + { + attackOne.text = "-" + attackTwo.text = "-" + attackThree.text = "-" + attackFour.text = "-" + } }) } } \ No newline at end of file diff --git a/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailViewModel.kt b/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailViewModel.kt index f26a20e..ff7d4f4 100644 --- a/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailViewModel.kt +++ b/app/src/main/java/com/benjaminledet/pokedex/ui/pokemon/detail/PokemonDetailViewModel.kt @@ -17,6 +17,8 @@ class PokemonDetailViewModel(application: Application): AndroidViewModel(applica val pokemon = pokemonId.switchMap { id -> pokemonRepository.getPokemonObservable(id) } + val moves = pokemon.switchMap { pokemon -> pokemonRepository.getMovesObservable(pokemon?.detail?.moves ?: listOf())} + val refreshState = pokemonId.switchMap { id -> pokemonRepository.refreshPokemon(viewModelScope, id) } } \ No newline at end of file diff --git a/app/src/main/res/anim/bounce.xml b/app/src/main/res/anim/bounce.xml new file mode 100644 index 0000000..c27f089 --- /dev/null +++ b/app/src/main/res/anim/bounce.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/color/card_ripple.xml b/app/src/main/res/color/card_ripple.xml new file mode 100644 index 0000000..6908dc9 --- /dev/null +++ b/app/src/main/res/color/card_ripple.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/font/play.xml b/app/src/main/res/font/play.xml new file mode 100644 index 0000000..aba3a66 --- /dev/null +++ b/app/src/main/res/font/play.xml @@ -0,0 +1,7 @@ + + + diff --git a/app/src/main/res/font/play_bold.xml b/app/src/main/res/font/play_bold.xml new file mode 100644 index 0000000..74d3eee --- /dev/null +++ b/app/src/main/res/font/play_bold.xml @@ -0,0 +1,7 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2ddcdb1..b4d3353 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -26,8 +26,7 @@ android:id="@+id/bottomNavigationView" android:layout_width="match_parent" android:layout_height="wrap_content" - app:menu="@menu/main_navigation" - android:background="@android:color/white"/> + app:menu="@menu/main_navigation" /> diff --git a/app/src/main/res/layout/fragment_account.xml b/app/src/main/res/layout/fragment_account.xml index a09bc9d..1b20ad1 100644 --- a/app/src/main/res/layout/fragment_account.xml +++ b/app/src/main/res/layout/fragment_account.xml @@ -1,75 +1,92 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_margin="16dp" + android:gravity="center_horizontal"> + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="@string/email"> + android:layout_width="match_parent" + android:layout_height="wrap_content"/> + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="@string/password" + app:endIconMode="password_toggle" + > + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:inputType="textPassword" + />