using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows.Input; using Sample.Silverlight.WCF.Infrastructure; using Sample.Silverlight.WCF.Infrastructure.Actions; using Sample.Silverlight.WCF.Infrastructure.Services; using Sample.Silverlight.WCF.Web.Services; namespace Sample.Silverlight.WCF.Views { public class TaskManagementViewModel : ViewModelBase { const string address = "http://localhost:2210/Services/TaskService.svc"; ServiceCallBuilder<ITaskService> build; Task selected; string description; public TaskManagementViewModel() : this(null) {} public TaskManagementViewModel(ITaskService instance) { InitializeServices(instance); InitializeProperties(); InitializeCommands(); } void InitializeServices(ITaskService instance) { build = new ServiceCallBuilder<ITaskService>(instance, address); } void InitializeProperties() { Tasks = new ObservableCollection<Task>(); } void InitializeCommands() { var cmd = new ViewModelCommandBuilder(this); CreateCommand = cmd.For(()=> Create()); DeleteCommand = cmd.For(()=> Delete()); MarkCompleteCommand = cmd.For(()=> MarkComplete()); } public ICommand CreateCommand { get; private set; } public ICommand MarkCompleteCommand { get; private set; } public ICommand DeleteCommand { get; private set; } public ObservableCollection<Task> Tasks { get; private set; } public Task Selected { get { return selected; } set { selected = value; NotifyOfPropertyChange(() => Selected); NotifyOfPropertyChange(() => CanDelete); NotifyOfPropertyChange(() => CanMarkComplete); } } public string Description { get { return description; } set { description = value; NotifyOfPropertyChange(() => Description); NotifyOfPropertyChange(() => CanCreate); } } public IEnumerable<IAction> Activate() { var action = build.Query(service => service.GetAll()); yield return action; foreach (Task each in action.Result) { Tasks.Add(each); } Selected = Tasks[0]; } public IEnumerable<IAction> Create() { var action = build.Query(service => service.Create(Description)); yield return action; Task newTask = action.Result; Tasks.Insert(0, newTask); Selected = newTask; Description = ""; } public bool CanCreate { get { return !string.IsNullOrEmpty(Description); } } public IEnumerable<IAction> MarkComplete() { Task task = Selected; var command = build.Query(service => service.MarkComplete(task.Id)); yield return command; Task updatedTask = command.Result; Replace(task, updatedTask); Selected = updatedTask; } public bool CanMarkComplete { get { return Selected != null && !Selected.IsCompleted; } } void Replace(Task prevTask, Task newTask) { var position = Tasks.IndexOf(prevTask); Tasks.RemoveAt(position); Tasks.Insert(position, newTask); } public IEnumerable<IAction> Delete() { var action = build.Command(service => service.Delete(Selected.Id)); yield return action; Tasks.Remove(Selected); if (Tasks.Count > 0) Selected = Tasks[0]; } public bool CanDelete { get { return Selected != null; } } } }
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.
This article, along with any associated source code and files, is licensed under The MIT License