AppListViewModel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using System.Collections.ObjectModel;
  12. using System.Windows.Data;
  13. using System.ComponentModel;
  14. using System.Linq;
  15. using VocabManager.Command;
  16. using VocabManager.Model;
  17. using VocabManager.View;
  18. namespace VocabManager.ViewModel
  19. {
  20. public class AppListViewModel : ViewModelBase
  21. {
  22. VocabManager.DataLayer.DeckRepository _deckRepository;
  23. private VocabManager.DataLayer.ItemRepository _itemRepository;
  24. private ObservableCollection<DeckViewModel> AllDecks { get; set; }
  25. CollectionViewSource _collectionViewSource;
  26. public ICollectionView Decks
  27. {
  28. get
  29. {
  30. return _collectionViewSource.View;
  31. }
  32. }
  33. public AppListViewModel(VocabManager.DataLayer.DeckRepository deckRepository,
  34. VocabManager.DataLayer.ItemRepository itemRepository)
  35. {
  36. _itemRepository = itemRepository;
  37. _deckRepository = deckRepository;
  38. LoadItems();
  39. }
  40. private void LoadItems()
  41. {
  42. AllDecks = new ObservableCollection<DeckViewModel>(
  43. _deckRepository.GetDeckList().Select(deck=> new DeckViewModel(this,_itemRepository,deck)));
  44. _collectionViewSource = new CollectionViewSource();
  45. _collectionViewSource.Source = AllDecks;
  46. Decks.CurrentChanged += new EventHandler(Decks_CurrentChanged);
  47. }
  48. void Decks_CurrentChanged(object sender, EventArgs e)
  49. {
  50. OnPropertyChanged("CurrentItem");
  51. }
  52. public string ApplicationName
  53. {
  54. get
  55. {
  56. return "Flash Card";
  57. }
  58. }
  59. public DeckViewModel CurrentDeck
  60. {
  61. get
  62. {
  63. return Decks.CurrentItem as DeckViewModel;
  64. }
  65. }
  66. #region Remove
  67. RelayCommand _removeCurrentDeckCommand;
  68. public ICommand RemoveCurrentDeckCommand
  69. {
  70. get
  71. {
  72. if (_removeCurrentDeckCommand == null)
  73. {
  74. _removeCurrentDeckCommand = new RelayCommand(
  75. param => this.RemoveCurrentDeck(),
  76. param => this.CanRemoveCurrentDeck()
  77. );
  78. }
  79. return _removeCurrentDeckCommand;
  80. }
  81. }
  82. private void RemoveCurrentDeck()
  83. {
  84. RemoveDeck(CurrentDeck);
  85. }
  86. private bool CanRemoveCurrentDeck()
  87. {
  88. return true;
  89. }
  90. public void RemoveDeck(DeckViewModel deck)
  91. {
  92. if (this.Decks.CurrentItem == deck)
  93. this.Decks.MoveCurrentToNext();
  94. this.AllDecks.Remove(deck);
  95. if (!deck.IsNew)
  96. _deckRepository.RemoveDeck(deck.Deck);
  97. }
  98. #endregion
  99. #region AddNew
  100. RelayCommand _addNewCommand;
  101. public ICommand AddNewCommand
  102. {
  103. get
  104. {
  105. if (_addNewCommand == null)
  106. {
  107. _addNewCommand = new RelayCommand(
  108. param => this.AddNew(),
  109. param => this.CanAddNew()
  110. );
  111. }
  112. return _addNewCommand;
  113. }
  114. }
  115. private bool CanAddNew()
  116. {
  117. throw new NotImplementedException();
  118. }
  119. private void AddNew()
  120. {
  121. Deck newDeck= Deck.CreateNew();
  122. DeckViewModel deckViewModel = new DeckViewModel(this,_itemRepository, newDeck);
  123. deckViewModel.IsNew = deckViewModel.IsInEditMode= true;
  124. AllDecks.Add(deckViewModel);
  125. Decks.MoveCurrentTo(deckViewModel);
  126. DeckViewEdit editView = new DeckViewEdit();
  127. editView.DataContext = deckViewModel;
  128. App.Instance.MainPage.LoadView(editView);
  129. }
  130. #endregion
  131. #region View
  132. RelayCommand _viewSelectedDeckCommand;
  133. public ICommand ViewSelectedDeckCommand
  134. {
  135. get
  136. {
  137. if (_viewSelectedDeckCommand == null)
  138. {
  139. _viewSelectedDeckCommand = new RelayCommand(
  140. param => this.ViewSelectedDeck(),
  141. param => this.CanViewSelectedDeckCommand()
  142. );
  143. }
  144. return _viewSelectedDeckCommand;
  145. }
  146. }
  147. private bool CanViewSelectedDeckCommand()
  148. {
  149. throw new NotImplementedException();
  150. }
  151. private void ViewSelectedDeck()
  152. {
  153. if(this.CurrentDeck != null)
  154. ViewDeck(this.CurrentDeck);
  155. }
  156. private void ViewDeck(DeckViewModel deckViewModel)
  157. {
  158. DeckView viewDeckView = new DeckView();
  159. deckViewModel.LoadItems();
  160. viewDeckView.DataContext = deckViewModel;
  161. App.Instance.MainPage.LoadView(viewDeckView);
  162. }
  163. #endregion
  164. internal void SaveDeck(DeckViewModel deck)
  165. {
  166. if (deck.IsNew)
  167. _deckRepository.AddNewDeck(deck.Deck);
  168. else
  169. _deckRepository.UpdateDeck(deck.Deck);
  170. }
  171. }
  172. }