private void UpdateCollection(SpeedCaptureLocation location, int driverId, int speed)
{
var collection = GetInnerCollection(location);
var existing = collection.Where(x => x.Driver.Id == driverId).FirstOrDefault();
if(existing != null)
{
if(speed <= existing.Speed)
{
return;
}
collection.Remove(existing);
}
if(collection.Count < MaxCollectionSize)
{
int insertIndex = collection.Count;
for(int i = 0; i < collection.Count; ++i)
{
if(speed > collection[i].Speed)
{
insertIndex = i;
break;
}
}
collection.Insert(insertIndex, CreateSpeedCaptureModel(location, driverId, speed));
}
else if(speed > collection.Last().Speed)
{
for(int i = 0; i < collection.Count; ++i)
{
if(speed > collection[i].Speed)
{
var previous = collection[i];
for(int j = i + 1; j < collection.Count; ++j)
{
var temp = collection[j];
collection[j] = previous;
previous = temp;
}
collection[i] = CreateSpeedCaptureModel(location, driverId, speed);
break;
}
}
}
NotifyIsEmptyChanged();
}