Exemplo n.º 1
0
        /// <summary>
        /// Create all of the logic for this class
        /// </summary>
        private void Init()
        {
            // Get the selected item from the OrderViewModel
            CurrentItem = OrderPageViewModel.VM.SelectedItem.Copy();

            // Event for the current items ingredients list
            CurrentItem.Ingredients.CollectionChanged += (sender, e) => UpdatePrices();

            // Load the ingredients available for this item
            switch (CurrentItem.Type)
            {
            // Load all of the pizza ingredients
            case Enums.Pizza:
                /* Copy over all of the ingredients to this class's list so
                 * that display prices can be changed without effecting the source
                 */
                Ingredients = new ObservableCollection <OrderItemViewModel>();
                DatabaseHelpers.AllIngredients.ToList().Where(i => i.Type == Enums.PizzaIngredient).ToList().ForEach(i => { Ingredients.Add(i.Copy()); });
                break;

            // Should never go here
            default: throw new Exception("Invalid object");
            }

            // Check once if the pizza is standard or if it has been changed before
            UpdatePrices();

            #region Create the commands

            // Create the accept command
            AcceptCommand = new RelayCommand((o) => {
                // Return the new item and close this page
                OrderPageViewModel.VM.SelectedItem = CurrentItem.Copy();
                OrderPageViewModel.VM.SubPage      = OrderSubPages.None;
            });

            // Create the AddIngredient command
            AddIngredientCommand = new RelayCommand((o) => {
                CurrentItem.Price += ((OrderItemViewModel)o).Price;
                CurrentItem.Ingredients.Add(((OrderItemViewModel)o).Copy());
            });

            // Create the AddIngredient command
            RemoveIngredientCommand = new RelayCommand((o) =>
            {
                /* If a free item is removed and the object contains 1 or more of the same
                 * ingredient that is an extra and not free, remove one of those first and
                 * keep the one that is free.
                 */
                if (((OrderItemViewModel)o).Price.Equals(0) && CurrentItem.Ingredients.ToList().Where(i => i.ProductID.Equals(((OrderItemViewModel)o).ProductID) && i.Price > 0).Count() > 0)
                {
                    CurrentItem.Price -= CurrentItem.Ingredients.First(i => i.ProductID.Equals(((OrderItemViewModel)o).ProductID) && i.Price > 0).Price;
                    CurrentItem.Ingredients.Remove(CurrentItem.Ingredients.First(i => i.ProductID.Equals(((OrderItemViewModel)o).ProductID) && i.Price > 0));
                }

                /* Else if an extra ingredient is removed, just
                 * remove that one as normal
                 */
                else
                {
                    CurrentItem.Price -= ((OrderItemViewModel)o).Price;
                    CurrentItem.Ingredients.Remove((OrderItemViewModel)o);
                }
            });

            #endregion
        }