Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reactive.Linq;
  6. using Walletico.DataServices;
  7. using Walletico.Models;
  8. using Walletico.Models.Base;
  9.  
  10. namespace Walletico.ViewModels
  11. {
  12. public class DetailListViewModel : FreshMvvm.FreshBasePageModel
  13. {
  14. private Transaction _transactionSelected;
  15. private Period _periodSelected;
  16. private List<Transaction> _transactions;
  17. private readonly IDataService _dataService;
  18. public DetailListViewModel()
  19. {
  20.  
  21. }
  22. public DetailListViewModel(IDataService dataService)
  23. {
  24. this._dataService = dataService;
  25. this.Transactions = this._dataService.GetAllPerMonthTransactions(1).ToList();
  26. this.Periods = this._dataService.GetAllMonths().ToList();
  27.  
  28. Observable.FromEventPattern<PropertyChangedEventArgs>(this, nameof(PropertyChanged))
  29. .Where(x => x.EventArgs.PropertyName == nameof(this.PeriodSelected) || x.EventArgs.PropertyName == nameof(this.TransactionSelected))
  30. .Select(
  31. _ =>
  32. {
  33. if (_.EventArgs.PropertyName == nameof(this.PeriodSelected))
  34. this.ChangeSelectedStatus(this.Periods);
  35. else
  36. this.ChangeSelectedStatus(this.Transactions);
  37. return _.EventArgs.PropertyName == nameof(this.PeriodSelected);
  38. }).Subscribe((isPeriod) => { if (isPeriod) this.PeriodSelected.IsSelected = true; else this.TransactionSelected.IsSelected = true; });
  39. }
  40.  
  41. #region Properties
  42. public List<Transaction> Transactions { get => _transactions ?? (_transactions = new List<Transaction>()); set => _transactions = value; }
  43.  
  44.  
  45. public List<Period> Periods { get; set; }
  46.  
  47. public Period PeriodSelected
  48. {
  49. get => _periodSelected;
  50. set
  51. {
  52. _periodSelected = value;
  53. this.RaisePropertyChanged();
  54. }
  55. }
  56.  
  57. protected void ChangeSelectedStatus(IEnumerable<ISelectable> list)
  58. {
  59. foreach (ISelectable selectable in list.Where(x => x.IsSelected))
  60. {
  61. selectable.IsSelected = false;
  62. }
  63. }
  64.  
  65. public Transaction TransactionSelected
  66. {
  67. get => _transactionSelected;
  68. set
  69. {
  70. _transactionSelected = value;
  71. this.RaisePropertyChanged();
  72. }
  73. }
  74.  
  75. public decimal Income => this.Transactions.Where(t => t.TransType == 0).Sum(t => t.Amount);
  76. public decimal Outcome => this.Transactions.Where(t => t.TransType == 1).Sum(t => t.Amount);
  77.  
  78. public decimal Current => this.Income - this.Outcome;
  79. #endregion
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement