From feb052e286b0eba1da8fab8ca7688f9d8cf28f6b Mon Sep 17 00:00:00 2001 From: "maxime.BATISTA@etu.uca.fr" Date: Sat, 3 Jun 2023 18:58:45 +0200 Subject: [PATCH] implement search --- .../Data/CatastrophicPerformancesDatabase.cs | 4 +- LocalServices/RecipesService.cs | 6 +++ Services/IRecipesService.cs | 6 +++ Views/Components/RecipeView.xaml.cs | 1 + Views/HomePage.xaml | 3 +- Views/HomePage.xaml.cs | 24 ++++++--- Views/SearchPage.xaml | 54 ++++++++++++------- Views/SearchPage.xaml.cs | 52 +++++++++++++++++- 8 files changed, 118 insertions(+), 32 deletions(-) diff --git a/LocalServices/Data/CatastrophicPerformancesDatabase.cs b/LocalServices/Data/CatastrophicPerformancesDatabase.cs index ba224f2..755a348 100644 --- a/LocalServices/Data/CatastrophicPerformancesDatabase.cs +++ b/LocalServices/Data/CatastrophicPerformancesDatabase.cs @@ -172,11 +172,11 @@ namespace LocalServices.Data } catch (IOException e) { - Debug.WriteLine(e + " in thread " + Thread.CurrentThread); + Debug.WriteLine(e.Message + " in thread " + Thread.CurrentThread.Name + " " + Thread.CurrentThread.ManagedThreadId); if (fs != null) fs.Dispose(); - Thread.Sleep(100); + Thread.Sleep(200); } } throw new TimeoutException("Could not access file '" + fullPath + "', maximum attempts reached."); diff --git a/LocalServices/RecipesService.cs b/LocalServices/RecipesService.cs index fcd9de1..89404a3 100644 --- a/LocalServices/RecipesService.cs +++ b/LocalServices/RecipesService.cs @@ -51,5 +51,11 @@ namespace LocalServices return data; } + public ImmutableList SearchRecipes(string prompt) + { + return db.ListAllRecipes() + .ConvertAll(r => r.Info) + .FindAll(i => i.Name.ToLower().Contains(prompt.ToLower())); + } } } diff --git a/Services/IRecipesService.cs b/Services/IRecipesService.cs index 7efbe1a..f2fc18c 100644 --- a/Services/IRecipesService.cs +++ b/Services/IRecipesService.cs @@ -14,6 +14,12 @@ namespace Services /// A list containg the popular recipes of the week public ImmutableList PopularRecipes(); + /// + /// performs a search over all the recipes + /// + /// A list containg the recipes that matches the prompt + public ImmutableList SearchRecipes(string prompt); + /// /// Retrieves a recipe from given RecipeInfo /// diff --git a/Views/Components/RecipeView.xaml.cs b/Views/Components/RecipeView.xaml.cs index e30c89e..ba9631c 100644 --- a/Views/Components/RecipeView.xaml.cs +++ b/Views/Components/RecipeView.xaml.cs @@ -6,6 +6,7 @@ public partial class RecipeView : ContentView { private readonly Action callback; + public RecipeInfo Info { get; set; } public RecipeView(RecipeInfo info, Action onClickCallback) { diff --git a/Views/HomePage.xaml b/Views/HomePage.xaml index 38dcd43..e47cb6d 100644 --- a/Views/HomePage.xaml +++ b/Views/HomePage.xaml @@ -38,7 +38,8 @@ + Placeholder="Search here..." + x:Name="SearchPrompt"/> { - Recipe recipe = service.GetRecipe(info); + Recipe recipe = recipes.GetRecipe(info); if (recipe != null) Shell.Current.Navigation.PushAsync(new RecipePage(recipe, preferences, 1)); else @@ -29,7 +29,7 @@ public partial class HomePage : ContentPage })); } - service.PopularRecipes().ForEach(recipe => PushRecipe(PopularsList, recipe)); + recipes.PopularRecipes().ForEach(recipe => PushRecipe(PopularsList, recipe)); preferences.GetRecommendedRecipes().ForEach(recipe => PushRecipe(RecommendedList, recipe)); ProfilePictureImage.Source = ImageSource.FromUri(account.User.ProfilePicture); @@ -38,6 +38,14 @@ public partial class HomePage : ContentPage private async void OnSyncButtonClicked(object sender, EventArgs e) { - await Shell.Current.Navigation.PushAsync(new SearchPage()); + string prompt = SearchPrompt.Text; + if (string.IsNullOrEmpty(prompt)) + { + return; + } + var searchPage = new SearchPage(recipes, preferences); + await Shell.Current.Navigation.PushAsync(searchPage); + searchPage.MakeSearch(SearchPrompt.Text); + } } \ No newline at end of file diff --git a/Views/SearchPage.xaml b/Views/SearchPage.xaml index 6d2dce6..4b2c7b2 100644 --- a/Views/SearchPage.xaml +++ b/Views/SearchPage.xaml @@ -49,18 +49,28 @@ Style="{StaticResource SecondaryBorderShadow}"> + Placeholder="Cake, Lasagna, Vegetarian..." + x:Name="SearchPrompt"/> - - + WidthRequest="40"> + + + + @@ -69,7 +79,7 @@