123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- 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.Windows.Data;
- using System.ComponentModel;
- using System.Linq;
- using VocabManager.Command;
- using VocabManager.Model;
- using VocabManager.View;
- namespace VocabManager.ViewModel
- {
- public class AppListViewModel : ViewModelBase
- {
- VocabManager.DataLayer.DeckRepository _deckRepository;
- private VocabManager.DataLayer.ItemRepository _itemRepository;
- private ObservableCollection<DeckViewModel> AllDecks { get; set; }
-
- CollectionViewSource _collectionViewSource;
- public ICollectionView Decks
- {
- get
- {
- return _collectionViewSource.View;
- }
- }
- public AppListViewModel(VocabManager.DataLayer.DeckRepository deckRepository,
- VocabManager.DataLayer.ItemRepository itemRepository)
- {
- _itemRepository = itemRepository;
- _deckRepository = deckRepository;
- LoadItems();
- }
- private void LoadItems()
- {
- AllDecks = new ObservableCollection<DeckViewModel>(
- _deckRepository.GetDeckList().Select(deck=> new DeckViewModel(this,_itemRepository,deck)));
- _collectionViewSource = new CollectionViewSource();
- _collectionViewSource.Source = AllDecks;
- Decks.CurrentChanged += new EventHandler(Decks_CurrentChanged);
- }
- void Decks_CurrentChanged(object sender, EventArgs e)
- {
- OnPropertyChanged("CurrentItem");
- }
- public string ApplicationName
- {
- get
- {
- return "Flash Card";
- }
- }
- public DeckViewModel CurrentDeck
- {
- get
- {
- return Decks.CurrentItem as DeckViewModel;
- }
- }
- #region Remove
- RelayCommand _removeCurrentDeckCommand;
- public ICommand RemoveCurrentDeckCommand
- {
- get
- {
- if (_removeCurrentDeckCommand == null)
- {
- _removeCurrentDeckCommand = new RelayCommand(
- param => this.RemoveCurrentDeck(),
- param => this.CanRemoveCurrentDeck()
- );
- }
- return _removeCurrentDeckCommand;
- }
- }
- private void RemoveCurrentDeck()
- {
- RemoveDeck(CurrentDeck);
- }
- private bool CanRemoveCurrentDeck()
- {
- return true;
- }
- public void RemoveDeck(DeckViewModel deck)
- {
- if (this.Decks.CurrentItem == deck)
- this.Decks.MoveCurrentToNext();
- this.AllDecks.Remove(deck);
- if (!deck.IsNew)
- _deckRepository.RemoveDeck(deck.Deck);
- }
- #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()
- {
- Deck newDeck= Deck.CreateNew();
- DeckViewModel deckViewModel = new DeckViewModel(this,_itemRepository, newDeck);
- deckViewModel.IsNew = deckViewModel.IsInEditMode= true;
- AllDecks.Add(deckViewModel);
- Decks.MoveCurrentTo(deckViewModel);
- DeckViewEdit editView = new DeckViewEdit();
- editView.DataContext = deckViewModel;
- App.Instance.MainPage.LoadView(editView);
- }
- #endregion
- #region View
- RelayCommand _viewSelectedDeckCommand;
- public ICommand ViewSelectedDeckCommand
- {
- get
- {
- if (_viewSelectedDeckCommand == null)
- {
- _viewSelectedDeckCommand = new RelayCommand(
- param => this.ViewSelectedDeck(),
- param => this.CanViewSelectedDeckCommand()
- );
- }
- return _viewSelectedDeckCommand;
- }
- }
- private bool CanViewSelectedDeckCommand()
- {
- throw new NotImplementedException();
- }
- private void ViewSelectedDeck()
- {
- if(this.CurrentDeck != null)
- ViewDeck(this.CurrentDeck);
- }
- private void ViewDeck(DeckViewModel deckViewModel)
- {
- DeckView viewDeckView = new DeckView();
- deckViewModel.LoadItems();
-
- viewDeckView.DataContext = deckViewModel;
- App.Instance.MainPage.LoadView(viewDeckView);
- }
- #endregion
- internal void SaveDeck(DeckViewModel deck)
- {
- if (deck.IsNew)
- _deckRepository.AddNewDeck(deck.Deck);
- else
- _deckRepository.UpdateDeck(deck.Deck);
- }
-
- }
- }
|