add 'RemoveFromMyList' button to remove a recipe from MyList
continuous-integration/drone/push Build is passing Details

master
Maxime BATISTA 2 years ago
parent 8e5e75d49d
commit 060618d89f

@ -79,6 +79,26 @@ namespace LocalEndpoint
} }
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) public void AddToFavorites(RecipeInfo info)
{ {
Guid userId = Account.User.Id; Guid userId = Account.User.Id;

@ -13,6 +13,10 @@ namespace Endpoint
public void SetReviewScore(RecipeInfo info, uint score); 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); public RecipeRate GetRate(RecipeInfo info);
public ImmutableList<RecipeInfo> GetFavorites(); public ImmutableList<RecipeInfo> GetFavorites();

@ -32,6 +32,7 @@ public partial class FavoritesPage : ContentPage
RecipeViewLayout.Children.Add(new RecipeView(info, () => RecipeViewLayout.Children.Add(new RecipeView(info, () =>
{ {
Recipe recipe = service.GetRecipe(info); Recipe recipe = service.GetRecipe(info);
Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, 1)); Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, 1));
})); }));
}); });

@ -49,7 +49,7 @@
<Image <Image
Aspect="AspectFill" Aspect="AspectFill"
HeightRequest="250" HeightRequest="250"
x:Name="RecipeImage"/> Source="{Binding Recipe.Info.Image}"/>
</Border> </Border>
<!--Steps--> <!--Steps-->
@ -138,25 +138,25 @@
<!--Footer--> <!--Footer-->
<FlexLayout <Grid
Grid.Row="1" Grid.Row="1"
Direction="Row" ColumnDefinitions="Auto, *, Auto"
JustifyContent="SpaceBetween" HeightRequest="50"
AlignItems="Center"
> >
<views:CounterView <views:CounterView
Grid.Column="0"
x:Name="Counter" x:Name="Counter"
CounterText="pers" CounterText="pers"
HeightRequest="41"
Count="1"/> Count="1"/>
<Button <Button
Grid.Column="1" Grid.Column="2"
Text="Add to list"
Style="{StaticResource UserButton}" Style="{StaticResource UserButton}"
TextColor="White"
BackgroundColor="Gray" BackgroundColor="Gray"
Clicked="OnAddToMyListClicked"> x:Name="MyListStateButton"
</Button> Clicked="OnFooterButtonClicked"/>
</FlexLayout>
</Grid>
</Grid> </Grid>
</ContentPage> </ContentPage>

@ -11,16 +11,18 @@ public partial class RecipePage : ContentPage
private readonly IAccountRecipesPreferences preferences; private readonly IAccountRecipesPreferences preferences;
private readonly IUserNotifier notifier; private readonly IUserNotifier notifier;
public Recipe Recipe { get; init; }
private uint note; private uint note;
private bool isFavorite; private bool isFavorite;
private bool isInMyList;
public Recipe Recipe { get; init; }
public ICommand StarCommand => new Command<string>(count => SetNote(uint.Parse(count))); public ICommand StarCommand => new Command<string>(count => SetNote(uint.Parse(count)));
public RecipePage(Recipe recipe, IUserNotifier notifier, IAccountRecipesPreferences preferences, uint amount) public RecipePage(Recipe recipe, IUserNotifier notifier, IAccountRecipesPreferences preferences, uint amount)
{ {
Recipe = recipe; Recipe = recipe;
BindingContext = this; BindingContext = this;
@ -36,12 +38,10 @@ public partial class RecipePage : ContentPage
SetFavorite(isFavorite); SetFavorite(isFavorite);
SetNote(note); SetNote(note);
SetIsInMyListState(preferences.IsInWeeklyList(recipe.Info));
Counter.Count = amount; Counter.Count = amount;
if (recipe.Info.Image != null)
RecipeImage.Source = ImageSource.FromUri(recipe.Info.Image);
foreach (Ingredient ingredient in recipe.Ingredients) foreach (Ingredient ingredient in recipe.Ingredients)
IngredientList.Add(new IngredientView(ingredient)); 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) private void SetNote(uint note)
{ {
this.note = note; this.note = note;
@ -89,12 +102,37 @@ public partial class RecipePage : ContentPage
notifier.Success("Your review has been successfuly submited"); 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)) if (!preferences.AddToWeeklyList(Recipe.Info, Counter.Count))
{
notifier.Notice("You already added this recipe to you weekly list!"); notifier.Notice("You already added this recipe to you weekly list!");
}
else else
{
notifier.Success("Recipe added to your weekly list."); notifier.Success("Recipe added to your weekly list.");
}
} }
private void SetFavorite(bool isFavorite) private void SetFavorite(bool isFavorite)

Loading…
Cancel
Save