MainViewModel.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Collections.ObjectModel;
  2. using System.Linq;
  3. using System.Windows.Controls;
  4. using Microsoft.Toolkit.Mvvm.Input;
  5. using MvvmHelpers;
  6. using MvvmHelpers.Commands;
  7. using MyMoviesWPF.MVVM.View.Pages;
  8. using MyMoviesWPF.MVVM.ViewModel.Core;
  9. namespace MyMoviesWPF.MVVM.ViewModel
  10. {
  11. public class MainViewModel : BaseViewModel
  12. {
  13. private string str;
  14. ObservableCollection<User> user = new ObservableCollection<User>(Service.db.Users.ToList());
  15. private RelayCommand _openCart;
  16. public Page _currentPage;
  17. public Page CurrentPage
  18. {
  19. get { return _currentPage; }
  20. set
  21. {
  22. if (_currentPage == value)
  23. return;
  24. _currentPage = value;
  25. OnPropertyChanged("CurrentPage");
  26. }
  27. }
  28. public string Cart
  29. {
  30. get
  31. {
  32. return str;
  33. }
  34. set
  35. {
  36. if (str == value)
  37. return;
  38. str = value;
  39. OnPropertyChanged("Cart");
  40. }
  41. }
  42. public MainViewModel()
  43. {
  44. Service.LoggedUser = user[0];
  45. UpdateCartStr();
  46. Service.MainViewModel = this;
  47. UpdatePage(Service.catalogPage);
  48. }
  49. public void UpdatePage(Page _page)
  50. {
  51. _currentPage = _page;
  52. OnPropertyChanged("CurrentPage");
  53. }
  54. public void UpdateCartStr()
  55. {
  56. if (Service.CartMoviesCollection.Count > 0)
  57. {
  58. str = "Корзина(" + (Service.CartMoviesCollection.Count) + ')';
  59. }
  60. else
  61. {
  62. str = "Корзина";
  63. }
  64. OnPropertyChanged("Cart");
  65. }
  66. public RelayCommand OpenCart
  67. {
  68. get
  69. {
  70. return _openCart
  71. ?? (_openCart = new RelayCommand(
  72. async () =>
  73. {
  74. CartPage page = new CartPage();
  75. UpdatePage(page);
  76. }));
  77. }
  78. }
  79. }
  80. }