Approved
Last Updated: 14 Aug 2017 11:54 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 20 Jul 2017 09:51
Category: Scheduler/Reminder
Type: Bug Report
1
FIX. RadScheduler - appointments move while resizing
To reproduce: Use the following code snippet:

            DateTime dt = DateTime.Now;
            this.radScheduler1.Appointments.Add(new Appointment(dt, TimeSpan.FromHours(4),"A1"));
            this.radScheduler1.Appointments.Add(new Appointment(dt, TimeSpan.FromHours(4),"A2"));

By default, when two appointments start at the same time, on the left side it is displayed the shorter appointment. Hence, if you start resizing an appointment it may jump to another location leaving the mouse in a  wrong and inconsistent position.  The attached gif illustartes the behavior.

Workaround: 

public RadForm1()
{
    InitializeComponent();

    this.radScheduler1.ElementProvider = new MyElementProvider(this.radScheduler1);

    DateTime dt = DateTime.Now;
    this.radScheduler1.Appointments.Add(new Appointment(dt, TimeSpan.FromHours(4),"A1"));
    this.radScheduler1.Appointments.Add(new Appointment(dt, TimeSpan.FromHours(4),"A2"));
}

private class AppointmentDateInfo : IComparable
{
    public AppointmentElement appointment;
    public DateTime date;
    public bool isEnd;
    private DateTimeComparer comparer;

    public AppointmentDateInfo(AppointmentElement appointment, DateTime date, bool isEnd)
    {
        this.comparer = new DateTimeComparer(appointment.Scheduler);
        this.appointment = appointment;
        this.date = date;
        this.isEnd = isEnd;
    }

    public int CompareTo(object obj)
    {
        AppointmentDateInfo other = obj as AppointmentDateInfo;
        if (other == null)
        {
            return 1;
        }

        int res = date.CompareTo(other.date);
        if (res == 0)
        {
            if (other.appointment != this.appointment)
            {
                res = other.isEnd.CompareTo(isEnd);
            }
            else
            {
                res = this.isEnd.CompareTo(other.isEnd);
            }
        }
        if (appointment.Start.Equals(other.appointment.Start))
        {
            return res != 0 ? res : appointment.AppointmentSubject.CompareTo(other.appointment.AppointmentSubject);
        }
        return res != 0 ? res : comparer.Compare(appointment, other.appointment);
    }
}

public class MyElementProvider : SchedulerElementProvider
{
    public MyElementProvider(RadScheduler scheduler) : base(scheduler)
    {
    }

    protected override T CreateElement<T>(SchedulerView view, object context)
    {
        if (typeof(T) == typeof(DayViewAppointmentsTable))
        {
            return new CustomDayViewAppointmentsTable(this.Scheduler, view, (DayViewAppointmentsArea)context)as T;
        }
        
        return base.CreateElement<T>(view, context);
    }
}

public class CustomDayViewAppointmentsTable : DayViewAppointmentsTable
{
    public CustomDayViewAppointmentsTable(RadScheduler scheduler, SchedulerView view, DayViewAppointmentsArea area) : base(scheduler, view, area)
    {
    }

    private int FindLaneSpan(AppointmentElement appointment, Dictionary<AppointmentElement, int> appointmentLanes, int maxLanes)
    {
        int lane = appointmentLanes[appointment];
        int span = maxLanes - lane;

        int cellsPerHour = this.GetDayViewBase().GetNumberOfCellsPerHour();
        int minutesPerSlot = 60 / cellsPerHour;

        DateTime roundedAppStart = this.GetRulerRoundedDateTime(appointment.Start, false);
        DateTime roundedAppEnd = this.GetRulerRoundedDateTime(appointment.End, true);

        if (appointment.Start == appointment.End)
        {
            roundedAppEnd = roundedAppEnd.AddMinutes(minutesPerSlot);
        }

        foreach (KeyValuePair<AppointmentElement, int> item in appointmentLanes)
        {
            if (appointment == item.Key)
            {
                continue;
            }

            DateTime roundedItemStart = this.GetRulerRoundedDateTime(item.Key.Start, false);
            DateTime roundedItemEnd = this.GetRulerRoundedDateTime(item.Key.End, true);

            if (item.Key.Start == item.Key.End)
            {
                roundedItemEnd = roundedItemEnd.AddMinutes(minutesPerSlot);
            }

            if (roundedItemStart < roundedAppEnd && roundedItemEnd > roundedAppStart && item.Value > lane)
            {
                span = Math.Min(span, item.Value - lane);
            }
        }

        return span;
    }

    private int FindFreeLane(List<AppointmentElement> overlappingAppointments, Dictionary<AppointmentElement, int> appointmentLanes)
    {
        List<int> indexes = new List<int>();

        for (int i = 0; i < overlappingAppointments.Count; i++)
        {
            indexes.Add(appointmentLanes[overlappingAppointments[i]]);
        }

        indexes.Sort();

        for (int i = 0; i < indexes.Count; i++)
        {
            if (i < indexes[i])
            {
                return i;
            }
        }

        return overlappingAppointments.Count;
    }
    
