private void GetIngredients(string text)
        {
            var listIngredients = MyStorage.ReadXml <List <Ingredient> >("IngredientData.xml");
            var ingredients     = new List <Ingredient>();

            var ingredientListBegin = (from n in listIngredients
                                       where n.name.ToLower().StartsWith(text.ToLower())
                                       select n.name).ToList();
            var ingredientListContain = (from n in listIngredients
                                         where n.name.ToLower().Contains(text.ToLower())
                                         select n.name).ToList();

            ingredientListBegin.Sort();
            ingredientListContain.Sort();
            ingredientListBegin.AddRange(ingredientListContain);
            var ingredientList = ingredientListBegin.Distinct().ToList();

            foreach (var ingAdded in recipeIngredients)
            {
                if (ingredientList.Contains(ingAdded.ingredientName))
                {
                    ingredientList.Remove(ingAdded.ingredientName);
                }
            }
            if (ingredientList.Count() > 0)
            {
                Lbx_ingredient.ItemsSource = ingredientList.Take(5);
            }
            else
            {
                Lbx_ingredient.Visibility  = Visibility.Collapsed;
                Lbx_ingredient.ItemsSource = null;
            }
        }
        private void CreateXMLData()
        {
            var listInput = MyStorage.ReadXml <List <Recipe> >("InputData.xml");
            var recipes   = new List <Recipe>();

            //initial load
            var recipeList = (from n in listInput
                              where n.ingredients.Count > 0
                              select n).Take(20).ToList();
        }
        private void StarImage_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var recipeId  = ((((sender as TextBlock).Parent as StackPanel).Parent as StackPanel).Children[0] as TextBlock).Text;
            var listInput = MyStorage.ReadXml <List <Recipe> >("InputData.xml");
            var recipe    = (from n in listInput
                             where n.id == Convert.ToInt32(recipeId)
                             select n).FirstOrDefault();

            recipe.isFavorite = recipe.isFavorite == true ? false : true;
            MyStorage.WriteXml <List <Recipe> >(listInput, "InputData.xml");
            GetRecipes();
        }
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     recipes           = MyStorage.ReadXml <ObservableCollection <Recipe> >("InputData.xml");
     recipeIngredients = new ObservableCollection <RecipeIngredient>();
     if (selectedRecipe != null)
     {
         selectedRecipe.instruction = selectedRecipe.instruction.Replace("\\n", "\n");
         DataContext = selectedRecipe;
         if (selectedRecipe.ingredients.Count > 0)
         {
             recipeIngredients             = new ObservableCollection <RecipeIngredient>(selectedRecipe.ingredients);
             Sp_Lbx_ingredients.Visibility = Visibility.Visible;
             Lbx_ingredients.ItemsSource   = recipeIngredients;
         }
     }
 }
        private void GetIngredients(string text)
        {
            var listIngredients = MyStorage.ReadXml <List <Ingredient> >("IngredientData.xml");
            var ingredients     = new List <Ingredient>();

            //initial load
            var ingredientListBegin = (from n in listIngredients
                                       where n.name.ToLower().StartsWith(text.ToLower())
                                       select n.name).ToList();
            var ingredientListContain = (from n in listIngredients
                                         where n.name.ToLower().Contains(text.ToLower())
                                         select n.name).ToList();

            ingredientListBegin.Sort();
            ingredientListContain.Sort();
            ingredientListBegin.AddRange(ingredientListContain);
            var ingredientList = ingredientListBegin.Distinct().ToList();

            foreach (var button in (Sp_filterIngredients as StackPanel).Children)
            {
                var buttonTxt = GetButtonContent(button);
                if (ingredientList.Contains(buttonTxt))
                {
                    ingredientList.Remove(buttonTxt);
                }
            }

            if (ingredientList.Count() > 0)
            {
                Lbx_ingredient.ItemsSource = ingredientList.Take(5);
                //Tbx_ingredient.BorderBrush = Brushes.Black;
            }
            else
            {
                Lbx_ingredient.Visibility  = Visibility.Collapsed;
                Lbx_ingredient.ItemsSource = null;
                //Tbx_ingredient.BorderBrush = Brushes.Red;
            }
        }
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     listInput = MyStorage.ReadXml <ObservableCollection <Recipe> >("InputData.xml");
     SetRecipeDetail();
 }
        private int SaveRecipe(bool fromSave)
        {
            if (Tbx_recipeName.Text != "" || Tbx_recipeDescription.Text.Trim() != "" || Tbx_instructions.Text.Trim() != "" || Tbx_image.Text != "" || Lbx_ingredients.Items.Count > 0)
            {
                storeData = true;
            }
            else
            {
                storeData = false;
            }

            if (storeData)
            {
                if (Tbx_recipeName.Text != "")
                {
                    string imagePath = Tbx_image.Text;
                    string destPath  = imagePath;
                    if (imagePath != "")
                    {
                        string[] parts = imagePath.Split('\\');
                        if (parts[0] != "")
                        {
                            string imageName = parts[parts.Length - 1];

                            destPath = @"\Images\" + imageName;
                            string currentDirectory = System.Environment.CurrentDirectory;
                            if (currentDirectory.EndsWith("\\bin\\Debug"))
                            {
                                int index = currentDirectory.IndexOf("\\bin\\Debug");
                                currentDirectory = currentDirectory.Substring(0, index);
                            }
                            File.Copy(imagePath, currentDirectory + destPath, true);
                        }
                        else
                        {
                            destPath = imagePath;
                        }
                    }
                    if (!isEdit)
                    {
                        var id = recipes.Max(m => m.id);
                        recipes.Add(new Recipe
                        {
                            isMyRecipe          = true,
                            name                = Tbx_recipeName.Text,
                            description         = Tbx_recipeDescription.Text,
                            id                  = id + 1,
                            timeRequiredHours   = Cbx_hours.SelectedValue.ToString(),
                            timeRequiredMinutes = Cbx_minutes.SelectedValue.ToString(),
                            servings            = Cbx_servings.SelectedValue.ToString(),
                            instruction         = Tbx_instructions.Text,
                            ingredients         = recipeIngredients.ToList(),
                            isFavorite          = Cbx_markFavorite.IsChecked.GetValueOrDefault(),
                            image               = imagePath == "" ? @"\Images\noimage.jpg" : destPath
                        });
                        var recipeList = recipes.ToList();
                        MyStorage.WriteXml <List <Recipe> >(recipeList, "InputData.xml");
                        MessageBox.Show("Your recipe has been added to the cookbook.");
                    }
                    else
                    {
                        var listInput = MyStorage.ReadXml <List <Recipe> >("InputData.xml");
                        var recipe    = (from n in listInput
                                         where n.id == Convert.ToInt32(selectedRecipe.id)
                                         select n).FirstOrDefault();
                        selectedRecipe.ingredients = recipeIngredients.ToList();
                        selectedRecipe.image       = destPath;
                        listInput.Remove(recipe);
                        listInput.Add(selectedRecipe);
                        MyStorage.WriteXml <List <Recipe> >(listInput, "InputData.xml");
                    }
                    return(1);
                }
                else
                {
                    if (!fromSave)
                    {
                        MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("You need to enter a recipe name to save the recipe. Click yes to enter the name.", "Save recipe confirmation", System.Windows.MessageBoxButton.YesNo);
                        if (messageBoxResult == MessageBoxResult.No || messageBoxResult == MessageBoxResult.Cancel || messageBoxResult == MessageBoxResult.None)
                        {
                            return(1);
                        }
                        else if (messageBoxResult == MessageBoxResult.Yes)
                        {
                            Tbx_recipeName.Focus();
                            return(0);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please enter a recipe name to save the recipe.", ":|", MessageBoxButton.OK, MessageBoxImage.Error);
                        Tbx_recipeName.Focus();
                        return(0);
                    }
                }
            }
            return(1);
        }
        private Ingredient GetIngDetail(string ingredient, bool getType)
        {
            var listIngredients = MyStorage.ReadXml <List <Ingredient> >("IngredientData.xml");

            return((from l in listIngredients where l.name.ToLower().Equals(ingredient.ToLower()) select l).FirstOrDefault());
        }
        private void GetRecipes()
        {
            var ingredient1 = "";
            var ingredient2 = "";
            var ingredient3 = "";

            for (int i = 0; i < (Sp_filterIngredients as StackPanel).Children.Count; i++)
            {
                var button = (Sp_filterIngredients as StackPanel).Children[i] as Button;

                if (i == 0)
                {
                    ingredient1 = GetButtonContent(button);
                }
                else if (i == 1)
                {
                    ingredient2 = GetButtonContent(button);
                }
                else
                {
                    ingredient3 = GetButtonContent(button);
                }
            }
            var listInput = MyStorage.ReadXml <List <Recipe> >("InputData.xml");
            var recipes   = new List <Recipe>();

            //initial load
            var recipeList = (from n in listInput
                              where
                              n.isDelete == false
                              select n).Take(20).ToList();

            //filter by ingredients
            if (ingredient1 != "")
            {
                var recipeListFilter1 = (from n in listInput
                                         where
                                         n.isDelete == false & n.ingredients.Count > 0
                                         from m in n.ingredients
                                         where m.ingredientName.ToLower().Contains(ingredient1.ToLower())
                                         select n).ToList();
                if (ingredient3 == "" && ingredient2 == "")
                {
                    recipes = recipeListFilter1;
                }
                else
                {
                    var recipeListFilter2 = (from n in recipeListFilter1
                                             from m in n.ingredients
                                             where m.ingredientName.ToLower().Contains(ingredient2.ToLower())
                                             select n).ToList();
                    if (ingredient3 == "")
                    {
                        recipes = recipeListFilter2;
                    }
                    else
                    {
                        var recipeListFilter3 = (from n in recipeListFilter2
                                                 from m in n.ingredients
                                                 where m.ingredientName.ToLower().Contains(ingredient3.ToLower())
                                                 select n).ToList();
                        recipes = recipeListFilter3;
                    }
                }
            }
            else
            {
                recipes = recipeList;
            }
            recipes = Cbx_getFavorites.IsChecked == true?recipes.Where(r => r.isFavorite == true).ToList() : recipes;

            recipes = Cbx_getMyRecipes.IsChecked == true?recipes.Where(r => r.isMyRecipe == true).ToList() : recipes;

            if (Tbx_filterName.Text.Length >= 1)
            {
                recipes = recipes.Where(r => r.name.ToLower().Contains(Tbx_filterName.Text.ToLower())).ToList();
            }
            if (recipes.Count > 0)
            {
                recipes = recipes.OrderBy(r => r.name).ToList();
                var observableRecipe = new ObservableCollection <Recipe>(recipes);
                Lbx_Items_Recipes.Visibility   = Visibility.Visible;
                Lbx_Items_Recipes.ItemsSource  = recipes;
                Tbk_RecipeListEmpty.Visibility = Visibility.Collapsed;
                Tbk_recipeListTitle.Visibility = Visibility.Visible;
            }
            else
            {
                Lbx_Items_Recipes.Visibility   = Visibility.Collapsed;
                Lbx_Items_Recipes.ItemsSource  = null;
                Tbk_RecipeListEmpty.Visibility = Visibility.Visible;
                Tbk_recipeListTitle.Visibility = Visibility.Collapsed;
            }
        }