forked from ical-org/ical.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodo.cs
More file actions
211 lines (189 loc) · 6.8 KB
/
Todo.cs
File metadata and controls
211 lines (189 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using Ical.Net.DataTypes;
using Ical.Net.Evaluation;
namespace Ical.Net.CalendarComponents
{
/// <summary>
/// A class that represents an RFC 5545 VTODO component.
/// </summary>
[DebuggerDisplay("{Summary} - {Status}")]
public class Todo : RecurringComponent, IAlarmContainer
{
private readonly TodoEvaluator _mEvaluator;
/// <summary>
/// The date/time the todo was completed.
/// </summary>
public IDateTime Completed
{
get => Properties.Get<IDateTime>("COMPLETED");
set => Properties.Set("COMPLETED", value);
}
/// <summary>
/// The start date/time of the todo item.
/// </summary>
public override IDateTime DtStart
{
get => base.DtStart;
set
{
base.DtStart = value;
ExtrapolateTimes(2);
}
}
/// <summary>
/// The due date of the todo item.
/// </summary>
public IDateTime Due
{
get => Properties.Get<IDateTime>("DUE");
set
{
Properties.Set("DUE", value);
ExtrapolateTimes(0);
}
}
/// <summary>
/// The duration of the todo item.
/// </summary>
// NOTE: Duration is not supported by all systems,
// (i.e. iPhone) and cannot co-exist with Due.
// RFC 5545 states:
//
// ; either 'due' or 'duration' may appear in
// ; a 'todoprop', but 'due' and 'duration'
// ; MUST NOT occur in the same 'todoprop'
//
// Therefore, Duration is not serialized, as Due
// should always be extrapolated from the duration.
public TimeSpan Duration
{
get => Properties.Get<TimeSpan>("DURATION");
set
{
Properties.Set("DURATION", value);
ExtrapolateTimes(1);
}
}
public GeographicLocation GeographicLocation
{
get => Properties.Get<GeographicLocation>("GEO");
set => Properties.Set("GEO", value);
}
public string Location
{
get => Properties.Get<string>("LOCATION");
set => Properties.Set("LOCATION", value);
}
public int PercentComplete
{
get => Properties.Get<int>("PERCENT-COMPLETE");
set => Properties.Set("PERCENT-COMPLETE", value);
}
public IList<string> Resources
{
get => Properties.GetMany<string>("RESOURCES");
set => Properties.Set("RESOURCES", value ?? new List<string>());
}
/// <summary>
/// The status of the todo item.
/// </summary>
public string Status
{
get => Properties.Get<string>(TodoStatus.Key);
set
{
if (string.Equals(Status, value, TodoStatus.Comparison))
{
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 = TodoStatus.Name;
_mEvaluator = new TodoEvaluator(this);
SetService(_mEvaluator);
}
/// <summary>
/// Use this method to determine if a todo item has been completed.
/// This takes into account recurrence items and the previous date
/// of completion, if any.
/// <note>
/// This method evaluates the recurrence pattern for this TODO
/// as necessary to ensure all relevant information is taken
/// into account to give the most accurate result possible.
/// </note>
/// </summary>
/// <returns>True if the todo item has been completed</returns>
public bool IsCompleted(IDateTime currDt)
{
if (Status == TodoStatus.Completed)
{
if (Completed == null || Completed.GreaterThan(currDt))
{
return true;
}
// Evaluate to the previous occurrence.
_mEvaluator.EvaluateToPreviousOccurrence(Completed, currDt);
return _mEvaluator.Periods.All(p => !p.StartTime.GreaterThan(Completed) || !currDt.GreaterThanOrEqual(p.StartTime));
}
return false;
}
/// <summary>
/// Returns 'True' if the todo item is Active as of <paramref name="currDt"/>.
/// An item is Active if it requires action of some sort.
/// </summary>
/// <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 bool IsActive(IDateTime currDt)
=> (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 bool IsCancelled => string.Equals(Status, TodoStatus.Cancelled, TodoStatus.Comparison);
protected override bool EvaluationIncludesReferenceDate => true;
protected override void OnDeserializing(StreamingContext context)
{
//ToDo: a necessary evil, for now
base.OnDeserializing(context);
}
private void ExtrapolateTimes(int source)
{
/*
* Source values, a fix introduced to prevent StackOverflow exceptions from occuring.
* 0 = Due
* 1 = Duration
* 2 = DtStart
*/
if (Due == null && DtStart != null && Duration != default && source != 0)
{
Due = DtStart.Add(Duration);
}
else if (Duration == default && DtStart != null && Due != null && source != 1)
{
Duration = Due.Subtract(DtStart);
}
else if (DtStart == null && Duration != default && Due != null && source != 2)
{
DtStart = Due.Subtract(Duration);
}
}
}
}