private void NewList_Click(object sender, RoutedEventArgs e)
        {
            // Instantiate the dialog box
            DateDialog dlg = new DateDialog();

            // Configure the dialog box
            dlg.Owner = this;

            // Open the dialog box modally
            dlg.ShowDialog();

            if (dlg.DialogResult.Value == false)
            {
                return;
            }

            DateTime date = dlg.SelectedDate;

            _startDate = date;

            int       numberOfUnits = dlg.NumberOfUnits;
            UnitsEnum unitSize      = dlg.UnitSize;

            if (unitSize == UnitsEnum.Weeks)
            {
                numberOfUnits *= 7;
            }

            if (unitSize == UnitsEnum.Months)
            {
                numberOfUnits *= 30;
            }

            // Clean and add new selected items
            var colData = (SelectedMealCollection)this.FindResource("SelectedMealCollectionData");

            colData.Clear();

            for (int unitIndex = 0; unitIndex < numberOfUnits; unitIndex++)
            {
                var mealDate = new SelectedMeal {
                    DateTime = date
                };
                colData.Add(mealDate);
                date = date.AddDays(1);
            }

            var ingData = (SelectedIngredientsCollection)this.FindResource("SelectedIngredientsCollectionData");

            ingData.Clear();
        }
        /// <summary>
        /// The drop event for adding a meal to the Daily List
        /// </summary>
        private void List_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("dragMealFormat"))
            {
                // sender is itemsControl
                // Update the table data
                int          updateIndex  = DecideDropTarget(e, (ItemsControl)sender);
                var          xElement     = e.Data.GetData("dragMealFormat") as XmlElement;
                var          colData      = (SelectedMealCollection)this.FindResource("SelectedMealCollectionData");
                SelectedMeal mealToUpdate = colData[updateIndex];

                mealToUpdate.addMeal(xElement.GetAttribute("name"));

                if (mealToUpdate.Ingredients == null)
                {
                    mealToUpdate.Ingredients = new List <string>();
                }

                foreach (XmlNode ingredientNode in xElement.SelectNodes("Ingredients/Ingredient"))
                {
                    mealToUpdate.Ingredients.Add(ingredientNode.InnerText);
                }

                // Update the selected ingredient view
                var ingredientData = (SelectedIngredientsCollection)this.FindResource("SelectedIngredientsCollectionData");
                if (mealToUpdate.Ingredients != null)
                {
                    foreach (string ingredient in mealToUpdate.Ingredients)
                    {
                        var selectedIngredient = new SelectedIngredient(ingredient, mealToUpdate.DateTime.ToString("yyyy-MM-dd"));
                        if (!ingredientData.Contains(selectedIngredient))
                        {
                            ingredientData.Add(selectedIngredient);
                        }
                    }
                }
            }

            _index = -1;
        }
Beispiel #3
0
        public async Task <DateTime> LoadLatest(string endPoint, SelectedMealCollection mealData, SelectedIngredientsCollection ingredientsData)
        {
            DateTime startDate = DateTime.MinValue;

            try
            {
                string data = await ServerGet(endPoint);

                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(data);
                var meals = xmlDoc.SelectNodes("//ArrayOfSelectedMeal/SelectedMeal");

                mealData.Clear();
                foreach (XmlNode xmlNode in meals)
                {
                    // <ArrayOfSelectedMeal>
                    // Now populate meals and staples
                    string date         = xmlNode.SelectSingleNode("DateTime").InnerText;
                    var    ingredients  = xmlNode.SelectNodes("Ingredients/string");
                    var    mealsForDate = xmlNode.SelectNodes("Meals/string");
                    var    selectedMeal = new SelectedMeal();

                    // Datetime conversion
                    DateTime mealDate;
                    if (DateTime.TryParse(date, out mealDate))
                    {
                        selectedMeal.DateTime = mealDate;
                        if (startDate == DateTime.MinValue || mealDate < startDate)
                        {
                            startDate = mealDate;
                        }
                    }

                    foreach (XmlNode mealNode in mealsForDate)
                    {
                        selectedMeal.addMeal(mealNode.InnerText);
                    }

                    foreach (XmlNode ingredientNode in ingredients)
                    {
                        selectedMeal.Ingredients.Add(ingredientNode.InnerText);
                    }

                    mealData.Add(selectedMeal);
                }

                //<ArrayOfSelectedIngredient>
                var selectedIngredients = xmlDoc.SelectNodes("//ArrayOfSelectedIngredient/SelectedIngredient");

                ingredientsData.Clear();
                foreach (XmlNode xmlNode in selectedIngredients)
                {
                    string ingredient = xmlNode.SelectSingleNode("Ingredient").InnerText;
                    string date       = xmlNode.SelectSingleNode("DateToUse").InnerText;

                    var selectedIngredient = new SelectedIngredient();
                    selectedIngredient.Ingredient = ingredient;
                    selectedIngredient.DateToUse  = date;
                    ingredientsData.Add(selectedIngredient);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to load data from server: " + ex.Message, "Failed Load", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return(startDate);
        }