Last active
December 16, 2015 17:38
-
-
Save BrianJVarley/b1daebc217b138ac6beb to your computer and use it in GitHub Desktop.
This file contains 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.Utility | |
{ | |
public class AwaitableMessages | |
{ | |
public static Task<T> NextMessageAsync<T>() | |
{ | |
var tcs = new TaskCompletionSource<T>(); | |
Messenger.Default.Register<T>(null, item => tcs.TrySetResult(item)); | |
return tcs.Task; | |
} | |
} | |
} |
This file contains 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<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; | |
_dialogService.CloseDetailDialog(); | |
} | |
private void LoadCommands() | |
{ | |
SaveCommand = new CustomCommand((c) => SaveCustomerAsync(c).FireAndLogErrors(), CanSaveOrder); | |
EditCommand = new CustomCommand(EditOrder, CanModifyOrder); | |
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) | |
{ | |
Messenger.Default.Send<ProductModel>(SelectedProduct); | |
_dialogService.ShowDetailDialog(); | |
} | |
private bool CanLoadWindow(object obj) | |
{ | |
return true; | |
} | |
private async Task WindowLoadedAsync(object obj) | |
{ | |
await InitializeAsync(); | |
this.Initialization = InitializeAsync(); | |
} | |
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 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
<UserControl x:Class="MongoDBApp.Views.CustomerOrdersView" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:boolean_converter="clr-namespace:MongoDBApp.Converters" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" | |
d:DesignHeight="300" | |
d:DesignWidth="300" | |
mc:Ignorable="d"> | |
<UserControl.Resources> | |
<boolean_converter:BooleanConverter x:Key="BooleanConverter" /> | |
</UserControl.Resources> | |
<Viewbox> | |
<xctk:BusyIndicator IsBusy="{Binding ButtonEnabled}"> | |
<Grid> | |
<Grid> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="*" /> | |
<RowDefinition Height="2*" /> | |
<RowDefinition Height="1*" /> | |
<RowDefinition Height="1*" /> | |
<RowDefinition Height="1*" /> | |
<RowDefinition Height="1*" /> | |
<RowDefinition Height="1*" /> | |
<RowDefinition Height="1*" /> | |
<RowDefinition Height="1*" /> | |
</Grid.RowDefinitions> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition Width="1*" /> | |
<ColumnDefinition Width="1*" /> | |
<ColumnDefinition Width="2*" /> | |
<ColumnDefinition Width="1*" /> | |
<ColumnDefinition Width="2*" /> | |
<ColumnDefinition Width="*" /> | |
</Grid.ColumnDefinitions> | |
<i:Interaction.Triggers> | |
<i:EventTrigger EventName="Loaded"> | |
<i:InvokeCommandAction Command="{Binding WindowLoadedCommand}" /> | |
</i:EventTrigger> | |
</i:Interaction.Triggers> | |
<DataGrid x:Name="customersgrid" | |
Grid.Row="0" | |
Grid.RowSpan="3" | |
Grid.Column="1" | |
Grid.ColumnSpan="4" | |
AutoGenerateColumns="False" | |
ItemsSource="{Binding CustomerOrders}" | |
SelectedItem="{Binding SelectedOrder}"> | |
<DataGrid.Columns> | |
<DataGridTextColumn Binding="{Binding Email}" Header="Email" /> | |
<DataGridTextColumn Binding="{Binding Date}" Header="Date" /> | |
<DataGridTextColumn Binding="{Binding Status}" Header="Shipping Status" /> | |
</DataGrid.Columns> | |
</DataGrid> | |
<Label Grid.Row="4" | |
Grid.Column="1" | |
HorizontalAlignment="Left" | |
VerticalAlignment="Top" | |
Content="Date:" /> | |
<TextBlock Grid.Row="4" | |
Grid.Column="2" | |
HorizontalAlignment="Left" | |
VerticalAlignment="Top" | |
Text="{Binding SelectedOrder.Date}" | |
TextWrapping="Wrap" /> | |
<Label Grid.Row="4" | |
Grid.Column="3" | |
HorizontalAlignment="Left" | |
VerticalAlignment="Top" | |
Content="Products:" /> | |
<ComboBox Grid.Row="4" | |
Grid.Column="4" | |
Grid.ColumnSpan="4" | |
Width="120" | |
HorizontalAlignment="Left" | |
VerticalAlignment="Top" | |
DisplayMemberPath="ProductId" | |
ItemsSource="{Binding SelectedOrder.Products}" | |
ScrollViewer.VerticalScrollBarVisibility="Visible" | |
SelectedItem="{Binding SelectedProduct}" /> | |
<Label Grid.Row="5" | |
Grid.Column="1" | |
HorizontalAlignment="Left" | |
VerticalAlignment="Top" | |
Content="Email:" /> | |
<TextBlock Grid.Row="5" | |
Grid.Column="2" | |
HorizontalAlignment="Left" | |
VerticalAlignment="Top" | |
Text="{Binding SelectedOrder.Email}" | |
TextWrapping="Wrap" /> | |
<RadioButton Grid.Row="5" | |
Grid.Column="3" | |
Grid.ColumnSpan="2" | |
HorizontalAlignment="Left" | |
VerticalAlignment="Top" | |
Content="Shipped" | |
IsChecked="{Binding SelectedOrder.Status, | |
Converter={StaticResource BooleanConverter}, | |
ConverterParameter='true', | |
Mode=TwoWay}" /> | |
<RadioButton Grid.Row="5" | |
Grid.Column="4" | |
Grid.ColumnSpan="2" | |
HorizontalAlignment="Left" | |
VerticalAlignment="Top" | |
Content="Processing" | |
IsChecked="{Binding SelectedOrder.Status, | |
Converter={StaticResource BooleanConverter}, | |
ConverterParameter='false', | |
Mode=TwoWay}" /> | |
<Button Grid.Row="7" | |
Grid.Column="3" | |
Grid.ColumnSpan="3" | |
Width="75" | |
HorizontalAlignment="Left" | |
VerticalAlignment="Top" | |
Command="{Binding EditCommand}" | |
Content="Edit" | |
IsEnabled="{Binding IsEnabled}" /> | |
<Button Grid.Row="7" | |
Grid.Column="3" | |
Grid.ColumnSpan="3" | |
Width="75" | |
HorizontalAlignment="Center" | |
VerticalAlignment="Top" | |
Command="{Binding SaveCommand}" | |
Content="Save" | |
IsEnabled="{Binding IsEnabled}" /> | |
</Grid> | |
</Grid> | |
</xctk:BusyIndicator> | |
</Viewbox> | |
</UserControl> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment