DeckViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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.Linq;
  13. using System.Windows.Data;
  14. using System.ComponentModel;
  15. using VocabManager.Command;
  16. using VocabManager.Model;
  17. using System.Collections.Generic;
  18. using VocabManager.View;
  19. namespace VocabManager.ViewModel
  20. {
  21. public class DeckViewModel : ViewModelBase
  22. {
  23. bool _isloaded;
  24. AppListViewModel _appViewModel;
  25. private Deck _deck;
  26. internal Deck Deck
  27. {
  28. get
  29. {
  30. return _deck;
  31. }
  32. }
  33. internal ObservableCollection<ItemViewModel> AllItems { get; set; }
  34. private VocabManager.DataLayer.ItemRepository _itemRepository;
  35. CollectionViewSource _collectionViewSource;
  36. public bool IsNew
  37. {
  38. get;
  39. set;
  40. }
  41. public bool IsInEditMode
  42. {
  43. get;
  44. set;
  45. }
  46. public ICollectionView CardItems
  47. {
  48. get
  49. {
  50. return _collectionViewSource.View;
  51. }
  52. }
  53. public ItemViewModel CurrentItem
  54. {
  55. get
  56. {
  57. return CardItems.CurrentItem as ItemViewModel;
  58. }
  59. }
  60. public DeckViewModel(AppListViewModel appViewModel,VocabManager.DataLayer.ItemRepository itemRepository,Deck deck)
  61. {
  62. _appViewModel = appViewModel;
  63. _itemRepository = itemRepository;
  64. _deck = deck;
  65. }
  66. public void LoadItems()
  67. {
  68. if (_isloaded)
  69. return;
  70. AllItems = new ObservableCollection<ItemViewModel>
  71. (_itemRepository.GetAllItems(_deck.Id).Select(item => new ItemViewModel(item, this)));
  72. _collectionViewSource = new CollectionViewSource();
  73. _collectionViewSource.Source = AllItems;
  74. CardItems.CurrentChanged += new EventHandler(CardItems_CurrentChanged);
  75. _isloaded = true;
  76. }
  77. void CardItems_CurrentChanged(object sender, EventArgs e)
  78. {
  79. if (this.CurrentItem != null && !CurrentItem.IsFrontSide)
  80. CurrentItem.IsFrontSide = true;
  81. OnPropertyChanged("CurrentItem");
  82. }
  83. public string Name
  84. {
  85. get
  86. {
  87. return _deck.Name;
  88. }
  89. set
  90. {
  91. if (_deck.Name == value)
  92. return;
  93. _deck.Name = value;
  94. OnPropertyChanged("Name");
  95. }
  96. }
  97. public string Description
  98. {
  99. get
  100. {
  101. return _deck.Description;
  102. }
  103. set
  104. {
  105. if (_deck.Description == value)
  106. return;
  107. _deck.Description = value;
  108. OnPropertyChanged("Description");
  109. }
  110. }
  111. public int Id
  112. {
  113. get
  114. {
  115. return _deck.Id;
  116. }
  117. }
  118. public string CreationTime
  119. {
  120. get
  121. {
  122. return _deck.CreationTime.ToString();
  123. }
  124. }
  125. #region search
  126. RelayCommand _searchCommand;
  127. public ICommand SearchCommand
  128. {
  129. get
  130. {
  131. if (_searchCommand == null)
  132. {
  133. _searchCommand = new RelayCommand(
  134. param => this.Search(),
  135. param => this.CanSearch()
  136. );
  137. }
  138. return _searchCommand;
  139. }
  140. }
  141. private bool CanSearch()
  142. {
  143. throw new NotImplementedException();
  144. }
  145. private object Search()
  146. {
  147. throw new NotImplementedException();
  148. }
  149. #endregion
  150. public void SaveItem(ItemViewModel item)
  151. {
  152. if(item.IsNew)
  153. _itemRepository.AddNewItem(item.Item);
  154. else
  155. _itemRepository.UpdateItem(item.Item);
  156. }
  157. #region Remove
  158. RelayCommand _removeCurrentCommand;
  159. public ICommand RemoveCurrentCommand
  160. {
  161. get
  162. {
  163. if (_removeCurrentCommand == null)
  164. {
  165. _removeCurrentCommand = new RelayCommand(
  166. param => this.RemoveCurrent(),
  167. param => this.CanRemoveCurrent()
  168. );
  169. }
  170. return _removeCurrentCommand;
  171. }
  172. }
  173. private void RemoveCurrent()
  174. {
  175. RemoveItem(this.CurrentItem);
  176. }
  177. private bool CanRemoveCurrent()
  178. {
  179. return true;
  180. }
  181. public void RemoveItem(ItemViewModel item)
  182. {
  183. if (this.CardItems.CurrentItem == item)
  184. this.CardItems.MoveCurrentToNext();
  185. this.AllItems.Remove(item);
  186. if (!item.IsNew)
  187. _itemRepository.RemoveItem(item.Item);
  188. }
  189. #endregion
  190. #region AddNew
  191. RelayCommand _addNewCommand;
  192. public ICommand AddNewCommand
  193. {
  194. get
  195. {
  196. if (_addNewCommand == null)
  197. {
  198. _addNewCommand = new RelayCommand(
  199. param => this.AddNew(),
  200. param => this.CanAddNew()
  201. );
  202. }
  203. return _addNewCommand;
  204. }
  205. }
  206. private bool CanAddNew()
  207. {
  208. throw new NotImplementedException();
  209. }
  210. private void AddNew()
  211. {
  212. Item newCard = Item.CreateNew();
  213. ItemViewModel itemViewModel = new ItemViewModel(newCard, this);
  214. itemViewModel.IsInEditMode = itemViewModel.IsNew = true;
  215. AllItems.Add(itemViewModel);
  216. CardItems.MoveCurrentTo(itemViewModel);
  217. }
  218. #endregion
  219. #region Next
  220. RelayCommand _nextCommand;
  221. public ICommand NextCommand
  222. {
  223. get
  224. {
  225. if (_nextCommand == null)
  226. {
  227. _nextCommand = new RelayCommand(
  228. param => this.GoToNext(),
  229. param => this.CanNext()
  230. );
  231. }
  232. return _nextCommand;
  233. }
  234. }
  235. private bool CanNext()
  236. {
  237. return true;
  238. }
  239. private void GoToNext()
  240. {
  241. if (this.CardItems.OfType<ItemViewModel>().Count() > 0)
  242. {
  243. this.CardItems.MoveCurrentToNext();
  244. if (this.CurrentItem == null)
  245. this.CardItems.MoveCurrentToFirst();
  246. }
  247. }
  248. #endregion
  249. #region Perevious
  250. RelayCommand _previousCommand;
  251. public ICommand PereviousCommand
  252. {
  253. get
  254. {
  255. if (_previousCommand == null)
  256. {
  257. _previousCommand = new RelayCommand(
  258. param => this.GoToPrevious(),
  259. param => this.CanGoToPrevious()
  260. );
  261. }
  262. return _previousCommand;
  263. }
  264. }
  265. private bool CanGoToPrevious()
  266. {
  267. return true;
  268. }
  269. private void GoToPrevious()
  270. {
  271. if (this.CardItems.OfType<ItemViewModel>().Count() > 0)
  272. {
  273. this.CardItems.MoveCurrentToPrevious();
  274. if (this.CurrentItem == null)
  275. this.CardItems.MoveCurrentToLast();
  276. }
  277. }
  278. #endregion
  279. #region Cancel
  280. RelayCommand _cancelEditCommand;
  281. public ICommand CancelEditCommand
  282. {
  283. get
  284. {
  285. if (_cancelEditCommand == null)
  286. {
  287. _cancelEditCommand = new RelayCommand(
  288. param => this.CancelEdit(),
  289. param => this.CanCancelEdit()
  290. );
  291. }
  292. return _cancelEditCommand;
  293. }
  294. }
  295. private bool CanCancelEdit()
  296. {
  297. return IsInEditMode;
  298. }
  299. Dictionary<string, object> _tempPropertyValues;
  300. private void CancelEdit()
  301. {
  302. if (IsNew)
  303. _appViewModel.RemoveCurrentDeckCommand.Execute(null);
  304. else
  305. {
  306. this.Name = _tempPropertyValues["Name"] as string;
  307. this.Description = _tempPropertyValues["Description"] as string;
  308. }
  309. this.IsInEditMode = false;
  310. App.Instance.MainPage.LoadDeckListView();
  311. }
  312. #endregion
  313. #region Save
  314. RelayCommand _saveCommand;
  315. public ICommand SaveCommand
  316. {
  317. get
  318. {
  319. if (_saveCommand == null)
  320. {
  321. _saveCommand = new RelayCommand(
  322. param => this.Save(),
  323. param => this.CanSave()
  324. );
  325. }
  326. return _saveCommand;
  327. }
  328. }
  329. private void Save()
  330. {
  331. _appViewModel.SaveDeck(this);
  332. this.IsNew = this.IsInEditMode = false;
  333. App.Instance.MainPage.LoadDeckListView();
  334. }
  335. private bool CanSave()
  336. {
  337. return IsInEditMode;
  338. }
  339. #endregion
  340. #region Edit
  341. RelayCommand _editCommand;
  342. public ICommand EditCommand
  343. {
  344. get
  345. {
  346. if (_editCommand == null)
  347. {
  348. _editCommand = new RelayCommand(
  349. param => this.Edit(),
  350. param => this.CanEdit()
  351. );
  352. }
  353. return _editCommand;
  354. }
  355. }
  356. private bool CanEdit()
  357. {
  358. return true;
  359. }
  360. private void Edit()
  361. {
  362. IsInEditMode = true;
  363. BeginEdit();
  364. DeckViewEdit editView = new DeckViewEdit();
  365. editView.DataContext = this;
  366. App.Instance.MainPage.LoadView(editView);
  367. }
  368. void BeginEdit()
  369. {
  370. _tempPropertyValues = new Dictionary<string, object>();
  371. _tempPropertyValues.Add("Name", this.Name);
  372. _tempPropertyValues.Add("Description", this.Description);
  373. }
  374. #endregion
  375. #region Review
  376. RelayCommand _reviewCommand;
  377. public ICommand ReviewCommand
  378. {
  379. get
  380. {
  381. if (_reviewCommand == null)
  382. {
  383. _reviewCommand = new RelayCommand(
  384. param => this.Review(),
  385. param => this.CanReview()
  386. );
  387. }
  388. return _reviewCommand;
  389. }
  390. }
  391. private bool CanReview()
  392. {
  393. return true;
  394. }
  395. private void Review()
  396. {
  397. if (this.CardItems.OfType<ItemViewModel>().Count() != 0)
  398. this.CardItems.MoveCurrentToFirst();
  399. }
  400. #endregion
  401. }
  402. }