diff --git a/MCTG/Views/ContentPages/AddRecipe.xaml.cs b/MCTG/Views/ContentPages/AddRecipe.xaml.cs index aa95ce7..471a4a1 100644 --- a/MCTG/Views/ContentPages/AddRecipe.xaml.cs +++ b/MCTG/Views/ContentPages/AddRecipe.xaml.cs @@ -13,6 +13,9 @@ namespace Views private Ingredient ingredient; private PreparationStep preparationStep; private string titleRecipe; + + public FileResult ImageSource { get; private set; } = null; + public string? ImageSourcePath { get; private set; } = null; public MasterManager Master => (Application.Current as App).Master; public User CurrentUser => Master.User.CurrentConnected; public Recipe RecipeToAdd{ get=> recipeToAdd; set => recipeToAdd = value; } @@ -38,20 +41,30 @@ namespace Views IngredientList = new List(); PreparationStepList = new List(); } - private void PickPhoto(object sender, EventArgs e) + private async void PickPhoto(object sender, EventArgs e) { - MediaPicker.PickPhotoAsync(); + ImageSource = await MediaPicker.Default.PickPhotoAsync(); } - private void AddRecipeValidation(object sender, EventArgs e) + private async void AddRecipeValidation(object sender, EventArgs e) { - if (string.IsNullOrWhiteSpace(TitleRecipe)) { - DisplayAlert("Erreur", "Entrez un nom de recette.", "Ok"); + await DisplayAlert("Erreur", "Entrez un nom de recette.", "Ok"); return; } + if (ImageSource != null) + { + // save the file into local storage + ImageSourcePath = Path.Combine(FileSystem.Current.AppDataDirectory, $"{TitleRecipe}.{ImageSource.FileName}"); + + using Stream sourceStream = await ImageSource.OpenReadAsync(); + using FileStream localFileStream = File.OpenWrite(ImageSourcePath); + + await sourceStream.CopyToAsync(localFileStream); + } + RecipeType newRecipeType = GetSelectedRecipeType(); Priority selectedPriority = GetSelectedPriority(); string authorMail = CurrentUser.Mail; @@ -62,7 +75,8 @@ namespace Views newRecipeType, selectedPriority, null, - authorMail + authorMail, + ImageSourcePath ); newRecipe.PreparationSteps.AddRange(PreparationStepList); newRecipe.Ingredients.AddRange(IngredientList); @@ -70,18 +84,18 @@ namespace Views bool isRecipeSave = Master.Recipe.AddRecipeToData(newRecipe); if (isRecipeSave) { - DisplayAlert("Succès", "La recette a été ajoutée avec succès", "OK"); + await DisplayAlert("Succès", "La recette a été ajoutée avec succès", "OK"); } else { - DisplayAlert("Echec", "La recette n'a pas été ajoutée", "OK"); + await DisplayAlert("Echec", "La recette n'a pas été ajoutée", "OK"); } newRecipe = new Recipe("Nouvelle Recette"); PreparationStepList.Clear(); IngredientList.Clear(); - Navigation.PopAsync(); + await Navigation.PopAsync(); } private void AddStepRecipe(object sender, EventArgs e) @@ -115,6 +129,12 @@ namespace Views private void AddIngredient(object sender, EventArgs e) { + if (nameIngredient.Text is null || quantityNumber.Text is null) + { + DisplayAlert("Warning", "some values are null, please provide correct values.", "Ok"); + return; + } + string ingredientName = nameIngredient.Text; int numberQuantity = Convert.ToInt32(quantityNumber.Text); Unit unitQuantity = (Unit)UnitPicker.SelectedItem; diff --git a/MCTG/Views/ContentPages/MyPosts.xaml b/MCTG/Views/ContentPages/MyPosts.xaml index bf8dcac..fc7bc07 100644 --- a/MCTG/Views/ContentPages/MyPosts.xaml +++ b/MCTG/Views/ContentPages/MyPosts.xaml @@ -1,6 +1,7 @@ @@ -8,45 +9,59 @@ + - + -