|
using System;
using ZipMyCode.Properties;
using ZipMyCode.Service.Tasks;
using ZipMyCode.UI.Mvvm;
namespace ZipMyCode.UI.Task
{
/// <summary>
/// Class acting as ViewModel for a ITask.
/// </summary>
class TaskViewModel : ViewModelBase
{
private readonly ITask task;
public TaskViewModel(ITask task)
{
if (task == null) throw new ArgumentNullException("task");
this.task = task;
}
/// <summary>
/// Gets the name of the task.
/// </summary>
public string Name
{
get { return task.Name; }
}
/// <summary>
/// Gets or sets the status of the task.
/// </summary>
public TaskStatus Status
{
get { return Property(() => Status); }
set
{
Property(() => Status, value);
// Trigger depending properties
OnPropertyChanged(() => StatusText);
OnPropertyChanged(() => StatusIcon);
}
}
/// <summary>
/// Gets the status text.
/// </summary>
public string StatusText
{
get
{
switch (Status)
{
case TaskStatus.Idle:
return Resources.TaskViewModel_Idle;
case TaskStatus.Running:
return Resources.TaskViewModel_Running;
case TaskStatus.Finished:
return Resources.TaskViewModel_Successful;
case TaskStatus.FinishedWithError:
return Resources.TaskViewModel_Unsuccessful;
case TaskStatus.Skipped:
return Resources.TaskViewModel_Skipped;
default:
throw new InvalidOperationException("Unsupported status: " + Status);
}
}
}
/// <summary>
/// Gets the status icon.
/// </summary>
public Uri StatusIcon
{
get
{
switch (Status)
{
case TaskStatus.Idle:
return new Uri("pack://application:,,,/ZipMyCode;component/Resources/TaskIdle.png");
case TaskStatus.Running:
return new Uri("pack://application:,,,/ZipMyCode;component/Resources/TaskRunning.png");
case TaskStatus.Finished:
return new Uri("pack://application:,,,/ZipMyCode;component/Resources/TaskSuccessful.png");
case TaskStatus.FinishedWithError:
return new Uri("pack://application:,,,/ZipMyCode;component/Resources/TaskUnsuccessful.png");
case TaskStatus.Skipped:
return new Uri("pack://application:,,,/ZipMyCode;component/Resources/TaskSkipped.png");
default:
throw new InvalidOperationException("Unsupported status: " + Status);
}
}
}
/// <summary>
/// Runs the task.
/// </summary>
/// <returns>true if task was successful; otherwise false.</returns>
public bool Run()
{
// Set "task is running" status
Status = TaskStatus.Running;
// Set task outcome
Status = task.Run();
return Status == TaskStatus.Finished;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Got my first computer in the 90's and loved it even though it sounded like a coffeemaker.
Now getting paid for designing cool applications, and drinks the coffee instead of listening to it being made.