/* * When the page is navigated to, check if we're editing an * Existing recipe. If so, load that one. Otherwise, create * a new recipe and use that as the context for this form. */ protected override async void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); recipes = (RecipeList)e.Parameter; if (recipes.isEditing()) { recipe = recipes.getSelected(); for (int i = 0; i < recipe.RecipeIngredients.Count; i++) { ingredients.Add(recipe.RecipeIngredients[i]); } for (int i = 0; i < recipe.RecipeImages.Count; i++) { images.Add(recipe.RecipeImages[i]); } for (int i = 0; i < recipe.RecipeSteps.Count; i++) { steps.Add(recipe.RecipeSteps[i]); } } else { // we're creating a new recipe recipe = new Recipe(); recipe.ID = RecipeList.recipeIdGenerator.getId(); } if (isNarrow()) { IList <PageStackEntry> backStack = Frame.BackStack; int backStackCount = backStack.Count; if (backStackCount > 0) { PageStackEntry masterPageEntry = backStack[backStackCount - 1]; backStack.RemoveAt(backStackCount - 1); PageStackEntry modifiedEntry = new PageStackEntry( typeof(RecipeMasterDetailPage), recipes, masterPageEntry.NavigationTransitionInfo ); backStack.Add(modifiedEntry); } } tempImageFolder = await RecipeList.tempFolder.CreateFolderAsync("" + recipe.ID, CreationCollisionOption.ReplaceExisting); this.imageFlipView.ItemsSource = images; this.ingredientList.ItemsSource = ingredients; this.recipeSteps.ItemsSource = steps; }
/* * Save the changes made to an existing recipe or add the new * recipe to the recipe list. */ private async void saveRecipe(object sender, RoutedEventArgs e) { String newRecipeName = this.recipeName.Text; // double newRecipeRating = this.recipeRating.Value; recipe.Name = newRecipeName; // recipe.Rating = newRecipeRating; recipe.setImages(images); recipe.setIngredients(ingredients); recipe.setSteps(steps); if (!recipes.isEditing()) { recipes.addRecipe(recipe); recipes.SelectedIndex = recipes.getRecipeList().Count - 1; } else { recipes.setEditing(false); RecipeBookDataAccessor.updateRecipe(recipe); } for (int i = 0; i < removedSteps.Count; i++) { RecipeBookDataAccessor.deleteStep(removedSteps[i]); } for (int i = 0; i < removedIngredients.Count; i++) { RecipeBookDataAccessor.deleteIngredient(removedIngredients[i]); } for (int i = 0; i < removedImages.Count; i++) { RecipeImage imageToRemove = removedImages[i]; StorageFile imageFile = await StorageFile.GetFileFromPathAsync(imageToRemove.getImagePath()); await imageFile.DeleteAsync(); RecipeBookDataAccessor.deleteImage(imageToRemove); } Frame.GoBack(); }