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 } }