public void UpdateOrder_MoveDown_SameItem() { var items = GenerateList(); var dropInfo = new MudItemDropInfo <ItemsWithOrder>(items[3], "something", 3); items.UpdateOrder(dropInfo, x => x.Prio); var expectedOrders = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var actualOrders = items.Select(x => x.Prio).ToList(); actualOrders.Should().ContainInOrder(expectedOrders); }
public void UpdateOrder_MoveUp_EdgeCase() { var items = GenerateList(); //move item item-5 to index 0 var dropInfo = new MudItemDropInfo <ItemsWithOrder>(items[9], "something", 0); items.UpdateOrder(dropInfo, x => x.Prio); var expectedOrders = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; var actualOrders = items.Select(x => x.Prio).ToList(); actualOrders.Should().ContainInOrder(expectedOrders); }
public static void UpdateOrder <T>(this IEnumerable <T> items, MudItemDropInfo <T> dropInfo, Expression <Func <T, int> > valueUpdater, int zoneOffset = 0) { var memberSelectorExpression = valueUpdater.Body as MemberExpression; if (memberSelectorExpression == null) { throw new InvalidOperationException(); } var property = memberSelectorExpression.Member as PropertyInfo; if (property == null) { throw new InvalidOperationException(); } var newIndex = dropInfo.IndexInZone + zoneOffset; var item = dropInfo.Item; int index = 0; foreach (var _item in items.OrderBy(x => (int)property.GetValue(x))) { if (_item.Equals(item) == true) { property.SetValue(item, newIndex); } else { if (index == newIndex) { index++; } property.SetValue(_item, index); index++; } } }