Пример #1
0
        private void searchTextbox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                listOfIngredientsStack.Children.Clear();

                this.ingredients = IngredientSearch.Insearch(Init, searchTextbox.Text);
                foreach (Ingredient ing in this.ingredients)
                {
                    Button btn = new Button();
                    Label  lbl = new Label();
                    lbl.Content = ing.IngredientName;
                    btn.Content = lbl;
                    btn.Click  += (sender, eventArgs) =>
                    {
                        this.selectedIngredient  = ing;
                        servingSizeLabel.Content = "1 " + ing.BaseMeasure;
                    };

                    listOfIngredientsStack.Children.Add(btn);
                }
            }
        }
        private void searchBar_searchButton_Click(object sender, RoutedEventArgs e)
        {
            string[] difficulties;
            if (_difficultyCombo.SelectedIndex == -1 || _difficultyCombo.SelectedIndex == 0)
            {
                difficulties = new string[] { "recipe" };
            }
            else
            {
                difficulties = _difficultyCombo.Text.Split(", ");
            }

            string[] cuisunes;
            if (_cuisuneCombo.SelectedIndex == -1 || _cuisuneCombo.SelectedIndex == 0)
            {
                cuisunes = new string[] { "recipe" };
            }
            else
            {
                cuisunes = _cuisuneCombo.Text.Split(", ");
            }


            string[] ingredients;
            if (string.IsNullOrEmpty(_ingredientTextbox.Text))
            {
                ingredients = new string[] { "recipe" };
            }
            else
            {
                List <Ingredient> ingList = IngredientSearch.Insearch(this.Init, _ingredientTextbox.Text);
                if (!ingList.Any()) //If the ingredient doesn't match
                {
                    ingredients = new string[] { null };
                }
                else
                {
                    ingredients             = new string[] { ingList.First().IngredientName };
                    _ingredientTextbox.Text = ingList.First().IngredientName;
                }
            }

            string[] tagList = new string[2 + difficulties.Length + cuisunes.Length + ingredients.Length];

            if (_vegetarianCheckbox.IsChecked ?? false)
            {
                tagList[0] = "Vegetarian";
            }
            else
            {
                tagList[0] = "recipe";
            }

            if (_veganCheckbox.IsChecked ?? false)
            {
                tagList[1] = "Vegan";
            }
            else
            {
                tagList[1] = "recipe";
            }


            difficulties.CopyTo(tagList, 2);
            cuisunes.CopyTo(tagList, 2 + difficulties.Length);
            ingredients.CopyTo(tagList, 2 + difficulties.Length + cuisunes.Length);



            List <Recipe> filterResults = Search.Searchrecipes(this.Init, tagList);

            List <Recipe> possibleSearches = new List <Recipe>();

            if (string.IsNullOrEmpty(searchBar_textBox.Text))
            {
                possibleSearches = Init.AllRecipes;
            }
            else
            {
                foreach (Recipe rcp in this.Init.AllRecipes)
                {
                    string lowerName = rcp.RecipeName.ToLower();
                    if (lowerName.Contains(searchBar_textBox.Text.ToLower()))
                    {
                        possibleSearches.Add(rcp);
                    }
                }
            }

            List <Recipe> searchResults = new List <Recipe>();

            foreach (Recipe rcp in possibleSearches)
            {
                if (filterResults.Contains(rcp))
                {
                    searchResults.Add(rcp);
                }
            }

            Switcher.SwitchContentPanel(new SearchResultsContent(this.Init, searchResults));
        }