/// <summary>
 /// Initializes a new instance of the <see cref="ItemReorderCompleteContext"/> class.
 /// </summary>
 public ItemReorderCompleteContext(object dataItem, IDataGroup dataGroup, object destinationItem, IDataGroup destinationGroup, ItemReorderPlacement placement)
 {
     this.Item             = dataItem;
     this.Group            = dataGroup;
     this.DestinationItem  = destinationItem;
     this.DestinationGroup = destinationGroup;
     this.Placement        = placement;
 }
        void IDragDropElement.OnDragDropComplete(DragCompleteContext context)
        {
            var data = context.PayloadData as ReorderItemsDragOperation;

            if (data != null)
            {
                this.FinalizeReorder(context);

                var                  isExecuted      = false;
                RadListViewItem      destinationItem = null;
                ItemReorderPlacement placement       = 0;

                if (data.InitialSourceIndex < data.CurrentSourceReorderIndex)
                {
                    destinationItem = this.reorderCoordinator.Host.ElementAt(data.CurrentSourceReorderIndex - 1) as RadListViewItem;
                    placement       = ItemReorderPlacement.After;

                    if (destinationItem == null)
                    {
                        destinationItem = this.reorderCoordinator.Host.ElementAt(data.CurrentSourceReorderIndex + 1) as RadListViewItem;
                        placement       = ItemReorderPlacement.Before;
                    }
                }
                else if (data.InitialSourceIndex > data.CurrentSourceReorderIndex)
                {
                    destinationItem = this.reorderCoordinator.Host.ElementAt(data.CurrentSourceReorderIndex + 1) as RadListViewItem;
                    placement       = ItemReorderPlacement.Before;

                    if (destinationItem == null)
                    {
                        destinationItem = this.reorderCoordinator.Host.ElementAt(data.CurrentSourceReorderIndex - 1) as RadListViewItem;
                        placement       = ItemReorderPlacement.After;
                    }
                }

                if (destinationItem != null)
                {
                    var dataItem            = data.Data;
                    var destinationDataItem = destinationItem.DataContext;

                    IDataGroup dataGroup            = null;
                    IDataGroup destinationDataGroup = null;

                    if (this.listView.GroupDescriptors.Count > 0)
                    {
                        dataGroup            = this.listView.Model.FindItemParentGroup(dataItem);
                        destinationDataGroup = this.listView.Model.FindItemParentGroup(destinationDataItem);
                    }

                    var commandContext = new ItemReorderCompleteContext(dataItem, dataGroup, destinationDataItem, destinationDataGroup, placement);

                    isExecuted = this.ListView.commandService.ExecuteCommand(CommandId.ItemReorderComplete, commandContext);
                }

                if (isExecuted)
                {
                    // TODO: Data provider does not handle well reordering of items in groups.
                    // Remove this workaround once we fix the reordering in the data provider.
                    if (this.listView.GroupDescriptors.Count > 0)
                    {
                        this.listView.updateService.RegisterUpdate((int)UpdateFlags.AffectsData);
                    }
                }
                else
                {
                    this.reorderCoordinator.CancelReorderOperation(this, data.InitialSourceIndex);
                }
            }
            else
            {
                if (context.DragSuccessful)
                {
                    double offset   = 0;
                    var    dragMode = DragDrop.GetDragPositionMode(this);

                    if (this.ListView.Orientation == Orientation.Horizontal)
                    {
                        switch (dragMode)
                        {
                        case DragPositionMode.RailXForward:
                            offset = Math.Max(0, this.dragY);
                            break;

                        case DragPositionMode.RailXBackwards:
                            offset = Math.Min(0, this.dragY);
                            break;

                        case DragPositionMode.RailX:
                            offset = this.dragY;
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        switch (dragMode)
                        {
                        case DragPositionMode.RailXForward:
                            offset = Math.Max(0, this.dragX);
                            break;

                        case DragPositionMode.RailXBackwards:
                            offset = Math.Min(0, this.dragX);
                            break;

                        case DragPositionMode.RailX:
                            offset = this.dragX;
                            break;

                        default:
                            break;
                        }
                    }
                    var swipeContext = new ItemSwipeActionCompleteContext(this.DataContext, this, offset);

                    bool isExecuted = this.ListView.commandService.ExecuteCommand(CommandId.ItemSwipeActionComplete, swipeContext);

                    if (isExecuted)
                    {
                        this.UpdateActionContentClipping(swipeContext.FinalDragOffset);
                        this.isDraggedForAction  = true;
                        this.ListView.swipedItem = this;
                    }
                    else
                    {
                        this.ClearActionContent();
                    }
                }
                else
                {
                    this.ClearActionContent();
                    this.ListView.CleanupSwipedItem();
                }
            }
            this.Opacity = 1;

            DragDrop.SetDragPositionMode(this, DragPositionMode.Free);
            this.ListView.InvalidatePanelArrange();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ItemReorderCompleteContext"/> class.
 /// </summary>
 public ItemReorderCompleteContext(object dataItem, object destinationItem, ItemReorderPlacement placement)
     : this(dataItem, null, destinationItem, null, placement)
 {
 }