public MainViewModel(ReadingEntity readingModel) { // Publish an event with eventName = PubSubTest. Others can subscribe to said eventName, in order to catch when it is raised PubSub <object> .PublishEvent("PubSubTest", PubSubCheckedHandler); // ReadingModel contains a list of movie objects data fetched from the database this.readingModel = readingModel; // Initialize other viewmodels WatchedMoviesViewModel = new WatchedMoviesViewModel(readingModel.WatchedMovies); // Pass list of movie objects to the WatchedMovies NonWatchedMoviesViewModel = new NonWatchedMoviesViewModel(readingModel.NonWatchedMovies); // Pass list of movie objects to the NonWatchedMovies AddMovieViewModel = new AddMovieViewModel(this, WatchedMoviesViewModel, NonWatchedMoviesViewModel); // Pass reference for mainviewmodel and both datagrids, so when we add a new movie we know where to put it // Subscribe to the ViewModels' OnpropertyChanged event WatchedMoviesViewModel.PropertyChanged += MoviesViewModel_PropertyChanged; NonWatchedMoviesViewModel.PropertyChanged += MoviesViewModel_PropertyChanged; this.PropertyChanged += MainWindowViewModel_PropertyChanged; // Subscribe to MainViewModels OnPropertyChanged event to check for changes in MainViewModel // Register commands so we are able to execute specific buttons RegisterCommand(SaveCommand = new ActionCommand(Save, CanSave)); RegisterCommand(LoadCommand = new ActionCommand(Load, CanLoad)); RegisterCommand(TestCommand = new ActionCommand(Test, CanTest)); }
public void UpdateValues(ReadingEntity readingModel) { // Update values by putting loaded values from DB into movies collection, which then repopulates the datagrid since the collection changed NonWatchedMoviesViewModel.LoadValues(readingModel.NonWatchedMovies); WatchedMoviesViewModel.LoadValues(readingModel.WatchedMovies); // Deactivate Load button after values updated ChangesDetected = false; }
// Method for handling save button logic private void Save() { //* Xml Serilizer to write data to an existing txt file */ XmlSerializer x = new XmlSerializer(typeof(ReadingEntity)); if (!string.IsNullOrWhiteSpace(DBPath) && File.Exists(DBPath)) { using (TextWriter tw = new StreamWriter(DBPath)) { // Update reading model object with new values // Temp list to hold sorted objects according to their IsMovieSeen value var tempWatched = new List <MovieModel>(); var tempNonWatched = new List <MovieModel>(); // Sort Nonwatched movies and move objects to correct list foreach (var movie in NonWatchedMoviesViewModel.SaveValues()) { if (movie.IsMovieSeen) { tempWatched.Add(movie); } else { tempNonWatched.Add(movie); } } // Sort Watched movies and move objects to correct list foreach (var movie in WatchedMoviesViewModel.SaveValues()) { if (movie.IsMovieSeen) { tempWatched.Add(movie); } else { tempNonWatched.Add(movie); } } // Update reading model object and save it to the db file readingModel.NonWatchedMovies = tempNonWatched; readingModel.WatchedMovies = tempWatched; x.Serialize(tw, readingModel); } MessageBox.Show("Values saved"); } // Reloades the just saved values from the db. // TODO: Delete the affected values from their respective Observable collection, instead of overriding with values from db Load(); // Collaps add movie view if visible //if (AddMovieViewVisibility) // ExpandOrCollapsAddMovieView(); // Deactivate Load button. // Because changes are saved to the db file, the filewatcher notices the change and activates the load button. // Consider making some kind of validation to check for same content in db file as that in the datagrid CheckCanLoad = false; // Deactive Save button when values are saved ChangesDetected = false; }