added favorites

Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
Matt Bruce 2016-09-05 14:18:39 -05:00
parent 0097334902
commit 08224b0801
3 changed files with 52 additions and 1 deletions

View File

@ -34,6 +34,10 @@ namespace KaraokePlayer.Classes
{
get { return string.Format("controllers/{0}/player/queue/0/", this.Id); }
}
private string FavoritesPath
{
get { return string.Format("controllers/{0}/favorites/", this.Id); }
}
public string Id { get; set; }
public Song CurrentSong { get; set; }
@ -92,13 +96,14 @@ namespace KaraokePlayer.Classes
private void CurrentSongDidChange()
{
var response = _client.Get(CurrentSongPath);
var response = _client.Get(CurrentSongPath);
var item = response.ResultAs<QueueItem>();
if (item == null) return;
if (CurrentSong == null || CurrentSong.Path != item.Song.Path)
{
CurrentSong = item.Song;
_songChanged(new ControllerSongChangedEventArgs(item.Song));
UpdateFavorites(item.Song);
}
}
@ -108,5 +113,35 @@ namespace KaraokePlayer.Classes
_state = newstate;
_stateChanged(new ControllerStateChangedEventArgs(newstate));
}
private void UpdateFavorites(Song song)
{
//get favorites
FavoriteSong favorite = null;
List<FavoriteSong> favorites = _client.Get(FavoritesPath).ResultAs<List<FavoriteSong>>();
if (favorites == null) favorites = new List<FavoriteSong>();
//unique song is based on the path
favorite = favorites.SingleOrDefault(f => f.Path == song.Path);
if (favorite == null)
{
favorite = new FavoriteSong();
favorite.Artist = song.Artist;
favorite.Title = song.Title;
favorite.Genre = song.Genre;
favorite.Path = song.Path;
favorite.Year = song.Year;
favorite.Count = 1;
favorites.Add(favorite);
}
else {
favorite.Count += 1;
}
//take the top 100 songs
var top100 = favorites.OrderByDescending(f => f.Count).Take(100);
_client.Set(FavoritesPath, top100);
}
}
}

View File

@ -0,0 +1,15 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Herse.Models
{
public class FavoriteSong : Song
{
[JsonProperty("count")]
public int Count { get; set; }
}
}

View File

@ -44,6 +44,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FavoriteSong.cs" />
<Compile Include="FileType.cs" />
<Compile Include="PlayerState.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />