Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Created December 21, 2015 15:54
Show Gist options
  • Save BrianJVarley/b8daabadddcd10efbc45 to your computer and use it in GitHub Desktop.
Save BrianJVarley/b8daabadddcd10efbc45 to your computer and use it in GitHub Desktop.
using MongoDBApp.Common;
using MongoDBApp.DAL;
using MongoDBApp.Models;
using MongoDBApp.Services;
using MongoDBApp.Utility;
using MongoDBApp.Views;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MongoDBApp.ViewModels
{
[ImplementPropertyChanged]
public class ApplicationViewModel : ObservableObject
{
#region Fields
private ICommand _changePageCommand;
public ICommand LogOutCommand { get; set; }
private IPageViewModel _currentPageViewModel;
private List<IPageViewModel> _pageViewModels;
private static IDataService<CustomerModel> customerDataService = new CustomerDataService(CustomerRepository.Instance);
private static IDataService<Country> countryDataService = new CountryDataService(CountryRepository.Instance);
private static IDataService<OrderModel> orderDataService = new OrderDataService(OrderRepository.Instance);
private static IDataService<ProductModel> productDataService = new ProductDataService(ProductRepository.Instance);
private static IDialogService dialogService = new EditProductView();
private bool IsLoggedIn = true;
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationViewModel"/> class.
/// </summary>
public ApplicationViewModel()
{
// Add available pages
PageViewModels.Add(new CustomerDetailsViewModel(customerDataService, countryDataService));
PageViewModels.Add(new CustomerOrdersViewModel(orderDataService, dialogService, productDataService));
PageViewModels.Add(new OrderStatisticsViewModel());
// Set starting page
CurrentPageViewModel = PageViewModels[0];
LoadCommands();
Messenger.Default.Register<string>(this, OnLoggedInMessageReceived);
}
#region Properties / Commands
/// <summary>
/// Gets the change page command.
/// </summary>
/// <value>
/// The change page command.
/// </value>
public ICommand ChangePageCommand
{
get
{
if (_changePageCommand == null)
{
_changePageCommand = new RelayCommand(
p => ChangeViewModel((IPageViewModel)p),
p => p is IPageViewModel);
}
return _changePageCommand;
}
}
/// <summary>
/// Gets the page view models.
/// </summary>
/// <value>
/// The page view models.
/// </value>
public List<IPageViewModel> PageViewModels
{
get
{
if (_pageViewModels == null)
_pageViewModels = new List<IPageViewModel>();
return _pageViewModels;
}
}
/// <summary>
/// Gets or sets the current page view model.
/// </summary>
/// <value>
/// The current page view model.
/// </value>
public IPageViewModel CurrentPageViewModel
{
get
{
return _currentPageViewModel;
}
set
{
if (_currentPageViewModel != value)
{
_currentPageViewModel = value;
OnPropertyChanged("CurrentPageViewModel");
}
}
}
/// <summary>
/// Gets or sets the name of the user.
/// </summary>
/// <value>
/// The name of the user.
/// </value>
public string AuthenticatedUserName { get; set; }
#endregion
#region Methods
private void OnLoggedInMessageReceived(string username)
{
IsLoggedIn = true;
AuthenticatedUserName = username;
}
private void LogOutUser(object obj)
{
AuthenticatedUserName = "un_authorized_user";
Messenger.Default.Send<string>(AuthenticatedUserName);
IsLoggedIn = false;
}
private void LoadCommands()
{
LogOutCommand = new RelayCommand(LogOutUser);
}
private void ChangeViewModel(IPageViewModel viewModel)
{
if (!PageViewModels.Contains(viewModel))
PageViewModels.Add(viewModel);
CurrentPageViewModel = PageViewModels
.FirstOrDefault(vm => vm == viewModel);
}
#endregion
}
}
using MongoDBApp.Models;
using MongoDBApp.Services;
using MongoDBApp.Utility;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDBApp.Extensions;
using System.Windows.Input;
using MongoDBApp.Common;
using MongoDBApp.Messages;
namespace MongoDBApp.ViewModels
{
[ImplementPropertyChanged]
public class CustomerOrdersViewModel : IPageViewModel
{
private IDataService<OrderModel> _orderDataService;
public ICommand SaveCommand { get; set; }
public ICommand EditCommand { get; set; }
public ICommand AddCommand { get; set; }
public ICommand WindowLoadedCommand { get; set; }
private IDialogService _dialogService;
private IDataService<ProductModel> _productDataService;
public CustomerOrdersViewModel(IDataService<OrderModel> orderDataService, IDialogService dialogservice, IDataService<ProductModel> productDataService)
{
this._orderDataService = orderDataService;
this._productDataService = productDataService;
this._dialogService = dialogservice;
Messenger.Default.Register<CustomerModel>(this, OnUpdateOrderMessageReceived);
LoadCommands();
}
#region properties
public string SelectedCustomerEmail { get; set; }
public ObservableCollection<OrderModel> CustomerOrders { get; set; }
public OrderModel SelectedOrder { get; set; }
public ProductModel SelectedProduct { get; set; }
public Task Initialization { get; set; }
public bool IsEnabled { get; set; }
public string Name
{
get
{
return "Order Details";
}
}
#endregion
#region methods
private void LoadCommands()
{
SaveCommand = new CustomCommand((c) => SaveCustomerAsync(c).FireAndLogErrors(), CanSaveOrder);
EditCommand = new CustomCommand(EditOrder, CanModifyOrder);
AddCommand = new CustomCommand(AddProduct, CanAddproduct);
WindowLoadedCommand = new CustomCommand((c) => WindowLoadedAsync(c).FireAndLogErrors(), CanLoadWindow);
}
private bool CanSaveOrder(object obj)
{
if (SelectedOrder != null && SelectedOrder.Email != null && SelectedOrder.Date != null &&
SelectedOrder.Id != null)
{
return true;
}
return false;
}
private bool CanModifyOrder(object obj)
{
if (SelectedOrder != null && SelectedOrder.Email != null && SelectedOrder.Date != null &&
SelectedOrder.Id != null && SelectedProduct != null )
{
return true;
}
return false;
}
private void EditOrder(object obj)
{
EditProductViewModel epvm = new EditProductViewModel(_dialogService);
epvm.Present(epvm);
Messenger.Default.Send<ProductModel>(SelectedProduct);
}
private bool CanAddproduct(object obj)
{
return true;
}
private void AddProduct(object obj)
{
ProductsViewModel pvm = new ProductsViewModel(_dialogService, _productDataService);
pvm.Present(pvm);
}
private async Task WindowLoadedAsync(object obj)
{
await LoadCustomerOrdersAsync(SelectedCustomerEmail);
IsEnabled = true;
}
private bool CanLoadWindow(object obj)
{
return true;
}
private void OnUpdateOrderMessageReceived(CustomerModel customer)
{
SelectedCustomerEmail = customer.Email;
IsEnabled = true;
}
public async Task LoadCustomerOrdersAsync(string email)
{
var ordersResult = await _orderDataService.GetAllByEmailAsync(email);
CustomerOrders = ordersResult.ToObservableCollection();
}
private async Task SaveCustomerAsync(object customer)
{
if (SelectedOrder != null)
{
IsEnabled = true;
await Task.Run(() => _orderDataService.UpdateAsync(SelectedOrder));
IsEnabled = false;
}
return;
}
#endregion
}
}
using MongoDBApp.Services;
using MongoDBApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MongoDBApp.Views
{
/// <summary>
/// Interaction logic for ProductView.xaml
/// </summary>
public partial class EditProductView : Window, IDialogService
{
public EditProductView()
{
InitializeComponent();
this.DataContext = new EditProductViewModel(this);
}
public void CloseDialog()
{
if (this != null)
this.Visibility = Visibility.Collapsed;
}
public void ShowDialog(EditProductViewModel editProdVM)
{
this.DataContext = editProdVM;
this.Show();
}
private void Window_Closed(object sender, EventArgs e)
{
this.Visibility = Visibility.Collapsed;
}
public void ShowDialog(ProductsViewModel prodVM)
{
throw new NotImplementedException();
}
}
}
using MongoDBApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDBApp.Services
{
public interface IDialogService
{
void CloseDialog();
void ShowDialog(EditProductViewModel editProdVM);
void ShowDialog(ProductsViewModel prodVM);
}
}
using MongoDBApp.Models;
using MongoDBApp.Services;
using MongoDBApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MongoDBApp.Views
{
/// <summary>
/// Interaction logic for ProductsView.xaml
/// </summary>
public partial class ProductsView : Window, IDialogService
{
private static IDataService<ProductModel> productDataService;
public ProductsView()
{
InitializeComponent();
this.DataContext = new ProductsViewModel(this, productDataService);
}
public void CloseDialog()
{
if (this != null)
this.Visibility = Visibility.Collapsed;
}
public void ShowDialog(EditProductViewModel editProdVM)
{
throw new NotImplementedException();
}
public void ShowDialog(ProductsViewModel prodVM)
{
this.DataContext = prodVM;
this.Show();
}
private void Window_Closed(object sender, EventArgs e)
{
this.Visibility = Visibility.Collapsed;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment