Beispiel #1
0
        private void BtnSaveRecipe_Click(object sender, RoutedEventArgs e)
        {
            List <string> errors  = Recipes.Validate(Recipes.Current);
            bool          success = false;

            if (errors.Count == 0)
            {
                // New Recipes always have 0 as their Id.
                if (Recipes.Current.Id == 0)
                {
                    success = Recipes.Add(Recipes.Current);
                }
                else // Existing recipes
                {
                    success = Recipes.Save();
                }

                if (success)
                {
                    MessageBox.Show(
                        "Recipe saved!",
                        "Success",
                        MessageBoxButton.OK,
                        MessageBoxImage.Information
                        );

                    // Turn off flag so we can close the window right away
                    PendingChanges = false;

                    // Update topmost parent window
                    ViewRecipes viewRecipes = FindViewRecipes();
                    if (viewRecipes != null)
                    {
                        viewRecipes.Refresh();
                    }

                    // TODO: also update owner if its not ViewRecipes

                    Close();
                }
                else
                {
                    MessageBox.Show(
                        "Unexpected error saving your recipe",
                        "Save failed",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error
                        );
                }
            }
            else
            {
                MessageBox.Show(
                    "There are problems saving your recipe:\r\n" + string.Join("\r\n", errors.ToArray()),
                    "Error",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                    );
            }
        }
Beispiel #2
0
        /// <summary>
        /// Run window close routine.
        /// </summary>
        /// <returns>True if it is safe to close, false otherwise</returns>
        private bool ClosingRoutine()
        {
            bool continueClose = true;

            // Prompt if there were changes
            if (PendingChanges)
            {
                // Prompt user for unsaved changes
                MessageBoxResult result = MessageBox.Show(
                    "You have unsaved changes. Discard them?",
                    "Continue?",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Warning
                    );

                // Discard and ensure Owner windows do not bear the unsaved changes
                if (result == MessageBoxResult.Yes)
                {
                    // Discard changes and propagate to Owner windows
                    Recipes.Discard();

                    /**
                     * There are only 2 known paths to this window:
                     * 1. Via ViewRecipe
                     * 2. Via ViewRecipes
                     *
                     * #1 has code to iteratively find the topmost window and run refresh, but #2
                     * still needs code to be traversed via iteration.
                     *
                     * Since there are only 2 known paths, we will target the other path (ViewRecipe)
                     * explicitly and not waste time writing an iterator to refresh every single
                     * Owner.
                     */

                    // Find viewRecipes and reset data contexts and item sources based on discard
                    ViewRecipes viewRecipes = FindViewRecipes();
                    if (viewRecipes != null)
                    {
                        viewRecipes.Refresh();
                    }

                    // Explicitly refer to ViewRecipe if Owner is such and refresh it
                    if (Owner is ViewRecipe)
                    {
                        ((ViewRecipe)Owner).Refresh();
                    }
                }
                else // User does not want to lose changes
                {
                    continueClose = false;
                }
            }

            return(continueClose);
        }
Beispiel #3
0
        /// <summary>
        /// Find a reference of RecipesModel and use it as Db on this window
        /// </summary>
        /// <returns>True if it found and set the Db reference. False otherwise</returns>
        private bool FindAndMaybeSetDb()
        {
            bool        set         = false;
            ViewRecipes viewRecipes = FindViewRecipes();

            if (viewRecipes != null)
            {
                Db  = viewRecipes.Db;
                set = true;
            }

            return(set);
        }