Ejemplo n.º 1
0
        public static bool Equals(NutritionInfo value1, NutritionInfo value2)
        {
            if (value1.Calories != value2.Calories)
            {
                return(false);
            }
            if (value1.Fat != value2.Fat)
            {
                return(false);
            }
            if (value1.Sodium != value2.Sodium)
            {
                return(false);
            }
            if (value1.Carbs != value2.Carbs)
            {
                return(false);
            }
            if (value1.Fiber != value2.Fiber)
            {
                return(false);
            }
            if (value1.Sugar != value2.Sugar)
            {
                return(false);
            }
            if (value1.AdditionalSugar != value2.AdditionalSugar)
            {
                return(false);
            }
            if (value1.Protein != value2.Protein)
            {
                return(false);
            }

            return(true);
        }
 public NutritionViewDialog(NutritionInfo info)
 {
     InitializeComponent();
     propertyGridControl1.SelectedObject = info;
 }
Ejemplo n.º 3
0
        private void DoCalculateNutrition()
        {
            var nutrition = new NutritionInfo();

            var succeeded    = true;
            var errorMessage = string.Empty;

            var ingredientTable = mEditRecipeParams.CreateDictionary();

            foreach (var item in recipeItemBindingSource.OfType <RecipeItem>())
            {
                if (ingredientTable.TryGetValue(item.ItemId ?? string.Empty, out BaseIngredient ingredient))
                {
                    // get the unit that the user entered into the recipe
                    var unit = Units.FromSelection((item.UnitId, item.UnitTypeName));
                    if (TryGetServingSize(ingredient, unit, out IServingSize servingSize))
                    {
                        try
                        {
                            // take the desired amount and convert
                            var totalUnit = unit.Factor * item.Quantity;

                            // lets say the serving size is 5 grams, and we have a total of 10 grams.
                            // To get the factor, we take the total grams and divide by the serving size
                            var servingSizeUnit = servingSize.Unit.Factor * servingSize.Amount;
                            var factor          = totalUnit / servingSizeUnit;

                            // with this factor, we need to scale the nutrition info
                            nutrition += (ingredient.Nutrition * factor);
                        }
                        catch
                        {
                            var sb = new StringBuilder();
                            sb.AppendLine("There was an error while calculating the nutrition. Make sure the following information is valid:");
                            sb.AppendLine();
                            sb.AppendLine($"Name: {ingredient.Name}");
                            sb.AppendLine($"Serving Size: {servingSize.Amount} {servingSize.Unit.Display}");
                            sb.AppendLine($"Amount: {item.Quantity}");
                            sb.AppendLine();
                            sb.AppendLine("The serving size and amount cannot be 0. You may need to edit the ingredient.");
                            errorMessage = $"{sb}";
                            succeeded    = false;
                            break;
                        }
                    }
                    else
                    {
                        var sb = new StringBuilder();
                        sb.AppendLine($"You entered in a {unit.Display}, but {ingredient.Name} does not have a {unit.Display} defined");

                        var units = new List <string>();
                        foreach (var serving in ingredient.Servings)
                        {
                            if (serving.Unit != null)
                            {
                                units.Add(serving.Unit.Display);
                            }
                        }

                        sb.AppendLine();
                        if (units.Count > 0)
                        {
                            sb.AppendLine($"{ingredient.Name} has these units defined:");
                            foreach (var value in units)
                            {
                                sb.AppendLine($"  * {value}");
                            }
                        }
                        else
                        {
                            sb.AppendLine($"Actually, {ingredient.Name} has no units defined.");
                        }

                        errorMessage = $"{sb}";
                        succeeded    = false;
                        break;
                    }
                }
                else
                {
                    errorMessage = $"{item.Text} could not be found; nutrition info cannot be calculated.";
                    succeeded    = false;
                    break;
                }
            }

            if (!succeeded)
            {
                ShowError(errorMessage);
            }
            else
            {
                nutritionInfoEdit.ReadNutrition(nutrition);
            }
        }