private void unitChanger_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string oldUnitStr = _ingredient._unitStr;
            //string newUnitStr = unitChanger.SelectedValue.ToString(); // THIS IS NULL WITH STRINGS
            string newUnitStr = unitChanger.SelectedItem.ToString(); // THANK YOU

            //Debug.WriteLine(oldUnitStr);
            //Debug.WriteLine("=-=-=-=-=-=");
            //Debug.WriteLine(newUnitStr);

            if (oldUnitStr == newUnitStr)
            {
                return;
            }
            // ONLY call update if oldUnitStr != newUnitStr
            // now update the measurement value
            _ingredient.convertMeasurement(newUnitStr);
            // then convert this value to str
            // get this string and set the primary string to this (which is fine since no secondary string will exist)
            //_ingredient._unitStr = newUnitStr;

            primaryText.Text = _ingredient._measurementStr;
            //Debug.WriteLine(primaryText.Text);
        }
Esempio n. 2
0
        //Incomplete
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            //Parse the amount box
            double d = -1;
            bool   frac;

            string[] amount = AmountBox.Text.Split('/');
            for (int i = 0; i < amount.Length; i++)
            {
                amount[i].Trim();
            }
            if (amount.Length == 2 && Double.TryParse(amount[0].ToString(), out d) && Double.TryParse(amount[1].ToString(), out d))
            {
                frac = true;
            }
            else
            {
                frac = false;
            }

            //Only do this when both are empty
            if (!string.IsNullOrWhiteSpace(ingBox.Text) && !string.IsNullOrWhiteSpace(AmountBox.Text) && measurementChanger.SelectedItem != null &&
                (unitChanger.SelectedItem != null || measurementChanger.SelectedItem.ToString() == "NONE") &&
                (Double.TryParse(AmountBox.Text.ToString(), out d)) || frac == true)
            {
                //Exit the popup
                modBox.IsEnabled       = false;
                this.modBox.Visibility = System.Windows.Visibility.Hidden;

                //When you are changing an item
                if (!changeAddFlag)
                {
                    if (frac == true)
                    {
                        //It's a fraction
                        int num = Convert.ToInt32(amount[0]);
                        int den = Convert.ToInt32(amount[1]);
                        _recipe._ingredients[ingNum]._measurement = (double)num / (double)den;
                    }
                    else
                    {
                        //Double.TryParse(AmountBox.Text.ToString(), out d);
                        _recipe._ingredients[ingNum]._measurement = d;
                    }

                    //_recipe._ingredients[ingNum]._measurement = d;//Convert.ToDouble(AmountBox.Text.ToString());


                    //Changer measurement number
                    //_recipe._ingredients[ingNum]._measurement = Convert.ToInt32(AmountBox.Text);
                    if (measurementChanger.SelectedItem.ToString() == "VOL.") // volume
                    {
                        _recipe._ingredients[ingNum]._unitType        = Ingredient.UnitType.VOLUME;
                        _recipe._ingredients[ingNum]._hasStandardUnit = true;
                        _recipe._ingredients[ingNum]._hasSpecialUnit  = false;
                    }
                    else if (measurementChanger.SelectedItem.ToString() == "MASS") // mass
                    {
                        _recipe._ingredients[ingNum]._unitType        = Ingredient.UnitType.MASS;
                        _recipe._ingredients[ingNum]._hasStandardUnit = true;
                        _recipe._ingredients[ingNum]._hasSpecialUnit  = false;
                    }
                    else if (measurementChanger.SelectedItem.ToString() == "LEN.")// length
                    {
                        _recipe._ingredients[ingNum]._unitType        = Ingredient.UnitType.LENGTH;
                        _recipe._ingredients[ingNum]._hasStandardUnit = true;
                        _recipe._ingredients[ingNum]._hasSpecialUnit  = false;
                    }
                    else if (measurementChanger.SelectedItem.ToString() == "SPEC.")
                    {
                        _recipe._ingredients[ingNum]._unitType        = Ingredient.UnitType.SPECIAL;
                        _recipe._ingredients[ingNum]._hasStandardUnit = false;
                        _recipe._ingredients[ingNum]._hasSpecialUnit  = true;
                    }
                    else
                    {
                        _recipe._ingredients[ingNum]._unitType        = Ingredient.UnitType.NONE;
                        _recipe._ingredients[ingNum]._hasStandardUnit = false;
                        _recipe._ingredients[ingNum]._hasSpecialUnit  = false;
                    }

                    if (measurementChanger.SelectedItem.ToString() == "NONE")
                    {
                        _recipe._ingredients[ingNum]._unitStr        = "";
                        _recipe._ingredients[ingNum]._measurementStr = AmountBox.Text;
                    }

                    else
                    {
                        _recipe._ingredients[ingNum]._unitStr = unitChanger.SelectedItem.ToString();
                        _recipe._ingredients[ingNum].convertMeasurement(unitChanger.SelectedItem.ToString());
                    }

                    _recipe._ingredients[ingNum]._mainText = ingBox.Text;

                    //measurement str
                }
                else //Don't delete, just add
                {
                    //Instantiate more stuff tha this
                    //You want to change ingredient name, amount, measurement type and units
                    //List<string> _substitutions = new List<string> { };
                    //Ingredient newIng = new Ingredient(0, "0", Ingredient.UnitType.NONE,"", "", _substitutions);
                    //need to be able to write fractions too right?
                    //newIng._measurement = Convert.ToDouble(AmountBox.Text.ToString());

                    Ingredient newIng;

                    double measurement;
                    string measurementStr;
                    Ingredient.UnitType unitType;
                    string        unitStr;
                    string        mainText;
                    List <string> substitutions = new List <string> {
                    };

                    if (frac == true)
                    {
                        //It's a fraction
                        int num = Convert.ToInt32(amount[0]);
                        int den = Convert.ToInt32(amount[1]);
                        measurement    = (double)num / (double)den;
                        measurementStr = num.ToString() + "/" + den.ToString();
                    }
                    else
                    {
                        //Double.TryParse(AmountBox.Text.ToString(), out d);
                        measurement    = d;
                        measurementStr = measurement.ToString();
                    }
                    //TODO: measurement str
                    if (measurementChanger.SelectedItem.ToString() == "VOL.") // volume
                    {
                        unitType = Ingredient.UnitType.VOLUME;
                    }
                    else if (measurementChanger.SelectedItem.ToString() == "MASS") // mass
                    {
                        unitType = Ingredient.UnitType.MASS;
                    }
                    else if (measurementChanger.SelectedItem.ToString() == "LEN.")// length
                    {
                        unitType = Ingredient.UnitType.LENGTH;
                    }
                    else if (measurementChanger.SelectedItem.ToString() == "SPEC.")
                    {
                        unitType = Ingredient.UnitType.SPECIAL;
                    }
                    else
                    {
                        unitType = Ingredient.UnitType.NONE;
                    }

                    mainText = ingBox.Text;



                    if (measurementChanger.SelectedItem.ToString() == "NONE")
                    {
                        unitStr        = "";
                        measurementStr = AmountBox.Text;
                        newIng         = new Ingredient(measurement, measurementStr, unitType, unitStr, mainText, substitutions);
                    }
                    else
                    {
                        unitStr = unitChanger.SelectedItem.ToString();
                        newIng  = new Ingredient(measurement, measurementStr, unitType, unitStr, mainText, substitutions);
                        newIng.convertMeasurement(unitChanger.SelectedItem.ToString());
                    }

                    _recipe._ingredients.Add(newIng);

                    //measurement str
                    //_recipe._ingredients[ingNum].convertMeasurement(unitChanger.SelectedItem.ToString());
                }
                ModIngredients updatePage = new ModIngredients(_recipe, recipeNum);
                ((MainWindow)App.Current.MainWindow).Main.Content = updatePage;
                mainGrid.IsEnabled       = true;
                InvalidInput.Visibility  = System.Windows.Visibility.Hidden;
                InvalidInput2.Visibility = System.Windows.Visibility.Hidden;
                InvalidInput3.Visibility = System.Windows.Visibility.Hidden;
            }
            else
            {
                if (string.IsNullOrWhiteSpace(ingBox.Text))
                {
                    InvalidInput.Visibility = System.Windows.Visibility.Visible;
                }
                if (string.IsNullOrWhiteSpace(AmountBox.Text) || measurementChanger.SelectedItem != null ||
                    (unitChanger.SelectedItem != null || measurementChanger.SelectedItem.ToString() == "NONE"))
                {
                    InvalidInput2.Visibility = System.Windows.Visibility.Visible;
                    InvalidInput3.Visibility = System.Windows.Visibility.Visible;
                }
                //MessageBox.Show("Ingredient name/Amount is empty OR meas/unit is not selected OR amount not in decimals");
            }
        }