From 64b2db7bd834f8c40014b2be919571e8114bf31d Mon Sep 17 00:00:00 2001 From: tdemeneghi Date: Wed, 1 May 2019 01:47:23 +0200 Subject: [PATCH] CollectionView Challenge - dog list implementation --- .../CollectionViewChallenge.Android.csproj | 6 +++ .../MainActivity.cs | 1 + .../CollectionViewChallenge.iOS.csproj | 6 +++ .../CollectionViewChallenge/AppShell.xaml | 2 +- .../CollectionViewChallenge.csproj | 7 +-- .../CollectionViewChallenge/Models/Dog.cs | 46 +++++++++++++++++ .../Services/DogApiService.cs | 36 +++++++++++++ .../ViewModels/DogViewModel.cs | 49 ++++++++++++++++++ .../Views/CollectionViewChallengePage.xaml | 51 +++++++++++++------ .../Views/CollectionViewChallengePage.xaml.cs | 13 +++++ 10 files changed, 195 insertions(+), 22 deletions(-) create mode 100644 CollectionViewChallenge/CollectionViewChallenge/Models/Dog.cs create mode 100644 CollectionViewChallenge/CollectionViewChallenge/Services/DogApiService.cs create mode 100644 CollectionViewChallenge/CollectionViewChallenge/ViewModels/DogViewModel.cs diff --git a/CollectionViewChallenge/CollectionViewChallenge.Android/CollectionViewChallenge.Android.csproj b/CollectionViewChallenge/CollectionViewChallenge.Android/CollectionViewChallenge.Android.csproj index d2c42fc..ae59a29 100644 --- a/CollectionViewChallenge/CollectionViewChallenge.Android/CollectionViewChallenge.Android.csproj +++ b/CollectionViewChallenge/CollectionViewChallenge.Android/CollectionViewChallenge.Android.csproj @@ -52,6 +52,12 @@ + + 12.0.2 + + + 3.0.0.5 + diff --git a/CollectionViewChallenge/CollectionViewChallenge.Android/MainActivity.cs b/CollectionViewChallenge/CollectionViewChallenge.Android/MainActivity.cs index 72e1935..a545d6e 100644 --- a/CollectionViewChallenge/CollectionViewChallenge.Android/MainActivity.cs +++ b/CollectionViewChallenge/CollectionViewChallenge.Android/MainActivity.cs @@ -22,6 +22,7 @@ protected override void OnCreate(Bundle savedInstanceState) global::Xamarin.Forms.Forms.SetFlags("Shell_Experimental", "Visual_Experimental", "CollectionView_Experimental", "FastRenderers_Experimental"); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); + ImageCircle.Forms.Plugin.Droid.ImageCircleRenderer.Init(); LoadApplication(new App()); } public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) diff --git a/CollectionViewChallenge/CollectionViewChallenge.iOS/CollectionViewChallenge.iOS.csproj b/CollectionViewChallenge/CollectionViewChallenge.iOS/CollectionViewChallenge.iOS.csproj index 49a7105..3708d01 100644 --- a/CollectionViewChallenge/CollectionViewChallenge.iOS/CollectionViewChallenge.iOS.csproj +++ b/CollectionViewChallenge/CollectionViewChallenge.iOS/CollectionViewChallenge.iOS.csproj @@ -133,6 +133,12 @@ + + 12.0.2 + + + 3.0.0.5 + diff --git a/CollectionViewChallenge/CollectionViewChallenge/AppShell.xaml b/CollectionViewChallenge/CollectionViewChallenge/AppShell.xaml index 9364c60..65fcaa3 100644 --- a/CollectionViewChallenge/CollectionViewChallenge/AppShell.xaml +++ b/CollectionViewChallenge/CollectionViewChallenge/AppShell.xaml @@ -72,7 +72,7 @@ - + diff --git a/CollectionViewChallenge/CollectionViewChallenge/CollectionViewChallenge.csproj b/CollectionViewChallenge/CollectionViewChallenge/CollectionViewChallenge.csproj index 3ccfc60..3c5c95c 100644 --- a/CollectionViewChallenge/CollectionViewChallenge/CollectionViewChallenge.csproj +++ b/CollectionViewChallenge/CollectionViewChallenge/CollectionViewChallenge.csproj @@ -11,6 +11,8 @@ + + @@ -20,9 +22,4 @@ MSBuild:UpdateDesignTimeXaml - - - - - \ No newline at end of file diff --git a/CollectionViewChallenge/CollectionViewChallenge/Models/Dog.cs b/CollectionViewChallenge/CollectionViewChallenge/Models/Dog.cs new file mode 100644 index 0000000..7070add --- /dev/null +++ b/CollectionViewChallenge/CollectionViewChallenge/Models/Dog.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; + +namespace CollectionViewChallenge.Models +{ + public class Dog + { + public Breed[] breeds { get; set; } + public string id { get; set; } + public string url { get; set; } + public int width { get; set; } + public int height { get; set; } + + public string breed => string.Join(", ", breeds.Select(x => x.name)); + public string temperament => string.Join(", ", breeds.Select(x => x.temperament)); + } + + public class Breed + { + public Weight weight { get; set; } + public Height height { get; set; } + public int id { get; set; } + public string name { get; set; } + public string bred_for { get; set; } + public string breed_group { get; set; } + public string life_span { get; set; } + public string temperament { get; set; } + public string origin { get; set; } + public string country_code { get; set; } + } + + public class Weight + { + public string imperial { get; set; } + public string metric { get; set; } + } + + public class Height + { + public string imperial { get; set; } + public string metric { get; set; } + } + +} diff --git a/CollectionViewChallenge/CollectionViewChallenge/Services/DogApiService.cs b/CollectionViewChallenge/CollectionViewChallenge/Services/DogApiService.cs new file mode 100644 index 0000000..eb952ff --- /dev/null +++ b/CollectionViewChallenge/CollectionViewChallenge/Services/DogApiService.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.Net.Http.Headers; +using CollectionViewChallenge.Models; + +namespace CollectionViewChallenge.Services +{ + public static class DogApiService + { + private static HttpClient CreateHttpClient() + { + var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + return httpClient; + } + + private static readonly HttpClient client = CreateHttpClient(); + + public static async Task> GetDogs() + { + var response = await client.GetAsync("https://api.thedogapi.com/v1/images/search?limit=50&page=1&api_key=5cb9aa6b-d6ec-4b05-95d2-5c6434b5e2b8"); + + if (response.IsSuccessStatusCode) + { + var json = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject>(json); + } + + return null; + } + } +} diff --git a/CollectionViewChallenge/CollectionViewChallenge/ViewModels/DogViewModel.cs b/CollectionViewChallenge/CollectionViewChallenge/ViewModels/DogViewModel.cs new file mode 100644 index 0000000..6603351 --- /dev/null +++ b/CollectionViewChallenge/CollectionViewChallenge/ViewModels/DogViewModel.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Threading.Tasks; +using CollectionViewChallenge.Models; +using CollectionViewChallenge.Services; +using System.Runtime.CompilerServices; + +namespace CollectionViewChallenge.ViewModels +{ + public class DogViewModel : INotifyPropertyChanged + { + public event PropertyChangedEventHandler PropertyChanged; + + void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + private List dogList; + + private ObservableCollection dogCollection; + + public ObservableCollection DogCollection + { + get { return dogCollection; } + set { dogCollection = value; OnPropertyChanged(); } + } + + private bool isBusy; + + public bool IsBusy + { + get { return isBusy; } + set { isBusy = value; OnPropertyChanged(); } + } + + public async Task LoadData() + { + IsBusy = true; + dogList = await DogApiService.GetDogs(); + DogCollection = new ObservableCollection(dogList); + IsBusy = false; + + } + } +} diff --git a/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml b/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml index f2da7f7..c690867 100644 --- a/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml +++ b/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml @@ -3,27 +3,46 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:controls="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin" mc:Ignorable="d" x:Class="CollectionViewChallenge.Views.CollectionViewChallengePage"> - - - - - - 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? - - + + + + + + + + - - + + + + + + + + + + + + + + + diff --git a/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml.cs b/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml.cs index 701124f..cc2c663 100644 --- a/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml.cs +++ b/CollectionViewChallenge/CollectionViewChallenge/Views/CollectionViewChallengePage.xaml.cs @@ -7,14 +7,27 @@ using Xamarin.Forms; using Xamarin.Forms.Xaml; +using CollectionViewChallenge.ViewModels; + namespace CollectionViewChallenge.Views { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class CollectionViewChallengePage : ContentPage { + DogViewModel vm; + public CollectionViewChallengePage() { InitializeComponent(); + + vm = new DogViewModel(); + BindingContext = vm; + } + + protected async override void OnAppearing() + { + base.OnAppearing(); + await vm.LoadData(); } } } \ No newline at end of file