From 563dd0000c6589ae38e6d99cf578058514a0e284 Mon Sep 17 00:00:00 2001 From: Maxime BATISTA Date: Mon, 15 May 2023 11:35:45 +0200 Subject: [PATCH 1/4] pass database from xaml to json --- .../Data/CatastrophicPerformancesDatabase.cs | 21 ++++++++++--------- LocalServices/Data/Database.cs | 2 +- LocalServices/RecipesService.cs | 2 +- Services/IRecipesService.cs | 2 +- Views/FavoritesPage.xaml.cs | 2 +- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/LocalServices/Data/CatastrophicPerformancesDatabase.cs b/LocalServices/Data/CatastrophicPerformancesDatabase.cs index 32c48c1..0d490a0 100644 --- a/LocalServices/Data/CatastrophicPerformancesDatabase.cs +++ b/LocalServices/Data/CatastrophicPerformancesDatabase.cs @@ -5,6 +5,8 @@ using System.Collections.Immutable; using System.Linq; using System.Runtime.Serialization; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace LocalEndpoint.Data @@ -62,9 +64,11 @@ namespace LocalEndpoint.Data Save(ACCOUNTS_FILENAME, ACCOUNTS_SERIALIZER, accountsData); } - public Recipe GetRecipe(Guid id) + public Recipe? GetRecipe(Guid id) { - return ConvertRecipeDataToRecipe(recipesData[id]); + if (recipesData.TryGetValue(id, out RecipeData? data)) + return ConvertRecipeDataToRecipe(data); + return null; } public RecipeRate GetRecipeRate(Guid user, Guid recipe) @@ -141,19 +145,16 @@ namespace LocalEndpoint.Data if (fileInfo.Length == 0) return new Dictionary(); //file is empty thus there is nothing to deserialize - Console.WriteLine(File.ReadAllText(file)); - using (var stream = File.OpenRead(file)) - return deserializer.ReadObject(stream) as Dictionary ?? throw new Exception("object read from " + file + " is not a dictionnary"); + string text = File.ReadAllText(file); + + return JsonSerializer.Deserialize>(text); } private void Save(string fileName, DataContractSerializer serializer, Dictionary dict) { - using (var stream = File.OpenWrite(dbPath + "/" + fileName)) - { - serializer.WriteObject(stream, dict); - stream.Flush(); - } + string json = JsonSerializer.Serialize(dict); + File.WriteAllText(dbPath + "/" + fileName, json); } } diff --git a/LocalServices/Data/Database.cs b/LocalServices/Data/Database.cs index 130093a..03f3975 100644 --- a/LocalServices/Data/Database.cs +++ b/LocalServices/Data/Database.cs @@ -9,7 +9,7 @@ namespace LocalEndpoint.Data public interface Database { - public Recipe GetRecipe(Guid id); + public Recipe? GetRecipe(Guid id); public RecipeRate GetRecipeRate(Guid user, Guid recipe); diff --git a/LocalServices/RecipesService.cs b/LocalServices/RecipesService.cs index 8969e9e..f47b19e 100644 --- a/LocalServices/RecipesService.cs +++ b/LocalServices/RecipesService.cs @@ -21,7 +21,7 @@ namespace LocalEndpoint return db.ListAllRecipes().Take(4).ToImmutableList().ConvertAll(v => v.Info); } - public Recipe GetRecipe(RecipeInfo info) + public Recipe? GetRecipe(RecipeInfo info) { return db.GetRecipe(info.Id); } diff --git a/Services/IRecipesService.cs b/Services/IRecipesService.cs index 88e8b65..232da18 100644 --- a/Services/IRecipesService.cs +++ b/Services/IRecipesService.cs @@ -8,7 +8,7 @@ namespace Endpoint { public ImmutableList PopularRecipes(); - public Recipe GetRecipe(RecipeInfo info); + public Recipe? GetRecipe(RecipeInfo info); public IAccountOwnedRecipes GetRecipesOf(Account account); public IAccountRecipesPreferences GetPreferencesOf(Account account); diff --git a/Views/FavoritesPage.xaml.cs b/Views/FavoritesPage.xaml.cs index 20b9ef2..fa9bf2a 100644 --- a/Views/FavoritesPage.xaml.cs +++ b/Views/FavoritesPage.xaml.cs @@ -31,7 +31,7 @@ public partial class FavoritesPage : ContentPage { 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)); })); }); From 70b1237f2ed3c378678758473110750d02e2764d Mon Sep 17 00:00:00 2001 From: Maxime BATISTA Date: Mon, 22 May 2023 16:12:13 +0200 Subject: [PATCH 2/4] add error notification when a RecipeView is clicked without the recipe being found --- Views/HomePage.xaml.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Views/HomePage.xaml.cs b/Views/HomePage.xaml.cs index b74cfd2..3b46cb2 100644 --- a/Views/HomePage.xaml.cs +++ b/Views/HomePage.xaml.cs @@ -20,8 +20,13 @@ public partial class HomePage : ContentPage { layout.Children.Add(new RecipeView(info, () => { - Recipe recipe = service.GetRecipe(info); - Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, 1)); + Recipe? recipe = service.GetRecipe(info); + if (recipe != null) + Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, 1)); + else + { + notifier.Error("Could not find recipe"); + } })); } From be38c9174692b05dddc4ca3020688f25b8ba5dbf Mon Sep 17 00:00:00 2001 From: "maxime.BATISTA@etu.uca.fr" Date: Wed, 24 May 2023 12:06:03 +0200 Subject: [PATCH 3/4] parallelize some events and database mutating actions --- .editorconfig | 4 + Controllers/MorePageController.cs | 8 +- .../Data/CatastrophicPerformancesDatabase.cs | 14 +-- Views/ConfirmMail.xaml | 4 +- Views/CreateRecipePage.xaml.cs | 8 +- Views/FavoritesPage.xaml.cs | 6 +- Views/HomePage.xaml.cs | 86 +++++++++---------- Views/MyListPage.xaml.cs | 2 +- Views/MyRecipesPage.xaml.cs | 6 +- Views/RecipePage.xaml.cs | 10 +-- Views/RegisterPage.xaml.cs | 2 +- 11 files changed, 76 insertions(+), 74 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..cde8b2a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CS1998: Async method lacks 'await' operators and will run synchronously +dotnet_diagnostic.CS1998.severity = none diff --git a/Controllers/MorePageController.cs b/Controllers/MorePageController.cs index c8ecee8..b71aa18 100644 --- a/Controllers/MorePageController.cs +++ b/Controllers/MorePageController.cs @@ -24,14 +24,14 @@ namespace ShoopNCook.Controllers app.ForceLogin(); } - public void GoToMyRecipesPage() + public async void GoToMyRecipesPage() { - Shell.Current.Navigation.PushAsync(new MyRecipesPage(account, endpoint.RecipesService, app.Notifier)); + await Shell.Current.Navigation.PushAsync(new MyRecipesPage(account, endpoint.RecipesService, app.Notifier)); } - public void GoToProfilePage() + public async void GoToProfilePage() { - Shell.Current.Navigation.PushAsync(new ProfilePage(account)); + await Shell.Current.Navigation.PushAsync(new ProfilePage(account)); } } } diff --git a/LocalServices/Data/CatastrophicPerformancesDatabase.cs b/LocalServices/Data/CatastrophicPerformancesDatabase.cs index 0d490a0..451a424 100644 --- a/LocalServices/Data/CatastrophicPerformancesDatabase.cs +++ b/LocalServices/Data/CatastrophicPerformancesDatabase.cs @@ -76,38 +76,38 @@ namespace LocalEndpoint.Data return usersData[user].Rates[recipe]; } - public void InsertInUserList(Guid userId, Guid recipeId, uint persAmount) + public async void InsertInUserList(Guid userId, Guid recipeId, uint persAmount) { usersData[userId].RecipesList[recipeId] = persAmount; Save(USERS_FILENAME, USERS_SERIALIZER, usersData); } - public void RemoveFromUserList(Guid userId, Guid recipeId) + public async void RemoveFromUserList(Guid userId, Guid recipeId) { usersData[userId].RecipesList.Remove(recipeId); Save(USERS_FILENAME, USERS_SERIALIZER, usersData); } - public void InsertRecipe(Recipe recipe) + public async void InsertRecipe(Recipe recipe) { recipesData[recipe.Info.Id] = new RecipeData(recipe.Info, recipe.Owner.Id, recipe.Ingredients, recipe.Steps); Save(RECIPES_FILENAME, RECIPES_SERIALIZER, recipesData); } - public void InsertUser(User user) + public async void InsertUser(User user) { usersData[user.Id] = new UserData(user, new Dictionary(), new Dictionary()); Save(USERS_FILENAME, USERS_SERIALIZER, usersData); } - public void InsertRate(Guid userId, Guid recipeId, RecipeRate rate) + public async void InsertRate(Guid userId, Guid recipeId, RecipeRate rate) { usersData[userId].Rates[recipeId] = rate; Save(USERS_FILENAME, USERS_SERIALIZER, usersData); } - public void RemoveRecipe(Guid id) + public async void RemoveRecipe(Guid id) { recipesData.Remove(id); Save(RECIPES_FILENAME, RECIPES_SERIALIZER, recipesData); @@ -151,7 +151,7 @@ namespace LocalEndpoint.Data return JsonSerializer.Deserialize>(text); } - private void Save(string fileName, DataContractSerializer serializer, Dictionary dict) + private async void Save(string fileName, DataContractSerializer serializer, Dictionary dict) { string json = JsonSerializer.Serialize(dict); File.WriteAllText(dbPath + "/" + fileName, json); diff --git a/Views/ConfirmMail.xaml b/Views/ConfirmMail.xaml index ae22bca..a4458ea 100644 --- a/Views/ConfirmMail.xaml +++ b/Views/ConfirmMail.xaml @@ -37,11 +37,9 @@ + RowDefinitions="*, Auto, Auto">