    protected override void ArrangeAppointments(SizeF finalSize)
    {
        RectangleF containerRect = new RectangleF(PointF.Empty, finalSize);
        List<AppointmentElement> appointmentsInLayout = new List<AppointmentElement>();

        foreach (AppointmentElement element in this.AppointmentElements)
        {
            if (element.Visibility != ElementVisibility.Collapsed)
            {
                appointmentsInLayout.Add(element);
            }
        }

        appointmentsInLayout.Sort(new DateTimeComparer(this.Scheduler));
        Dictionary<AppointmentElement, object> arrangedAppointments = new Dictionary<AppointmentElement, object>();

        foreach (AppointmentElement appointmentElement in appointmentsInLayout)
        {
            if (arrangedAppointments.ContainsKey(appointmentElement))
            {
                continue;
            }

            appointmentElement.RelatedAppointments.Sort(new DateTimeComparer(this.Scheduler));

            List<AppointmentDateInfo> appointmentDates = new List<AppointmentDateInfo>();
            List<AppointmentElement> overlappingAppointments = new List<AppointmentElement>();
            Dictionary<AppointmentElement, int> appointmentLanes = new Dictionary<AppointmentElement, int>();

            foreach (AppointmentElement relatedAppointment in appointmentElement.RelatedAppointments)
            {
                if (relatedAppointment.Visibility == ElementVisibility.Collapsed)
                {
                    continue;
                }

                DateTime appStart = relatedAppointment.Start <= relatedAppointment.End ? relatedAppointment.Start : relatedAppointment.End;
                DateTime appEnd = relatedAppointment.Start <= relatedAppointment.End ? relatedAppointment.End : relatedAppointment.Start;

                if (!this.Scheduler.EnableExactTimeRendering)
                {
                    if (appStart != appEnd)
                    {
                        appointmentDates.Add(new AppointmentDateInfo(relatedAppointment, this.GetRulerRoundedDateTime(appStart, false), false));
                        appointmentDates.Add(new AppointmentDateInfo(relatedAppointment, this.GetRulerRoundedDateTime(appEnd, true), true));
                    }
                    else
                    {
                        int cellsPerHour = this.GetDayViewBase().GetNumberOfCellsPerHour();
                        int minutesPerSlot = 60 / cellsPerHour;

                        appointmentDates.Add(new AppointmentDateInfo(relatedAppointment, this.GetRulerRoundedDateTime(appStart, false), false));
                        appointmentDates.Add(new AppointmentDateInfo(relatedAppointment, this.GetRulerRoundedDateTime(appEnd, true).AddMinutes(minutesPerSlot),
                            true));
                    }
                }
                else
                {
                    appointmentDates.Add(new AppointmentDateInfo(relatedAppointment, appStart, false));
                    appointmentDates.Add(new AppointmentDateInfo(relatedAppointment, appEnd, true));
                }
            }

            appointmentDates.Sort();
            int maxLanesCount = 0;

            for (int i = 0; i < appointmentDates.Count; i++)
            {
                if (!appointmentDates[i].isEnd)
                {
                    int freeLane = FindFreeLane(overlappingAppointments, appointmentLanes);
                    appointmentLanes.Add(appointmentDates[i].appointment, freeLane);
                    overlappingAppointments.Add(appointmentDates[i].appointment);
                    maxLanesCount = Math.Max(maxLanesCount, freeLane + 1);
                }
                else
                {
                    overlappingAppointments.Remove(appointmentDates[i].appointment);
                }

                if (overlappingAppointments.Count == 0)
                {
                    foreach (KeyValuePair<AppointmentElement, int> item in appointmentLanes)
                    {
                        AppointmentElement appointment = item.Key;
                        int lane = item.Value;
                        DateTime rulerRoundStart = this.GetRulerRoundedDateTime(appointment.Start, false);
                        DateTime rulerRoundEnd = this.GetRulerRoundedDateTime(appointment.End, true);

                        if (appointment.Start == appointment.End)
                        {
                            int cellsPerHour = this.GetDayViewBase().GetNumberOfCellsPerHour();
                            int minutesPerSlot = 60 / cellsPerHour;

                            rulerRoundEnd = rulerRoundEnd.AddMinutes(minutesPerSlot);
                        }

                        PointF location = GetItemLocationInTable(appointment.Start, finalSize);

                        float dayWidth = this.Area.DayViewElement.GetColumnWidth(this.Area.DayViewElement.GetColumnForDate(rulerRoundStart), finalSize.Width);
                        int laneSpan = FindLaneSpan(appointment, appointmentLanes, maxLanesCount);

                        SizeF size = new SizeF(dayWidth / maxLanesCount * laneSpan, this.GetItemHeight(appointment.Start, appointment.End));
                        location.X += dayWidth / maxLanesCount * lane + this.Area.DayViewElement.AppointmentMargin.Left;
                        location.Y += this.Area.DayViewElement.AppointmentMargin.Top;
                        size.Width -= this.Area.DayViewElement.AppointmentMargin.Horizontal;

                        appointment.DesiredBounds = this.OnAppointmentLayout(appointment, new RectangleF(location, size), finalSize);
                        if (!arrangedAppointments.ContainsKey(appointment))
                        {
                            arrangedAppointments.Add(appointment, null);
                        }
                    }

                    appointmentLanes.Clear();
                    maxLanesCount = 0;
                }
            }

            for (int i = 0; i < appointmentsInLayout.Count; i++)
            {
                AppointmentElement appointment = appointmentsInLayout[i];

                if (appointment.Visibility != ElementVisibility.Visible)
                    continue;

                RectangleF rectangle = appointment.DesiredBounds;

                if (this.RightToLeft)
                {
                    rectangle = LayoutUtils.RTLTranslateNonRelative(rectangle, containerRect);
                }

                appointment.Arrange(rectangle);
            }
        }
    }
}

0 comments