From f34896ffc42854ade429620512f7f795df4e5a71 Mon Sep 17 00:00:00 2001 From: Luis Beltran Date: Mon, 29 Apr 2019 15:08:11 +0200 Subject: [PATCH] =?UTF-8?q?CollectionViewChallenge=20-=20Pok=C3=A9dex=20by?= =?UTF-8?q?=20Luis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionViewChallenge.Android.csproj | 3 + .../CollectionViewChallenge.iOS.csproj | 3 + .../CollectionViewChallenge/AppShell.xaml | 2 +- .../Class/Constants.cs | 7 + .../CollectionViewChallenge.csproj | 6 +- .../CollectionViewChallenge/Models/Pokemon.cs | 56 +++++ .../Services/PokemonApiService.cs | 37 +++ .../ViewModels/PokemonViewModel.cs | 115 +++++++++ .../Views/CollectionViewChallengePage.xaml | 228 ++++++++++++++++-- .../Views/CollectionViewChallengePage.xaml.cs | 18 ++ 10 files changed, 447 insertions(+), 28 deletions(-) create mode 100644 CollectionViewChallenge/CollectionViewChallenge/Class/Constants.cs create mode 100644 CollectionViewChallenge/CollectionViewChallenge/Models/Pokemon.cs create mode 100644 CollectionViewChallenge/CollectionViewChallenge/Services/PokemonApiService.cs create mode 100644 CollectionViewChallenge/CollectionViewChallenge/ViewModels/PokemonViewModel.cs diff --git a/CollectionViewChallenge/CollectionViewChallenge.Android/CollectionViewChallenge.Android.csproj b/CollectionViewChallenge/CollectionViewChallenge.Android/CollectionViewChallenge.Android.csproj index d2c42fc..75bcff4 100644 --- a/CollectionViewChallenge/CollectionViewChallenge.Android/CollectionViewChallenge.Android.csproj +++ b/CollectionViewChallenge/CollectionViewChallenge.Android/CollectionViewChallenge.Android.csproj @@ -52,6 +52,9 @@ + + 12.0.2 + diff --git a/CollectionViewChallenge/CollectionViewChallenge.iOS/CollectionViewChallenge.iOS.csproj b/CollectionViewChallenge/CollectionViewChallenge.iOS/CollectionViewChallenge.iOS.csproj index 49a7105..d7c92c3 100644 --- a/CollectionViewChallenge/CollectionViewChallenge.iOS/CollectionViewChallenge.iOS.csproj +++ b/CollectionViewChallenge/CollectionViewChallenge.iOS/CollectionViewChallenge.iOS.csproj @@ -133,6 +133,9 @@ + + 12.0.2 + diff --git a/CollectionViewChallenge/CollectionViewChallenge/AppShell.xaml b/CollectionViewChallenge/CollectionViewChallenge/AppShell.xaml index 9364c60..147c249 100644 --- a/CollectionViewChallenge/CollectionViewChallenge/AppShell.xaml +++ b/CollectionViewChallenge/CollectionViewChallenge/AppShell.xaml @@ -72,7 +72,7 @@ - + diff --git a/CollectionViewChallenge/CollectionViewChallenge/Class/Constants.cs b/CollectionViewChallenge/CollectionViewChallenge/Class/Constants.cs new file mode 100644 index 0000000..1d64800 --- /dev/null +++ b/CollectionViewChallenge/CollectionViewChallenge/Class/Constants.cs @@ -0,0 +1,7 @@ +namespace CollectionViewChallenge.Class +{ + public static class Constants + { + public const double factor = 100 / 255.0; + } +} diff --git a/CollectionViewChallenge/CollectionViewChallenge/CollectionViewChallenge.csproj b/CollectionViewChallenge/CollectionViewChallenge/CollectionViewChallenge.csproj index 3ccfc60..3077535 100644 --- a/CollectionViewChallenge/CollectionViewChallenge/CollectionViewChallenge.csproj +++ b/CollectionViewChallenge/CollectionViewChallenge/CollectionViewChallenge.csproj @@ -11,6 +11,7 @@ + @@ -20,9 +21,4 @@ MSBuild:UpdateDesignTimeXaml - - - - - \ No newline at end of file diff --git a/CollectionViewChallenge/CollectionViewChallenge/Models/Pokemon.cs b/CollectionViewChallenge/CollectionViewChallenge/Models/Pokemon.cs new file mode 100644 index 0000000..cd5de70 --- /dev/null +++ b/CollectionViewChallenge/CollectionViewChallenge/Models/Pokemon.cs @@ -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 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; } + } +} diff --git a/CollectionViewChallenge/CollectionViewChallenge/Services/PokemonApiService.cs b/CollectionViewChallenge/CollectionViewChallenge/Services/PokemonApiService.cs new file mode 100644 index 0000000..4a9de53 --- /dev/null +++ b/CollectionViewChallenge/CollectionViewChallenge/Services/PokemonApiService.cs @@ -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> 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>(json); + return list; + } + + return default(List); + } + } +} diff --git a/CollectionViewChallenge/CollectionViewChallenge/ViewModels/PokemonViewModel.cs b/CollectionViewChallenge/CollectionViewChallenge/ViewModels/PokemonViewModel.cs new file mode 100644 index 0000000..3774975 --- /dev/null +++ b/CollectionViewChallenge/CollectionViewChallenge/ViewModels/PokemonViewModel.cs @@ -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 Pokedex; + + private ObservableCollection pokemons; + + public ObservableCollection PokemonList + { + get { return pokemons; } + set { pokemons = value; OnPropertyChanged(); } + } + + public async Task LoadPokedex() + { + IsLoading = true; + + Pokedex = await PokemonApiService.GetPokemon(); + PokemonList = new ObservableCollection(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 icePokemonList; + + public ObservableCollection IcePokemonList + { + get { return icePokemonList; } + set { icePokemonList = value; OnPropertyChanged(); } + } + + private ObservableCollection darkPokemonList; + + public ObservableCollection DarkPokemonList + { + get { return darkPokemonList; } + set { darkPokemonList = value; OnPropertyChanged(); } + } + + public async Task LoadIceDarkPokedex() + { + IsLoading = true; + + Pokedex = await PokemonApiService.GetPokemon(); + //PokemonList = new ObservableCollection(Pokedex); + + IcePokemonList = new ObservableCollection(Pokedex.Where(x => x.Types.Contains("Ice"))); + DarkPokemonList = new ObservableCollection(Pokedex.Where(x => x.Types.Contains("Dark"))); + + IsLoading = false; + } + + public ICommand SearchPokemonCommand => new Command(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(results) + : new ObservableCollection(); + } + else + { + PokemonList = new ObservableCollection(Pokedex); + } + } + } +} \ No newline at end of file diff --git a/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml b/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml index f2da7f7..3a93115 100644 --- a/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml +++ b/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml @@ -4,29 +4,213 @@ xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" - x:Class="CollectionViewChallenge.Views.CollectionViewChallengePage"> + x:Class="CollectionViewChallenge.Views.CollectionViewChallengePage" + Title="Pokédex"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - This is a CollectionView! - Your feedback on the experience of converting a ListView to a CollectionView is incredibly appreciated. - Here are three general questions: - 1. How was the experience of converting your existing ListView to a CollectionView? - 2. How is the performance compared to the ListView? - 3. Is there a specific piece of functionality that you'd like to see? - - - - - - - - - + + + + \ No newline at end of file diff --git a/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml.cs b/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml.cs index 701124f..7e6ec53 100644 --- a/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml.cs +++ b/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml.cs @@ -7,14 +7,32 @@ using Xamarin.Forms; using Xamarin.Forms.Xaml; +using CollectionViewChallenge.ViewModels; + namespace CollectionViewChallenge.Views { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class CollectionViewChallengePage : ContentPage { + PokemonViewModel vm; + public CollectionViewChallengePage() { InitializeComponent(); + vm = new PokemonViewModel(); + BindingContext = vm; + } + + protected async override void OnAppearing() + { + base.OnAppearing(); + + await vm.LoadIceDarkPokedex(); + + await Task.Delay(5000); + + var articuno = vm.IcePokemonList.Where(x => x.name.english == "Articuno").FirstOrDefault(); + collectionViewIce.ScrollTo(articuno, position: ScrollToPosition.Center, animate: false); } } } \ No newline at end of file