private void UpdateTotals(string scope)
                {
                    FoodData totals = new FoodData();

                    if (View.Model.MainWindow.UserData.PlannedDiet == null)
                    {
                        return;
                    }
                    foreach (DietPlanEntry entry in View.Model.MainWindow.UserData.PlannedDiet)
                    {
                        // Restrict results to the specified scope
                        if (entry.Scope != scope)
                        {
                            continue;
                        }

                        FoodData data = USDA.GetFoodData(entry.FoodName);

                        // Adjust the nutrient values to reflect the quantity and unit
                        double quantity = Convert.ToDouble(entry.Quantity);
                        string NDB_No   = USDA.GetNDBNumber(entry.FoodName);
                        double factor   = USDA.GetNutrientConversionFactor(NDB_No, entry.Unit, quantity);
                        data *= factor;

                        // Add the adjusted data to the total
                        totals += data;
                    }

                    // Apply rounding to the data if the option is set and update the data
                    if (Settings.Default.Rounding == true)
                    {
                        totals = FoodData.Round(totals, Settings.Default.RoundingNumberOfFractionalDigits);
                    }
                    View.Model.MainWindow.FoodData = totals;
                }
예제 #2
0
        // The user has completed a drop over the (diet plan) datagrid control with the source as either the listbox or the treeview
        private void DataGrid_Drop(object sender, DragEventArgs e)
        {
            DataObject item = (((DragEventArgs)e).Data) as DataObject;
            string     food = item.GetData(DataFormats.StringFormat) as string;
            DataGrid   grid = (DataGrid)e.Source;

            // Set the units to display in the combobox for the new row in the datagrid
            List <string> units = new List <string>();

            units.Add("Gram");
            List <string> foodunits = USDA.GetUnits(USDA.GetNDBNumber(food));

            foreach (string unit in foodunits)
            {
                units.Add(unit.FirstLetterToUpper());
            }
            foodunits = null;

            // Create a new DietEntry object and set values
            DietPlanEntry d = new DietPlanEntry();

            d.FoodName = food;
            d.Units    = units;
            d.Quantity = "100";
            d.Unit     = "gram";
            d.Meal     = "Breakfast";
            d.Scope    = ((ListBoxItem)DietPlanScopeListBox.SelectedItem).Content.ToString();

            // Add the new DietEntry object to the diet plan tab
            View.Model.MainWindow.UserData.PlannedDiet.Add(d);

            // Save user data
            View.Model.MainWindow.UserData.Save();

            e.Handled = true;
        }