playertimeline max size

This commit is contained in:
andy 2021-10-05 07:58:29 +01:00
parent bb6ad45a98
commit 28b7b954c1
3 changed files with 91 additions and 1 deletions

View File

@ -45,6 +45,70 @@ namespace Selector.Tests
timeline.Count.Should().Be(currentlyPlaying.Length);
}
public static IEnumerable<object[]> MaxSizeData =>
new List<object[]>
{
new object[] {
new CurrentlyPlayingContext[]
{
Helper.CurrentPlayback(Helper.FullTrack("uri1"))
}, 5, 1
},
new object[] {
new CurrentlyPlayingContext[]
{
Helper.CurrentPlayback(Helper.FullTrack("uri1")),
Helper.CurrentPlayback(Helper.FullTrack("uri2")),
Helper.CurrentPlayback(Helper.FullTrack("uri3"))
}, 1, 1
},
new object[] {
new CurrentlyPlayingContext[]
{
Helper.CurrentPlayback(Helper.FullTrack("uri1")),
Helper.CurrentPlayback(Helper.FullTrack("uri2")),
Helper.CurrentPlayback(Helper.FullTrack("uri3")),
Helper.CurrentPlayback(Helper.FullTrack("uri4")),
Helper.CurrentPlayback(Helper.FullTrack("uri5")),
Helper.CurrentPlayback(Helper.FullTrack("uri6")),
Helper.CurrentPlayback(Helper.FullTrack("uri7")),
Helper.CurrentPlayback(Helper.FullTrack("uri8")),
}, 5, 5
},
new object[] {
new CurrentlyPlayingContext[]
{
Helper.CurrentPlayback(Helper.FullTrack("uri1")),
Helper.CurrentPlayback(Helper.FullTrack("uri2")),
Helper.CurrentPlayback(Helper.FullTrack("uri3")),
Helper.CurrentPlayback(Helper.FullTrack("uri4")),
Helper.CurrentPlayback(Helper.FullTrack("uri5")),
Helper.CurrentPlayback(Helper.FullTrack("uri6")),
Helper.CurrentPlayback(Helper.FullTrack("uri7")),
Helper.CurrentPlayback(Helper.FullTrack("uri8")),
Helper.CurrentPlayback(Helper.FullTrack("uri9")),
Helper.CurrentPlayback(Helper.FullTrack("uri10"))
}, null, 10
}
};
[Theory]
[MemberData(nameof(MaxSizeData))]
public void MaxSize(CurrentlyPlayingContext[] currentlyPlaying, int? maxSize, int finalCount)
{
var timeline = new PlayerTimeline
{
MaxSize = maxSize
};
foreach (var i in currentlyPlaying)
{
timeline.Add(i);
}
timeline.Count.Should().Be(finalCount);
}
[Fact]
public void Clear()
{

View File

@ -11,11 +11,26 @@ namespace Selector
IEnumerable<TimelineItem<CurrentlyPlayingContext>>
{
private List<TimelineItem<CurrentlyPlayingContext>> recentlyPlayed = new List<TimelineItem<CurrentlyPlayingContext>>();
private List<TimelineItem<CurrentlyPlayingContext>> recentlyPlayed = new();
public IEqual EqualityChecker { get; set; }
public bool SortOnBackDate { get; set; } = true;
public int Count { get => recentlyPlayed.Count; }
private int? max = 1000;
public int? MaxSize {
get => max;
set {
if(value is null)
{
max = value;
}
else
{
max = Math.Max(1, (int) value);
}
}
}
public void Add(CurrentlyPlayingContext item) => Add(item, DateHelper.FromUnixMilli(item.Timestamp));
public void Add(CurrentlyPlayingContext item, DateTime timestamp)
{
@ -28,6 +43,8 @@ namespace Selector
{
Sort();
}
CheckSize();
}
public void Sort()
@ -37,6 +54,13 @@ namespace Selector
.ToList();
}
private void CheckSize()
{
if (MaxSize is int maxSize && Count > maxSize) {
recentlyPlayed.RemoveRange(0, Count - maxSize);
}
}
public void Clear() => recentlyPlayed.Clear();
public CurrentlyPlayingContext Get()

View File

@ -9,5 +9,7 @@ namespace Selector
{
public Task WatchOne();
public Task Watch(CancellationToken cancelToken);
public int PollPeriod { get; set; }
}
}