Last active
December 18, 2015 21:30
-
-
Save BrianJVarley/9715b57de5f23ae7ad61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 IDialogService dialogService = new ProductView(); | |
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)); | |
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 WindowLoadedCommand { get; set; } | |
private IDialogService _dialogService; | |
public CustomerOrdersViewModel(IDataService<OrderModel> orderDataService, IDialogService dialogservice) | |
{ | |
this._orderDataService = orderDataService; | |
this._dialogService = dialogservice; | |
Messenger.Default.Register<CustomerModel>(this, OnUpdateOrderMessageReceived); | |
Messenger.Default.Register<ProductModel>(this, OnUpdateProductMessageReceived); | |
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 OnUpdateProductMessageReceived(ProductModel product) | |
{ | |
SelectedProduct = product; | |
} | |
private void LoadCommands() | |
{ | |
SaveCommand = new CustomCommand((c) => SaveCustomerAsync(c).FireAndLogErrors(), CanSaveOrder); | |
EditCommand = new CustomCommand(EditOrder, CanModifyOrder); | |
WindowLoadedCommand = new CustomCommand(WindowLoaded, 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) | |
{ | |
ProductViewModel pvm = new ProductViewModel(_dialogService); | |
//can't access the Show(); with pvm. | |
Messenger.Default.Send<ProductModel>(SelectedProduct); | |
} | |
private bool CanLoadWindow(object obj) | |
{ | |
return true; | |
} | |
private void WindowLoaded(object obj) | |
{ | |
} | |
private void OnUpdateOrderMessageReceived(CustomerModel customer) | |
{ | |
SelectedCustomerEmail = customer.Email; | |
LoadCustomerOrdersAsync(SelectedCustomerEmail); | |
IsEnabled = true; | |
} | |
//private async Task InitializeAsync() | |
//{ | |
// var customer = await AwaitableMessages.NextMessageAsync<CustomerModel>(); | |
// SelectedCustomerEmail = customer.Email; | |
// await LoadCustomerOrdersAsync(SelectedCustomerEmail); | |
// 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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ProductView : Window, IDialogService | |
{ | |
private ProductViewModel ViewModel { get; set; } | |
private IDialogService _dialogService; | |
public ProductView() | |
{ | |
InitializeComponent(); | |
ViewModel = new ProductViewModel(_dialogService); | |
this.DataContext = ViewModel; | |
} | |
public void CloseDialog() | |
{ | |
this.Close(); | |
} | |
public new void ShowDialog() | |
{ | |
this.Show(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MongoDBApp.Common; | |
using MongoDBApp.Messages; | |
using MongoDBApp.Models; | |
using MongoDBApp.Services; | |
using MongoDBApp.Utility; | |
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 ProductViewModel | |
{ | |
public ICommand SaveCommand { get; set; } | |
public ICommand DeleteCommand { get; set; } | |
private IDialogService _dialogService; | |
public ProductViewModel(IDialogService dialogService) | |
{ | |
this._dialogService = dialogService; | |
Messenger.Default.Register<ProductModel>(this, OnSelectedProductReceived); | |
SaveCommand = new CustomCommand(SaveProduct, CanSaveProduct); | |
DeleteCommand = new CustomCommand(DeleteProduct, CanDeleteProduct); | |
} | |
#region properties | |
public bool IsEnabled { get; set; } | |
public ProductModel SelectedProduct { get; set; } | |
#endregion | |
#region methods | |
public void OnSelectedProductReceived(ProductModel product) | |
{ | |
SelectedProduct = product; | |
} | |
private bool CanDeleteProduct(object product) | |
{ | |
return true; | |
} | |
private void DeleteProduct(object product) | |
{ | |
SelectedProduct.ProductId = " "; | |
_dialogService.CloseDialog(); | |
Messenger.Default.Send<ProductModel>(SelectedProduct); | |
} | |
private bool CanSaveProduct(object product) | |
{ | |
if (SelectedProduct != null && SelectedProduct.Description != null && SelectedProduct.ProductId != null) | |
{ | |
return true; | |
} | |
return false; | |
} | |
private void SaveProduct(object product) | |
{ | |
_dialogService.CloseDialog(); | |
Messenger.Default.Send<ProductModel>(SelectedProduct); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment