Skip to content

Commit

Permalink
Modified ExtrapolateTimes():
Browse files Browse the repository at this point in the history
- Prevented multiple passes while extrapolating
- Extended setter logic, so that duration is adjusted when end changes (and vice versa)
  • Loading branch information
HitTheDrum committed Aug 20, 2017
1 parent 52f9ccd commit 2971e35
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 26 deletions.
32 changes: 24 additions & 8 deletions v2/ical.NET/Components/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public virtual IDateTime DtEnd
if (!Equals(DtEnd, value))
{
Properties.Set("DTEND", value);
ExtrapolateTimes();
ExtrapolateTimes(nameof(DtEnd));
}
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ public virtual TimeSpan Duration
if (!Equals(Duration, value))
{
Properties.Set("DURATION", value);
ExtrapolateTimes();
ExtrapolateTimes(nameof(Duration));
}
}
}
Expand Down Expand Up @@ -266,20 +266,36 @@ protected override void OnDeserialized(StreamingContext context)
ExtrapolateTimes();
}

private void ExtrapolateTimes()
/// <summary>
/// Flag to check if ExtrapolaTimes is currently invoking to prevent multi extrapolating
/// </summary>
private bool _isExtrapolating;
private void ExtrapolateTimes(string propertyName = null)
{
if (_isExtrapolating) return;
_isExtrapolating = true;

if (DtEnd == null && DtStart != null && Duration != default(TimeSpan))
{
DtEnd = DtStart.Add(Duration);
}
else if (Duration == default(TimeSpan) && DtStart != null && DtEnd != null)
{
Duration = DtEnd.Subtract(DtStart);
if (propertyName == nameof(DtEnd))
Duration = TimeSpan.Zero;

if (propertyName != nameof(DtEnd))
DtEnd = DtStart.Add(Duration);
}
else if (DtStart == null && Duration != default(TimeSpan) && DtEnd != null)
{
DtStart = DtEnd.Subtract(Duration);
}
else if (/*Duration == default(TimeSpan) && */ DtStart != null && DtEnd != null)
{
if (propertyName == nameof(Duration))
DtEnd = DtStart.Add(Duration);
else
Duration = DtEnd.Subtract(DtStart);
}

_isExtrapolating = false;
}

protected bool Equals(Event other)
Expand Down
34 changes: 25 additions & 9 deletions v2/ical.NET/Components/Todo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override IDateTime DtStart
set
{
base.DtStart = value;
ExtrapolateTimes();
ExtrapolateTimes(nameof(DtStart));
}
}

Expand All @@ -49,7 +49,7 @@ public virtual IDateTime Due
set
{
Properties.Set("DUE", value);
ExtrapolateTimes();
ExtrapolateTimes(nameof(Due));
}
}

Expand All @@ -72,7 +72,7 @@ public virtual TimeSpan Duration
set
{
Properties.Set("DURATION", value);
ExtrapolateTimes();
ExtrapolateTimes(nameof(Duration));
}
}

Expand Down Expand Up @@ -198,20 +198,36 @@ protected override void OnDeserializing(StreamingContext context)
base.OnDeserializing(context);
}

private void ExtrapolateTimes()
/// <summary>
/// Flag to check if ExtrapolaTimes is currently invoking to prevent multi extrapolating
/// </summary>
private bool _isExtrapolating;
private void ExtrapolateTimes(string propertyName = null)
{
if (_isExtrapolating) return;
_isExtrapolating = true;

if (Due == null && DtStart != null && Duration != default(TimeSpan))
{
Due = DtStart.Add(Duration);
}
else if (Duration == default(TimeSpan) && DtStart != null && Due != null)
{
Duration = Due.Subtract(DtStart);
if (propertyName == nameof(Due))
Duration = TimeSpan.Zero;

if (propertyName != nameof(Due))
Due = DtStart.Add(Duration);
}
else if (DtStart == null && Duration != default(TimeSpan) && Due != null)
{
DtStart = Due.Subtract(Duration);
}
else if (DtStart != null && Due != null)
{
if (propertyName == nameof(Duration))
Due = DtStart.Add(Duration);
else
Duration = Due.Subtract(DtStart);
}

_isExtrapolating = false;
}
}
}
34 changes: 25 additions & 9 deletions v2/ical.NET/DataTypes/Period.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,36 @@ public override string ToString()
return periodSerializer.SerializeToString(this);
}

private void ExtrapolateTimes()
/// <summary>
/// Flag to check if ExtrapolaTimes is currently invoking to prevent multi extrapolating
/// </summary>
private bool _isExtrapolating;
private void ExtrapolateTimes(string propertyName = null)
{
if (_isExtrapolating) return;
_isExtrapolating = true;

if (EndTime == null && StartTime != null && Duration != default(TimeSpan))
{
EndTime = StartTime.Add(Duration);
}
else if (Duration == default(TimeSpan) && StartTime != null && EndTime != null)
{
Duration = EndTime.Subtract(StartTime);
if (propertyName == nameof(EndTime))
Duration = TimeSpan.Zero;

if (propertyName != nameof(EndTime))
EndTime = StartTime.Add(Duration);
}
else if (StartTime == null && Duration != default(TimeSpan) && EndTime != null)
{
StartTime = EndTime.Subtract(Duration);
}
else if (StartTime != null && EndTime != null)
{
if (propertyName == nameof(Duration))
EndTime = StartTime.Add(Duration);
else
Duration = EndTime.Subtract(StartTime);
}

_isExtrapolating = false;
}

private IDateTime _startTime;
Expand All @@ -135,7 +151,7 @@ public virtual IDateTime StartTime
return;
}
_startTime = value;
ExtrapolateTimes();
ExtrapolateTimes(nameof(StartTime));
}
}

Expand All @@ -150,7 +166,7 @@ public virtual IDateTime EndTime
return;
}
_endTime = value;
ExtrapolateTimes();
ExtrapolateTimes(nameof(EndTime));
}
}

Expand All @@ -174,7 +190,7 @@ public virtual TimeSpan Duration
return;
}
_duration = value;
ExtrapolateTimes();
ExtrapolateTimes(nameof(Duration));
}
}

Expand Down

0 comments on commit 2971e35

Please sign in to comment.