Skip to content

Commit

Permalink
COnvert ToDo statuses from enum to class #318
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian Stockbower committed Nov 8, 2017
1 parent a3c3025 commit cd782b5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 deletions.
17 changes: 17 additions & 0 deletions net-core/Ical.Net/Ical.Net.UnitTests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,22 @@ public void EventStatusAllCaps()
var eventStatus = calendar.Events.First().Status;
Assert.IsTrue(string.Equals(EventStatus.Confirmed, eventStatus, EventStatus.Comparison));
}

[Test]
public void ToDoStatusAllCaps()
{
var component = new Todo
{
Status = TodoStatus.NeedsAction
};

var c = new Calendar {Todos = {component}};
var serialized = SerializeToString(c);
Assert.IsTrue(serialized.Contains(TodoStatus.NeedsAction, TodoStatus.Comparison));

var calendar = UnserializeCalendar(serialized);
var status = calendar.Todos.First().Status;
Assert.IsTrue(string.Equals(TodoStatus.NeedsAction, status, TodoStatus.Comparison));
}
}
}
49 changes: 21 additions & 28 deletions net-core/Ical.Net/Ical.Net/Components/Todo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,34 @@ public virtual IList<string> Resources
/// <summary>
/// The status of the todo item.
/// </summary>
public virtual TodoStatus Status
public virtual string Status
{
get => Properties.Get<TodoStatus>("STATUS");
get => Properties.Get<string>(TodoStatus.Key);
set
{
if (Status != value)
if (string.Equals(Status, value, TodoStatus.Comparison))
{
// Automatically set/unset the Completed time, once the
// component is fully loaded (When deserializing, it shouldn't
// automatically set the completed time just because the
// status was changed).
if (IsLoaded)
{
Completed = value == TodoStatus.Completed
? CalDateTime.Now
: null;
}

Properties.Set("STATUS", value);
return;
}

// Automatically set/unset the Completed time, once the
// component is fully loaded (When deserializing, it shouldn't
// automatically set the completed time just because the
// status was changed).
if (IsLoaded)
{
Completed = string.Equals(value, TodoStatus.Completed, TodoStatus.Comparison)
? CalDateTime.Now
: null;
}

Properties.Set(TodoStatus.Key, value);
}
}

public Todo()
{
Name = Components.Todo;
Name = TodoStatus.Name;

_mEvaluator = new TodoEvaluator(this);
SetService(_mEvaluator);
Expand Down Expand Up @@ -169,23 +171,14 @@ public virtual bool IsCompleted(IDateTime currDt)
/// <param name="currDt">The date and time to test.</param>
/// <returns>True if the item is Active as of <paramref name="currDt"/>, False otherwise.</returns>
public virtual bool IsActive(IDateTime currDt)
{
if (DtStart == null)
{
return !IsCompleted(currDt) && !IsCancelled();
}
if (currDt.GreaterThanOrEqual(DtStart))
{
return !IsCompleted(currDt) && !IsCancelled();
}
return false;
}
=> (DtStart == null || currDt.GreaterThanOrEqual(DtStart))
&& (!IsCompleted(currDt) && !IsCancelled);

/// <summary>
/// Returns True if the todo item was cancelled.
/// </summary>
/// <returns>True if the todo was cancelled, False otherwise.</returns>
public virtual bool IsCancelled() => Status == TodoStatus.Cancelled;
public virtual bool IsCancelled => string.Equals(Status, TodoStatus.Cancelled, TodoStatus.Comparison);

protected override bool EvaluationIncludesReferenceDate => true;

Expand Down
24 changes: 16 additions & 8 deletions net-core/Ical.Net/Ical.Net/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Ical.Net
using System;

namespace Ical.Net
{
public enum AlarmAction
{
Expand All @@ -19,7 +21,6 @@ public static class Components
public const string Alarm = "VALARM";
public const string Calendar = "VCALENDAR";
public const string Freebusy = "VFREEBUSY";
public const string Todo = "VTODO";
public const string Journal = "VJOURNAL";
public const string Timezone = "VTIMEZONE";
public const string Daylight = "DAYLIGHT";
Expand Down Expand Up @@ -113,20 +114,27 @@ public class SerializationConstants
/// </summary>
public static class EventStatus
{
public const string Name = "VEVENT";
public static readonly StringComparison Comparison = StringComparison.Ordinal;

public const string Tentative = "TENTATIVE";
public const string Confirmed = "CONFIRMED";
public const string Cancelled = "CANCELLED";
}

/// <summary>
/// Status codes available to a <see cref="Components.Todo"/> item.
/// Status codes available to a <see cref="Todo"/> item.
/// </summary>
public enum TodoStatus
public static class TodoStatus
{
NeedsAction,
Completed,
InProcess,
Cancelled
public const string Name = "VTODO";
public const string Key = "STATUS";
public static readonly StringComparison Comparison = StringComparison.Ordinal;

public const string NeedsAction = "NEEDS-ACTION";
public const string Completed = "COMPLETED";
public const string InProcess = "IN-PROCESS";
public const string Cancelled = "CANCELLED";
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public virtual ICalendarComponent Build(string objectName)
case Components.Timezone:
c = new VTimeZone();
break;
case Components.Todo:
case TodoStatus.Name:
c = new Todo();
break;
case Components.Calendar:
Expand Down

0 comments on commit cd782b5

Please sign in to comment.