Exemple #1
0
        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));
        }
Exemple #2
0
 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;
 }
Exemple #3
0
        public ActionResult AddMovieWatchList(WatchedMoviesViewModel wmvm)
        {
            if (VarGlobal.SearchWatchList == null)
            {
                VarGlobal.SearchWatchList = "";
            }

            List <Film>     filmovi       = db.Film.ToList();
            List <Film>     resultFilmovi = new List <Film>();
            String          ajdi          = VarGlobal.GlobalUserID;
            List <Korisnik> korisnici     = db.Korisnik.ToList();

            VarGlobal.WatchList = "";
            VarGlobal.WishList  = "";
            int index = -1;

            for (int t = 0; t < korisnici.Count; t++)
            {
                if (korisnici[t].Id.ToString() == ajdi)
                {
                    index = t;
                    break;
                }
            }

            if (index < 0)
            {
                VarGlobal.WatchList = "Error, Please Log off then Log in ";
                VarGlobal.WishList  = "";
                return(RedirectToAction("MyMovies"));
            }


            bool exists = false;

            int indexFilma = -1;



            for (int t = 0; t < filmovi.Count; t++)
            {
                if (filmovi[t].NazivFilma.ToLower() == wmvm.NewMovie.ToLower())
                {
                    exists = true;
                    VarGlobal.WatchList = "";
                    VarGlobal.WishList  = "";
                    indexFilma          = t;
                }
            }



            if (!exists)
            {
                VarGlobal.WatchList = "Movie does not exist ";
                VarGlobal.WishList  = "";
                return(RedirectToAction("MyMovies"));
            }

            exists = false;
            String     watch = korisnici[index].ListaFilmova;
            List <int> films = StringToArray(watch);

            for (int t = 0; t < films.Count; t++)
            {
                for (int k = 0; k < filmovi.Count; k++)
                {
                    if (filmovi[k].Id == films[t])
                    {
                        if (filmovi[k].NazivFilma.ToLower() == wmvm.NewMovie.ToLower())
                        {
                            exists = true;
                            VarGlobal.WatchList = "Movie already exists in watchlist";
                            VarGlobal.WishList  = "";
                            return(RedirectToAction("MyMovies"));
                        }
                    }
                }
            }

            if (!exists)
            {
                VarGlobal.WatchList = "";
                VarGlobal.WishList  = "";
                films.Add(filmovi[indexFilma].Id);
                watch = ArrayToString(films);
                Korisnik usher = korisnici[index];
                usher.ListaFilmova = watch;
                db.Korisnik.AddOrUpdate(usher);
                db.SaveChanges();
            }
            return(RedirectToAction("MyMovies"));
        }
Exemple #4
0
        // 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;
        }