Added Edit Transaction
This commit is contained in:
243
ViewModel/Dialogs/EditTransactionDialogViewModel.cs
Normal file
243
ViewModel/Dialogs/EditTransactionDialogViewModel.cs
Normal file
@@ -0,0 +1,243 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using DebtMgr.Model;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using SQLiteNetExtensions.Extensions;
|
||||
|
||||
namespace DebtMgr.ViewModel.Dialogs
|
||||
{
|
||||
public class EditTransactionDialogViewModel : ViewModelBase
|
||||
{
|
||||
#region Public Properties
|
||||
|
||||
public event EventHandler RequestClose;
|
||||
|
||||
public Guid TransactionId { get; set; }
|
||||
|
||||
#endregion
|
||||
#region Fields
|
||||
|
||||
private Transaction _transaction;
|
||||
|
||||
#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 AmountTextBoxText (string) Property
|
||||
|
||||
/// <summary>
|
||||
/// Privater Teil von <see cref="AmountTextBoxText" />
|
||||
/// </summary>
|
||||
private string _amountTextBoxText;
|
||||
|
||||
/// <summary>
|
||||
/// Comment
|
||||
///</summary>
|
||||
public string AmountTextBoxText
|
||||
{
|
||||
get { return _amountTextBoxText; }
|
||||
|
||||
set
|
||||
{
|
||||
_amountTextBoxText = value;
|
||||
RaisePropertyChanged(() => AmountTextBoxText);
|
||||
SaveTransactionButtonClickCommand.RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// <summary> Gets the amount text box text as number representation. </summary>
|
||||
///
|
||||
/// <value> The amount text box text number representation. </value>
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
public double AmountTextBoxTextNumberRepresentation
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_amountTextBoxText)) return 0.0;
|
||||
return double.Parse(_amountTextBoxText, CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region PersonNameLabelContent (string) Property
|
||||
|
||||
/// <summary>
|
||||
/// Privater Teil von <see cref="PersonNameLabelContent" />
|
||||
/// </summary>
|
||||
private string _personNameLabelContent;
|
||||
|
||||
/// <summary>
|
||||
/// Comment
|
||||
///</summary>
|
||||
public string PersonNameLabelContent
|
||||
{
|
||||
get { return _personNameLabelContent; }
|
||||
|
||||
set
|
||||
{
|
||||
_personNameLabelContent = value;
|
||||
RaisePropertyChanged(() => PersonNameLabelContent);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region DescriptionTextBoxText (string) Property
|
||||
|
||||
/// <summary>
|
||||
/// Privater Teil von <see cref="DescriptionTextBoxText" />
|
||||
/// </summary>
|
||||
private string _descriptionTextBoxText;
|
||||
|
||||
/// <summary>
|
||||
/// Comment
|
||||
///</summary>
|
||||
public string DescriptionTextBoxText
|
||||
{
|
||||
get { return _descriptionTextBoxText; }
|
||||
|
||||
set
|
||||
{
|
||||
_descriptionTextBoxText = value;
|
||||
RaisePropertyChanged(() => DescriptionTextBoxText);
|
||||
SaveTransactionButtonClickCommand.RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region DatePickerSelectedDate (DateTime?) Property
|
||||
|
||||
/// <summary>
|
||||
/// Privater Teil von <see cref="DatePickerSelectedDate" />
|
||||
/// </summary>
|
||||
private DateTime? _datePickerSelectedDate;
|
||||
|
||||
/// <summary>
|
||||
/// Comment
|
||||
///</summary>
|
||||
public DateTime? DatePickerSelectedDate
|
||||
{
|
||||
get { return _datePickerSelectedDate; }
|
||||
|
||||
set
|
||||
{
|
||||
_datePickerSelectedDate = value;
|
||||
RaisePropertyChanged(() => DatePickerSelectedDate);
|
||||
SaveTransactionButtonClickCommand.RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SaveTransactionButtonClickCommand Command
|
||||
|
||||
/// <summary>
|
||||
/// Private member backing variable for <see cref="SaveTransactionButtonClickCommand" />
|
||||
/// </summary>
|
||||
private RelayCommand _saveTransactionButtonClickCommand = null;
|
||||
|
||||
/// <summary>
|
||||
/// Comment
|
||||
/// </summary>
|
||||
public RelayCommand SaveTransactionButtonClickCommand => _saveTransactionButtonClickCommand ?? (_saveTransactionButtonClickCommand = new RelayCommand(SaveTransactionButtonClickCommand_Execute, SaveTransactionButtonClickCommand_CanExecute));
|
||||
|
||||
private bool SaveTransactionButtonClickCommand_CanExecute()
|
||||
{
|
||||
if (_transaction == null) return false;
|
||||
if (AmountTextBoxTextNumberRepresentation < 0.01) return false;
|
||||
if (string.IsNullOrWhiteSpace(DescriptionTextBoxText)) return false;
|
||||
if (DatePickerSelectedDate == null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void SaveTransactionButtonClickCommand_Execute()
|
||||
{
|
||||
if (SaveTransactionButtonClickCommand_CanExecute())
|
||||
{
|
||||
var transaction = App.Database.Get<Transaction>(TransactionId);
|
||||
|
||||
if (DatePickerSelectedDate != null)
|
||||
{
|
||||
transaction.Amount = AmountTextBoxTextNumberRepresentation;
|
||||
transaction.Description = DescriptionTextBoxText;
|
||||
transaction.Time = DatePickerSelectedDate.Value;
|
||||
}
|
||||
|
||||
App.Database.InsertOrReplace(transaction);
|
||||
RequestClose?.Invoke(null, null);
|
||||
App.Locator.MainView.UpdatePersonsList();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region LoadTransaction()
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// <summary> Loads the transaction. </summary>
|
||||
///
|
||||
/// <remarks> Andre Beging, 12.09.2017. </remarks>
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
public void LoadTransaction()
|
||||
{
|
||||
if (TransactionId == Guid.Empty) return;
|
||||
_transaction = App.Database.Get<Transaction>(TransactionId);
|
||||
|
||||
if (_transaction == null) return;
|
||||
App.Database.GetChildren(_transaction);
|
||||
|
||||
PersonNameLabelContent = string.Format("{0} {1}", _transaction.Person.FirstName,
|
||||
_transaction.Person.LastName);
|
||||
AmountTextBoxText = _transaction.Amount.ToString(CultureInfo.InvariantCulture);
|
||||
DescriptionTextBoxText = _transaction.Description;
|
||||
DatePickerSelectedDate = _transaction.Time;
|
||||
|
||||
if (_transaction.Type == TransactionType.Charge)
|
||||
WindowTitle = "Edit Charge";
|
||||
else if (_transaction.Type == TransactionType.Deposit)
|
||||
WindowTitle = "Edit Deposit";
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region ClearView()
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// <summary> Clears the view. </summary>
|
||||
///
|
||||
/// <remarks> Andre Beging, 12.09.2017. </remarks>
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
public void ClearView()
|
||||
{
|
||||
PersonNameLabelContent = string.Empty;
|
||||
AmountTextBoxText = string.Empty;
|
||||
DescriptionTextBoxText = string.Empty;
|
||||
DatePickerSelectedDate = null;
|
||||
|
||||
TransactionId = Guid.Empty;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -174,6 +174,7 @@ namespace DebtMgr.ViewModel
|
||||
_transactionHistoryListViewSelectedItem = value;
|
||||
RaisePropertyChanged(() => TransactionHistoryListViewSelectedItem);
|
||||
DeleteTransactionContextMenuCommand.RaiseCanExecuteChanged();
|
||||
EditTransactionContextMenuCommand.RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -389,6 +390,34 @@ namespace DebtMgr.ViewModel
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region EditTransactionContextMenuCommand Command
|
||||
|
||||
/// <summary>
|
||||
/// Private member backing variable for <see cref="EditTransactionContextMenuCommand" />
|
||||
/// </summary>
|
||||
private RelayCommand _editTransactionContextMenuCommand = null;
|
||||
|
||||
/// <summary>
|
||||
/// Comment
|
||||
/// </summary>
|
||||
public RelayCommand EditTransactionContextMenuCommand => _editTransactionContextMenuCommand ?? (_editTransactionContextMenuCommand = new RelayCommand(EditTransactionContextMenuCommand_Execute, EditTransactionContextMenuCommand_CanExecute));
|
||||
|
||||
private bool EditTransactionContextMenuCommand_CanExecute()
|
||||
{
|
||||
if (TransactionHistoryListViewSelectedItem != null)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private void EditTransactionContextMenuCommand_Execute()
|
||||
{
|
||||
if (TransactionHistoryListViewSelectedItem == null) return;
|
||||
|
||||
var window = new EditTransactionDialogView(TransactionHistoryListViewSelectedItem.Id);
|
||||
window.ShowDialog();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SwitchDatabaseMenuCommand Command
|
||||
@@ -416,7 +445,6 @@ namespace DebtMgr.ViewModel
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region OpenDatabaseLocationMenuCommand Command
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -46,12 +46,14 @@ namespace DebtMgr.ViewModel
|
||||
SimpleIoc.Default.Register<NewPersonDialogViewModel>();
|
||||
SimpleIoc.Default.Register<AddTransactionViewModel>();
|
||||
SimpleIoc.Default.Register<DatabaseSelectorDialogViewModel>();
|
||||
SimpleIoc.Default.Register<EditTransactionDialogViewModel>();
|
||||
}
|
||||
|
||||
public MainViewModel MainView => ServiceLocator.Current.GetInstance<MainViewModel>();
|
||||
public NewPersonDialogViewModel NewPersonDialogView => ServiceLocator.Current.GetInstance<NewPersonDialogViewModel>();
|
||||
public AddTransactionViewModel AddTransactionView => ServiceLocator.Current.GetInstance<AddTransactionViewModel>();
|
||||
public DatabaseSelectorDialogViewModel DatabaseSelectorDialogView => ServiceLocator.Current.GetInstance<DatabaseSelectorDialogViewModel>();
|
||||
public EditTransactionDialogViewModel EditTransactionDialogView => ServiceLocator.Current.GetInstance<EditTransactionDialogViewModel>();
|
||||
|
||||
public static void Cleanup()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user