using DebtMgr.Model; using DebtMgr.View.Dialogs; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using SQLiteNetExtensions.Extensions; using System.Collections.Generic; using System.Linq; using System.Windows; using System; using System.Threading; using System.IO; using System.Diagnostics; namespace DebtMgr.ViewModel { public class MainViewModel : ViewModelBase { #region PersonListViewItemSource (List) Property /// /// Privater Teil von /// private List _personListViewItemSource; /// /// Comment /// public List PersonListViewItemSource { get { return _personListViewItemSource; } set { _personListViewItemSource = value; RaisePropertyChanged(() => PersonListViewItemSource); } } #endregion #region PersonListViewSelectedItem (Person) Property /// /// Privater Teil von /// private Person _personListViewSelectedItem; /// /// PersonListViewSelectedItem /// public Person PersonListViewSelectedItem { get { return _personListViewSelectedItem; } set { _personListViewSelectedItem = value; RaisePropertyChanged(() => PersonListViewSelectedItem); DeletePersonContextMenuCommand.RaiseCanExecuteChanged(); UpdateDetailView(); } } #endregion #region DetailViewHeaderLabelContent (string) Property /// /// Privater Teil von /// private string _detailViewHeaderLabelContent; /// /// Comment /// public string DetailViewHeaderLabelContent { get { return _detailViewHeaderLabelContent; } set { _detailViewHeaderLabelContent = value; RaisePropertyChanged(() => DetailViewHeaderLabelContent); } } #endregion #region DetailViewBalanceLabel (string) Property /// /// Privater Teil von /// private string _detailViewBalanceLabel; /// /// Comment /// public string DetailViewBalanceLabel { get { return _detailViewBalanceLabel; } set { _detailViewBalanceLabel = value; RaisePropertyChanged(() => DetailViewBalanceLabel); } } #endregion #region OverallBalanceLabel (string) Property /// /// Privater Teil von /// private string _overallBalanceLabel; /// /// Comment /// public string OverallBalanceLabel { get { return _overallBalanceLabel; } set { _overallBalanceLabel = value; RaisePropertyChanged(() => OverallBalanceLabel); } } #endregion #region TransactionHistoryListViewItemSource (List) Property /// /// Privater Teil von /// private List _transactionHistoryListViewItemSource; /// /// Comment /// public List TransactionHistoryListViewItemSource { get { return _transactionHistoryListViewItemSource; } set { _transactionHistoryListViewItemSource = value; RaisePropertyChanged(() => TransactionHistoryListViewItemSource); } } #endregion #region TransactionHistoryListViewSelectedItem (Transaction) Property /// /// Privater Teil von /// private Transaction _transactionHistoryListViewSelectedItem; /// /// Comment /// public Transaction TransactionHistoryListViewSelectedItem { get { return _transactionHistoryListViewSelectedItem; } set { _transactionHistoryListViewSelectedItem = value; RaisePropertyChanged(() => TransactionHistoryListViewSelectedItem); DeleteTransactionContextMenuCommand.RaiseCanExecuteChanged(); EditTransactionContextMenuCommand.RaiseCanExecuteChanged(); } } #endregion #region MenuNewPersonCommand Command /// /// Private member backing variable for /// private RelayCommand _menuNewPersonCommand = null; /// /// Comment /// 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 /// The sort person list view command. private RelayCommand _sortPersonListViewCommand = null; //////////////////////////////////////////////////////////////////////////////////////////////////// /// Gets the sort person list view command. /// /// The sort person list view command. //////////////////////////////////////////////////////////////////////////////////////////////////// public RelayCommand SortPersonListViewCommand { get { if (_sortPersonListViewCommand == null) _sortPersonListViewCommand = new RelayCommand(SortPersonListViewCommand_Execute); return _sortPersonListViewCommand; } } //////////////////////////////////////////////////////////////////////////////////////////////////// /// Sort person list view command execute. /// /// Andre Beging, 08.09.2017. /// /// Name of the column. //////////////////////////////////////////////////////////////////////////////////////////////////// private void SortPersonListViewCommand_Execute(string columnName) { if (string.IsNullOrWhiteSpace(columnName)) return; switch (columnName) { case "FirstName": PersonListViewItemSource = PersonListViewItemSource.OrderBy(x => x.FirstName).ToList(); break; case "LastName": PersonListViewItemSource = PersonListViewItemSource.OrderBy(x => x.LastName).ToList(); break; case "Total": PersonListViewItemSource = PersonListViewItemSource.OrderBy(x => x.Total).ToList(); break; } } #endregion #region AddChargeContextMenuCommand Command /// /// Private member backing variable for /// private RelayCommand _addChargeContextMenuCommand = null; /// /// Comment /// public RelayCommand AddChargeContextMenuCommand => _addChargeContextMenuCommand ?? (_addChargeContextMenuCommand = new RelayCommand(AddChargeContextMenuCommand_Execute)); private void AddChargeContextMenuCommand_Execute() { var window = new AddTransactionView(TransactionType.Charge, PersonListViewSelectedItem); window.ShowDialog(); } #endregion #region AddDepositContextMenuCommand Command /// /// Private member backing variable for /// private RelayCommand _addDepositContextMenuCommand = null; /// /// Comment /// public RelayCommand AddDepositContextMenuCommand => _addDepositContextMenuCommand ?? (_addDepositContextMenuCommand = new RelayCommand(AddDepositContextMenuCommand_Execute)); private void AddDepositContextMenuCommand_Execute() { var window = new AddTransactionView(TransactionType.Deposit, PersonListViewSelectedItem); window.ShowDialog(); } #endregion #region NewPersonContextMenuCommand Command /// /// Private member backing variable for /// private RelayCommand _newPersonContextMenuCommand = null; /// /// Comment /// public RelayCommand NewPersonContextMenuCommand => _newPersonContextMenuCommand ?? (_newPersonContextMenuCommand = new RelayCommand(NewPersonContextMenuCommand_Execute)); private void NewPersonContextMenuCommand_Execute() { var window = new NewPersonDialogView(); window.ShowDialog(); } #endregion #region DeletePersonContextMenuCommand Command /// /// Private member backing variable for /// private RelayCommand _deletePersonContextMenuCommand = null; /// /// Comment /// public RelayCommand DeletePersonContextMenuCommand => _deletePersonContextMenuCommand ?? (_deletePersonContextMenuCommand = new RelayCommand(DeletePersonContextMenuCommand_Execute, DeletePersonContextMenuCommand_CanExecute)); private bool DeletePersonContextMenuCommand_CanExecute() { if (PersonListViewSelectedItem != null) return true; return false; } private void DeletePersonContextMenuCommand_Execute() { if (PersonListViewSelectedItem == null) return; var result = MessageBox.Show( string.Format( "Are you sure to delete?\n\n{0} {1}", PersonListViewSelectedItem.FirstName, PersonListViewSelectedItem.LastName), "Delete Person", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { App.Database.Delete(PersonListViewSelectedItem, true); UpdatePersonsList(); } } #endregion #region DeleteTransactionContextMenuCommand Command /// /// Private member backing variable for /// private RelayCommand _deleteTransactionContextMenuCommand = null; /// /// Comment /// public RelayCommand DeleteTransactionContextMenuCommand => _deleteTransactionContextMenuCommand ?? (_deleteTransactionContextMenuCommand = new RelayCommand(DeleteTransactionContextMenuCommand_Execute, DeleteTransactionContextMenuCommand_CanExecute)); private bool DeleteTransactionContextMenuCommand_CanExecute() { if (TransactionHistoryListViewSelectedItem != null) return true; return false; } private void DeleteTransactionContextMenuCommand_Execute() { if (TransactionHistoryListViewSelectedItem == null) return; var result = MessageBox.Show( string.Format( "Are you sure to delete?\n\n{1} €\n{0}", TransactionHistoryListViewSelectedItem.Description, TransactionHistoryListViewSelectedItem.Amount), "Delete Transaction", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { App.Database.Delete(TransactionHistoryListViewSelectedItem.Id); UpdatePersonsList(); } } #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 /// /// Private member backing variable for /// private RelayCommand _PrivateCommandName = null; /// /// Comment /// public RelayCommand SwitchDatabaseMenuCommand => _PrivateCommandName ?? (_PrivateCommandName = new RelayCommand(SwitchDatabaseMenuCommand_Execute)); private void SwitchDatabaseMenuCommand_Execute() { Properties.Settings.Default["Database"] = string.Empty; Properties.Settings.Default.Save(); Thread.Sleep(100); System.Diagnostics.Process.Start(Application.ResourceAssembly.Location); Application.Current.Shutdown(); } #endregion #region OpenDatabaseLocationMenuCommand Command /// /// Private member backing variable for /// private RelayCommand _openDatabaseLocationMenuCommand = null; /// /// Comment /// public RelayCommand OpenDatabaseLocationMenuCommand => _openDatabaseLocationMenuCommand ?? (_openDatabaseLocationMenuCommand = new RelayCommand(OpenDatabaseLocationMenuCommand_Execute)); private void OpenDatabaseLocationMenuCommand_Execute() { if (File.Exists(Properties.Settings.Default.Database)) { Process.Start("explorer.exe", "/select, " + Properties.Settings.Default.Database); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// /// Initializes a new instance of the MainViewModel class. /// /// Andre Beging, 10.09.2017. //////////////////////////////////////////////////////////////////////////////////////////////////// public MainViewModel() { CheckDatabase(); UpdatePersonsList(); UpdateDetailView(); } #region UpdatePersonsList() //////////////////////////////////////////////////////////////////////////////////////////////////// /// Updates the persons list. /// /// Andre Beging, 08.09.2017. //////////////////////////////////////////////////////////////////////////////////////////////////// public void UpdatePersonsList() { // Remember selection var rememberSelection = PersonListViewSelectedItem?.Id; var personList = App.Database.GetAllWithChildren(); PersonListViewItemSource = personList; var overallBalance = personList.Sum(x => x.Total); OverallBalanceLabel = overallBalance.ToString(); // Restore selection if (rememberSelection != null && PersonListViewItemSource.Any(x => x.Id == rememberSelection)) PersonListViewSelectedItem = PersonListViewItemSource.First(x => x.Id == rememberSelection); UpdateDetailView(); } #endregion #region UpdateDetailView() //////////////////////////////////////////////////////////////////////////////////////////////////// /// Updates the detail view. /// /// Andre Beging, 10.09.2017. //////////////////////////////////////////////////////////////////////////////////////////////////// private void UpdateDetailView() { if (PersonListViewSelectedItem == null) { DetailViewHeaderLabelContent = string.Empty; DetailViewBalanceLabel = string.Empty; TransactionHistoryListViewItemSource = null; return; }; DetailViewHeaderLabelContent = string.Format("{0} {1}", PersonListViewSelectedItem.FirstName, PersonListViewSelectedItem.LastName); DetailViewBalanceLabel = PersonListViewSelectedItem.Total.ToString(); TransactionHistoryListViewItemSource = PersonListViewSelectedItem.Transactions.OrderByDescending(x => x.Time).ToList(); } #endregion #region CheckDatabase() //////////////////////////////////////////////////////////////////////////////////////////////////// /// Check database. /// /// Andre Beging, 10.09.2017. //////////////////////////////////////////////////////////////////////////////////////////////////// private void CheckDatabase() { var databasePath = Properties.Settings.Default.Database; if (string.IsNullOrWhiteSpace(databasePath)) { var window = new DatabaseSelectorDialogView(); var result = window.ShowDialog(); } // Check if provided file path is a valid database try { App.Database.Table(); } catch (Exception) { Properties.Settings.Default["Database"] = string.Empty; CheckDatabase(); } } #endregion } }