From 09a09d4af45cc74a4668b260d4999333d4ec955e Mon Sep 17 00:00:00 2001 From: Andre Beging Date: Tue, 12 Sep 2017 00:36:50 +0200 Subject: [PATCH] Added Edit Transaction --- DebtMgr.csproj | 8 + View/Dialogs/AddTransactionView.xaml.cs | 7 + View/Dialogs/EditTransactionDialogView.xaml | 49 ++++ .../Dialogs/EditTransactionDialogView.xaml.cs | 72 ++++++ View/MainView.xaml | 1 + .../Dialogs/EditTransactionDialogViewModel.cs | 243 ++++++++++++++++++ ViewModel/MainViewModel.cs | 30 ++- ViewModel/ViewModelLocator.cs | 2 + 8 files changed, 411 insertions(+), 1 deletion(-) create mode 100644 View/Dialogs/EditTransactionDialogView.xaml create mode 100644 View/Dialogs/EditTransactionDialogView.xaml.cs create mode 100644 ViewModel/Dialogs/EditTransactionDialogViewModel.cs diff --git a/DebtMgr.csproj b/DebtMgr.csproj index e789219..3fea663 100644 --- a/DebtMgr.csproj +++ b/DebtMgr.csproj @@ -105,6 +105,7 @@ + @@ -114,6 +115,9 @@ DatabaseSelectorDialogView.xaml + + EditTransactionDialogView.xaml + NewPersonDialogView.xaml @@ -132,6 +136,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile diff --git a/View/Dialogs/AddTransactionView.xaml.cs b/View/Dialogs/AddTransactionView.xaml.cs index 0b85d3a..fb923a3 100644 --- a/View/Dialogs/AddTransactionView.xaml.cs +++ b/View/Dialogs/AddTransactionView.xaml.cs @@ -27,6 +27,8 @@ namespace DebtMgr.View.Dialogs DataContext = App.Locator.AddTransactionView; } + #region TextBox_OnKeyUp() + //////////////////////////////////////////////////////////////////////////////////////////////////// /// Event handler. Called by TextBox for on key up events. /// @@ -41,6 +43,9 @@ namespace DebtMgr.View.Dialogs App.Locator.AddTransactionView.AddTransactionButtonClickCommand.Execute(null); } + #endregion + #region Window_OnKeyUp() + //////////////////////////////////////////////////////////////////////////////////////////////////// /// Event handler. Called by Window for on key up events. /// @@ -54,5 +59,7 @@ namespace DebtMgr.View.Dialogs if (e.Key.Equals(Key.Escape)) Close(); } + + #endregion } } diff --git a/View/Dialogs/EditTransactionDialogView.xaml b/View/Dialogs/EditTransactionDialogView.xaml new file mode 100644 index 0000000..e1d5ef4 --- /dev/null +++ b/View/Dialogs/EditTransactionDialogView.xaml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/View/Dialogs/EditTransactionDialogView.xaml.cs b/View/Dialogs/EditTransactionDialogView.xaml.cs new file mode 100644 index 0000000..a0041f9 --- /dev/null +++ b/View/Dialogs/EditTransactionDialogView.xaml.cs @@ -0,0 +1,72 @@ +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; +using DebtMgr.Extensions; + +namespace DebtMgr.View.Dialogs +{ + /// + /// Interaktionslogik für EditTransactionDialogView.xaml + /// + public partial class EditTransactionDialogView : Window + { + public EditTransactionDialogView(Guid transactionId) + { + InitializeComponent(); + this.CenterOnParent(); + + App.Locator.EditTransactionDialogView.RequestClose += (s, e) => Close(); + App.Locator.EditTransactionDialogView.ClearView(); + App.Locator.EditTransactionDialogView.TransactionId = transactionId; + App.Locator.EditTransactionDialogView.LoadTransaction(); + + DataContext = App.Locator.EditTransactionDialogView; + } + + #region Window_KeyUp() + + //////////////////////////////////////////////////////////////////////////////////////////////////// + /// Event handler. Called by Window for key up events. + /// + /// Andre Beging, 12.09.2017. + /// + /// Source of the event. + /// Key event information. + //////////////////////////////////////////////////////////////////////////////////////////////////// + private void Window_KeyUp(object sender, KeyEventArgs e) + { + if (e.Key.Equals(Key.Escape)) + Close(); + } + + #endregion + + #region TextBox_OnKeyUp() + + //////////////////////////////////////////////////////////////////////////////////////////////////// + /// Event handler. Called by TextBox for on key up events. + /// + /// Andre Beging, 12.09.2017. + /// + /// Source of the event. + /// Key event information. + //////////////////////////////////////////////////////////////////////////////////////////////////// + private void TextBox_OnKeyUp(object sender, KeyEventArgs e) + { + if (e.Key.Equals(Key.Enter) || e.Key.Equals(Key.Return)) + App.Locator.EditTransactionDialogView.SaveTransactionButtonClickCommand.Execute(null); + } + + #endregion + } +} diff --git a/View/MainView.xaml b/View/MainView.xaml index 94cf160..4593369 100644 --- a/View/MainView.xaml +++ b/View/MainView.xaml @@ -120,6 +120,7 @@ + diff --git a/ViewModel/Dialogs/EditTransactionDialogViewModel.cs b/ViewModel/Dialogs/EditTransactionDialogViewModel.cs new file mode 100644 index 0000000..07cfc85 --- /dev/null +++ b/ViewModel/Dialogs/EditTransactionDialogViewModel.cs @@ -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 + + /// + /// Privater Teil von + /// + private string _windowTitle; + + /// + /// Comment + /// + public string WindowTitle + { + get { return _windowTitle; } + + set + { + _windowTitle = value; + RaisePropertyChanged(() => WindowTitle); + } + } + + #endregion + #region AmountTextBoxText (string) Property + + /// + /// Privater Teil von + /// + private string _amountTextBoxText; + + /// + /// Comment + /// + public string AmountTextBoxText + { + get { return _amountTextBoxText; } + + set + { + _amountTextBoxText = value; + RaisePropertyChanged(() => AmountTextBoxText); + SaveTransactionButtonClickCommand.RaiseCanExecuteChanged(); + } + } + + //////////////////////////////////////////////////////////////////////////////////////////////////// + /// Gets the amount text box text as number representation. + /// + /// The amount text box text number representation. + //////////////////////////////////////////////////////////////////////////////////////////////////// + public double AmountTextBoxTextNumberRepresentation + { + get + { + if (string.IsNullOrWhiteSpace(_amountTextBoxText)) return 0.0; + return double.Parse(_amountTextBoxText, CultureInfo.InvariantCulture); + } + } + + #endregion + #region PersonNameLabelContent (string) Property + + /// + /// Privater Teil von + /// + private string _personNameLabelContent; + + /// + /// Comment + /// + public string PersonNameLabelContent + { + get { return _personNameLabelContent; } + + set + { + _personNameLabelContent = value; + RaisePropertyChanged(() => PersonNameLabelContent); + } + } + + #endregion + #region DescriptionTextBoxText (string) Property + + /// + /// Privater Teil von + /// + private string _descriptionTextBoxText; + + /// + /// Comment + /// + public string DescriptionTextBoxText + { + get { return _descriptionTextBoxText; } + + set + { + _descriptionTextBoxText = value; + RaisePropertyChanged(() => DescriptionTextBoxText); + SaveTransactionButtonClickCommand.RaiseCanExecuteChanged(); + } + } + + #endregion + #region DatePickerSelectedDate (DateTime?) Property + + /// + /// Privater Teil von + /// + private DateTime? _datePickerSelectedDate; + + /// + /// Comment + /// + public DateTime? DatePickerSelectedDate + { + get { return _datePickerSelectedDate; } + + set + { + _datePickerSelectedDate = value; + RaisePropertyChanged(() => DatePickerSelectedDate); + SaveTransactionButtonClickCommand.RaiseCanExecuteChanged(); + } + } + + #endregion + + #region SaveTransactionButtonClickCommand Command + + /// + /// Private member backing variable for + /// + private RelayCommand _saveTransactionButtonClickCommand = null; + + /// + /// Comment + /// + 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(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() + + //////////////////////////////////////////////////////////////////////////////////////////////////// + /// Loads the transaction. + /// + /// Andre Beging, 12.09.2017. + //////////////////////////////////////////////////////////////////////////////////////////////////// + public void LoadTransaction() + { + if (TransactionId == Guid.Empty) return; + _transaction = App.Database.Get(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() + + //////////////////////////////////////////////////////////////////////////////////////////////////// + /// Clears the view. + /// + /// Andre Beging, 12.09.2017. + //////////////////////////////////////////////////////////////////////////////////////////////////// + public void ClearView() + { + PersonNameLabelContent = string.Empty; + AmountTextBoxText = string.Empty; + DescriptionTextBoxText = string.Empty; + DatePickerSelectedDate = null; + + TransactionId = Guid.Empty; + } + + #endregion + } +} \ No newline at end of file diff --git a/ViewModel/MainViewModel.cs b/ViewModel/MainViewModel.cs index ee8f0ed..956f1f7 100644 --- a/ViewModel/MainViewModel.cs +++ b/ViewModel/MainViewModel.cs @@ -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 + + /// + /// Private member backing variable for + /// + private RelayCommand _editTransactionContextMenuCommand = null; + + /// + /// Comment + /// + 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 /// diff --git a/ViewModel/ViewModelLocator.cs b/ViewModel/ViewModelLocator.cs index 8b465f6..bff0a36 100644 --- a/ViewModel/ViewModelLocator.cs +++ b/ViewModel/ViewModelLocator.cs @@ -46,12 +46,14 @@ namespace DebtMgr.ViewModel SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); + SimpleIoc.Default.Register(); } public MainViewModel MainView => ServiceLocator.Current.GetInstance(); public NewPersonDialogViewModel NewPersonDialogView => ServiceLocator.Current.GetInstance(); public AddTransactionViewModel AddTransactionView => ServiceLocator.Current.GetInstance(); public DatabaseSelectorDialogViewModel DatabaseSelectorDialogView => ServiceLocator.Current.GetInstance(); + public EditTransactionDialogViewModel EditTransactionDialogView => ServiceLocator.Current.GetInstance(); public static void Cleanup() {