You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SAE-2.01/MCTG/Views/ContentPages/AddRecipe.xaml.cs

197 lines
6.9 KiB

using Model;
using System.Diagnostics;
using Microsoft.Maui.Media;
using Microsoft.Maui.Controls;
namespace Views
{
public partial class AddRecipe : ContentPage
{
private List<Ingredient> ingredientList;
private List<PreparationStep> preparationStepList;
private Recipe recipeToAdd;
private Ingredient ingredient;
private PreparationStep preparationStep;
private string titleRecipe;
public MasterManager Master => (Application.Current as App).Master;
public User CurrentUser => Master.User.CurrentConnected;
public Recipe RecipeToAdd{ get=> recipeToAdd; set => recipeToAdd = value; }
public List<Unit> UnitList { get; set; } = new List<Unit> { Unit.unit, Unit.kG, Unit.mG, Unit.G, Unit.L, Unit.cL, Unit.mL };
public List<Ingredient> IngredientList { get => ingredientList; set => ingredientList = value; }
public List<PreparationStep> PreparationStepList { get => preparationStepList; set => preparationStepList = value; }
public Ingredient Ingredient { get => ingredient; set => ingredient = value ; }
public string TitleRecipe { get=> titleRecipe; set => titleRecipe = value; }
public PreparationStep PreparationStepAdd { get => preparationStep; set => preparationStep = value; }
public static readonly BindableProperty PreparationStepOrderProperty =
BindableProperty.Create(nameof(PreparationStepOrder), typeof(int), typeof(Entry), 1);
public int PreparationStepOrder
{
get => (int)GetValue(PreparationStepOrderProperty);
set => SetValue(PreparationStepOrderProperty, value);
}
public AddRecipe()
{
InitializeComponent();
BindingContext = this;
IngredientList = new List<Ingredient>();
PreparationStepList = new List<PreparationStep>();
}
private void PickPhoto(object sender, EventArgs e)
{
MediaPicker.PickPhotoAsync();
}
private void AddRecipeValidation(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(TitleRecipe))
{
DisplayAlert("Erreur", "Entrez un nom de recette.", "Ok");
return;
}
RecipeType newRecipeType = GetSelectedRecipeType();
Priority selectedPriority = GetSelectedPriority();
string authorMail = CurrentUser.Mail;
Recipe newRecipe = new Recipe
(
TitleRecipe,
newRecipeType,
selectedPriority,
null,
authorMail
);
newRecipe.PreparationSteps.AddRange(PreparationStepList);
newRecipe.Ingredients.AddRange(IngredientList);
bool isRecipeSave = Master.Recipe.AddRecipeToData(newRecipe);
if (isRecipeSave)
{
DisplayAlert("Succ<63>s", "La recette a <20>t<EFBFBD> ajout<75>e avec succ<63>s", "OK");
}
else
{
DisplayAlert("Echec", "La recette n'a pas <20>t<EFBFBD> ajout<75>e", "OK");
}
newRecipe = new Recipe("Nouvelle Recette");
PreparationStepList.Clear();
IngredientList.Clear();
Navigation.PopAsync();
}
private void AddStepRecipe(object sender, EventArgs e)
{
string description = PreparationDescription.Text;
PreparationStep PreparationStepAdd = new PreparationStep
(
PreparationStepOrder,
description
);
PreparationStepList.Add( PreparationStepAdd );
PreparationStepOrder++;
PreparationDescription.Text = string.Empty;
}
private void RemoveStepRecipe(object sender, EventArgs e)
{
if (PreparationStepList.Count > 0)
{
PreparationStepList.RemoveAt(PreparationStepList.Count - 1);
PreparationStepOrder--;
DisplayAlert("Suppression", "La suppression de l'<27>tape de la recette est effectu<74>e avec succ<63>s", "Ok");
}
else
{
DisplayAlert("Liste d'<27>tape vide", "Suppression impossible car la liste des <20>tapes de description est vide.", "Ok");
}
}
private void AddIngredient(object sender, EventArgs e)
{
string ingredientName = nameIngredient.Text;
int numberQuantity = Convert.ToInt32(quantityNumber.Text);
Unit unitQuantity = (Unit)UnitPicker.SelectedItem;
Ingredient newIngredient = new Ingredient
(
ingredientName,
new Quantity
(
numberQuantity,
unitQuantity
)
);
IngredientList.Add( newIngredient );
nameIngredient.Text = string.Empty;
quantityNumber.Text = string.Empty;
UnitPicker.SelectedItem = null;
}
private void RemoveIngredient(object sender, EventArgs e)
{
if (IngredientList.Count > 0)
{
IngredientList.RemoveAt(IngredientList.Count - 1);
DisplayAlert("Suppression", "La suppression de l'ingr<67>dient est effectu<74>e avec succ<63>s", "Ok");
}
else
{
DisplayAlert("Liste d'ingr<67>dient vide", "Suppression impossible car la liste des ingr<67>dients est vide.", "Ok");
}
}
private RecipeType GetSelectedRecipeType()
{
if (CheckEntree.IsChecked)
{
return RecipeType.Starter;
}
else if (CheckPlat.IsChecked)
{
return RecipeType.Dish;
}
else if (CheckDessert.IsChecked)
{
return RecipeType.Dessert;
}
else
{
return RecipeType.Unspecified;
}
}
private Priority GetSelectedPriority()
{
if (checkEconomique.IsChecked)
{
return Priority.Economic;
}
else if (checkFast.IsChecked)
{
return Priority.Fast;
}
else if (checkEasy.IsChecked)
{
return Priority.Easy;
}
else if (checkLight.IsChecked)
{
return Priority.Light;
}
else if (checkGourmet.IsChecked)
{
return Priority.Gourmet;
}
else
{
return Priority.Gourmet;
}
}
}
}