/// <summary> /// Gets the user entry for the name and the list of categories, then adds the dish to the meal, and saves it to file. /// It is called by the code behind in DishEditPage View. /// </summary> private void SaveButtonExecute() { // build the dish category list InputDishCategories = new List <string>(); if (DishCategoryCheckBox1) { InputDishCategories.Add(DishCategories["1"]); } if (DishCategoryCheckBox2) { InputDishCategories.Add(DishCategories["2"]); } if (DishCategoryCheckBox3) { InputDishCategories.Add(DishCategories["3"]); } if (DishCategoryCheckBox4) { InputDishCategories.Add(DishCategories["4"]); } if (DishCategoryCheckBox5) { InputDishCategories.Add(DishCategories["5"]); } // if the page is opened to create a new dish, add the dish to the database if (SelectedDish == null) { DishDB.Add(new Dish(EntryName, InputDishCategories, UserData)); DishDBIO.WriteDishesToJSON(DishDB); MessagingCenter.Send(this, "DB updated"); // refresh the search result } // otherwise edit the dish else { NameBeforeEdit = string.Copy(SelectedDish.Name); // save the dish name before change // if editing from the DishDB, find the dish in DishDB and update it if (IsFromDB) { foreach (Dish dish in DishDB) { if (dish.Name == SelectedDish.Name) { dish.Name = EntryName; dish.ThisDishCategories = InputDishCategories; DishDBIO.WriteDishesToJSON(DishDB); MessagingCenter.Send(this, "DB updated"); // refresh the search result break; } } } // if editing from the meal, update the dish in the meal else { SelectedMeal.EditDish(SelectedDish, EntryName, InputDishCategories); UserDaysIO.WriteUserDaysToJSON(UserDays); } } }
/// <summary> /// Add the Dish as built in the page to the meal, and save to file /// </summary> private void AddToMealExecute() { SelectedMeal.AddDish(EntryName, InputDishCategories, UserData); if (!UserDays.Select(day => day.ThisDate).Contains(SelectedDay.ThisDate)) { UserDays.Add(SelectedDay); } UserDaysIO.WriteUserDaysToJSON(UserDays); }
/// <summary> /// After editing a Dish and the initial saving is done, more action can be done after the prompt to the user: /// If the Dish in the DishDB was edited, the occurrences of the Dish in the meals in the future can be updated. /// If the Dish in the Meal was edited, the DishDB can be updated. /// </summary> private void AdditionalEditDishExecute() { // if DishDB is edited, update all cases of the dish in the user data, today and later if (IsFromDB) { foreach (Day day in UserDays) { if (day.ThisDate >= DateTime.Today) { foreach (Meal meal in day.Meals) { foreach (Dish dish in meal.Dishes) { if (dish.Name == NameBeforeEdit) { dish.Name = EntryName; dish.ThisDishCategories = InputDishCategories; } } } } } UserDaysIO.WriteUserDaysToJSON(UserDays); } // if the Dish in the Meal was edited, update the Dish in the database else { foreach (Dish dish in DishDB) { if (dish.Name == NameBeforeEdit) { dish.Name = EntryName; dish.ThisDishCategories = InputDishCategories; break; } } DishDBIO.WriteDishesToJSON(DishDB); MessagingCenter.Send(this, "DB updated"); // refresh the search result } }