async void searchButton_Clicked(object sender, EventArgs e) { if (nameEntry.Text != null) { //getting input and constructing request url string userInput; userInput = nameEntry.Text.ToLower(); //seting up URI and creatting HTTP client and response holder string nameApiEndpoint = globals.GlobalVariables.searchByNameString + userInput; Uri nameApiUri = new Uri(nameApiEndpoint); HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(nameApiUri); //response stream into string string jsonContent = await response.Content.ReadAsStringAsync(); if (jsonContent != globals.GlobalVariables.notFoundString) { //parse into an object of type PokemonJson var pokemon = PokemonJson.FromJson(jsonContent); //sending the object to the next page await Navigation.PushAsync(new PokemonPage(pokemon)); } else //pop up message if the pokemon doesn't exist { await DisplayAlert("NOT FOUND", "Please try again!", "OK"); } } else { await DisplayAlert("EMPTY FIELD", "Please enter a pokemon!", "OK"); } }
/// <summary> /// This method will match each attribute form PokemonJson to Pokemon model /// </summary> /// <param name="jPokemon"> generated PokemonJson model </param> /// <returns> /// A SimplifiedPokemon or null of no data form in input model /// </returns> private Pokemon MapModels(PokemonJson jPokemon) { // if input is null if (jPokemon == null) { return(null); } var pokemon = new Pokemon { Id = jPokemon.Id, Name = jPokemon.Name, Weight = jPokemon.Height, Height = jPokemon.Height, Order = jPokemon.Order, BaseExperience = jPokemon.BaseExperience, Image = jPokemon.Sprites.FrontDefault }; // call MapStats method to map stat data MapStats(pokemon, jPokemon); // call MapTypes method ot map Type data pokemon.Types = MapTypes(jPokemon); // call MapAbilities method to map Ability data pokemon.Abilities = MapAbility(jPokemon); return(pokemon); }
/// <summary> /// This helper method will match get required stat attribute form PokemonJson to SimplifiedPokemon /// </summary> /// <param name="pokemon">input Pokemon object </param> /// <param name="jPokemon">input helper pokemon object </param> private static void MapStats(Pokemon pokemon, PokemonJson jPokemon) { foreach (var stat in jPokemon.Stats) { switch (stat.StatStat.Name) { // the switch status conditions are from Pokemon Api documentation // from url https://pokeapi.co/docs/v2.html#stats case "speed": pokemon.StatSpeed = stat.BaseStat; break; case "special-defense": pokemon.StatSpecialDefence = stat.BaseStat; break; case "special-attack": pokemon.StatSpecialAttack = stat.BaseStat; break; case "defense": pokemon.StatDefence = stat.BaseStat; break; case "attack": pokemon.StatAttack = stat.BaseStat; break; case "hp": pokemon.StatHp = stat.BaseStat; break; } } }
public void SetPokemon(int id) { var tempPokemonJson = new PokemonJson(); var uri = new Uri("https://pokeapi.co/api/v2/pokemon/" + id + "/"); tempPokemonJson = (PokemonJson)MapToObject.MapJsonToModel(uri, tempPokemonJson); _pokemonModel = MapModels(tempPokemonJson); }
/// <summary> /// This helper function will get Ability data of helper model jPokemon /// </summary> /// <param name="jPokemon">generated PokemonJson model</param> /// <returns> /// Ability data dictionary /// </returns> private Dictionary <string, string> MapAbility(PokemonJson jPokemon) { var tempAbilityDictionary = new Dictionary <string, string>(); // loop-through Ability model foreach (var ability in jPokemon.Abilities) { // get ability uri for Api endpoint var abilityUri = ability.AbilityAbility.Url; // get ability data from ability uri my calling GetAbilityTuple helper method tempAbilityDictionary.Add(GetAbilityTuple(abilityUri).Item1, GetAbilityTuple(abilityUri).Item2); } return(tempAbilityDictionary); }
async void openPokemonPage(string nameOrNum) { // sets up URI and creates HTTP client and response holder. string nameApiEndpoint = globals.GlobalVariables.searchByNameString + nameOrNum; Uri nameApiUri = new Uri(nameApiEndpoint); HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(nameApiUri); //response stream into string string jsonContent = await response.Content.ReadAsStringAsync(); if (jsonContent != globals.GlobalVariables.notFoundString) { //parses into an object of type PokemonJson var pokemon = PokemonJson.FromJson(jsonContent); globals.GlobalVariables.searchHistory.Add(pokemon); //sends the object to the next page. await Navigation.PushAsync(new PokemonPage(pokemon, globals.GlobalVariables.endpoint)); } }
/// <summary> /// This helper method will get type data from helper pokemon model /// then create a dictionary according to it. /// The dictionary contains Type name and color code for the type /// The color codes are from the web reference https://bulbapedia.bulbagarden.net/wiki/Type /// </summary> /// <param name="jPokemon"> input helper pokemon object </param> /// <returns> /// A dictionary with (String:Type name, Hexadecimal:Type color code) /// </returns> private Dictionary <string, string> MapTypes(PokemonJson jPokemon) { var tempTypes = new Dictionary <string, string>(); foreach (var types in jPokemon.Types) { switch (types.Type.Name) { case "normal": tempTypes.Add("Normal", "#A8A878"); break; case "fire": tempTypes.Add("Fire", "#f08030"); break; case "fighting": tempTypes.Add("Fighting", "#c03028"); break; case "water": tempTypes.Add("Water", "#6890f0"); break; case "flying": tempTypes.Add("Flying", "#a890f0"); break; case "grass": tempTypes.Add("Grass", "#78c850"); break; case "poison": tempTypes.Add("Poison", "#a040a0"); break; case "electric": tempTypes.Add("Electric", "#f8d030"); break; case "ground": tempTypes.Add("Ground", "#e0c068"); break; case "psychic": tempTypes.Add("Psychic", "#f85888"); break; case "rock": tempTypes.Add("Rock", "#b8a038"); break; case "ice": tempTypes.Add("Ice", "#98d8d8"); break; case "bug": tempTypes.Add("Bug", "#a8b820"); break; case "dragon": tempTypes.Add("Dragon", "#7038f8"); break; case "ghost": tempTypes.Add("Ghost", "#705898"); break; case "dark": tempTypes.Add("Dark", "#705848"); break; case "steel": tempTypes.Add("Steel", "#b8b8d0"); break; case "fairy": tempTypes.Add("Fairy", "#ee99ac"); break; default: tempTypes.Add("Unspecified", "#68a090"); break; } } return(tempTypes); }
public PokemonPage(PokemonJson pokemonToUse, string endpoint) { InitializeComponent(); theEndpoint = endpoint; //sets the favorite button. Depending on whether the pokemon is on the team already or not. for (int i = 0; i < globals.GlobalVariables.favoritesList.Count; i++) { if (pokemonToUse.Name == globals.GlobalVariables.favoritesList[i].Name) { isFavorite = true; break; //loop is no longer needed at this point } else { isFavorite = false; } } if (isFavorite == false) { isFavorite_Button.Source = "https://cdn2.iconfinder.com/data/icons/picons-basic-1/57/basic1-132_star_off-512.png"; } else { isFavorite_Button.Source = "https://cdn3.iconfinder.com/data/icons/basicolor-votting-awards/24/198_star_favorite_vote_achievement-512.png"; } isFavorite = false; // handle if the pokemon only has a single type. ( duplicating it so index is not null in type check below for setting background colors ) if (pokemonToUse.Types.Count == 1) { pokemonToUse.Types.Add(pokemonToUse.Types[0]); } //Capitalize the first letter of the pokemon's name string UppercaseFirst(string s) { // Checking for an empty string. if (string.IsNullOrEmpty(s)) { return(string.Empty); } //Return char and concatinate substring. return(char.ToUpper(s[0]) + s.Substring(1)); } //sets pokemon's name pokemonName_Label.Text = UppercaseFirst(pokemonToUse.Name); //sets pokemon's pictures pokemon_pic.Source = pokemonToUse.Sprites.FrontDefault; //sets pokemon's base experience baseExp_Label.Text = pokemonToUse.BaseExperience.ToString(); //sets pokemon's types type1_Label.Text = UppercaseFirst(pokemonToUse.Types[1].Type.Name); type2_Label.Text = UppercaseFirst(pokemonToUse.Types[0].Type.Name); //sets pokemon's weight and height weight_Label.Text = UppercaseFirst(pokemonToUse.Weight.ToString()); height_Label.Text = UppercaseFirst(pokemonToUse.Height.ToString()); //sets and populating the moves list: ############################################# movesListView.ItemsSource = movesList; for (int i = 0; i < pokemonToUse.Moves.Count; i++) { movesList.Add(UppercaseFirst(pokemonToUse.Moves[i].MoveMove.Name)); } //#################################################################################### //sets the games list: ############################################################ gamesListView.ItemsSource = gamesList; for (int i = 0; i < pokemonToUse.GameIndices.Count; i++) { gamesList.Add(UppercaseFirst(pokemonToUse.GameIndices[i].Version.Name)); } //#################################################################################### //checks pokemon types to set background color. if (pokemonToUse.Types[0].Type.Name == "grass" || pokemonToUse.Types[1].Type.Name == "grass") { this.BackgroundColor = Color.FromHex("#ccffcc"); } else if (pokemonToUse.Types[0].Type.Name == "fire" || pokemonToUse.Types[1].Type.Name == "fire") { this.BackgroundColor = Color.FromHex("#ffad99"); } else if (pokemonToUse.Types[0].Type.Name == "water" || pokemonToUse.Types[1].Type.Name == "water") { this.BackgroundColor = Color.FromHex("#b3f0ff"); } else if (pokemonToUse.Types[0].Type.Name == "electric" || pokemonToUse.Types[1].Type.Name == "electric") { this.BackgroundColor = Color.FromHex("#ffff99"); } else if (pokemonToUse.Types[0].Type.Name == "fairy" || pokemonToUse.Types[1].Type.Name == "fairy") { this.BackgroundColor = Color.FromHex("#ffe6ff"); } else if (pokemonToUse.Types[0].Type.Name == "fighting" || pokemonToUse.Types[1].Type.Name == "fighting") { this.BackgroundColor = Color.FromHex("#e6ccb3"); } else { this.BackgroundColor = Color.Silver; } pokemonToTeam = pokemonToUse; }