Mercurial > silverbladetech
view SilverlightExampleApp/SilverlightExampleApp/ViewModels/ClientSearchViewModel.cs @ 49:502f5f365649
Initial load for Silverlight Example Application
author | Steven Hollidge <stevenhollidge@hotmail.com> |
---|---|
date | Tue, 17 Apr 2012 17:57:52 +0100 |
parents | |
children | 3591c26bd63e |
line wrap: on
line source
using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Windows.Input; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Messaging; using SilverlightExampleApp.ClientDataServiceReference; using SilverlightExampleApp.Dialogs; using SilverlightExampleApp.Helpers; using SilverlightExampleApp.Messages; namespace SilverlightExampleApp.ViewModels { public class ClientSearchViewModel : ViewModelBase { #region Fields // services private readonly ClientDataServiceClient _service; private readonly IMessenger _messenger; private readonly INavigationMap _navigationMap; private readonly IDialogService _dialogService; // view model fields private const string SelectedClientPropertyName = "SelectedClient"; private Client _selectedClient; #endregion #region Constructors public ClientSearchViewModel() : this(new ClientDataServiceClient(), new DialogService(), Messenger.Default, new NavigationMap()) { } public ClientSearchViewModel(ClientDataServiceClient clientDataService, IDialogService dialogService, IMessenger messenger, INavigationMap navigationMap) { if (clientDataService == null) throw new ArgumentNullException("clientDataService"); if (dialogService == null) throw new ArgumentNullException("dialogService"); if (messenger == null) throw new ArgumentNullException("messenger"); if (navigationMap == null) throw new ArgumentNullException("navigationMap"); _service = clientDataService; _dialogService = dialogService; _messenger = messenger; _navigationMap = navigationMap; Initialize(); } #endregion #region Properties public ObservableCollection<Client> Clients { get; private set; } public Client SelectedClient { get { return _selectedClient; } set { if (_selectedClient == value) return; _selectedClient = value; RaisePropertyChanged(SelectedClientPropertyName); RequeryCommandEnabled(); } } #endregion #region Commands public ICommand AddCommand { get; private set; } public RelayCommand EditCommand { get; private set; } public RelayCommand DeleteCommand { get; private set; } public ICommand DeleteKeyCommand { get; private set; } public ICommand SearchCommand { get; private set; } private void RequeryCommandEnabled() { EditCommand.RaiseCanExecuteChanged(); DeleteCommand.RaiseCanExecuteChanged(); } #endregion #region Methods private void Initialize() { _service.GetAllCompleted += GetAllCompleted; _service.DeleteCompleted += DeleteCompleted; SearchCommand = new RelayCommand(SearchCommand_Execute); AddCommand = new RelayCommand(AddCommand_Execute); EditCommand = new RelayCommand(EditCommand_Execute, EnableWhenClientSelected); DeleteCommand = new RelayCommand(DeleteCommand_Execute, EnableWhenClientSelected); // Delete key functionality DeleteKeyCommand = new RelayCommand<KeyEventArgs>(DeleteKeyCommand_Execute); Clients = new ObservableCollection<Client>(); SelectedClient = null; } private void LoadData() { _messenger.Send(new StatusBarMessage("Loading data...")); _service.GetAllAsync(); } private void GetAllCompleted(object sender, GetAllCompletedEventArgs e) { string message = null; if (e.Cancelled) { message = "Cancelled"; } if (e.Error != null) { _dialogService.ShowDialog("Exception", e.Error.Message, false, null); } else { Clients.Clear(); foreach (Client client in e.Result) { Clients.Add(client); } } _messenger.Send(new StatusBarMessage(message)); } private void DeleteCompleted(object sender, AsyncCompletedEventArgs e) { string message = null; if (e.Cancelled) { message = "Cancelled"; } if (e.Error != null) { _dialogService.ShowDialog("Exception", e.Error.Message, false, null); } else { Clients.Remove(SelectedClient); SelectedClient = null; } _messenger.Send(new StatusBarMessage(message)); } private bool EnableWhenClientSelected() { return SelectedClient != null ? true : false; } private void DeleteKeyCommand_Execute(KeyEventArgs e) { if (e.Key == Key.Delete && SelectedClient != null) DeleteCommand.Execute(null); } private void AddCommand_Execute() { Uri destination = _navigationMap.ResolveDestination(NavigationDestination.ClientAddEdit); var message = new NavigationMessage {NavigateTo = destination}; _messenger.Send(message); } private void EditCommand_Execute() { Uri destination = _navigationMap.ResolveDestination(NavigationDestination.ClientAddEdit, SelectedClient.Id.ToString()); var message = new NavigationMessage {NavigateTo = destination}; _messenger.Send(message); } private void DeleteCommand_Execute() { _messenger.Send(new StatusBarMessage("Deleting item...")); // Swap for DialogService.ShowMessage _dialogService.ShowDialog("Confirmation", "Are you sure you want to delete this item?", true, DeleteClient); } private void DeleteClient(bool response) { if (response) _service.DeleteAsync(SelectedClient); } private void SearchCommand_Execute() { LoadData(); } #endregion } }