Added functionality to edit persons

This commit is contained in:
Andre Beging
2017-09-12 21:01:51 +02:00
parent 4a6c6ef544
commit ffbf5d7be9
8 changed files with 216 additions and 59 deletions

View File

@@ -16,4 +16,15 @@ namespace DebtMgr.Model
[Description("Charge")] [Description("Charge")]
Charge Charge
} }
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Values that represent person dialog modes. </summary>
///
/// <remarks> Andre Beging, 12.09.2017. </remarks>
////////////////////////////////////////////////////////////////////////////////////////////////////
public enum PersonDialogMode
{
New,
Edit
}
} }

View File

@@ -24,10 +24,6 @@ namespace DebtMgr.View.Dialogs
App.Locator.AddTransactionView.RequestClose += (s, e) => Close(); App.Locator.AddTransactionView.RequestClose += (s, e) => Close();
DataContext = App.Locator.AddTransactionView; DataContext = App.Locator.AddTransactionView;
//Uri iconUri = new Uri("pack://application:,,,/Content/money_red.ico", UriKind.RelativeOrAbsolute);
//this.Icon = BitmapFrame.Create(iconUri);
} }
#region TextBox_OnKeyUp() #region TextBox_OnKeyUp()

View File

@@ -7,7 +7,7 @@
mc:Ignorable="d" mc:Ignorable="d"
Name="NewPersonDialogViewWindow" Name="NewPersonDialogViewWindow"
Icon="../../Content/addperson.ico" Icon="../../Content/addperson.ico"
Title="New Person" Height="205" Width="300" Title="{Binding WindowTitle}" Height="205" Width="300"
KeyUp="Window_KeyUp"> KeyUp="Window_KeyUp">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
@@ -16,7 +16,7 @@
<RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Label Grid.Row="0" Content="New Person" FontWeight="Bold" FontSize="18"></Label> <Label Grid.Row="0" Content="{Binding ElementName=NewPersonDialogViewWindow, Path=Title}" FontWeight="Bold" FontSize="18"></Label>
<StackPanel Grid.Row="1" Orientation="Vertical" Margin="10 0"> <StackPanel Grid.Row="1" Orientation="Vertical" Margin="10 0">
<TextBox KeyUp="TextBox_OnKeyUp" Name="FirstNameTextBox" Text="{Binding FirstNameTextBoxText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="16" Margin="0 0 0 -5"></TextBox> <TextBox KeyUp="TextBox_OnKeyUp" Name="FirstNameTextBox" Text="{Binding FirstNameTextBoxText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="16" Margin="0 0 0 -5"></TextBox>
<Label Content="First name"></Label> <Label Content="First name"></Label>
@@ -30,7 +30,7 @@
<ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Button Grid.Column="1" Content="Create Person" FontSize="14" Padding="5" Margin="10 -10" Command="{Binding CreatePersonButtonClickCommand}"></Button> <Button Grid.Column="1" Content="{Binding SaveButtonText}" FontSize="14" Padding="5" Margin="10 -10" Command="{Binding CreatePersonButtonClickCommand}"></Button>
</Grid> </Grid>
</Grid> </Grid>
</Window> </Window>

View File

@@ -1,6 +1,8 @@
using System.Windows; using System;
using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using DebtMgr.Extensions; using DebtMgr.Extensions;
using DebtMgr.Model;
namespace DebtMgr.View.Dialogs namespace DebtMgr.View.Dialogs
{ {
@@ -9,17 +11,24 @@ namespace DebtMgr.View.Dialogs
/// </summary> /// </summary>
public partial class NewPersonDialogView : Window public partial class NewPersonDialogView : Window
{ {
public NewPersonDialogView() public NewPersonDialogView(PersonDialogMode mode, Guid personId = default(Guid))
{ {
InitializeComponent(); InitializeComponent();
this.CenterOnParent(); this.CenterOnParent();
App.Locator.NewPersonDialogView.ClearView();
App.Locator.NewPersonDialogView.DialogMode = mode;
App.Locator.NewPersonDialogView.EditPersonId = personId;
App.Locator.NewPersonDialogView.SetModeSpecifics();
App.Locator.NewPersonDialogView.RequestClose += (s, e) => Close(); App.Locator.NewPersonDialogView.RequestClose += (s, e) => Close();
DataContext = App.Locator.NewPersonDialogView; DataContext = App.Locator.NewPersonDialogView;
FirstNameTextBox.Focus(); FirstNameTextBox.Focus();
} }
#region TextBox_OnKeyUp()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Event handler. Called by TextBox for on key up events. </summary> /// <summary> Event handler. Called by TextBox for on key up events. </summary>
/// ///
@@ -34,6 +43,10 @@ namespace DebtMgr.View.Dialogs
App.Locator.NewPersonDialogView.CreatePersonButtonClickCommand.Execute(null); App.Locator.NewPersonDialogView.CreatePersonButtonClickCommand.Execute(null);
} }
#endregion
#region Window_KeyUp()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Event handler. Called by Window for key up events. </summary> /// <summary> Event handler. Called by Window for key up events. </summary>
/// ///
@@ -47,5 +60,7 @@ namespace DebtMgr.View.Dialogs
if (e.Key.Equals(Key.Escape)) if (e.Key.Equals(Key.Escape))
Close(); Close();
} }
#endregion
} }
} }

