MainViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System.Collections.ObjectModel;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using Microsoft.Toolkit.Mvvm.Input;
  6. using MvvmHelpers;
  7. using MvvmHelpers.Commands;
  8. using MyMoviesWPF.MVVM.View;
  9. using MyMoviesWPF.MVVM.View.Pages;
  10. using MyMoviesWPF.MVVM.ViewModel.Core;
  11. namespace MyMoviesWPF.MVVM.ViewModel
  12. {
  13. public class MainViewModel : BaseViewModel
  14. {
  15. private string str;
  16. private RelayCommand _openLogInView;
  17. private Visibility _authVis;
  18. private Visibility _nameVis;
  19. private User _user;
  20. ObservableCollection<User> user = new ObservableCollection<User>(Service.db.Users.ToList());
  21. private RelayCommand _openCart;
  22. public Page _currentPage;
  23. public User User
  24. {
  25. get { return _user; }
  26. set
  27. {
  28. if (_user == value)
  29. return;
  30. _user = value;
  31. OnPropertyChanged("User");
  32. }
  33. }
  34. public RelayCommand OpenLogInView
  35. {
  36. get
  37. {
  38. return _openLogInView
  39. ?? (_openLogInView = new RelayCommand(
  40. async () =>
  41. {
  42. LogInView logInView = new LogInView();
  43. logInView.Show();
  44. }));
  45. }
  46. }
  47. public Page CurrentPage
  48. {
  49. get { return _currentPage; }
  50. set
  51. {
  52. if (_currentPage == value)
  53. return;
  54. _currentPage = value;
  55. OnPropertyChanged("CurrentPage");
  56. }
  57. }
  58. public string Cart
  59. {
  60. get
  61. {
  62. return str;
  63. }
  64. set
  65. {
  66. if (str == value)
  67. return;
  68. str = value;
  69. OnPropertyChanged("Cart");
  70. }
  71. }
  72. public Visibility AuthVis
  73. {
  74. get
  75. {
  76. return _authVis;
  77. }
  78. set
  79. {
  80. _authVis = value;
  81. OnPropertyChanged("AuthVis");
  82. }
  83. }
  84. public Visibility NameVis
  85. {
  86. get
  87. {
  88. return _nameVis;
  89. }
  90. set
  91. {
  92. _nameVis = value;
  93. OnPropertyChanged("NameVis");
  94. }
  95. }
  96. public MainViewModel()
  97. {
  98. //Service.LoggedUser = user[0];
  99. UpdateCartStr();
  100. Service.MainViewModel = this;
  101. UpdatePage(Service.catalogPage);
  102. SetVisibility(true);
  103. }
  104. public void SetVisibility(bool _switch)
  105. {
  106. if (_switch == false)
  107. {
  108. AuthVis = Visibility.Hidden;
  109. NameVis = Visibility.Visible;
  110. _user = Service.LoggedUser;
  111. }
  112. else if (_switch == true)
  113. {
  114. NameVis = Visibility.Hidden;
  115. AuthVis = Visibility.Visible;
  116. }
  117. OnPropertyChanged("AuthVis");
  118. OnPropertyChanged("NameVis");
  119. OnPropertyChanged("User");
  120. }
  121. public void UpdatePage(Page _page)
  122. {
  123. _currentPage = _page;
  124. OnPropertyChanged("CurrentPage");
  125. }
  126. public void UpdateCartStr()
  127. {
  128. if (Service.CartMoviesCollection.Count > 0)
  129. {
  130. str = "Корзина(" + (Service.CartMoviesCollection.Count) + ')';
  131. }
  132. else
  133. {
  134. str = "Корзина";
  135. }
  136. OnPropertyChanged("Cart");
  137. }
  138. public RelayCommand OpenCart
  139. {
  140. get
  141. {
  142. return _openCart
  143. ?? (_openCart = new RelayCommand(
  144. async () =>
  145. {
  146. CartPage page = new CartPage();
  147. UpdatePage(page);
  148. }));
  149. }
  150. }
  151. }
  152. }