Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.2</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="4.0.0.346134-pre9" />
<PackageReference Include="Xamarin.Android.Support.ViewPager" Version="28.0.0.1" />
<PackageReference Include="Xamarin.Android.Support.Design" Version="28.0.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@
<Reference Include="System.Numerics.Vectors" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.2</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="4.0.0.346134-pre9" />
<PackageReference Include="Xamarin.Essentials" Version="1.1.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<ShellItem>
<ShellSection Title="CollectionView" Icon="tab_feed.png">
<ShellContent Title="CollectionView">
<local:CollectionViewChallengePage Title="CollectionView"/>
<local:CollectionViewChallengePage Title="Pokédex"/>
</ShellContent>
</ShellSection>
</ShellItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CollectionViewChallenge.Class
{
public static class Constants
{
public const double factor = 100 / 255.0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Xamarin.Forms" Version="4.0.0.346134-pre9" />
<PackageReference Include="Xamarin.Essentials" Version="1.1.0" />
</ItemGroup>
Expand All @@ -20,9 +21,4 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Folder Include="Models\" />
<Folder Include="ViewModels\" />
</ItemGroup>
</Project>
56 changes: 56 additions & 0 deletions CollectionViewChallenge/CollectionViewChallenge/Models/Pokemon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using CollectionViewChallenge.Class;

namespace CollectionViewChallenge.Models
{
public class Pokemon
{
public int id { get; set; }
public Name name { get; set; }
public List<string> type { get; set; }

[JsonProperty("base")]
public Base _base { get; set; }

public string ImageURL
{
get
{
var num = id.ToString().PadLeft(3, '0');
return $"https://raw.githubusercontent.com/fanzeyi/pokemon.json/master/images/{num}{name.english}.png";
}
}

public string Types => string.Join(", ", type);

public double HP => _base.HP * Constants.factor;
public double Attack => _base.Attack * Constants.factor;
public double Defense => _base.Defense * Constants.factor;
public double SpAttack => _base.SpAttack * Constants.factor;
public double SpDefense => _base.SpDefense * Constants.factor;
public double Speed => _base.Speed * Constants.factor;
}

public class Name
{
public string english { get; set; }
public string japanese { get; set; }
public string chinese { get; set; }
}

public class Base
{
public int HP { get; set; }
public int Attack { get; set; }
public int Defense { get; set; }

[JsonProperty("Sp. Attack")]
public int SpAttack { get; set; }

[JsonProperty("Sp. Defense")]
public int SpDefense { get; set; }

public int Speed { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using System.Threading.Tasks;
using CollectionViewChallenge.Models;
using Newtonsoft.Json;

namespace CollectionViewChallenge.Services
{
public static class PokemonApiService
{
private static readonly HttpClient client = CrearHttpClient();

private static HttpClient CrearHttpClient()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
return httpClient;
}

public async static Task<List<Pokemon>> GetPokemon()
{
var response = await client.GetAsync("https://raw.githubusercontent.com/fanzeyi/pokemon.json/master/pokedex.json");

if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
var list = JsonConvert.DeserializeObject<List<Pokemon>>(json);
return list;
}

return default(List<Pokemon>);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Input;
using CollectionViewChallenge.Models;
using CollectionViewChallenge.Services;
using Xamarin.Forms;

namespace CollectionViewChallenge.ViewModels
{
public class PokemonViewModel : INotifyPropertyChanged
{
private List<Pokemon> Pokedex;

private ObservableCollection<Pokemon> pokemons;

public ObservableCollection<Pokemon> PokemonList
{
get { return pokemons; }
set { pokemons = value; OnPropertyChanged(); }
}

public async Task LoadPokedex()
{
IsLoading = true;

Pokedex = await PokemonApiService.GetPokemon();
PokemonList = new ObservableCollection<Pokemon>(Pokedex);

IsLoading = false;
}

private bool isLoading;

public bool IsLoading
{
get { return isLoading; }
set { isLoading = value; OnPropertyChanged(); }
}

public event PropertyChangedEventHandler PropertyChanged;

void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

// New code
private ObservableCollection<Pokemon> icePokemonList;

public ObservableCollection<Pokemon> IcePokemonList
{
get { return icePokemonList; }
set { icePokemonList = value; OnPropertyChanged(); }
}

private ObservableCollection<Pokemon> darkPokemonList;

public ObservableCollection<Pokemon> DarkPokemonList
{
get { return darkPokemonList; }
set { darkPokemonList = value; OnPropertyChanged(); }
}

public async Task LoadIceDarkPokedex()
{
IsLoading = true;

Pokedex = await PokemonApiService.GetPokemon();
//PokemonList = new ObservableCollection<Pokemon>(Pokedex);

IcePokemonList = new ObservableCollection<Pokemon>(Pokedex.Where(x => x.Types.Contains("Ice")));
DarkPokemonList = new ObservableCollection<Pokemon>(Pokedex.Where(x => x.Types.Contains("Dark")));

IsLoading = false;
}

public ICommand SearchPokemonCommand => new Command<string>(SearchPokemon);




public Pokemon SearchScrollPokemon(string pokemon)
{
if (!string.IsNullOrWhiteSpace(pokemon))
{
var results = Pokedex.Where(x => x.name.english.ToLower().Contains(pokemon.ToLower()));
return results.FirstOrDefault();
}

return PokemonList.First();
}

public void SearchPokemon(string pokemon)
{
if (!string.IsNullOrWhiteSpace(pokemon))
{
var results = Pokedex.Where(x => x.name.english.ToLower().Contains(pokemon.ToLower()));

PokemonList = (results.Count() > 0)
? new ObservableCollection<Pokemon>(results)
: new ObservableCollection<Pokemon>();
}
else
{
PokemonList = new ObservableCollection<Pokemon>(Pokedex);
}
}
}
}
Loading