Added Artist view

This commit is contained in:
amazing-username
2019-05-30 21:24:03 -04:00
parent 8e365cc07f
commit bf2371f1ef
5 changed files with 203 additions and 6 deletions
+18
View File
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
namespace Mear.Models
{
public class Artist
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("song_count")]
public int SongCount { get; set; }
}
}
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using RestSharp;
using Mear.Constants.API;
using Mear.Models;
using Mear.Repositories.Database;
namespace Mear.Repositories.Remote
{
public class RemoteArtistRepository
{
#region Fields
#endregion
#region Properties
#endregion
#region Constructors
#endregion
#region Methods
public List<Artist> RetrieveArtists()
{
try
{
var client = new RestClient(API.ApiUrl);
var apiEndpoint = $@"api/{API.APIVersion}/artist";
var request = new RestRequest(apiEndpoint, Method.GET);
var tkRepo = new DBTokenRepository();
var token = tkRepo.RetrieveToken();
request.AddHeader("Authorization", $"Bearer {token.AccessToken}");
var response = client.Execute(request);
var artists = JsonConvert.DeserializeObject<List<Artist>>(response.Content);
return artists;
}
catch (Exception ex)
{
var msg = ex.Message;
}
return null;
}
#endregion
}
}
+59
View File
@@ -1,10 +1,69 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Mear.Models;
using Mear.Repositories.Remote;
namespace Mear.ViewModels namespace Mear.ViewModels
{ {
public class ArtistViewModel : BaseViewModel public class ArtistViewModel : BaseViewModel
{ {
#region Fields
private ObservableCollection<Artist> _artistItems;
private Command _refreshArtists;
#endregion
#region Properties
public ObservableCollection<Artist> ArtistItems
{
get => _artistItems;
}
public Command RefreshArtists
{
get => _refreshArtists;
}
#endregion
#region Constructors
public ArtistViewModel()
{
_artistItems = new ObservableCollection<Artist>();
_refreshArtists = new Command(async () => await PopulateArtistsAsync());
PopulateArtistsAsync();
}
#endregion
#region Methods
private async Task PopulateArtistsAsync()
{
try
{
var artistRepo = new RemoteArtistRepository();
var artists = artistRepo.RetrieveArtists().OrderBy(a => a.Name).ToList();
_artistItems.Clear();
foreach (var artist in artists)
{
_artistItems.Add(artist);
}
}
catch (Exception ex)
{
var msg = ex.Message;
}
}
#endregion
} }
} }
+43 -4
View File
@@ -4,10 +4,49 @@
xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" mc:Ignorable="d"
x:Class="Mear.Views.ArtistView"> x:Class="Mear.Views.ArtistView"
<ContentPage.Content> xmlns:vm="clr-namespace:Mear.ViewModels"
Title="{Binding Title}">
<ContentPage.BindingContext>
<vm:ArtistViewModel />
</ContentPage.BindingContext>
<StackLayout> <StackLayout>
<Label Text="Artist view!" /> <ListView x:Name="ArtistListView"
ItemsSource="{Binding ArtistItems}"
VerticalOptions="FillAndExpand"
HasUnevenRows="False"
IsPullToRefreshEnabled="True"
RowHeight="80"
RefreshCommand="{Binding RefreshArtists}"
ItemSelected="ArtistListView_ItemSelected" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0">
<Label Text="{Binding Name}"
d:Text="{Binding .}"
LineBreakMode="NoWrap"
FontSize="16" />
<Label Text="{Binding SongCount}"
d:Text="{Binding .}"
LineBreakMode="NoWrap"
FontSize="16" />
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout> </StackLayout>
</ContentPage.Content>
</ContentPage> </ContentPage>
+26 -1
View File
@@ -7,14 +7,39 @@ using System.Threading.Tasks;
using Xamarin.Forms; using Xamarin.Forms;
using Xamarin.Forms.Xaml; using Xamarin.Forms.Xaml;
using Mear.ViewModels;
namespace Mear.Views namespace Mear.Views
{ {
[XamlCompilation(XamlCompilationOptions.Compile)] [XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ArtistView : ContentPage public partial class ArtistView : ContentPage
{ {
#region Fields
private ArtistViewModel _viewModel;
#endregion
#region Properties
#endregion
#region Constructors
public ArtistView() public ArtistView()
{ {
InitializeComponent(); InitializeComponent();
}
BindingContext = _viewModel = new ArtistViewModel();
}
#endregion
#region Methods
#region Events
private void ArtistListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
}
#endregion
#endregion
} }
} }