View File

@@ -57,7 +57,7 @@
<TextBlock Text="€" Foreground="{Binding OverallBalanceLabel, Converter={StaticResource AmountToColorConverter}}"></TextBlock> <TextBlock Text="€" Foreground="{Binding OverallBalanceLabel, Converter={StaticResource AmountToColorConverter}}"></TextBlock>
</TextBlock> </TextBlock>
<Button Grid.Column="1" Padding="5" ToolTip="New Person" Margin="5 0 5 5" Command="{Binding NewPersonContextMenuCommand}"> <Button Grid.Column="1" Padding="5" ToolTip="New Person" Margin="5 0 5 5" Command="{Binding NewPersonContextMenuCommand}">
<Image Source="../Content/addperson.ico" Width="30" /> <Image Source="../Content/addperson.ico" Height="30" />
</Button> </Button>
</Grid> </Grid>
@@ -82,10 +82,12 @@
</ListView.View> </ListView.View>
<ListBox.Resources> <ListBox.Resources>
<ContextMenu x:Key="PersonListViewContextMenu"> <ContextMenu x:Key="PersonListViewContextMenu">
<MenuItem Header="Add charge" Command="{Binding AddChargeContextMenuCommand}" /> <MenuItem Header="_Edit Person" Command="{Binding EditPersonContextMenuCommand}" />
<MenuItem Header="Add deposit" Command="{Binding AddDepositContextMenuCommand}" /> <MenuItem Header="Delete Person" Command="{Binding DeletePersonContextMenuCommand}" />
<MenuItem Header="New Person" Command="{Binding NewPersonContextMenuCommand}" /> <MenuItem Header="_New Person" Command="{Binding NewPersonContextMenuCommand}" />
<MenuItem Header="Delete Person" Command="{Binding DeletePersonContextMenuCommand}" CommandParameter="{Binding Path=SelectedItem}" /> <Separator></Separator>
<MenuItem Header="Add _Charge" Command="{Binding AddChargeContextMenuCommand}" />
<MenuItem Header="Add _Deposit" Command="{Binding AddDepositContextMenuCommand}" />
</ContextMenu> </ContextMenu>
</ListBox.Resources> </ListBox.Resources>
<ListBox.ContextMenu> <ListBox.ContextMenu>
@@ -122,10 +124,10 @@
<TextBlock Text="€" Foreground="{Binding DetailViewBalanceLabel, Converter={StaticResource AmountToColorConverter}}"></TextBlock> <TextBlock Text="€" Foreground="{Binding DetailViewBalanceLabel, Converter={StaticResource AmountToColorConverter}}"></TextBlock>
</TextBlock> </TextBlock>
<Button Grid.Column="1" Padding="5" ToolTip="Add Charge" Margin="5 0 0 5" Command="{Binding AddChargeContextMenuCommand}"> <Button Grid.Column="1" Padding="5" ToolTip="Add Charge" Margin="5 0 0 5" Command="{Binding AddChargeContextMenuCommand}">
<Image Source="../Content/money_red.ico" Width="30" /> <Image Source="../Content/money_red.ico" Height="30" />
</Button> </Button>
<Button Grid.Column="2" Padding="5" ToolTip="Add Deposit" Margin="5 0 5 5" Command="{Binding AddDepositContextMenuCommand}"> <Button Grid.Column="2" Padding="5" ToolTip="Add Deposit" Margin="5 0 5 5" Command="{Binding AddDepositContextMenuCommand}">
<Image Source="../Content/money_green.ico" Width="30" /> <Image Source="../Content/money_green.ico" Height="30" />
</Button> </Button>
</Grid> </Grid>
<ListView Name="TransactionHistoryListView" Grid.Row="2" ItemsSource="{Binding TransactionHistoryListViewItemSource}" SelectedItem="{Binding TransactionHistoryListViewSelectedItem}" HorizontalContentAlignment="Stretch"> <ListView Name="TransactionHistoryListView" Grid.Row="2" ItemsSource="{Binding TransactionHistoryListViewItemSource}" SelectedItem="{Binding TransactionHistoryListViewSelectedItem}" HorizontalContentAlignment="Stretch">
@@ -149,10 +151,11 @@
</ListView.View> </ListView.View>
<ListBox.Resources> <ListBox.Resources>
<ContextMenu x:Key="TransactionHistoryListViewContextMenu"> <ContextMenu x:Key="TransactionHistoryListViewContextMenu">
<MenuItem Header="Edit Transaction" Command="{Binding EditTransactionContextMenuCommand}" CommandParameter="{Binding Path=SelectedItem}" /> <MenuItem Header="_Edit Transaction" Command="{Binding EditTransactionContextMenuCommand}" CommandParameter="{Binding Path=SelectedItem}" />
<MenuItem Header="Add charge" Command="{Binding AddChargeContextMenuCommand}" />
<MenuItem Header="Add deposit" Command="{Binding AddDepositContextMenuCommand}" />
<MenuItem Header="Delete Transaction" Command="{Binding DeleteTransactionContextMenuCommand}" CommandParameter="{Binding Path=SelectedItem}" /> <MenuItem Header="Delete Transaction" Command="{Binding DeleteTransactionContextMenuCommand}" CommandParameter="{Binding Path=SelectedItem}" />
<Separator></Separator>
<MenuItem Header="Add _Charge" Command="{Binding AddChargeContextMenuCommand}" />
<MenuItem Header="Add _Deposit" Command="{Binding AddDepositContextMenuCommand}" />
</ContextMenu> </ContextMenu>
</ListBox.Resources> </ListBox.Resources>
<ListBox.ContextMenu> <ListBox.ContextMenu>

