Example #1
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);
        }
        void PrintPage(object sender, PrintPageEventArgs e)
        {
            SelectedMealCollection colData = (SelectedMealCollection)this.FindResource("SelectedMealCollectionData");
            float leftMargin  = e.MarginBounds.Left;
            float rightMargin = e.MarginBounds.Right;
            float topMargin   = e.MarginBounds.Top;

            float yPos      = leftMargin;
            float xPos      = 0;
            float centrePos = 2 * (rightMargin - leftMargin) / 5;
            float rightPos  = 4 * (rightMargin - leftMargin) / 5;
            int   count     = 0;

            System.Drawing.Font       printFont = new System.Drawing.Font("Arial", 10);
            System.Drawing.SolidBrush myBrush   = new System.Drawing.SolidBrush(System.Drawing.Color.Black);

            // Work out the number of lines per page, using the MarginBounds.
            float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            float lineCount    = 0;

            foreach (var selectedMeal in colData)
            {
                // calculate the next line position based on the height of the font according to the printing device
                yPos = topMargin + (count++ *printFont.GetHeight(e.Graphics));
                e.Graphics.DrawString(selectedMeal.Date, printFont, myBrush, xPos, yPos);
                lineCount++;

                yPos = topMargin + (count++ *printFont.GetHeight(e.Graphics));

                foreach (string meal in selectedMeal.Meals)
                {
                    e.Graphics.DrawString(meal, printFont, myBrush, xPos + 20, yPos);
                }

                lineCount++;

                if (lineCount > linesPerPage)
                {
                    break;
                }
            }

            lineCount = 0;
            count     = 0;

            SelectedIngredientsCollection ingredientData = (SelectedIngredientsCollection)this.FindResource("SelectedIngredientsCollectionData");

            foreach (SelectedIngredient ingredient in ingredientData)
            {
                // calculate the next line position based on the height of the font according to the printing device
                yPos = topMargin + (count++ *printFont.GetHeight(e.Graphics));
                xPos = centrePos;
                if (lineCount > linesPerPage)
                {
                    xPos = rightPos;
                }
                e.Graphics.DrawString(ingredient.Ingredient, printFont, myBrush, xPos, yPos);
                lineCount++;
            }

            // If there are more lines, print another page.
            if (lineCount > linesPerPage)
            {
                //     e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
                myBrush.Dispose();
            }
        }