From 23d17650fea3f5910f2b8e5306f5b916482a1051 Mon Sep 17 00:00:00 2001 From: Fabio Buracchi <45599613+Sph00b@users.noreply.github.com> Date: Thu, 9 Jul 2020 02:12:09 +0200 Subject: [PATCH] Fix #29 refactor - Remove boilerplace, add model methods - Replace client with custom Call, add getter in some model - Only affects highlight class --- app/build.gradle | 33 +- .../model/utility/common/Description.java | 8 + .../data/model/utility/common/Effect.java | 8 + .../data/model/utility/common/Encounter.java | 20 + .../data/model/utility/common/FlavorText.java | 12 + .../utility/common/GenerationGameIndex.java | 8 + .../utility/common/MachineVersionDetail.java | 8 + .../data/model/utility/common/Name.java | 8 + .../model/utility/common/ResourceList.java | 50 + .../model/utility/common/VerboseEffect.java | 12 + .../common/VersionEncounterDetail.java | 12 + .../utility/common/VersionGameIndex.java | 8 + .../common/VersionGroupFlavorText.java | 12 + .../pokedex/data/remote/ApiError.java | 12 - .../pokedex/data/remote/PokeApi.java | 531 ------- .../pokedex/data/remote/PokeApiFactory.java | 70 + .../pokedex/data/remote/PokeApiService.java | 1336 +++++++++-------- .../pokedex/data/remote/call/PokeCall.java | 28 + .../data/remote/call/PokeCallAdapter.java | 32 + .../remote/call/PokeCallAdapterFactory.java | 33 + .../PokeTypeAdapterFactory.java} | 22 +- .../data/repository/LanguageRepository.java | 18 +- .../com/xeroxparc/pokedex/utils/Utils.java | 8 + 23 files changed, 1137 insertions(+), 1152 deletions(-) create mode 100644 app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/ResourceList.java delete mode 100644 app/src/main/java/com/xeroxparc/pokedex/data/remote/ApiError.java delete mode 100644 app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApi.java create mode 100644 app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApiFactory.java create mode 100644 app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCall.java create mode 100644 app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCallAdapter.java create mode 100644 app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCallAdapterFactory.java rename app/src/main/java/com/xeroxparc/pokedex/data/remote/{ResourceTypeAdapterFactory.java => type/PokeTypeAdapterFactory.java} (71%) diff --git a/app/build.gradle b/app/build.gradle index d231618..8186d83 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,27 +34,38 @@ android { } dependencies { + + def lifecycle_version = "2.2.0" + def navigation_version = "2.3.0" + def retrofit_version = "2.9.0" + def room_version = "2.2.5" + def paging_version = "2.1.2" + implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' - implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" + implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' - implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' + implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" implementation 'com.google.android.material:material:1.1.0' - implementation 'androidx.navigation:navigation-fragment:2.3.0' - implementation 'androidx.navigation:navigation-runtime:2.3.0' - implementation 'androidx.navigation:navigation-ui:2.3.0' + implementation "androidx.navigation:navigation-fragment:$navigation_version" + implementation "androidx.navigation:navigation-runtime:$navigation_version" + implementation "androidx.navigation:navigation-ui:$navigation_version" - implementation 'com.squareup.retrofit2:retrofit:2.9.0' - implementation 'com.squareup.retrofit2:converter-gson:2.9.0' + implementation "com.squareup.retrofit2:retrofit:$retrofit_version" + implementation "com.squareup.retrofit2:converter-gson:$retrofit_version" implementation 'com.squareup.okhttp3:logging-interceptor:4.7.2' - - implementation 'androidx.room:room-runtime:2.2.5' - annotationProcessor "androidx.room:room-compiler:2.2.5" - + + implementation "androidx.room:room-runtime:$room_version" + annotationProcessor "androidx.room:room-compiler:$room_version" + + implementation "androidx.paging:paging-runtime:$paging_version" + testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Description.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Description.java index 9205a59..b945e66 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Description.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Description.java @@ -16,4 +16,12 @@ public class Description { @SerializedName("language") private Language language; + public String getText() { + return text; + } + + public Language getLanguage() { + return language; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Effect.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Effect.java index 0abb98e..1dfb090 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Effect.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Effect.java @@ -16,4 +16,12 @@ public class Effect { @SerializedName("language") private Language language; + public String getText() { + return text; + } + + public Language getLanguage() { + return language; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Encounter.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Encounter.java index 6558457..2a588e2 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Encounter.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Encounter.java @@ -31,4 +31,24 @@ public class Encounter { @SerializedName("method") private EncounterMethod encounterMethod; + public Integer getMinLevel() { + return minLevel; + } + + public Integer getMaxLevel() { + return maxLevel; + } + + public List getEncounterConditionValueList() { + return encounterConditionValueList; + } + + public Integer getChance() { + return chance; + } + + public EncounterMethod getEncounterMethod() { + return encounterMethod; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/FlavorText.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/FlavorText.java index 9ad614c..bc1b02c 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/FlavorText.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/FlavorText.java @@ -21,4 +21,16 @@ public class FlavorText { @SerializedName("version") private Version version; + public String getText() { + return text; + } + + public Language getLanguage() { + return language; + } + + public Version getVersion() { + return version; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/GenerationGameIndex.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/GenerationGameIndex.java index 633f2bb..8caf476 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/GenerationGameIndex.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/GenerationGameIndex.java @@ -16,4 +16,12 @@ public class GenerationGameIndex { @SerializedName("generation") private Generation generation; + public Integer getGameIndex() { + return gameIndex; + } + + public Generation getGeneration() { + return generation; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/MachineVersionDetail.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/MachineVersionDetail.java index 5278e5f..7aeeb53 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/MachineVersionDetail.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/MachineVersionDetail.java @@ -17,4 +17,12 @@ public class MachineVersionDetail { @SerializedName("version_group") private VersionGroup versionGroup; + public Machine getMachine() { + return machine; + } + + public VersionGroup getVersionGroup() { + return versionGroup; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Name.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Name.java index 10e3943..741d05c 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Name.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/Name.java @@ -16,4 +16,12 @@ public class Name { @SerializedName("language") private Language language; + public String getText() { + return text; + } + + public Language getLanguage() { + return language; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/ResourceList.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/ResourceList.java new file mode 100644 index 0000000..91b0ff1 --- /dev/null +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/ResourceList.java @@ -0,0 +1,50 @@ +package com.xeroxparc.pokedex.data.model.utility.common; + +import androidx.annotation.Nullable; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +/** + * @author Fabio Buracchi + */ +public class ResourceList { + + // The total number of resources available from this API. + @SerializedName("count") + private Integer count; + + // The next page number in the list. + @Nullable + @SerializedName("next") + private Integer nextPageNumber; + + // The previous page number in the list + @Nullable + @SerializedName("previous") + private Integer previousPageNumber; + + // A list of resources. + @SerializedName("results") + private List results; + + public Integer getCount() { + return count; + } + + @Nullable + public Integer getNextPageNumber() { + return nextPageNumber; + } + + @Nullable + public Integer getPreviousPageNumber() { + return previousPageNumber; + } + + public List getResults() { + return results; + } + +} diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VerboseEffect.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VerboseEffect.java index 355c759..89bfe9f 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VerboseEffect.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VerboseEffect.java @@ -20,4 +20,16 @@ public class VerboseEffect { @SerializedName("language") private Language language; + public String getEffect() { + return effect; + } + + public String getShortEffect() { + return shortEffect; + } + + public Language getLanguage() { + return language; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionEncounterDetail.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionEncounterDetail.java index 51a1773..0067ae4 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionEncounterDetail.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionEncounterDetail.java @@ -22,4 +22,16 @@ public class VersionEncounterDetail { @SerializedName("encounter_details") private List encounterDetailList; + public Version getVersion() { + return version; + } + + public Integer getMaxChance() { + return maxChance; + } + + public List getEncounterDetailList() { + return encounterDetailList; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionGameIndex.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionGameIndex.java index 3600b57..ff2b40d 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionGameIndex.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionGameIndex.java @@ -16,4 +16,12 @@ public class VersionGameIndex { @SerializedName("version") private Version version; + public Integer getGameIndex() { + return gameIndex; + } + + public Version getVersion() { + return version; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionGroupFlavorText.java b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionGroupFlavorText.java index 6223950..aa2e001 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionGroupFlavorText.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/model/utility/common/VersionGroupFlavorText.java @@ -21,4 +21,16 @@ public class VersionGroupFlavorText { @SerializedName("version_group") private VersionGroup versionGroup; + public String getText() { + return text; + } + + public Language getLanguage() { + return language; + } + + public VersionGroup getVersionGroup() { + return versionGroup; + } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/remote/ApiError.java b/app/src/main/java/com/xeroxparc/pokedex/data/remote/ApiError.java deleted file mode 100644 index 6f48c5c..0000000 --- a/app/src/main/java/com/xeroxparc/pokedex/data/remote/ApiError.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.xeroxparc.pokedex.data.remote; - -/** - * @author Fabio Buracchi - */ -public class ApiError extends Exception { - - ApiError(int code, String message) { - super("(" + code + "): " + message); - } - -} diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApi.java b/app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApi.java deleted file mode 100644 index 43f8817..0000000 --- a/app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApi.java +++ /dev/null @@ -1,531 +0,0 @@ -package com.xeroxparc.pokedex.data.remote; - -import androidx.annotation.NonNull; - -import com.xeroxparc.pokedex.data.model.berry.Berry; -import com.xeroxparc.pokedex.data.model.berry.firmness.BerryFirmness; -import com.xeroxparc.pokedex.data.model.berry.flavor.BerryFlavor; -import com.xeroxparc.pokedex.data.model.contest.effect.ContestEffect; -import com.xeroxparc.pokedex.data.model.contest.supereffect.SuperContestEffect; -import com.xeroxparc.pokedex.data.model.contest.type.ContestType; -import com.xeroxparc.pokedex.data.model.encounter.condition.EncounterCondition; -import com.xeroxparc.pokedex.data.model.encounter.conditionvalue.EncounterConditionValue; -import com.xeroxparc.pokedex.data.model.encounter.method.EncounterMethod; -import com.xeroxparc.pokedex.data.model.evolution.chain.EvolutionChain; -import com.xeroxparc.pokedex.data.model.evolution.trigger.EvolutionTrigger; -import com.xeroxparc.pokedex.data.model.game.generation.Generation; -import com.xeroxparc.pokedex.data.model.game.pokedex.Pokedex; -import com.xeroxparc.pokedex.data.model.game.version.Version; -import com.xeroxparc.pokedex.data.model.game.versiongroup.VersionGroup; -import com.xeroxparc.pokedex.data.model.item.Item; -import com.xeroxparc.pokedex.data.model.item.attribute.ItemAttribute; -import com.xeroxparc.pokedex.data.model.item.category.ItemCategory; -import com.xeroxparc.pokedex.data.model.item.effect.ItemFlingEffect; -import com.xeroxparc.pokedex.data.model.item.pocket.ItemPocket; -import com.xeroxparc.pokedex.data.model.location.Location; -import com.xeroxparc.pokedex.data.model.location.area.LocationArea; -import com.xeroxparc.pokedex.data.model.location.palparckarea.PalParkArea; -import com.xeroxparc.pokedex.data.model.location.region.Region; -import com.xeroxparc.pokedex.data.model.machine.Machine; -import com.xeroxparc.pokedex.data.model.move.Move; -import com.xeroxparc.pokedex.data.model.move.ailment.MoveAilment; -import com.xeroxparc.pokedex.data.model.move.battlestyle.MoveBattleStyle; -import com.xeroxparc.pokedex.data.model.move.categoty.MoveCategory; -import com.xeroxparc.pokedex.data.model.move.damageclass.MoveDamageClass; -import com.xeroxparc.pokedex.data.model.move.learnmethod.MoveLearnMethod; -import com.xeroxparc.pokedex.data.model.move.target.MoveTarget; -import com.xeroxparc.pokedex.data.model.pokemon.Pokemon; -import com.xeroxparc.pokedex.data.model.pokemon.ability.Ability; -import com.xeroxparc.pokedex.data.model.pokemon.characteristic.Characteristic; -import com.xeroxparc.pokedex.data.model.pokemon.color.PokemonColor; -import com.xeroxparc.pokedex.data.model.pokemon.egggroup.EggGroup; -import com.xeroxparc.pokedex.data.model.pokemon.form.PokemonForm; -import com.xeroxparc.pokedex.data.model.pokemon.gender.Gender; -import com.xeroxparc.pokedex.data.model.pokemon.growthrate.GrowthRate; -import com.xeroxparc.pokedex.data.model.pokemon.habitat.PokemonHabitat; -import com.xeroxparc.pokedex.data.model.pokemon.nature.Nature; -import com.xeroxparc.pokedex.data.model.pokemon.pokeathlonstats.PokeathlonStat; -import com.xeroxparc.pokedex.data.model.pokemon.shape.PokemonShape; -import com.xeroxparc.pokedex.data.model.pokemon.species.PokemonSpecies; -import com.xeroxparc.pokedex.data.model.pokemon.stats.Stat; -import com.xeroxparc.pokedex.data.model.pokemon.type.Type; -import com.xeroxparc.pokedex.data.model.utility.language.Language; - -import java.util.List; - -import retrofit2.Call; -import retrofit2.http.GET; -import retrofit2.http.Path; -import retrofit2.http.Query; - -/** - * @author Fabio Buracchi - */ -public interface PokeApi { - - @GET("berry/") - Call> getBerryList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("berry/{id}") - Call getBerry(@Path("id") @NonNull Integer id); - - @GET("berry/{name}") - Call getBerry(@Path("name") @NonNull String name); - - @GET("berry-firmness/") - Call> getBerryFirmnessList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("berry-firmness/{id}") - Call getBerryFirmness(@Path("id") @NonNull Integer id); - - @GET("berry-firmness/{name}") - Call getBerryFirmness(@Path("name") @NonNull String name); - - @GET("berry-flavor/") - Call> getBerryFlavorList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("berry-flavor/{id}") - Call getBerryFlavor(@Path("id") @NonNull Integer id); - - @GET("berry-flavor/{name}") - Call getBerryFlavor(@Path("name") @NonNull String name); - - @GET("contest-type/") - Call> getContestTypeList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("contest-type/{id}") - Call getContestType(@Path("id") @NonNull Integer id); - - @GET("contest-type/{name}") - Call getContestType(@Path("name") @NonNull String name); - - @GET("contest-effect/") - Call> getContestEffectList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("contest-effect/{id}") - Call getContestEffect(@Path("id") @NonNull Integer id); - - @GET("super-contest-effect/") - Call> getSuperContestEffectList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("super-contest-effect/{id}") - Call getSuperContestEffect(@Path("id") @NonNull Integer id); - - @GET("encounter-method/") - Call> getEncounterMethodList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("encounter-method/{id}") - Call getEncounterMethod(@Path("id") @NonNull Integer id); - - @GET("encounter-method/{name}") - Call getEncounterMethod(@Path("name") @NonNull String name); - - @GET("encounter-condition/") - Call> getEncounterConditionList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("encounter-condition/{id}") - Call getEncounterCondition(@Path("id") @NonNull Integer id); - - @GET("encounter-condition/{name}") - Call getEncounterCondition(@Path("name") @NonNull String name); - - @GET("encounter-condition-value/") - Call>getEncounterConditionValueList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("encounter-condition-value/{id}") - Call getEncounterConditionValue(@Path("id") @NonNull Integer id); - - @GET("encounter-condition-value/{name}") - Call getEncounterConditionValue(@Path("name") @NonNull String name); - - @GET("evolution-chain/") - Call> getEvolutionChainList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("evolution-chain/{id}") - Call getEvolutionChain(@Path("id") @NonNull Integer id); - - @GET("evolution-trigger/") - Call> getEvolutionTriggerList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("evolution-trigger/{id}") - Call getEvolutionTrigger(@Path("id") @NonNull Integer id); - - @GET("evolution-trigger/{name}") - Call getEvolutionTrigger(@Path("name") @NonNull String name); - - @GET("generation/") - Call> getGenerationList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("generation/{id}") - Call getGeneration(@Path("id") @NonNull Integer id); - - @GET("generation/{name}") - Call getGeneration(@Path("name") @NonNull String name); - - @GET("pokedex/") - Call> getPokedexList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("pokedex/{id}") - Call getPokedex(@Path("id") @NonNull Integer id); - - @GET("pokedex/{name}") - Call getPokedex(@Path("name") @NonNull String name); - - @GET("version/") - Call> getVersionList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("version/{id}") - Call getVersion(@Path("id") @NonNull Integer id); - - @GET("version/{name}") - Call getVersion(@Path("name") @NonNull String name); - - @GET("version-group/") - Call> getVersionGroupList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("version-group/{id}") - Call getVersionGroup(@Path("id") @NonNull Integer id); - - @GET("version-group/{name}") - Call getVersionGroup(@Path("name") @NonNull String name); - - @GET("item/") - Call> getItemList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("item/{id}") - Call getItem(@Path("id") @NonNull Integer id); - - @GET("item/{name}") - Call getItem(@Path("name") @NonNull String name); - - @GET("item-attribute/") - Call> getItemAttributeList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("item-attribute/{id}") - Call getItemAttribute(@Path("id") @NonNull Integer id); - - @GET("item-attribute/{name}") - Call getItemAttribute(@Path("name") @NonNull String name); - - @GET("item-category/") - Call> getItemCategoryList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("item-category/{id}") - Call getItemCategory(@Path("id") @NonNull Integer id); - - @GET("item-category/{name}") - Call getItemCategory(@Path("name") @NonNull String name); - - @GET("item-fling-effect/") - Call> getItemFlingEffectList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("item-fling-effect/{id}") - Call getItemFlingEffect(@Path("id") @NonNull Integer id); - - @GET("item-fling-effect/{name}") - Call getItemFlingEffect(@Path("name") @NonNull String name); - - @GET("item-pocket/") - Call> getItemPocketList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("item-pocket/{id}") - Call getItemPocket(@Path("id") @NonNull Integer id); - - @GET("item-pocket/{name}") - Call getItemPocket(@Path("name") @NonNull String name); - - @GET("location/") - Call> getLocationList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("location/{id}") - Call getLocation(@Path("id") @NonNull Integer id); - - @GET("location/{name}") - Call getLocation(@Path("name") @NonNull String name); - - @GET("location-area/") - Call> getLocationAreaList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("location-area/{id}") - Call getLocationArea(@Path("id") @NonNull Integer id); - - @GET("location-area/{name}") - Call getLocationArea(@Path("name") @NonNull String name); - - @GET("pal-park-area/") - Call> getPalParkAreaList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("pal-park-area/{id}") - Call getPalParkArea(@Path("id") @NonNull Integer id); - - @GET("pal-park-area/{name}") - Call getPalParkArea(@Path("name") @NonNull String name); - - @GET("region/") - Call> getRegionList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("region/{id}") - Call getRegion(@Path("id") @NonNull Integer id); - - @GET("region/{name}") - Call getRegion(@Path("name") @NonNull String name); - - @GET("machine/") - Call> getMachineList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("machine/{id}") - Call getMachine(@Path("id") @NonNull Integer id); - - @GET("move/") - Call> getMoveList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("move/{id}") - Call getMove(@Path("id") @NonNull Integer id); - - @GET("move/{name}") - Call getMove(@Path("name") @NonNull String name); - - @GET("move-ailment/") - Call> getMoveAilmentList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("move-ailment/{id}") - Call getMoveAilment(@Path("id") @NonNull Integer id); - - @GET("move-ailment/{name}") - Call getMoveAilment(@Path("name") @NonNull String name); - - @GET("move-battle-style/") - Call> getMoveBattleStyleList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("move-battle-style/{id}") - Call getMoveBattleStyle(@Path("id") @NonNull Integer id); - - @GET("move-battle-style/{name}") - Call getMoveBattleStyle(@Path("name") @NonNull String name); - - @GET("move-category/") - Call> getModelNameList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("move-category/{id}") - Call getModelName(@Path("id") @NonNull Integer id); - - @GET("move-category/{name}") - Call getModelName(@Path("name") @NonNull String name); - - @GET("move-damage-class/") - Call> getMoveDamageClassList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("move-damage-class/{id}") - Call getMoveDamageClass(@Path("id") @NonNull Integer id); - - @GET("move-damage-class/{name}") - Call getMoveDamageClass(@Path("name") @NonNull String name); - - @GET("move-learn-method/") - Call> getMoveLearnMethodList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("move-learn-method/{id}") - Call getMoveLearnMethod(@Path("id") @NonNull Integer id); - - @GET("move-learn-method/{name}") - Call getMoveLearnMethod(@Path("name") @NonNull String name); - - @GET("move-target/") - Call> getMoveTargetList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("move-target/{id}") - Call getMoveTarget(@Path("id") @NonNull Integer id); - - @GET("move-target/{name}") - Call getMoveTarget(@Path("name") @NonNull String name); - - @GET("ability/") - Call> getAbilityList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("ability/{id}") - Call getAbility(@Path("id") @NonNull Integer id); - - @GET("ability/{name}") - Call getAbility(@Path("name") @NonNull String name); - - @GET("characteristic/") - Call> getCharacteristicList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("characteristic/{id}") - Call getCharacteristic(@Path("id") @NonNull Integer id); - - @GET("egg-group/") - Call> getEggGroupList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("egg-group/{id}") - Call getEggGroup(@Path("id") @NonNull Integer id); - - @GET("egg-group/{name}") - Call getEggGroup(@Path("name") @NonNull String name); - - @GET("gender/") - Call> getGenderList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("gender/{id}") - Call getGender(@Path("id") @NonNull Integer id); - - @GET("gender/{name}") - Call getGender(@Path("name") @NonNull String name); - - @GET("growth-rate/") - Call> getGrowthRateList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("growth-rate/{id}") - Call getGrowthRate(@Path("id") @NonNull Integer id); - - @GET("growth-rate/{name}") - Call getGrowthRate(@Path("name") @NonNull String name); - - @GET("nature/") - Call> getNatureList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("nature/{id}") - Call getNature(@Path("id") @NonNull Integer id); - - @GET("nature/{name}") - Call getNature(@Path("name") @NonNull String name); - - @GET("pokeathlon-stat/") - Call> getPokeathlonStatList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("pokeathlon-stat/{id}") - Call getPokeathlonStat(@Path("id") @NonNull Integer id); - - @GET("pokeathlon-stat/{name}") - Call getPokeathlonStat(@Path("name") @NonNull String name); - - @GET("pokemon/") - Call> getPokemonList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("pokemon/{id}") - Call getPokemon(@Path("id") @NonNull Integer id); - - @GET("pokemon/{name}") - Call getPokemon(@Path("name") @NonNull String name); - - @GET("pokemon-color/") - Call> getPokemonColorList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("pokemon-color/{id}") - Call getPokemonColor(@Path("id") @NonNull Integer id); - - @GET("pokemon-color/{name}") - Call getPokemonColor(@Path("name") @NonNull String name); - - @GET("pokemon-form/") - Call> getPokemonFormList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("pokemon-form/{id}") - Call getPokemonForm(@Path("id") @NonNull Integer id); - - @GET("pokemon-form/{name}") - Call getPokemonForm(@Path("name") @NonNull String name); - - @GET("pokemon-habitat/") - Call> getPokemonHabitatList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("pokemon-habitat/{id}") - Call getPokemonHabitat(@Path("id") @NonNull Integer id); - - @GET("pokemon-habitat/{name}") - Call getPokemonHabitat(@Path("name") @NonNull String name); - - @GET("pokemon-shape/") - Call> getPokemonShapeList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("pokemon-shape/{id}") - Call getPokemonShape(@Path("id") @NonNull Integer id); - - @GET("pokemon-shape/{name}") - Call getPokemonShape(@Path("name") @NonNull String name); - - @GET("pokemon-species/") - Call> getPokemonSpeciesList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("pokemon-species/{id}") - Call getPokemonSpecies(@Path("id") @NonNull Integer id); - - @GET("pokemon-species/{name}") - Call getPokemonSpecies(@Path("name") @NonNull String name); - - @GET("stat/") - Call> getStatList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("stat/{id}") - Call getStat(@Path("id") @NonNull Integer id); - - @GET("stat/{name}") - Call getStat(@Path("name") @NonNull String name); - - @GET("type/") - Call> getTypeList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("type/{id}") - Call getType(@Path("id") @NonNull Integer id); - - @GET("type/{name}") - Call getType(@Path("name") @NonNull String name); - - @GET("language/") - Call> getLanguageList(@Query("limit") @NonNull Integer limit, - @Query("offset") @NonNull Integer offset); - - @GET("language/{id}") - Call getLanguage(@Path("id") @NonNull Integer id); - - @GET("language/{name}") - Call getLanguage(@Path("name") @NonNull String name); - -} \ No newline at end of file diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApiFactory.java b/app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApiFactory.java new file mode 100644 index 0000000..7660600 --- /dev/null +++ b/app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApiFactory.java @@ -0,0 +1,70 @@ +package com.xeroxparc.pokedex.data.remote; + +import android.util.Log; + +import com.google.gson.FieldNamingPolicy; +import com.google.gson.GsonBuilder; +import com.xeroxparc.pokedex.data.remote.call.PokeCallAdapterFactory; +import com.xeroxparc.pokedex.data.remote.type.PokeTypeAdapterFactory; + +import java.io.IOException; + +import okhttp3.OkHttpClient; +import okhttp3.logging.HttpLoggingInterceptor; +import retrofit2.Call; +import retrofit2.HttpException; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + +/** + * @author Fabio Buracchi + */ +public abstract class PokeApiFactory implements PokeApiService { + + private static PokeApiService service; + + private PokeApiFactory() {} + + public static synchronized PokeApiService getService() { + if (service == null) { + service = new Retrofit.Builder() + .baseUrl("https://pokeapi.co/api/v2/") + .client( + new OkHttpClient.Builder() + .addInterceptor(new HttpLoggingInterceptor( + s -> Log.d("API", s)) + .setLevel(HttpLoggingInterceptor.Level.BASIC) + ) + .build() + ) + .addConverterFactory(GsonConverterFactory.create( + new GsonBuilder() + .setFieldNamingPolicy( + FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES + ) + .registerTypeAdapterFactory( + new PokeTypeAdapterFactory() + ) + .create() + ) + ) + .addCallAdapterFactory( + new PokeCallAdapterFactory() + ) + .build() + .create(PokeApiService.class); + } + return service; + } + + public static T result(Call request) throws IOException { + Response response = request.execute(); + if (response.isSuccessful()) { + return response.body(); + } else { + throw new HttpException(response); + } + } + +} diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApiService.java b/app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApiService.java index 0b98b3a..f89efb7 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApiService.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/remote/PokeApiService.java @@ -1,11 +1,7 @@ package com.xeroxparc.pokedex.data.remote; -import android.util.Log; - import androidx.annotation.NonNull; -import com.google.gson.FieldNamingPolicy; -import com.google.gson.GsonBuilder; import com.xeroxparc.pokedex.data.model.berry.Berry; import com.xeroxparc.pokedex.data.model.berry.firmness.BerryFirmness; import com.xeroxparc.pokedex.data.model.berry.flavor.BerryFlavor; @@ -53,593 +49,761 @@ import com.xeroxparc.pokedex.data.model.pokemon.species.PokemonSpecies; import com.xeroxparc.pokedex.data.model.pokemon.stats.Stat; import com.xeroxparc.pokedex.data.model.pokemon.type.Type; +import com.xeroxparc.pokedex.data.model.utility.common.ResourceList; import com.xeroxparc.pokedex.data.model.utility.language.Language; +import com.xeroxparc.pokedex.data.remote.call.PokeCall; -import java.io.IOException; -import java.util.List; - -import okhttp3.OkHttpClient; -import okhttp3.logging.HttpLoggingInterceptor; -import retrofit2.Call; -import retrofit2.Response; -import retrofit2.Retrofit; -import retrofit2.converter.gson.GsonConverterFactory; +import retrofit2.http.GET; +import retrofit2.http.Path; +import retrofit2.http.Query; /** - * Access point to the PokeAPI services - * * @author Fabio Buracchi */ -public class PokeApiService { - - private static PokeApi service; - - public PokeApiService() { - getService(); - } - - private synchronized void getService() { - if (service == null) { - service = new Retrofit.Builder() - .baseUrl("https://pokeapi.co/api/v2/") - .client( - new OkHttpClient.Builder() - .addInterceptor(new HttpLoggingInterceptor( - s -> Log.d("API", s)) - .setLevel(HttpLoggingInterceptor.Level.BASIC) - ) - .build() - ) - .addConverterFactory(GsonConverterFactory.create( - new GsonBuilder() - .setFieldNamingPolicy( - FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES - ) - .registerTypeAdapterFactory( - new ResourceTypeAdapterFactory() - ) - .create() - ) - ) - .build() - .create(PokeApi.class); - } - } - - private T result(@NonNull Call request) throws IOException, ApiError { - Response response = request.execute(); - if (response.isSuccessful()) { - return response.body(); - } else { - throw new ApiError(response.code(), response.message()); - } - } - - public List getBerryList(@NonNull Integer limit, @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getBerryList(limit, offset)); - } - - public Berry getBerry(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getBerry(id)); - } - - public List getBerryFirmnessList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getBerryFirmnessList(limit, offset)); - } - - public BerryFirmness getBerryFirmness(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getBerryFirmness(id)); - } - - public List getBerryFlavorList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getBerryFlavorList(limit, offset)); - } - - public BerryFlavor getBerryFlavor(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getBerryFlavor(id)); - } - - public List getContestTypeList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getContestTypeList(limit, offset)); - } - - public ContestType getContestType(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getContestType(id)); - } - - public List getContestEffectList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getContestEffectList(limit, offset)); - } - - public ContestEffect getContestEffect(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getContestEffect(id)); - } - - public List getSuperContestEffectList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getSuperContestEffectList(limit, offset)); - } - - public SuperContestEffect getSuperContestEffect(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getSuperContestEffect(id)); - } - - public List getEncounterMethodList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getEncounterMethodList(limit, offset)); - } - - public EncounterMethod getEncounterMethod(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getEncounterMethod(id)); - } - - public List getEncounterConditionList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getEncounterConditionList(limit, offset)); - } - - public EncounterCondition getEncounterCondition(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getEncounterCondition(id)); - } - - public List getEncounterConditionValueList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getEncounterConditionValueList(limit, offset)); - } - - public EncounterConditionValue getEncounterConditionValue(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getEncounterConditionValue(id)); - } - - public List getEvolutionChainList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getEvolutionChainList(limit, offset)); - } - - public EvolutionChain getEvolutionChain(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getEvolutionChain(id)); - } - - public List getEvolutionTriggerList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getEvolutionTriggerList(limit, offset)); - } - - public EvolutionTrigger getEvolutionTrigger(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getEvolutionTrigger(id)); - } - - public List getGenerationList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getGenerationList(limit, offset)); - } - - public Generation getGeneration(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getGeneration(id)); - } - - public List getPokedexList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getPokedexList(limit, offset)); - } - - public Pokedex getPokedex(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getPokedex(id)); - } - - public List getVersionList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getVersionList(limit, offset)); - } - - public Version getVersion(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getVersion(id)); - } - - public List getVersionGroupList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getVersionGroupList(limit, offset)); - } - - public VersionGroup getVersionGroup(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getVersionGroup(id)); - } - - public List getItemList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getItemList(limit, offset)); - } - - public Item getItem(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getItem(id)); - } - - public List getItemAttributeList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getItemAttributeList(limit, offset)); - } - - public ItemAttribute getItemAttribute(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getItemAttribute(id)); - } - - public List getItemCategoryList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getItemCategoryList(limit, offset)); - } - - public ItemCategory getItemCategory(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getItemCategory(id)); - } - - public List getItemFlingEffectList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getItemFlingEffectList(limit, offset)); - } - - public ItemFlingEffect getItemFlingEffect(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getItemFlingEffect(id)); - } - - public List getItemPocketList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getItemPocketList(limit, offset)); - } - - public ItemPocket getItemPocket(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getItemPocket(id)); - } - - public List getLocationList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getLocationList(limit, offset)); - } - - public Location getLocation(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getLocation(id)); - } - - public List getLocationAreaList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getLocationAreaList(limit, offset)); - } - - public LocationArea getLocationArea(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getLocationArea(id)); - } - - public List getPalParkAreaList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getPalParkAreaList(limit, offset)); - } - - public PalParkArea getPalParkArea(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getPalParkArea(id)); - } - - public List getRegionList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getRegionList(limit, offset)); - } - - public Region getRegion(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getRegion(id)); - } - - public List getMachineList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getMachineList(limit, offset)); - } - - public Machine getMachine(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getMachine(id)); - } - - public List getMoveList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getMoveList(limit, offset)); - } - - public Move getMove(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getMove(id)); - } - - public List getMoveAilmentList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getMoveAilmentList(limit, offset)); - } - - public MoveAilment getMoveAilment(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getMoveAilment(id)); - } - - public List getMoveBattleStyleList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getMoveBattleStyleList(limit, offset)); - } - - public MoveBattleStyle getMoveBattleStyle(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getMoveBattleStyle(id)); - } - - public List getModelNameList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getModelNameList(limit, offset)); - } - - public MoveCategory getModelName(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getModelName(id)); - } - - public List getMoveDamageClassList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getMoveDamageClassList(limit, offset)); - } - - public MoveDamageClass getMoveDamageClass(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getMoveDamageClass(id)); - } - - public List getMoveLearnMethodList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getMoveLearnMethodList(limit, offset)); - } - - public MoveLearnMethod getMoveLearnMethod(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getMoveLearnMethod(id)); - } - - public List getMoveTargetList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getMoveTargetList(limit, offset)); - } - - public MoveTarget getMoveTarget(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getMoveTarget(id)); - } - - public List getAbilityList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getAbilityList(limit, offset)); - } - - public Ability getAbility(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getAbility(id)); - } - - public List getCharacteristicList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getCharacteristicList(limit, offset)); - } - - public Characteristic getCharacteristic(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getCharacteristic(id)); - } - - public List getEggGroupList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getEggGroupList(limit, offset)); - } - - public EggGroup getEggGroup(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getEggGroup(id)); - } - - public List getGenderList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getGenderList(limit, offset)); - } - - public Gender getGender(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getGender(id)); - } - - public List getGrowthRateList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getGrowthRateList(limit, offset)); - } - - public GrowthRate getGrowthRate(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getGrowthRate(id)); - } - - public List getNatureList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getNatureList(limit, offset)); - } - - public Nature getNature(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getNature(id)); - } - - public List getPokeathlonStatList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getPokeathlonStatList(limit, offset)); - } - - public PokeathlonStat getPokeathlonStat(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getPokeathlonStat(id)); - } - - public List getPokemonList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getPokemonList(limit, offset)); - } - - public Pokemon getPokemon(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getPokemon(id)); - } - - public List getPokemonColorList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getPokemonColorList(limit, offset)); - } - - public PokemonColor getPokemonColor(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getPokemonColor(id)); - } - - public List getPokemonFormList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getPokemonFormList(limit, offset)); - } - - public PokemonForm getPokemonForm(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getPokemonForm(id)); - } - - public List getPokemonHabitatList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getPokemonHabitatList(limit, offset)); - } - - public PokemonHabitat getPokemonHabitat(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getPokemonHabitat(id)); - } - - public List getPokemonShapeList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getPokemonShapeList(limit, offset)); - } - - public PokemonShape getPokemonShape(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getPokemonShape(id)); - } - - public List getPokemonSpeciesList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getPokemonSpeciesList(limit, offset)); - } - - public PokemonSpecies getPokemonSpecies(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getPokemonSpecies(id)); - } - - public List getStatList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getStatList(limit, offset)); - } - - public Stat getStat(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getStat(id)); - } - - public List getTypeList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getTypeList(limit, offset)); - } - - public Type getType(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getType(id)); - } - - public List getLanguageList(@NonNull Integer limit, - @NonNull Integer offset) - throws IOException, ApiError { - return result(service.getLanguageList(limit, offset)); - } - - public Language getLanguage(@NonNull Integer id) - throws IOException, ApiError { - return result(service.getLanguage(id)); - } +public interface PokeApiService { + + + @GET("berry/") + PokeCall> getBerryList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("berry/{id}") + PokeCall getBerry( + @Path("id") @NonNull Integer id + ); + + @GET("berry/{name}") + PokeCall getBerry( + @Path("name") @NonNull String name + ); + + @GET("berry-firmness/") + PokeCall> getBerryFirmnessList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("berry-firmness/{id}") + PokeCall getBerryFirmness( + @Path("id") @NonNull Integer id + ); + + @GET("berry-firmness/{name}") + PokeCall getBerryFirmness( + @Path("name") @NonNull String name + ); + + @GET("berry-flavor/") + PokeCall> getBerryFlavorList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("berry-flavor/{id}") + PokeCall getBerryFlavor( + @Path("id") @NonNull Integer id + ); + + @GET("berry-flavor/{name}") + PokeCall getBerryFlavor( + @Path("name") @NonNull String name + ); + + @GET("contest-type/") + PokeCall> getContestTypeList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("contest-type/{id}") + PokeCall getContestType( + @Path("id") @NonNull Integer id + ); + + @GET("contest-type/{name}") + PokeCall getContestType( + @Path("name") @NonNull String name + ); + + @GET("contest-effect/") + PokeCall> getContestEffectList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("contest-effect/{id}") + PokeCall getContestEffect( + @Path("id") @NonNull Integer id + ); + + @GET("super-contest-effect/") + PokeCall> getSuperContestEffectList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("super-contest-effect/{id}") + PokeCall getSuperContestEffect( + @Path("id") @NonNull Integer id + ); + + @GET("encounter-method/") + PokeCall> getEncounterMethodList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("encounter-method/{id}") + PokeCall getEncounterMethod( + @Path("id") @NonNull Integer id + ); + + @GET("encounter-method/{name}") + PokeCall getEncounterMethod( + @Path("name") @NonNull String name + ); + + @GET("encounter-condition/") + PokeCall> getEncounterConditionList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("encounter-condition/{id}") + PokeCall getEncounterCondition( + @Path("id") @NonNull Integer id + ); + + @GET("encounter-condition/{name}") + PokeCall getEncounterCondition( + @Path("name") @NonNull String name + ); + + @GET("encounter-condition-value/") + PokeCall> getEncounterConditionValueList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("encounter-condition-value/{id}") + PokeCall getEncounterConditionValue( + @Path("id") @NonNull Integer id + ); + + @GET("encounter-condition-value/{name}") + PokeCall getEncounterConditionValue( + @Path("name") @NonNull String name + ); + + @GET("evolution-chain/") + PokeCall> getEvolutionChainList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("evolution-chain/{id}") + PokeCall getEvolutionChain( + @Path("id") @NonNull Integer id + ); + + @GET("evolution-trigger/") + PokeCall> getEvolutionTriggerList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("evolution-trigger/{id}") + PokeCall getEvolutionTrigger( + @Path("id") @NonNull Integer id + ); + + @GET("evolution-trigger/{name}") + PokeCall getEvolutionTrigger( + @Path("name") @NonNull String name + ); + + @GET("generation/") + PokeCall> getGenerationList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("generation/{id}") + PokeCall getGeneration( + @Path("id") @NonNull Integer id + ); + + @GET("generation/{name}") + PokeCall getGeneration( + @Path("name") @NonNull String name + ); + + @GET("pokedex/") + PokeCall> getPokedexList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("pokedex/{id}") + PokeCall getPokedex( + @Path("id") @NonNull Integer id + ); + + @GET("pokedex/{name}") + PokeCall getPokedex( + @Path("name") @NonNull String name + ); + + @GET("version/") + PokeCall> getVersionList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("version/{id}") + PokeCall getVersion( + @Path("id") @NonNull Integer id + ); + + @GET("version/{name}") + PokeCall getVersion( + @Path("name") @NonNull String name + ); + + @GET("version-group/") + PokeCall> getVersionGroupList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("version-group/{id}") + PokeCall getVersionGroup( + @Path("id") @NonNull Integer id + ); + + @GET("version-group/{name}") + PokeCall getVersionGroup( + @Path("name") @NonNull String name + ); + + @GET("item/") + PokeCall> getItemList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("item/{id}") + PokeCall getItem( + @Path("id") @NonNull Integer id + ); + + @GET("item/{name}") + PokeCall getItem( + @Path("name") @NonNull String name + ); + + @GET("item-attribute/") + PokeCall> getItemAttributeList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("item-attribute/{id}") + PokeCall getItemAttribute( + @Path("id") @NonNull Integer id + ); + + @GET("item-attribute/{name}") + PokeCall getItemAttribute( + @Path("name") @NonNull String name + ); + + @GET("item-category/") + PokeCall> getItemCategoryList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("item-category/{id}") + PokeCall getItemCategory( + @Path("id") @NonNull Integer id + ); + + @GET("item-category/{name}") + PokeCall getItemCategory( + @Path("name") @NonNull String name + ); + + @GET("item-fling-effect/") + PokeCall> getItemFlingEffectList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("item-fling-effect/{id}") + PokeCall getItemFlingEffect( + @Path("id") @NonNull Integer id + ); + + @GET("item-fling-effect/{name}") + PokeCall getItemFlingEffect( + @Path("name") @NonNull String name + ); + + @GET("item-pocket/") + PokeCall> getItemPocketList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("item-pocket/{id}") + PokeCall getItemPocket( + @Path("id") @NonNull Integer id + ); + + @GET("item-pocket/{name}") + PokeCall getItemPocket( + @Path("name") @NonNull String name + ); + + @GET("location/") + PokeCall> getLocationList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("location/{id}") + PokeCall getLocation( + @Path("id") @NonNull Integer id + ); + + @GET("location/{name}") + PokeCall getLocation( + @Path("name") @NonNull String name + ); + + @GET("location-area/") + PokeCall> getLocationAreaList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("location-area/{id}") + PokeCall getLocationArea( + @Path("id") @NonNull Integer id + ); + + @GET("location-area/{name}") + PokeCall getLocationArea( + @Path("name") @NonNull String name + ); + + @GET("pal-park-area/") + PokeCall> getPalParkAreaList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("pal-park-area/{id}") + PokeCall getPalParkArea( + @Path("id") @NonNull Integer id + ); + + @GET("pal-park-area/{name}") + PokeCall getPalParkArea( + @Path("name") @NonNull String name + ); + + @GET("region/") + PokeCall> getRegionList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("region/{id}") + PokeCall getRegion( + @Path("id") @NonNull Integer id + ); + + @GET("region/{name}") + PokeCall getRegion( + @Path("name") @NonNull String name + ); + + @GET("machine/") + PokeCall> getMachineList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("machine/{id}") + PokeCall getMachine( + @Path("id") @NonNull Integer id + ); + + @GET("move/") + PokeCall> getMoveList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("move/{id}") + PokeCall getMove( + @Path("id") @NonNull Integer id + ); + + @GET("move/{name}") + PokeCall getMove( + @Path("name") @NonNull String name + ); + + @GET("move-ailment/") + PokeCall> getMoveAilmentList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("move-ailment/{id}") + PokeCall getMoveAilment( + @Path("id") @NonNull Integer id + ); + + @GET("move-ailment/{name}") + PokeCall getMoveAilment( + @Path("name") @NonNull String name + ); + + @GET("move-battle-style/") + PokeCall> getMoveBattleStyleList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("move-battle-style/{id}") + PokeCall getMoveBattleStyle( + @Path("id") @NonNull Integer id + ); + + @GET("move-battle-style/{name}") + PokeCall getMoveBattleStyle( + @Path("name") @NonNull String name + ); + + @GET("move-category/") + PokeCall> getModelNameList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("move-category/{id}") + PokeCall getModelName( + @Path("id") @NonNull Integer id + ); + + @GET("move-category/{name}") + PokeCall getModelName( + @Path("name") @NonNull String name + ); + + @GET("move-damage-class/") + PokeCall> getMoveDamageClassList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("move-damage-class/{id}") + PokeCall getMoveDamageClass( + @Path("id") @NonNull Integer id + ); + + @GET("move-damage-class/{name}") + PokeCall getMoveDamageClass( + @Path("name") @NonNull String name + ); + + @GET("move-learn-method/") + PokeCall> getMoveLearnMethodList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("move-learn-method/{id}") + PokeCall getMoveLearnMethod( + @Path("id") @NonNull Integer id + ); + + @GET("move-learn-method/{name}") + PokeCall getMoveLearnMethod( + @Path("name") @NonNull String name + ); + + @GET("move-target/") + PokeCall> getMoveTargetList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("move-target/{id}") + PokeCall getMoveTarget( + @Path("id") @NonNull Integer id + ); + + @GET("move-target/{name}") + PokeCall getMoveTarget( + @Path("name") @NonNull String name + ); + + @GET("ability/") + PokeCall> getAbilityList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("ability/{id}") + PokeCall getAbility( + @Path("id") @NonNull Integer id + ); + + @GET("ability/{name}") + PokeCall getAbility( + @Path("name") @NonNull String name + ); + + @GET("characteristic/") + PokeCall> getCharacteristicList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("characteristic/{id}") + PokeCall getCharacteristic( + @Path("id") @NonNull Integer id + ); + + @GET("egg-group/") + PokeCall> getEggGroupList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("egg-group/{id}") + PokeCall getEggGroup( + @Path("id") @NonNull Integer id + ); + + @GET("egg-group/{name}") + PokeCall getEggGroup( + @Path("name") @NonNull String name + ); + + @GET("gender/") + PokeCall> getGenderList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("gender/{id}") + PokeCall getGender( + @Path("id") @NonNull Integer id + ); + + @GET("gender/{name}") + PokeCall getGender( + @Path("name") @NonNull String name + ); + + @GET("growth-rate/") + PokeCall> getGrowthRateList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("growth-rate/{id}") + PokeCall getGrowthRate( + @Path("id") @NonNull Integer id + ); + + @GET("growth-rate/{name}") + PokeCall getGrowthRate( + @Path("name") @NonNull String name + ); + + @GET("nature/") + PokeCall> getNatureList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("nature/{id}") + PokeCall getNature( + @Path("id") @NonNull Integer id + ); + + @GET("nature/{name}") + PokeCall getNature( + @Path("name") @NonNull String name + ); + + @GET("pokeathlon-stat/") + PokeCall> getPokeathlonStatList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("pokeathlon-stat/{id}") + PokeCall getPokeathlonStat( + @Path("id") @NonNull Integer id + ); + + @GET("pokeathlon-stat/{name}") + PokeCall getPokeathlonStat( + @Path("name") @NonNull String name + ); + + @GET("pokemon/") + PokeCall> getPokemonList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("pokemon/{id}") + PokeCall getPokemon( + @Path("id") @NonNull Integer id + ); + + @GET("pokemon/{name}") + PokeCall getPokemon( + @Path("name") @NonNull String name + ); + + @GET("pokemon-color/") + PokeCall> getPokemonColorList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("pokemon-color/{id}") + PokeCall getPokemonColor( + @Path("id") @NonNull Integer id + ); + + @GET("pokemon-color/{name}") + PokeCall getPokemonColor( + @Path("name") @NonNull String name + ); + + @GET("pokemon-form/") + PokeCall> getPokemonFormList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("pokemon-form/{id}") + PokeCall getPokemonForm( + @Path("id") @NonNull Integer id + ); + + @GET("pokemon-form/{name}") + PokeCall getPokemonForm( + @Path("name") @NonNull String name + ); + + @GET("pokemon-habitat/") + PokeCall> getPokemonHabitatList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("pokemon-habitat/{id}") + PokeCall getPokemonHabitat( + @Path("id") @NonNull Integer id + ); + + @GET("pokemon-habitat/{name}") + PokeCall getPokemonHabitat( + @Path("name") @NonNull String name + ); + + @GET("pokemon-shape/") + PokeCall> getPokemonShapeList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("pokemon-shape/{id}") + PokeCall getPokemonShape( + @Path("id") @NonNull Integer id + ); + + @GET("pokemon-shape/{name}") + PokeCall getPokemonShape( + @Path("name") @NonNull String name + ); + + @GET("pokemon-species/") + PokeCall> getPokemonSpeciesList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("pokemon-species/{id}") + PokeCall getPokemonSpecies( + @Path("id") @NonNull Integer id + ); + + @GET("pokemon-species/{name}") + PokeCall getPokemonSpecies( + @Path("name") @NonNull String name + ); + + @GET("stat/") + PokeCall> getStatList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("stat/{id}") + PokeCall getStat( + @Path("id") @NonNull Integer id + ); + + @GET("stat/{name}") + PokeCall getStat( + @Path("name") @NonNull String name + ); + + @GET("type/") + PokeCall> getTypeList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("type/{id}") + PokeCall getType( + @Path("id") @NonNull Integer id + ); + + @GET("type/{name}") + PokeCall getType( + @Path("name") @NonNull String name + ); + + @GET("language/") + PokeCall> getLanguageList( + @Query("limit") @NonNull Integer limit, + @Query("offset") @NonNull Integer offset + ); + + @GET("language/{id}") + PokeCall getLanguage( + @Path("id") @NonNull Integer id + ); + + @GET("language/{name}") + PokeCall getLanguage( + @Path("name") @NonNull String name + ); } diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCall.java b/app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCall.java new file mode 100644 index 0000000..fc6fddd --- /dev/null +++ b/app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCall.java @@ -0,0 +1,28 @@ +package com.xeroxparc.pokedex.data.remote.call; + +import java.io.IOException; + +import retrofit2.Call; +import retrofit2.HttpException; +import retrofit2.Response; + +/** + * @author Fabio Buracchi + */ +public class PokeCall { + Call call; + + public PokeCall(Call call) { + this.call = call; + } + + public T result() throws IOException { + Response response = call.execute(); + if (response.isSuccessful()) { + return response.body(); + } else { + throw new HttpException(response); + } + } + +} diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCallAdapter.java b/app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCallAdapter.java new file mode 100644 index 0000000..8e858ba --- /dev/null +++ b/app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCallAdapter.java @@ -0,0 +1,32 @@ +package com.xeroxparc.pokedex.data.remote.call; + +import androidx.annotation.NonNull; + +import java.lang.reflect.Type; + +import retrofit2.Call; +import retrofit2.CallAdapter; + +/** + * @author Fabio Buracchi + */ +public class PokeCallAdapter implements CallAdapter> { + + private Type responseType; + + public PokeCallAdapter(Type responseType) { + this.responseType = responseType; + } + + @NonNull + @Override + public Type responseType() { + return responseType; + } + + @NonNull + @Override + public PokeCall adapt(@NonNull Call call) { + return new PokeCall<>(call); + } +} diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCallAdapterFactory.java b/app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCallAdapterFactory.java new file mode 100644 index 0000000..44af21f --- /dev/null +++ b/app/src/main/java/com/xeroxparc/pokedex/data/remote/call/PokeCallAdapterFactory.java @@ -0,0 +1,33 @@ +package com.xeroxparc.pokedex.data.remote.call; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.lang.annotation.Annotation; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +import retrofit2.CallAdapter; +import retrofit2.Retrofit; + +/** + * @author Fabio Buracchi + */ +public class PokeCallAdapterFactory extends CallAdapter.Factory{ + + @Nullable + @Override + public CallAdapter get( + @NonNull Type returnType, + @NonNull Annotation[] annotations, + @NonNull Retrofit retrofit + ) { + if (getRawType(returnType) != PokeCall.class) { + return null; + } + return new PokeCallAdapter<>( + getParameterUpperBound(0, (ParameterizedType) returnType) + ); + } + +} diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/remote/ResourceTypeAdapterFactory.java b/app/src/main/java/com/xeroxparc/pokedex/data/remote/type/PokeTypeAdapterFactory.java similarity index 71% rename from app/src/main/java/com/xeroxparc/pokedex/data/remote/ResourceTypeAdapterFactory.java rename to app/src/main/java/com/xeroxparc/pokedex/data/remote/type/PokeTypeAdapterFactory.java index 3fc3109..6ef970a 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/remote/ResourceTypeAdapterFactory.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/remote/type/PokeTypeAdapterFactory.java @@ -1,4 +1,4 @@ -package com.xeroxparc.pokedex.data.remote; +package com.xeroxparc.pokedex.data.remote.type; import androidx.annotation.NonNull; @@ -16,13 +16,14 @@ import java.util.Map; import static com.xeroxparc.pokedex.utils.Utils.urlToId; +import static com.xeroxparc.pokedex.utils.Utils.urlToPageNumber; /** * Custom TypeAdapterFactory to correctly handle JSON results * * @author Fabio Buracchi */ -public class ResourceTypeAdapterFactory implements TypeAdapterFactory { +public class PokeTypeAdapterFactory implements TypeAdapterFactory { @Override public TypeAdapter create(@NonNull Gson gson, @NonNull TypeToken type) { final TypeAdapter delegateAdapter = gson.getDelegateAdapter(this, type); @@ -46,11 +47,22 @@ private JsonElement afterRead(@NonNull JsonElement jsonElement) { JsonObject jsonObject = ((JsonObject) jsonElement); for (Map.Entry entry : jsonObject.entrySet()) { if (entry.getValue() instanceof JsonPrimitive) { + String url; switch (entry.getKey()) { - case "count": - return jsonObject.getAsJsonArray("results"); + case "next": + url = jsonObject.get(entry.getKey()).toString() + .replace("\"", ""); + jsonObject.addProperty("next", urlToPageNumber(url)); + afterRead(entry.getValue()); + break; + case "previous": + url = jsonObject.get(entry.getKey()).toString() + .replace("\"", ""); + jsonObject.addProperty("previous", urlToPageNumber(url)); + afterRead(entry.getValue()); + break; case "url": - String url = jsonObject.get(entry.getKey()).toString() + url = jsonObject.get(entry.getKey()).toString() .replace("\"", ""); jsonObject.addProperty("id", urlToId(url)); jsonObject.remove("url"); diff --git a/app/src/main/java/com/xeroxparc/pokedex/data/repository/LanguageRepository.java b/app/src/main/java/com/xeroxparc/pokedex/data/repository/LanguageRepository.java index e1d82ac..4934371 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/data/repository/LanguageRepository.java +++ b/app/src/main/java/com/xeroxparc/pokedex/data/repository/LanguageRepository.java @@ -9,11 +9,13 @@ import com.xeroxparc.pokedex.data.database.PokeDatabase; import com.xeroxparc.pokedex.data.database.dao.utility.LanguageDao; import com.xeroxparc.pokedex.data.model.utility.language.Language; -import com.xeroxparc.pokedex.data.remote.ApiError; +import com.xeroxparc.pokedex.data.remote.PokeApiFactory; import com.xeroxparc.pokedex.data.remote.PokeApiService; import java.io.IOException; +import retrofit2.HttpException; + /** * Repository class. * Encapsulate the logic required to access data sources. @@ -22,12 +24,13 @@ */ public class LanguageRepository { - PokeApiService service; - private LanguageDao languageDao; + private final PokeApiService service; + private final PokeDatabase database; + private final LanguageDao languageDao; public LanguageRepository(Application application) { - service = new PokeApiService(); - PokeDatabase database = PokeDatabase.getDatabase(application); + service = PokeApiFactory.getService(); + database = PokeDatabase.getDatabase(application); languageDao = database.languageDao(); } @@ -36,8 +39,8 @@ public LiveData getLanguage(int id, boolean forceRefresh) { AsyncTask.execute(() -> { if (forceRefresh || languageDao.getLanguage(id) == null) { try { - languageDao.insert(service.getLanguage(id)); - } catch (IOException | ApiError e) { + languageDao.insert(service.getLanguage(id).result()); + } catch (IOException | HttpException e) { e.printStackTrace(); } } @@ -45,4 +48,5 @@ public LiveData getLanguage(int id, boolean forceRefresh) { }); return language; } + } diff --git a/app/src/main/java/com/xeroxparc/pokedex/utils/Utils.java b/app/src/main/java/com/xeroxparc/pokedex/utils/Utils.java index fb54a1b..b74f560 100644 --- a/app/src/main/java/com/xeroxparc/pokedex/utils/Utils.java +++ b/app/src/main/java/com/xeroxparc/pokedex/utils/Utils.java @@ -24,4 +24,12 @@ public static Integer urlToId(@NonNull String url) { null; } + @Nullable + public static Integer urlToPageNumber(@NonNull String url) { + Matcher matcher = Pattern.compile("(?<=offset=)[0-9]+").matcher(url); + return matcher.find() ? + Integer.valueOf(matcher.group()) : + null; + } + }