Task Details can be Edited story
In looking at the task details can be edited story, I noticed that a lot of the features it has is the same as the new task screen; so instead of writing a separate screen, I'm going to refactor the NewTask screen to include editing capabilities. The first step is to provide a means to distinguish between adding and editing. I do this by storing a reference to the task.
private Task CurrentTask
{
get { return _currentTask; }
set { _currentTask = value; }
}
Only when editing does a current task exist. But how does it get populated? The current task gets assigned from the EditTask method. I use a method because a task only needs to be notified about editing once, instead of having that potential through the setter of the property:
public void EditTask(Task task)
{
this.CurrentTask = task;
//Set the property values to the controls on the screen
this.TaskName = task.Name;
this.Category = task.Category;
if (task.Attributes.Contains("IsReoccurring"))
this.IsReoccuring = task.GetAttributeValue<bool>("IsReoccurring");
if (task.Attributes.Contains("ReoccurrenceAmount"))
this.ReoccurrenceAmount = task.GetAttributeValue<int>("ReoccurrenceAmount");
if (task.Attributes.Contains("ReoccurrenceMeasurement"))
this.ReoccurrenceMeasurement = task.GetAttributeValue<string>("ReoccurrenceMeasurement");
if (task.Attributes.Contains("DueDate"))
this.DueDate = task.GetAttributeValue<DateTime>("DueDate");
if (task.Attributes.Contains("Description"))
this.Description = task.GetAttributeValue<string>("Description");
this.gbAction.Enabled = true;
}
In the OK button click, I know have to determine whether the task is being edited or added as such below:
Task task = null;
if (this.CurrentTask == null)
task = new Task(this.TaskName);
else
task = this.CurrentTask;
In addition, I have to move over the Edit Task form code, so the following code is the new mix of add/edit functionality. Note that gbAction is a group box holding the complete/defer options in the edit form.
//If editing a task, which means this feature is enabled
if (this.gbAction.Enabled)
{
if (this.rbComplete.Checked)
this.CurrentTask.Complete();
else if (this.rbDefer.Checked)
{
int amount = 0;
//If not a valid integer measurement, display an error message
if (!int.TryParse(this.txtDeferAmount.Text, out amount))
{
this.ShowErrorNotification("Please provide an amount to defer the task by", this.txtDeferAmount);
return;
}
else
//Defer the task by a specified number of days
this.CurrentTask.Defer(TimeSpan.FromDays(Convert.ToDouble(amount)));
}
}
else
{
//Add the task to the collection
MGR.Tasks.Add(task);
//Assign the new task as the current task; future changes will be updates
this.CurrentTask = task;
this.gbAction.Enabled = true;
}
This button now drives the completion and deferral through the object model. In addition, it also adds the task to the collection. A picture of the screen is attached.