public async void exportShoppingListAsync(ShoppingList sl, ObservableCollection <Recipe> recipes) { //Create a new PDF document. PdfDocument document = new PdfDocument(); //Add a page to the document. PdfPage page = document.Pages.Add(); //Create PDF graphics for the page. PdfGraphics graphics = page.Graphics; //Set the standard font. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); //Draw the text. graphics.DrawString("Shopping List", font, PdfBrushes.Black, 0, 0); graphics.DrawString(sl.id.ToString(), font, PdfBrushes.Black, 0, 20); graphics.DrawString(sl.date.ToLongDateString(), font, PdfBrushes.Black, 0, 40); //calculate draw positions with a counter and create formulas //so that some parts wont be overwritten int counter = 60; foreach (Recipe r in recipes) { graphics.DrawString("Recipe: " + r.title, font, PdfBrushes.Black, 0, counter); graphics.DrawString("Ingredients:", font, PdfBrushes.Black, 0, counter + 20); foreach (Ingredient i in r.ingredients) { graphics.DrawString(i.Name, font, PdfBrushes.Black, 0, counter + 40); graphics.DrawString(i.Amount.ToString(), font, PdfBrushes.Black, 7 + (i.Name.Length * 10), counter + 40); graphics.DrawString(i.Unit.Unit.ToString(), font, PdfBrushes.Black, (i.Amount.ToString().Length * 20) + 60, counter + 40); counter += 50; } counter += 50; } //Save the document. MemoryStream stream = new MemoryStream(); await document.SaveAsync(stream); //Close the documents document.Close(true); //Save the stream as PDF document file in local machine Save(stream, "Result.pdf"); //Close the document. document.Close(true); }
/** * Add shopping list to given user id * */ public static int AddShoppinglistAsync(ShoppingList sl, int id = -1) { int usedId = 0; bool added = false; Dictionary <int, int> recipeList = new Dictionary <int, int>(); //get last shopping id int useIndex = RecipeManager.instance.lastShoppingID; if (id > -1) { useIndex = id; } for (int i = 0; i < sl.recipes.Count; i++) { int amountOne = 0; for (int y = 0; y < sl.recipes.Count; y++) { if (sl.recipes[i].id == sl.recipes[y].id) { amountOne++; } } if (!recipeList.ContainsKey(sl.recipes[i].id)) { recipeList.Add(sl.recipes[i].id, amountOne); } } foreach (KeyValuePair <int, int> item in recipeList) { if (item.Value > 0) { //form query string query = @"INSERT INTO shoppinglist VALUES (" + useIndex + ", 'NOW()', " + item.Key + ", " + RecipeManager.instance.GetCurrentUser().id + ", " + item.Value + ")"; DBConnector.initAsync(); MySqlCommand cmd = new MySqlCommand(query, DBConnector.conn); cmd.ExecuteNonQuery(); added = true; DBConnector.conn.Close(); } } if (added) { usedId = RecipeManager.instance.lastShoppingID; RecipeManager.instance.lastShoppingID++; return(usedId); } else { return(-1); } /* * String query = @"INSERT INTO shoppinglist VALUES ('NULL', 'NOW()', ''"; * DBConnector.initAsync(); * MySqlCommand cmd = new MySqlCommand(query, DBConnector.conn); * cmd.ExecuteNonQuery(); * * * foreach(Recipe r in sl.recipes) * { * IngredientDAO.AddIngredient((int)cmd.LastInsertedId, r); * } */ }
//add shopping lsit to user public void AddShoppingList(ShoppingList shoppingList) { shoppingLists.Add(shoppingList); }