diff --git a/LocalServices/AccountRecipesPreferences.cs b/LocalServices/AccountRecipesPreferences.cs index a7d848a..520c767 100644 --- a/LocalServices/AccountRecipesPreferences.cs +++ b/LocalServices/AccountRecipesPreferences.cs @@ -75,9 +75,29 @@ namespace LocalEndpoint db.InsertInUserList(Account.User.Id, info.Id, persAmount); + return true; + } + + + public bool RemoveFromWeeklyList(RecipeInfo info) + { + var weeklyDict = db.GetRecipeListOf(Account.User.Id); + if (!weeklyDict.ContainsKey(info.Id)) + return false; + + db.RemoveFromUserList(Account.User.Id, info.Id); + return true; } + public bool IsInWeeklyList(RecipeInfo info) + { + foreach (var (inf, _) in GetWeeklyList()) + { + if (inf == info) return true; + } + return false; + } public void AddToFavorites(RecipeInfo info) { diff --git a/Services/IAccountRecipesPreferences.cs b/Services/IAccountRecipesPreferences.cs index 64d4f35..7f138db 100644 --- a/Services/IAccountRecipesPreferences.cs +++ b/Services/IAccountRecipesPreferences.cs @@ -11,7 +11,11 @@ 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 bool AddToWeeklyList(RecipeInfo info, uint persAmount); + + public bool RemoveFromWeeklyList(RecipeInfo info); + + public bool IsInWeeklyList(RecipeInfo info); public RecipeRate GetRate(RecipeInfo info); diff --git a/Views/Components/RecipeView.xaml.cs b/Views/Components/RecipeView.xaml.cs index de7cbc9..43abdc4 100644 --- a/Views/Components/RecipeView.xaml.cs +++ b/Views/Components/RecipeView.xaml.cs @@ -1,44 +1,44 @@ -using Models; - -namespace ShoopNCook.Views -{ - public partial class RecipeView : ContentView - { - private readonly Action callback; - - - public RecipeView(RecipeInfo info, Action callback) - { - this.callback = callback; - BindingContext = info; - InitializeComponent(); - Note = info.AverageNote; - } - - public float Note - { - set => SetNote(value); - } - - - private void SetNote(float note) - { - note = (uint)note; //truncate integer as we currently do not handle semi stars - foreach (Image img in Stars.Children.Reverse()) - { - if (note > 0) - { - img.Opacity = 1; - note--; - } - else img.Opacity = 0; - } - } - - private void OnRecipeTapped(object sender, EventArgs e) - { - callback(); - } - } -} - +using Models; + +namespace ShoopNCook.Views +{ + public partial class RecipeView : ContentView + { + private readonly Action callback; + + + public RecipeView(RecipeInfo info, Action callback) + { + this.callback = callback; + BindingContext = info; + InitializeComponent(); + Note = info.AverageNote; + } + + public float Note + { + set => SetNote(value); + } + + + private void SetNote(float note) + { + note = (uint)note; //truncate integer as we currently do not handle semi stars + foreach (Image img in Stars.Children.Reverse()) + { + if (note > 0) + { + img.Opacity = 1; + note--; + } + else img.Opacity = 0; + } + } + + private void OnRecipeTapped(object sender, EventArgs e) + { + callback(); + } + } +} + diff --git a/Views/FavoritesPage.xaml.cs b/Views/FavoritesPage.xaml.cs index 20b9ef2..cc4fff6 100644 --- a/Views/FavoritesPage.xaml.cs +++ b/Views/FavoritesPage.xaml.cs @@ -32,6 +32,7 @@ public partial class FavoritesPage : ContentPage RecipeViewLayout.Children.Add(new RecipeView(info, () => { Recipe recipe = service.GetRecipe(info); + Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, 1)); })); }); diff --git a/Views/RecipePage.xaml b/Views/RecipePage.xaml index 9cadc58..38449fb 100644 --- a/Views/RecipePage.xaml +++ b/Views/RecipePage.xaml @@ -49,7 +49,7 @@ + Source="{Binding Recipe.Info.Image}"/> @@ -138,25 +138,25 @@ - - + x:Name="MyListStateButton" + Clicked="OnFooterButtonClicked"/> + + \ No newline at end of file diff --git a/Views/RecipePage.xaml.cs b/Views/RecipePage.xaml.cs index 8b425dc..de43776 100644 --- a/Views/RecipePage.xaml.cs +++ b/Views/RecipePage.xaml.cs @@ -11,19 +11,21 @@ public partial class RecipePage : ContentPage private readonly IAccountRecipesPreferences preferences; private readonly IUserNotifier notifier; - public Recipe Recipe { get; init; } - private uint note; private bool isFavorite; + private bool isInMyList; + public Recipe Recipe { get; init; } + public ICommand StarCommand => new Command(count => SetNote(uint.Parse(count))); public RecipePage(Recipe recipe, IUserNotifier notifier, IAccountRecipesPreferences preferences, uint amount) { + Recipe = recipe; BindingContext = this; - + InitializeComponent(); this.preferences = preferences; @@ -36,12 +38,10 @@ public partial class RecipePage : ContentPage SetFavorite(isFavorite); SetNote(note); + SetIsInMyListState(preferences.IsInWeeklyList(recipe.Info)); Counter.Count = amount; - if (recipe.Info.Image != null) - RecipeImage.Source = ImageSource.FromUri(recipe.Info.Image); - foreach (Ingredient ingredient in recipe.Ingredients) IngredientList.Add(new IngredientView(ingredient)); @@ -58,6 +58,19 @@ public partial class RecipePage : ContentPage } } + private void SetIsInMyListState(bool isInMyList) + { + this.isInMyList = isInMyList; + if (isInMyList) + { + MyListStateButton.Text = "Remove From My List"; + } + else + { + MyListStateButton.Text = "Add To My List"; + } + } + private void SetNote(uint note) { this.note = note; @@ -89,12 +102,37 @@ public partial class RecipePage : ContentPage notifier.Success("Your review has been successfuly submited"); } - private void OnAddToMyListClicked(object o, EventArgs e) + private void OnFooterButtonClicked(object o, EventArgs e) + { + SetIsInMyListState(!isInMyList); + if (isInMyList) + RemoveFromMyList(); + else + AddToMyList(); + } + + private void RemoveFromMyList() + { + if (!preferences.RemoveFromWeeklyList(Recipe.Info)) + { + notifier.Notice("This recipe does not figures in your personnal list"); + } + else + { + notifier.Success("Recipe added to your weekly list."); + } + } + + private void AddToMyList() { if (!preferences.AddToWeeklyList(Recipe.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)