adding timeline, manager interfaces, adding IEqual
This commit is contained in:
parent
81ded7eb7c
commit
2136a7953b
@ -38,8 +38,7 @@ namespace Selector.Tests
|
||||
for (var i = 0; i < playing.Count; i++)
|
||||
{
|
||||
await watcher.WatchOne();
|
||||
var current = watcher.NowPlaying();
|
||||
current.Should().Be(playing[i]);
|
||||
watcher.Live.Should().Be(playing[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
24
Selector/Equality/Equal.cs
Normal file
24
Selector/Equality/Equal.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
class Equal : IEqual
|
||||
{
|
||||
private Dictionary<Type, object> comps;
|
||||
|
||||
public bool IsEqual<T>(T item, T other)
|
||||
{
|
||||
if (comps.ContainsKey(typeof(T)))
|
||||
{
|
||||
var comp = (IEqualityComparer<T>) comps[typeof(T)];
|
||||
return comp.Equals(item, other);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"{typeof(T)} had no corresponding equality checker");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Selector/Equality/IEqual.cs
Normal file
11
Selector/Equality/IEqual.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
public interface IEqual
|
||||
{
|
||||
public bool IsEqual<T>(T item, T other);
|
||||
}
|
||||
}
|
11
Selector/Helpers/DateHelper.cs
Normal file
11
Selector/Helpers/DateHelper.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
public static class DateHelper
|
||||
{
|
||||
public static DateTime FromUnixMilli(long milli) => new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(milli);
|
||||
}
|
||||
}
|
13
Selector/Timeline/Interfaces/ITimeline.cs
Normal file
13
Selector/Timeline/Interfaces/ITimeline.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
public interface ITimeline<T>
|
||||
{
|
||||
public int Count { get; set; }
|
||||
public T Get(DateTime at);
|
||||
public T Get();
|
||||
public void Add(T item, DateTime timestamp);
|
||||
public void Clear();
|
||||
}
|
||||
}
|
10
Selector/Timeline/Interfaces/ITimelineItem.cs
Normal file
10
Selector/Timeline/Interfaces/ITimelineItem.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
public interface ITimelineItem<T>
|
||||
{
|
||||
public T Item { get; set; }
|
||||
public DateTime Time { get; set; }
|
||||
}
|
||||
}
|
10
Selector/Timeline/Playable.cs
Normal file
10
Selector/Timeline/Playable.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
class Playable
|
||||
{
|
||||
public Type Type;
|
||||
public object Obj;
|
||||
}
|
||||
}
|
29
Selector/Timeline/Timeline.cs
Normal file
29
Selector/Timeline/Timeline.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
public class Timeline<T> : ITimeline<T>
|
||||
{
|
||||
public int Count { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
||||
|
||||
public void Add(T item, DateTime timestamp)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T Get(DateTime at)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T Get()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
19
Selector/Timeline/TimelineItem.cs
Normal file
19
Selector/Timeline/TimelineItem.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
public class TimelineItem<T>: ITimelineItem<T>
|
||||
{
|
||||
public T Item { get; set; }
|
||||
public DateTime Time { get; set; }
|
||||
|
||||
public static TimelineItem<TT> From<TT>(TT item, DateTime time)
|
||||
{
|
||||
return new TimelineItem<TT>()
|
||||
{
|
||||
Item = item,
|
||||
Time = time
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
29
Selector/Watcher/BaseWatcher.cs
Normal file
29
Selector/Watcher/BaseWatcher.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
public abstract class BaseWatcher: IWatcher
|
||||
{
|
||||
public abstract Task WatchOne();
|
||||
|
||||
public async Task Watch(CancellationToken cancelToken)
|
||||
{
|
||||
while (!cancelToken.IsCancellationRequested)
|
||||
{
|
||||
await WatchOne();
|
||||
await Task.Delay(PollPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
private int _pollPeriod;
|
||||
public int PollPeriod
|
||||
{
|
||||
get => _pollPeriod;
|
||||
set => _pollPeriod = Math.Max(0, value);
|
||||
}
|
||||
}
|
||||
}
|
@ -6,5 +6,14 @@ namespace Selector
|
||||
public class ListeningChangeEventArgs: EventArgs {
|
||||
public CurrentlyPlayingContext Previous;
|
||||
public CurrentlyPlayingContext Current;
|
||||
|
||||
public static ListeningChangeEventArgs From(CurrentlyPlayingContext previous, CurrentlyPlayingContext current)
|
||||
{
|
||||
return new ListeningChangeEventArgs()
|
||||
{
|
||||
Previous = previous,
|
||||
Current = current
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
14
Selector/Watcher/Interfaces/IManager.cs
Normal file
14
Selector/Watcher/Interfaces/IManager.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
interface IManager
|
||||
{
|
||||
public void Add(IWatcher watcher);
|
||||
|
||||
public bool Start();
|
||||
public bool Stop();
|
||||
}
|
||||
}
|
@ -15,8 +15,5 @@ namespace Selector
|
||||
public event EventHandler<ListeningChangeEventArgs> VolumeChange;
|
||||
public event EventHandler<ListeningChangeEventArgs> DeviceChange;
|
||||
public event EventHandler<ListeningChangeEventArgs> PlayingChange;
|
||||
|
||||
public CurrentlyPlayingContext NowPlaying();
|
||||
// recently playing
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,10 @@ using SpotifyAPI.Web;
|
||||
|
||||
namespace Selector
|
||||
{
|
||||
public class PlayerWatcher: IPlayerWatcher
|
||||
public class PlayerWatcher: BaseWatcher, IPlayerWatcher
|
||||
{
|
||||
private readonly IPlayerClient spotifyClient;
|
||||
private IEqualityChecker equalityChecker;
|
||||
private readonly IEqualityChecker equalityChecker;
|
||||
|
||||
public event EventHandler<ListeningChangeEventArgs> ItemChange;
|
||||
public event EventHandler<ListeningChangeEventArgs> AlbumChange;
|
||||
@ -21,15 +21,10 @@ namespace Selector
|
||||
public event EventHandler<ListeningChangeEventArgs> DeviceChange;
|
||||
public event EventHandler<ListeningChangeEventArgs> PlayingChange;
|
||||
|
||||
private CurrentlyPlayingContext live { get; set; }
|
||||
public CurrentlyPlayingContext Live { get; private set; }
|
||||
public ITimeline<CurrentlyPlayingContext> Past { get; private set; }
|
||||
private List<List<CurrentlyPlayingContext>> lastPlays { get; set; }
|
||||
|
||||
private int _pollPeriod;
|
||||
public int PollPeriod {
|
||||
get => _pollPeriod;
|
||||
set => _pollPeriod = Math.Max(0, value);
|
||||
}
|
||||
|
||||
public PlayerWatcher(IPlayerClient spotifyClient,
|
||||
IEqualityChecker equalityChecker,
|
||||
int pollPeriod = 3000) {
|
||||
@ -41,101 +36,72 @@ namespace Selector
|
||||
lastPlays = new List<List<CurrentlyPlayingContext>>();
|
||||
}
|
||||
|
||||
public async Task WatchOne()
|
||||
public override async Task WatchOne()
|
||||
{
|
||||
try{
|
||||
var polledCurrent = await spotifyClient.GetCurrentPlayback();
|
||||
|
||||
if (polledCurrent != null) StoreCurrentPlaying(live, polledCurrent);
|
||||
if (polledCurrent != null) StoreCurrentPlaying(Live, polledCurrent);
|
||||
|
||||
// swap new item into live and bump existing down to previous
|
||||
CurrentlyPlayingContext previous;
|
||||
if(live is null) {
|
||||
live = polledCurrent;
|
||||
if(Live is null) {
|
||||
Live = polledCurrent;
|
||||
previous = polledCurrent;
|
||||
}
|
||||
else {
|
||||
previous = live;
|
||||
live = polledCurrent;
|
||||
previous = Live;
|
||||
Live = polledCurrent;
|
||||
}
|
||||
|
||||
// NOT PLAYING
|
||||
if(previous is null && live is null)
|
||||
if(previous is null && Live is null)
|
||||
{
|
||||
// Console.WriteLine("not playing");
|
||||
}
|
||||
else
|
||||
{
|
||||
// STARTED PLAYBACK
|
||||
if(previous is null && (live.Item is FullTrack || live.Item is FullEpisode))
|
||||
if(previous is null && (Live.Item is FullTrack || Live.Item is FullEpisode))
|
||||
{
|
||||
// Console.WriteLine("started playing");
|
||||
OnPlayingChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
OnPlayingChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
// STOPPED PLAYBACK
|
||||
else if((previous.Item is FullTrack || previous.Item is FullEpisode) && live is null)
|
||||
else if((previous.Item is FullTrack || previous.Item is FullEpisode) && Live is null)
|
||||
{
|
||||
// Console.WriteLine("stopped playing");
|
||||
OnPlayingChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
OnPlayingChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
// CONTINUING PLAYBACK
|
||||
else {
|
||||
|
||||
// MUSIC
|
||||
if(previous.Item is FullTrack && live.Item is FullTrack)
|
||||
if(previous.Item is FullTrack previousTrack && Live.Item is FullTrack currentTrack)
|
||||
{
|
||||
var previousItem = (FullTrack) previous.Item;
|
||||
var currentItem = (FullTrack) live.Item;
|
||||
|
||||
if(!equalityChecker.Track(previousItem, currentItem, true)) {
|
||||
OnItemChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
if(!equalityChecker.Track(previousTrack, currentTrack, true)) {
|
||||
OnItemChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
|
||||
if(!equalityChecker.Album(previousItem.Album, currentItem.Album)) {
|
||||
OnAlbumChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
if(!equalityChecker.Album(previousTrack.Album, currentTrack.Album)) {
|
||||
OnAlbumChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
|
||||
if(!equalityChecker.Artist(previousItem.Artists[0], currentItem.Artists[0])) {
|
||||
OnArtistChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
if(!equalityChecker.Artist(previousTrack.Artists[0], currentTrack.Artists[0])) {
|
||||
OnArtistChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
}
|
||||
// CHANGED CONTENT
|
||||
else if(previous.Item is FullTrack && live.Item is FullEpisode
|
||||
|| previous.Item is FullEpisode && live.Item is FullTrack)
|
||||
else if(previous.Item is FullTrack && Live.Item is FullEpisode
|
||||
|| previous.Item is FullEpisode && Live.Item is FullTrack)
|
||||
{
|
||||
OnContentChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
OnItemChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
OnContentChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
OnItemChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
// PODCASTS
|
||||
else if(previous.Item is FullEpisode && live.Item is FullEpisode)
|
||||
else if(previous.Item is FullEpisode previousEp && Live.Item is FullEpisode currentEp)
|
||||
{
|
||||
var previousItem = (FullEpisode) previous.Item;
|
||||
var currentItem = (FullEpisode) live.Item;
|
||||
|
||||
if(!equalityChecker.Episode(previousItem, currentItem)) {
|
||||
OnItemChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
if(!equalityChecker.Episode(previousEp, currentEp)) {
|
||||
OnItemChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
}
|
||||
else {
|
||||
@ -143,35 +109,23 @@ namespace Selector
|
||||
}
|
||||
|
||||
// CONTEXT
|
||||
if(!equalityChecker.Context(previous.Context, live.Context)) {
|
||||
OnContextChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
if(!equalityChecker.Context(previous.Context, Live.Context)) {
|
||||
OnContextChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
|
||||
// DEVICE
|
||||
if(!equalityChecker.Device(previous?.Device, live?.Device)) {
|
||||
OnDeviceChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
if(!equalityChecker.Device(previous?.Device, Live?.Device)) {
|
||||
OnDeviceChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
|
||||
// IS PLAYING
|
||||
if(previous.IsPlaying != live.IsPlaying) {
|
||||
OnPlayingChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
if(previous.IsPlaying != Live.IsPlaying) {
|
||||
OnPlayingChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
|
||||
// VOLUME
|
||||
if(previous.Device.VolumePercent != live.Device.VolumePercent) {
|
||||
OnVolumeChange(new ListeningChangeEventArgs(){
|
||||
Previous = previous,
|
||||
Current = live
|
||||
});
|
||||
if(previous.Device.VolumePercent != Live.Device.VolumePercent) {
|
||||
OnVolumeChange(ListeningChangeEventArgs.From(previous, Live));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -190,20 +144,6 @@ namespace Selector
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Watch(CancellationToken cancelToken)
|
||||
{
|
||||
while (!cancelToken.IsCancellationRequested)
|
||||
{
|
||||
await WatchOne();
|
||||
await Task.Delay(PollPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
public CurrentlyPlayingContext NowPlaying()
|
||||
{
|
||||
return live;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Store currently playing in last plays. Determine whether new list or appending required
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user