123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- using System;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Windows.Data;
- using System.ComponentModel;
- using VocabManager.Command;
- using VocabManager.Model;
- using System.Collections.Generic;
- using VocabManager.View;
- namespace VocabManager.ViewModel
- {
- public class DeckViewModel : ViewModelBase
- {
- bool _isloaded;
- AppListViewModel _appViewModel;
- private Deck _deck;
- internal Deck Deck
- {
- get
- {
- return _deck;
- }
- }
-
- internal ObservableCollection<ItemViewModel> AllItems { get; set; }
- private VocabManager.DataLayer.ItemRepository _itemRepository;
- CollectionViewSource _collectionViewSource;
- public bool IsNew
- {
- get;
- set;
- }
- public bool IsInEditMode
- {
- get;
- set;
- }
- public ICollectionView CardItems
- {
- get
- {
- return _collectionViewSource.View;
- }
- }
- public ItemViewModel CurrentItem
- {
- get
- {
- return CardItems.CurrentItem as ItemViewModel;
- }
- }
- public DeckViewModel(AppListViewModel appViewModel,VocabManager.DataLayer.ItemRepository itemRepository,Deck deck)
- {
- _appViewModel = appViewModel;
- _itemRepository = itemRepository;
- _deck = deck;
-
- }
- public void LoadItems()
- {
- if (_isloaded)
- return;
- AllItems = new ObservableCollection<ItemViewModel>
- (_itemRepository.GetAllItems(_deck.Id).Select(item => new ItemViewModel(item, this)));
- _collectionViewSource = new CollectionViewSource();
- _collectionViewSource.Source = AllItems;
- CardItems.CurrentChanged += new EventHandler(CardItems_CurrentChanged);
- _isloaded = true;
- }
-
- void CardItems_CurrentChanged(object sender, EventArgs e)
- {
- if (this.CurrentItem != null && !CurrentItem.IsFrontSide)
- CurrentItem.IsFrontSide = true;
- OnPropertyChanged("CurrentItem");
- }
- public string Name
- {
- get
- {
- return _deck.Name;
- }
- set
- {
- if (_deck.Name == value)
- return;
- _deck.Name = value;
- OnPropertyChanged("Name");
- }
- }
- public string Description
- {
- get
- {
- return _deck.Description;
- }
- set
- {
- if (_deck.Description == value)
- return;
- _deck.Description = value;
- OnPropertyChanged("Description");
- }
- }
- public int Id
- {
- get
- {
- return _deck.Id;
- }
- }
- public string CreationTime
- {
- get
- {
- return _deck.CreationTime.ToString();
- }
- }
- #region search
- RelayCommand _searchCommand;
- public ICommand SearchCommand
- {
- get
- {
- if (_searchCommand == null)
- {
- _searchCommand = new RelayCommand(
- param => this.Search(),
- param => this.CanSearch()
- );
- }
- return _searchCommand;
- }
- }
- private bool CanSearch()
- {
- throw new NotImplementedException();
- }
- private object Search()
- {
- throw new NotImplementedException();
- }
- #endregion
- public void SaveItem(ItemViewModel item)
- {
- if(item.IsNew)
- _itemRepository.AddNewItem(item.Item);
- else
- _itemRepository.UpdateItem(item.Item);
- }
- #region Remove
- RelayCommand _removeCurrentCommand;
- public ICommand RemoveCurrentCommand
- {
- get
- {
- if (_removeCurrentCommand == null)
- {
- _removeCurrentCommand = new RelayCommand(
- param => this.RemoveCurrent(),
- param => this.CanRemoveCurrent()
- );
- }
- return _removeCurrentCommand;
- }
- }
- private void RemoveCurrent()
- {
- RemoveItem(this.CurrentItem);
- }
- private bool CanRemoveCurrent()
- {
- return true;
- }
- public void RemoveItem(ItemViewModel item)
- {
- if (this.CardItems.CurrentItem == item)
- this.CardItems.MoveCurrentToNext();
- this.AllItems.Remove(item);
- if (!item.IsNew)
- _itemRepository.RemoveItem(item.Item);
- }
- #endregion
- #region AddNew
- RelayCommand _addNewCommand;
- public ICommand AddNewCommand
- {
- get
- {
- if (_addNewCommand == null)
- {
- _addNewCommand = new RelayCommand(
- param => this.AddNew(),
- param => this.CanAddNew()
- );
- }
- return _addNewCommand;
- }
- }
- private bool CanAddNew()
- {
- throw new NotImplementedException();
- }
- private void AddNew()
- {
- Item newCard = Item.CreateNew();
- ItemViewModel itemViewModel = new ItemViewModel(newCard, this);
-
- itemViewModel.IsInEditMode = itemViewModel.IsNew = true;
- AllItems.Add(itemViewModel);
- CardItems.MoveCurrentTo(itemViewModel);
- }
- #endregion
- #region Next
- RelayCommand _nextCommand;
- public ICommand NextCommand
- {
- get
- {
- if (_nextCommand == null)
- {
- _nextCommand = new RelayCommand(
- param => this.GoToNext(),
- param => this.CanNext()
- );
- }
- return _nextCommand;
- }
- }
- private bool CanNext()
- {
- return true;
- }
- private void GoToNext()
- {
- if (this.CardItems.OfType<ItemViewModel>().Count() > 0)
- {
- this.CardItems.MoveCurrentToNext();
- if (this.CurrentItem == null)
- this.CardItems.MoveCurrentToFirst();
- }
- }
- #endregion
- #region Perevious
- RelayCommand _previousCommand;
- public ICommand PereviousCommand
- {
- get
- {
- if (_previousCommand == null)
- {
- _previousCommand = new RelayCommand(
- param => this.GoToPrevious(),
- param => this.CanGoToPrevious()
- );
- }
- return _previousCommand;
- }
- }
- private bool CanGoToPrevious()
- {
- return true;
-
- }
- private void GoToPrevious()
- {
- if (this.CardItems.OfType<ItemViewModel>().Count() > 0)
- {
- this.CardItems.MoveCurrentToPrevious();
- if (this.CurrentItem == null)
- this.CardItems.MoveCurrentToLast();
- }
-
- }
- #endregion
- #region Cancel
- RelayCommand _cancelEditCommand;
- public ICommand CancelEditCommand
- {
- get
- {
- if (_cancelEditCommand == null)
- {
- _cancelEditCommand = new RelayCommand(
- param => this.CancelEdit(),
- param => this.CanCancelEdit()
- );
- }
- return _cancelEditCommand;
- }
- }
- private bool CanCancelEdit()
- {
- return IsInEditMode;
- }
- Dictionary<string, object> _tempPropertyValues;
- private void CancelEdit()
- {
- if (IsNew)
- _appViewModel.RemoveCurrentDeckCommand.Execute(null);
- else
- {
-
- this.Name = _tempPropertyValues["Name"] as string;
- this.Description = _tempPropertyValues["Description"] as string;
- }
- this.IsInEditMode = false;
- App.Instance.MainPage.LoadDeckListView();
- }
- #endregion
- #region Save
- RelayCommand _saveCommand;
- public ICommand SaveCommand
- {
- get
- {
- if (_saveCommand == null)
- {
- _saveCommand = new RelayCommand(
- param => this.Save(),
- param => this.CanSave()
- );
- }
- return _saveCommand;
- }
- }
- private void Save()
- {
- _appViewModel.SaveDeck(this);
- this.IsNew = this.IsInEditMode = false;
- App.Instance.MainPage.LoadDeckListView();
- }
- private bool CanSave()
- {
- return IsInEditMode;
- }
- #endregion
- #region Edit
- RelayCommand _editCommand;
- public ICommand EditCommand
- {
- get
- {
- if (_editCommand == null)
- {
- _editCommand = new RelayCommand(
- param => this.Edit(),
- param => this.CanEdit()
- );
- }
- return _editCommand;
- }
- }
- private bool CanEdit()
- {
- return true;
- }
- private void Edit()
- {
- IsInEditMode = true;
- BeginEdit();
- DeckViewEdit editView = new DeckViewEdit();
- editView.DataContext = this;
- App.Instance.MainPage.LoadView(editView);
- }
- void BeginEdit()
- {
- _tempPropertyValues = new Dictionary<string, object>();
- _tempPropertyValues.Add("Name", this.Name);
- _tempPropertyValues.Add("Description", this.Description);
- }
- #endregion
- #region Review
- RelayCommand _reviewCommand;
- public ICommand ReviewCommand
- {
- get
- {
- if (_reviewCommand == null)
- {
- _reviewCommand = new RelayCommand(
- param => this.Review(),
- param => this.CanReview()
- );
- }
- return _reviewCommand;
- }
- }
- private bool CanReview()
- {
- return true;
- }
- private void Review()
- {
- if (this.CardItems.OfType<ItemViewModel>().Count() != 0)
- this.CardItems.MoveCurrentToFirst();
- }
- #endregion
- }
- }
|