diff --git a/MCTG/Managers/DataDefaultManager.cs b/MCTG/Managers/DataDefaultManager.cs index 5326602..52f8409 100644 --- a/MCTG/Managers/DataDefaultManager.cs +++ b/MCTG/Managers/DataDefaultManager.cs @@ -10,19 +10,19 @@ namespace Managers #region Attributes & Properties public IDataSerializer Serializer { get; set; } - public Dictionary> Data { get; private set; } + public IDictionary> Data { get; private set; } #endregion #region Constructors /// /// Constructor of the DataDefaultManager class. Take a IDataManager that will provide methods for the serialisation of the data. /// - /// The data manager that know how to serialize a file. + /// The data manager that know how to serialize a file. /// The data set of the application. - public DataDefaultManager(IDataSerializer dataManager, - Dictionary>? data = null) + public DataDefaultManager(IDataSerializer dataSerializer, + IDictionary>? data = null) { - Serializer = dataManager; + Serializer = dataSerializer; if (data is null) Data = new Dictionary>(); diff --git a/MCTG/Model/Managers/IDataManager.cs b/MCTG/Model/Managers/IDataManager.cs index 0358988..db1f408 100644 --- a/MCTG/Model/Managers/IDataManager.cs +++ b/MCTG/Model/Managers/IDataManager.cs @@ -22,7 +22,7 @@ namespace Model /// /// The collection of all data. Each line of this dictionary has the type of the data as it key and the data for values. /// - Dictionary> Data { get; } + IDictionary> Data { get; } /// diff --git a/MCTG/Model/Managers/IDataSerializer.cs b/MCTG/Model/Managers/IDataSerializer.cs index eba0cb6..adcedcd 100644 --- a/MCTG/Model/Managers/IDataSerializer.cs +++ b/MCTG/Model/Managers/IDataSerializer.cs @@ -15,13 +15,13 @@ namespace Model /// Save all the data in a file. /// /// The data to save. - void Save(Dictionary> elements); + void Save(IDictionary> elements); /// /// Load all the data from a file. /// /// The data loaded. - Dictionary> Load(); + IDictionary> Load(); /// /// Import an element to the collection of data. diff --git a/MCTG/Persistance/DataPersistence/DataContractJSON.cs b/MCTG/Persistance/DataPersistence/DataContractJSON.cs index bdb25d4..8d0544a 100644 --- a/MCTG/Persistance/DataPersistence/DataContractJSON.cs +++ b/MCTG/Persistance/DataPersistence/DataContractJSON.cs @@ -77,7 +77,7 @@ namespace DataPersistence return new KeyValuePair(typeName, obj); } - public Dictionary> Load() + public IDictionary> Load() { Dictionary>? elements = new Dictionary>(); var jsonSerializer = new DataContractJsonSerializer(typeof(Dictionary>), _dataContractJsonSerializerSettings); @@ -92,7 +92,7 @@ namespace DataPersistence return elements; } - public void Save(Dictionary> elements) + public void Save(IDictionary> elements) { var jsonSerializer = new DataContractJsonSerializer(typeof(Dictionary>), _dataContractJsonSerializerSettings); using (FileStream stream = File.Create(Path.Combine(_jsonFolderPath, "data.json"))) diff --git a/MCTG/Persistance/DataPersistence/DataContractXML.cs b/MCTG/Persistance/DataPersistence/DataContractXML.cs index e8443c4..fd95a54 100644 --- a/MCTG/Persistance/DataPersistence/DataContractXML.cs +++ b/MCTG/Persistance/DataPersistence/DataContractXML.cs @@ -87,7 +87,7 @@ namespace DataPersistence return new KeyValuePair(typeName, obj); } - public Dictionary> Load() + public IDictionary> Load() { Dictionary>? elements; var serializer = new DataContractSerializer(typeof(Dictionary>), _dataContractSerializerSettings); @@ -102,7 +102,7 @@ namespace DataPersistence return elements; } - public void Save(Dictionary> elements) + public void Save(IDictionary> elements) { var serializer = new DataContractSerializer(typeof(Dictionary>), _dataContractSerializerSettings); using (TextWriter textWriter = File.CreateText(Path.Combine(_xmlFolderPath, "data.xml"))) diff --git a/MCTG/Persistance/FakePersistance/Stubs.cs b/MCTG/Persistance/FakePersistance/Stubs.cs index 2104e48..baa8bec 100644 --- a/MCTG/Persistance/FakePersistance/Stubs.cs +++ b/MCTG/Persistance/FakePersistance/Stubs.cs @@ -13,7 +13,7 @@ namespace FakePersistance { private IPasswordManager psswdMgr = new PasswordSHA256Manager(); - public Dictionary> Load() + public IDictionary> Load() { Dictionary> data = new Dictionary> { @@ -186,7 +186,7 @@ namespace FakePersistance } #region Not supported methods - public void Save(Dictionary> elements) + public void Save(IDictionary> elements) { throw new NotSupportedException(); } diff --git a/MCTG/Views/App.xaml.cs b/MCTG/Views/App.xaml.cs index 1a42638..94d46da 100644 --- a/MCTG/Views/App.xaml.cs +++ b/MCTG/Views/App.xaml.cs @@ -3,6 +3,8 @@ using FakePersistance; using DataPersistence; using Managers; using System.Diagnostics; +using System.Runtime.Serialization; +using System.Diagnostics.CodeAnalysis; namespace Views { @@ -26,9 +28,17 @@ namespace Views : new DataContractJSON(path); // Initialize the data manager - IDataManager dataManager = (!File.Exists(Path.Combine(path, $"data.{strategy}"))) ? - new DataDefaultManager(new Stubs()) - : new DataDefaultManager(dataSerializer); + IDataManager dataManager; + if (!File.Exists(Path.Combine(path, $"data.{strategy}"))) + { + var data = LoadXMLBundledFilesAsync("data.xml"); + dataManager = new DataDefaultManager(dataSerializer, data); + } + else + { + dataManager = new DataDefaultManager(dataSerializer); + dataManager.LoadData(); + } // Initialize the other managers IRecipeManager recipeManager = new RecipeDefaultManager(dataManager); @@ -37,16 +47,9 @@ namespace Views // Initialize the master manager Master = new MasterManager(dataManager, recipeManager, userManager); - Master.Setup(); - - // Change the data serializer if the one in place is 'Stubs' - if (Master.Data.Serializer.GetType() == typeof(Stubs)) - { - Master.Data.Serializer = dataSerializer; - } // Save data. - Debug.Write("[ --SAVE-- ]:\t"); + Debug.Write($"[ {DateTime.Now:H:mm:ss} ] Saving...\t"); Master.Data.SaveData(); Debug.WriteLine("Done."); @@ -59,7 +62,7 @@ namespace Views protected override void OnSleep() { // Save data. - Debug.Write("[ --SAVE-- ]:\t"); + Debug.Write($"[ {DateTime.Now:H:mm:ss} ] Saving...\t"); Master.Data.SaveData(); Debug.WriteLine("Done."); @@ -67,5 +70,31 @@ namespace Views base.OnSleep(); } + + /// + /// Load XML raw assets from data. + /// + /// The path in the raw assets directory. + /// A dictionary containing the data loaded. + private static IDictionary> LoadXMLBundledFilesAsync(string path) + { + //using Stream stream = await FileSystem.Current.OpenAppPackageFileAsync(path); + DataContractSerializerSettings _dataContractSerializerSettings + = new DataContractSerializerSettings() + { + KnownTypes = new Type[] + { + typeof(Recipe), typeof(RecipeType), typeof(Priority), typeof(Review), typeof(User), typeof(Ingredient), typeof(Quantity) + }, + PreserveObjectReferences = true + }; + var serializer = new DataContractSerializer(typeof(Dictionary>), _dataContractSerializerSettings); + IDictionary> data; + + using Stream stream = FileSystem.Current.OpenAppPackageFileAsync(path).Result; + data = serializer.ReadObject(stream) as IDictionary>; + + return data; + } } } diff --git a/MCTG/Views/Resources/Raw/data.xml b/MCTG/Views/Resources/Raw/data.xml new file mode 100644 index 0000000..00d3f13 --- /dev/null +++ b/MCTG/Views/Resources/Raw/data.xml @@ -0,0 +1,368 @@ + + + + Recipe + + + admin@mctg.fr + 9806 + room_service_icon.png + + + Patates + + 23 + unit + + + + Farine + + 23 + G + + + + + + Faire cuire. + 1 + + + Manger. + 2 + + + Easy + + + + Bonne recette, je recommande ! + 30967 + 4 + + + + Bof bof, mais mangeable... + 27370 + 3 + + + Cookies classiques + Dessert + + + + 4678 + + + + + + 200 + G + + + + + + Moulinez la pâte. + 1 + + + Faire cuire pendant une bonne heure. + 2 + + + Sortir du four et mettre dans un plat. + 3 + + + Fast + + Cookies au chocolat + Dessert + + + + 28213 + + + + + + 200 + G + + + + Lait + + 2 + L + + + + + + Achetez les ingrédients. + 1 + + + Préparez le matériel. Ustensiles et tout. + 2 + + + Pleurez. + 3 + + + Gourmet + + + pedrosamigos@hotmail.com + C'était vraiment IN-CROY-ABLE !!! + 5127 + 5 + + + Gateau nature + Dessert + + + + 27448 + + + + + Achetez les légumes. + 1 + + + Préparez le plat. Ustensiles et préchauffez le four. + 2 + + + Coupez les pommes en morceaux et disposez-les sur le plat. + 3 + + + Mettez enfin le plat au four, puis une fois cuit, dégustez ! + 4 + + + Light + + Gateau au pommes + Dessert + + + + 14217 + + + + Mais + + 2 + kG + + + + Sachet pépites de chocolat + + 1 + unit + + + + Dinde + + 2 + G + + + + + + Ajouter les oeufs. + 1 + + + Ajouter la farine. + 2 + + + Ajouter 100g de chocolat fondu. + 3 + + + Mélanger le tout. + 4 + + + Faire cuire 45h au four traditionnel. + 5 + + + Economic + + Gateau au chocolat + Dessert + + + + 3856 + + + + Morceaux de bois + + 2 + unit + + + + Sachet gélatine + + 1 + unit + + + + Jambon + + 2 + kG + + + + + + Faire une cuisson bien sec de la dinde à la poêle + 1 + + + Mettre la dinde au frigo. + 2 + + + Mettre le jambon dans le micro-onde. + 3 + + + Faire chauffer 3min. + 4 + + + Présentez sur un plat la dinde et le jambon : Miam ! + 5 + + + Easy + + Dinde au jambon + Dish + + + + 29272 + + + + Pissenlis + + 200 + unit + + + + Boule de pétanque + + 10 + unit + + + + Poivre + + 4 + mG + + + + + + Trouvez des épices de curry. + 1 + + + Trouvez maintenant du poulet. + 2 + + + Coupez la tête du poulet et posez-la dans un plat. + 3 + + + Parsemez d'épices curry la tête de la poule. + 4 + + + Mettre le tout au four traditionnel 30min. + 5 + + + Dégustez en famille ! + 6 + + + Gourmet + + + + Meilleure recette que j'ai avalé de tout les temps !!!!!!! + 7846 + 5 + + + Poulet au curry + Dish + + + + + User + + + 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 + + Admin + + Gourmet + Economic + Fast + Light + Easy + + default_picture.png + + + + df7415f099b2e105822cb6052a0de0a4eb6a4c4060b5ea191bff1271e1c377fa + + Pedros + + Gourmet + Economic + Fast + Light + Easy + + + Amigos + + + + \ No newline at end of file