View File

@@ -12,9 +12,13 @@ namespace DebtMgr.ViewModel.Dialogs
{ {
public class DatabaseSelectorDialogViewModel : ViewModelBase public class DatabaseSelectorDialogViewModel : ViewModelBase
{ {
#region Public Properties
public event EventHandler RequestClose; public event EventHandler RequestClose;
public bool ProgramRequestedClose; public bool ProgramRequestedClose;
#endregion
#region SelectDatabasePathText (string) Property #region SelectDatabasePathText (string) Property
/// <summary> /// <summary>

View File

@@ -8,7 +8,59 @@ namespace DebtMgr.ViewModel.Dialogs
{ {
public class NewPersonDialogViewModel : ViewModelBase public class NewPersonDialogViewModel : ViewModelBase
{ {
#region Public Properties
public event EventHandler RequestClose; public event EventHandler RequestClose;
public PersonDialogMode DialogMode { get; set; }
public Guid EditPersonId { get; set; }
public Person EditPerson { get; set; }
#endregion
#region WindowTitle (string) Property
/// <summary>
/// Privater Teil von <see cref="WindowTitle" />
/// </summary>
private string _windowTitle;
/// <summary>
/// Comment
///</summary>
public string WindowTitle
{
get { return _windowTitle; }
set
{
_windowTitle = value;
RaisePropertyChanged(() => WindowTitle);
}
}
#endregion
#region SaveButtonText (string) Property
/// <summary>
/// Privater Teil von <see cref="SaveButtonText" />
/// </summary>
private string _saveButtonText;
/// <summary>
/// Comment
///</summary>
public string SaveButtonText
{
get { return _saveButtonText; }
set
{
_saveButtonText = value;
RaisePropertyChanged(() => SaveButtonText);
}
}
#endregion
#region FirstNameTextBoxText (string) Property #region FirstNameTextBoxText (string) Property
@@ -82,20 +134,32 @@ namespace DebtMgr.ViewModel.Dialogs
{ {
if (CreatePersonButtonClickCommand_CanExecute()) if (CreatePersonButtonClickCommand_CanExecute())
{ {
var newPerson = new Person bool success = false;
if (DialogMode == PersonDialogMode.New)
{ {
FirstName = FirstNameTextBoxText, var newPerson = new Person
LastName = LastNameTextBoxText {
}; FirstName = FirstNameTextBoxText,
LastName = LastNameTextBoxText
};
var resultId = App.Database.Insert(newPerson); if (App.Database.Insert(newPerson) == 1)
success = true;
}
if (resultId == 1) if (DialogMode == PersonDialogMode.Edit)
{
EditPerson.FirstName = FirstNameTextBoxText;
EditPerson.LastName = LastNameTextBoxText;
if(App.Database.InsertOrReplace(EditPerson) == 1)
success = true;
}
if (success)
{ {
App.Locator.MainView.UpdatePersonsList(); App.Locator.MainView.UpdatePersonsList();
App.Locator.AddTransactionView.UpdatesPersonsComboBox(); App.Locator.AddTransactionView.UpdatesPersonsComboBox();
RequestClose?.Invoke(null, null); RequestClose?.Invoke(null, null);
ClearView();
} }
else else
{ {
@@ -106,6 +170,38 @@ namespace DebtMgr.ViewModel.Dialogs
#endregion #endregion
#region SetModeSpecifics()
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Sets mode specifics. </summary>
///
/// <remarks> Andre Beging, 12.09.2017. </remarks>
////////////////////////////////////////////////////////////////////////////////////////////////////
public void SetModeSpecifics()
{
if (DialogMode != PersonDialogMode.Edit || EditPersonId == Guid.Empty)
{
ClearView();
return;
}
// edit mode
EditPerson = App.Database.Get<Person>(EditPersonId);
if (EditPerson == null)
{
ClearView();
return;
}
FirstNameTextBoxText = EditPerson.FirstName;
LastNameTextBoxText = EditPerson.LastName;
WindowTitle = "Edit Person";
SaveButtonText = "Save";
}
#endregion
#region ClearView() #region ClearView()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -113,10 +209,14 @@ namespace DebtMgr.ViewModel.Dialogs
/// ///
/// <remarks> Andre Beging, 08.09.2017. </remarks> /// <remarks> Andre Beging, 08.09.2017. </remarks>
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
private void ClearView() public void ClearView()
{ {
FirstNameTextBoxText = string.Empty; FirstNameTextBoxText = string.Empty;
LastNameTextBoxText = string.Empty; LastNameTextBoxText = string.Empty;
DialogMode = PersonDialogMode.New;
WindowTitle = "New Person";
SaveButtonText = "Create";
EditPersonId = Guid.Empty;
} }
#endregion #endregion

View File

@@ -56,6 +56,7 @@ namespace DebtMgr.ViewModel
_personListViewSelectedItem = value; _personListViewSelectedItem = value;
RaisePropertyChanged(() => PersonListViewSelectedItem); RaisePropertyChanged(() => PersonListViewSelectedItem);
DeletePersonContextMenuCommand.RaiseCanExecuteChanged(); DeletePersonContextMenuCommand.RaiseCanExecuteChanged();
EditPersonContextMenuCommand.RaiseCanExecuteChanged();
UpdateDetailView(); UpdateDetailView();
} }
@@ -180,30 +181,6 @@ namespace DebtMgr.ViewModel
#endregion #endregion
#region MenuNewPersonCommand Command
/// <summary>
/// Private member backing variable for <see cref="MenuNewPersonCommand" />
/// </summary>
private RelayCommand _menuNewPersonCommand = null;
/// <summary>
/// Comment
/// </summary>
public RelayCommand MenuNewPersonCommand => _menuNewPersonCommand ?? (_menuNewPersonCommand = new RelayCommand(MenuNewPersonCommand_Execute, MenuNewPersonCommand_CanExecute));
private bool MenuNewPersonCommand_CanExecute()
{
return true;
}
private void MenuNewPersonCommand_Execute()
{
var window = new NewPersonDialogView();
window.ShowDialog();
}
#endregion
#region SortPersonListViewCommand #region SortPersonListViewCommand
/// <summary> The sort person list view command. </summary> /// <summary> The sort person list view command. </summary>
@@ -262,7 +239,14 @@ namespace DebtMgr.ViewModel
/// <summary> /// <summary>
/// Comment /// Comment
/// </summary> /// </summary>
public RelayCommand AddChargeContextMenuCommand => _addChargeContextMenuCommand ?? (_addChargeContextMenuCommand = new RelayCommand(AddChargeContextMenuCommand_Execute)); public RelayCommand AddChargeContextMenuCommand => _addChargeContextMenuCommand ?? (_addChargeContextMenuCommand = new RelayCommand(AddChargeContextMenuCommand_Execute, AddChargeContextMenuCommand_CanExecute));
private bool AddChargeContextMenuCommand_CanExecute()
{
if (PersonListViewItemSource != null & PersonListViewItemSource.Count > 0)
return true;
return false;
}
private void AddChargeContextMenuCommand_Execute() private void AddChargeContextMenuCommand_Execute()
{ {
@@ -281,7 +265,14 @@ namespace DebtMgr.ViewModel
/// <summary> /// <summary>
/// Comment /// Comment
/// </summary> /// </summary>
public RelayCommand AddDepositContextMenuCommand => _addDepositContextMenuCommand ?? (_addDepositContextMenuCommand = new RelayCommand(AddDepositContextMenuCommand_Execute)); public RelayCommand AddDepositContextMenuCommand => _addDepositContextMenuCommand ?? (_addDepositContextMenuCommand = new RelayCommand(AddDepositContextMenuCommand_Execute, AddDepositContextMenuCommand_CanExecute));
private bool AddDepositContextMenuCommand_CanExecute()
{
if (PersonListViewItemSource != null & PersonListViewItemSource.Count > 0)
return true;
return false;
}
private void AddDepositContextMenuCommand_Execute() private void AddDepositContextMenuCommand_Execute()
{ {
@@ -304,10 +295,41 @@ namespace DebtMgr.ViewModel
private void NewPersonContextMenuCommand_Execute() private void NewPersonContextMenuCommand_Execute()
{ {
var window = new NewPersonDialogView(); var window = new NewPersonDialogView(PersonDialogMode.New);
window.ShowDialog(); window.ShowDialog();
} }
#endregion
#region EditPersonContextMenuCommand Command
/// <summary>
/// Private member backing variable for <see cref="EditPersonContextMenuCommand" />
/// </summary>
private RelayCommand _editPersonContextMenuCommand = null;
/// <summary>
/// Comment
/// </summary>
public RelayCommand EditPersonContextMenuCommand => _editPersonContextMenuCommand ?? (_editPersonContextMenuCommand = new RelayCommand(EditPersonContextMenuCommand_Execute, EditPersonContextMenuCommand_CanExecute));
private bool EditPersonContextMenuCommand_CanExecute()
{
if (PersonListViewSelectedItem != null)
return true;
return false;
}
private void EditPersonContextMenuCommand_Execute()
{
if (PersonListViewSelectedItem != null)
{
var window = new NewPersonDialogView(PersonDialogMode.Edit, PersonListViewSelectedItem.Id);
window.ShowDialog();
}
}
#endregion #endregion
#region DeletePersonContextMenuCommand Command #region DeletePersonContextMenuCommand Command
@@ -334,7 +356,7 @@ namespace DebtMgr.ViewModel
var result = MessageBox.Show( var result = MessageBox.Show(
string.Format( string.Format(
"Are you sure to delete?\n\n{0} {1}", "Are you sure to delete this person including all transactions?\n\n{0} {1}",
PersonListViewSelectedItem.FirstName, PersonListViewSelectedItem.FirstName,
PersonListViewSelectedItem.LastName), PersonListViewSelectedItem.LastName),
"Delete Person", "Delete Person",
@@ -376,10 +398,13 @@ namespace DebtMgr.ViewModel
var result = MessageBox.Show( var result = MessageBox.Show(
string.Format( string.Format(
"Are you sure to delete?\n\n{1} <20>\n{0}", "Are you sure to delete this {0}?\n\n{1} {2}\nAmount: {3} <20>\n{4}",
TransactionHistoryListViewSelectedItem.Description, TransactionHistoryListViewSelectedItem.Type,
TransactionHistoryListViewSelectedItem.Amount), TransactionHistoryListViewSelectedItem.Person.FirstName,
"Delete Transaction", TransactionHistoryListViewSelectedItem.Person.LastName,
TransactionHistoryListViewSelectedItem.Amount,
TransactionHistoryListViewSelectedItem.Description),
string.Format("Delete {0}", TransactionHistoryListViewSelectedItem.Type),
MessageBoxButton.YesNo, MessageBoxButton.YesNo,
MessageBoxImage.Question); MessageBoxImage.Question);
@@ -497,6 +522,9 @@ namespace DebtMgr.ViewModel
var overallBalance = personList.Sum(x => x.Total); var overallBalance = personList.Sum(x => x.Total);
OverallBalanceLabel = overallBalance.ToString(); OverallBalanceLabel = overallBalance.ToString();
AddChargeContextMenuCommand.RaiseCanExecuteChanged();
AddDepositContextMenuCommand.RaiseCanExecuteChanged();
// Restore selection // Restore selection
if (rememberSelection != null && PersonListViewItemSource.Any(x => x.Id == rememberSelection)) if (rememberSelection != null && PersonListViewItemSource.Any(x => x.Id == rememberSelection))