diff --git a/Views/SearchPage.xaml.cs b/Views/SearchPage.xaml.cs index 1e8e0ed..ef27179 100644 --- a/Views/SearchPage.xaml.cs +++ b/Views/SearchPage.xaml.cs @@ -1,15 +1,60 @@ -namespace ShoopNCook.Pages; +using Microsoft.Maui.Storage; using Models; +using Services; using ShoopNCook.Views; +using System.Collections.ObjectModel; + +namespace ShoopNCook.Pages; public partial class SearchPage : ContentPage { - public SearchPage() + private readonly IRecipesService recipesService; + private readonly IAccountRecipesPreferencesService preferences; + + + public ObservableCollection FoundRecipes { get; private init; } = new ObservableCollection(); + + public SearchPage(IRecipesService recipes, IAccountRecipesPreferencesService preferences) { - InitializeComponent(); - //TODO + BindingContext = this; + this.recipesService = recipes; + this.preferences = preferences; + InitializeComponent(); + } + + public void MakeSearch(string prompt) + { + if (string.IsNullOrEmpty(prompt)) + { + return; + } + FoundRecipes.Clear(); + foreach (RecipeInfo info in recipesService.SearchRecipes(prompt)) + { + FoundRecipes.Add(new RecipeView(info, () => + { + Recipe recipe = recipesService.GetRecipe(info); + if (recipe != null) + Shell.Current.Navigation.PushAsync(new RecipePage(recipe, preferences, 1)); + else + UserNotifier.Error("Could not find recipe"); + })); + } } + private async void OnBackButtonClicked(object sender, EventArgs e) { await Navigation.PopAsync(); } + + private void OnSortByRateClicked(object sender, EventArgs e) + { + FoundRecipes.OrderBy(view => view.Info) + } + + private void OnSearchClicked(object sender, EventArgs e) + { + MakeSearch(SearchPrompt.Text); + } + + } \ No newline at end of file