Exemple #1
0
        public static IngredientBinding Create(Guid ingId, Guid recipeId, Single?qty, Units usageUnit, UnitType convType, Int32 unitWeight,
                                               Units formUnit, Single equivAmount, Units equivUnit)
        {
            var rawUnit = KitchenPC.Unit.GetDefaultUnitType(convType);

            if (qty.HasValue && rawUnit != usageUnit)
            {
                if (UnitConverter.CanConvert(usageUnit, rawUnit))
                {
                    qty = UnitConverter.Convert(qty.Value, usageUnit, rawUnit);
                }
                else
                {
                    var ing = new Ingredient
                    {
                        Id             = ingId,
                        ConversionType = convType,
                        UnitWeight     = unitWeight
                    };

                    var form = new IngredientForm
                    {
                        FormUnitType = formUnit,
                        FormAmount   = new Amount(equivAmount, equivUnit),
                        IngredientId = ingId
                    };

                    var usage = new Ingredients.IngredientUsage
                    {
                        Form       = form,
                        Ingredient = ing,
                        Amount     = new Amount(qty.Value, usageUnit)
                    };

                    try
                    {
                        var newAmt = FormConversion.GetNativeAmountForUsage(ing, usage);
                        qty = UnitConverter.Convert(newAmt.SizeHigh, newAmt.Unit, rawUnit); //Ingredient graph only stores high amounts
                    }
                    catch (Exception e)
                    {
                        throw new DataLoadException(e);
                    }
                }
            }

            return(new IngredientBinding
            {
                RecipeId = recipeId,
                IngredientId = ingId,
                Qty = qty.HasValue ? (float?)Math.Round(qty.Value, 3) : null,
                Unit = rawUnit
            });
        }
Exemple #2
0
        public void TestUnitConverter()
        {
            //CanConvert
            Assert.AreEqual(false, UnitConverter.CanConvert(Units.Unit, Units.Cup));
            Assert.AreEqual(false, UnitConverter.CanConvert(Units.Pound, Units.Cup));
            Assert.AreEqual(true, UnitConverter.CanConvert(Units.Teaspoon, Units.Tablespoon));
            Assert.AreEqual(true, UnitConverter.CanConvert(Units.Gram, Units.Ounce));

            //Convert
            Assert.AreEqual(16.0f, UnitConverter.Convert(2.0f, Units.Cup, Units.FluidOunce));
            Assert.AreEqual(32.0f, UnitConverter.Convert(2.0f, Units.Pound, Units.Ounce));
        }
Exemple #3
0
        public virtual IngredientAggregation AddUsage(IngredientUsage ingredient)
        {
            if (ingredient.Ingredient.Id != this.Ingredient.Id)
            {
                throw new ArgumentException("Can only call IngredientAggregation::AddUsage() on original ingredient.");
            }

            //Calculate new total
            if (this.Amount.Unit == ingredient.Amount.Unit || UnitConverter.CanConvert(this.Amount.Unit, ingredient.Amount.Unit)) //Just add
            {
                this.Amount += ingredient.Amount;
            }
            else //Find a conversion path between Ingredient and Form
            {
                var amount = FormConversion.GetNativeAmountForUsage(this.Ingredient, ingredient);
                this.Amount += amount;
            }

            return(this); // Allows AddUsage calls to be chained together
        }
        public Single?Amt;          //Optional amount of ingredient, expressed in default units for ingredient

        public PantryItem(Ingredients.IngredientUsage usage)
        {
            IngredientId = usage.Ingredient.Id;

            //Need to convert IngredientUsage into proper Pantry form
            if (usage.Amount != null)
            {
                var toUnit = Unit.GetDefaultUnitType(usage.Ingredient.ConversionType);
                if (UnitConverter.CanConvert(usage.Form.FormUnitType, toUnit))
                {
                    Amt = UnitConverter.Convert(usage.Amount, toUnit).SizeHigh; //Always take high amount for pantry items
                }
                else //Find conversion path
                {
                    var amount = FormConversion.GetNativeAmountForUsage(usage.Ingredient, usage);
                    Amt = UnitConverter.Convert(amount, toUnit).SizeHigh; //Always take high amount for pantry items
                }
            }
            else
            {
                Amt = null;
            }
        }