From 8ca9eaea7fb1ed7591c9da901c9b59a889fdce59 Mon Sep 17 00:00:00 2001 From: "maxime.BATISTA@etu.uca.fr" Date: Thu, 11 May 2023 18:41:49 +0200 Subject: [PATCH] add MyList registraction to LocalEndpoint and app side --- Endpoint/IAccountRecipesPreferences.cs | 6 +----- LocalEndpoint/AccountRecipesPreferences.cs | 11 +++++++++++ Views/MyListPage.xaml | 3 ++- Views/MyListPage.xaml.cs | 20 +++++++++++++++++++- Views/RecipePage.xaml | 3 ++- Views/RecipePage.xaml.cs | 8 ++++++++ 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Endpoint/IAccountRecipesPreferences.cs b/Endpoint/IAccountRecipesPreferences.cs index cc2ae12..1a40745 100644 --- a/Endpoint/IAccountRecipesPreferences.cs +++ b/Endpoint/IAccountRecipesPreferences.cs @@ -1,10 +1,5 @@ using Models; -using System; -using System.Collections.Generic; using System.Collections.Immutable; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Endpoint { @@ -16,6 +11,7 @@ namespace Endpoint public void AddToFavorites(RecipeInfo info); public void RemoveFromFavorites(RecipeInfo info); public void SetReviewScore(RecipeInfo info, uint score); + public bool AddToWeeklyList(RecipeInfo info, uint persAmount); public AccountRecipeRate GetRate(RecipeInfo info); diff --git a/LocalEndpoint/AccountRecipesPreferences.cs b/LocalEndpoint/AccountRecipesPreferences.cs index 65b41b9..ce8f296 100644 --- a/LocalEndpoint/AccountRecipesPreferences.cs +++ b/LocalEndpoint/AccountRecipesPreferences.cs @@ -76,6 +76,17 @@ namespace LocalEndpoint ratings[info.Id] = new AccountRecipeRate(true, rate.Rate); } + + public bool AddToWeeklyList(RecipeInfo info, uint persAmount) + { + if (weekly.ContainsKey(info.Id)) + return false; + + weekly[info.Id] = persAmount; + + return true; + } + public void RemoveFromFavorites(RecipeInfo info) { AccountRecipeRate rate = GetRate(info); diff --git a/Views/MyListPage.xaml b/Views/MyListPage.xaml index 1568766..1272415 100644 --- a/Views/MyListPage.xaml +++ b/Views/MyListPage.xaml @@ -4,7 +4,8 @@ x:Class="ShoopNCook.Pages.MyListPage" Title="MyList" BackgroundColor="{StaticResource BackgroundPrimary}" - xmlns:views="clr-namespace:ShoopNCook.Views"> + xmlns:views="clr-namespace:ShoopNCook.Views" + NavigatedTo="ContentPage_NavigatedTo"> diff --git a/Views/MyListPage.xaml.cs b/Views/MyListPage.xaml.cs index 973dde1..87cddb5 100644 --- a/Views/MyListPage.xaml.cs +++ b/Views/MyListPage.xaml.cs @@ -7,11 +7,25 @@ namespace ShoopNCook.Pages; public partial class MyListPage : ContentPage { + + private readonly IAccountRecipesPreferences preferences; + private readonly IUserNotifier notifier; + private readonly IRecipesService service; + public MyListPage(Account account, IUserNotifier notifier, IRecipesService service) { InitializeComponent(); - IAccountRecipesPreferences preferences = service.GetPreferencesOf(account); + this.preferences = service.GetPreferencesOf(account); + this.notifier = notifier; + this.service = service; + + UpdateMyList(); + } + + private void UpdateMyList() + { + RecipesLayout.Children.Clear(); preferences.GetWeeklyList().ForEach(tuple => { RecipeInfo info = tuple.Item1; @@ -21,6 +35,10 @@ public partial class MyListPage : ContentPage Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, amount)); })); }); + } + private void ContentPage_NavigatedTo(object sender, NavigatedToEventArgs e) + { + UpdateMyList(); } } \ No newline at end of file diff --git a/Views/RecipePage.xaml b/Views/RecipePage.xaml index dbb8080..bca49c4 100644 --- a/Views/RecipePage.xaml +++ b/Views/RecipePage.xaml @@ -171,7 +171,8 @@ Text="Add to list" Style="{StaticResource UserButton}" TextColor="White" - BackgroundColor="Gray"> + BackgroundColor="Gray" + Clicked="OnAddToMyListClicked"> diff --git a/Views/RecipePage.xaml.cs b/Views/RecipePage.xaml.cs index 5e650cb..d40478d 100644 --- a/Views/RecipePage.xaml.cs +++ b/Views/RecipePage.xaml.cs @@ -88,6 +88,14 @@ public partial class RecipePage : ContentPage notifier.Success("Your review has been successfuly submited"); } + private void OnAddToMyListClicked(object o, EventArgs e) + { + if (!preferences.AddToWeeklyList(info, Counter.Count)) + notifier.Notice("You already added this recipe to you weekly list!"); + else + notifier.Success("Recipe added to your weekly list."); + } + private void SetFavorite(bool isFavorite) { this.isFavorite = isFavorite;