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/App.xaml.cs

102 lines
3.8 KiB

using Model;
using FakePersistance;
using DataPersistence;
using Managers;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Diagnostics.CodeAnalysis;
namespace Views
{
public partial class App : Application
{
/// <summary>
/// Master manager - access to the Model.
/// </summary>
public MasterManager Master { get; private set; }
public App()
{
Debug.WriteLine("Hello, World!\n\n");
string path = FileSystem.Current.AppDataDirectory; // - path to the save file
string strategy = "xml"; // - strategy is 'xml' or 'json' (/!\ this is case sensitive)
// Initialize the data serializer
IDataSerializer dataSerializer = (strategy == "xml") ?
new DataContractXML(path)
: new DataContractJSON(path);
// Initialize the data manager
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);
IPasswordManager passwordManager = new PasswordSHA256Manager();
IUserManager userManager = new UserDefaultManager(dataManager, passwordManager);
// Initialize the master manager
Master = new MasterManager(dataManager, recipeManager, userManager);
// Save data.
Debug.Write($"[ {DateTime.Now:H:mm:ss} ] Saving...\t");
Master.Data.SaveData();
Debug.WriteLine("Done.");
InitializeComponent();
UserAppTheme = AppTheme.Light;
MainPage = new AddRecipe();
//MainPage = new MyPosts();
}
protected override void OnSleep()
{
// Save data.
Debug.Write($"[ {DateTime.Now:H:mm:ss} ] Saving...\t");
Master.Data.SaveData();
Debug.WriteLine("Done.");
Debug.WriteLine(FileSystem.Current.AppDataDirectory);
base.OnSleep();
}
/// <summary>
/// Load XML raw assets from data.
/// </summary>
/// <param name="path">The path in the raw assets directory.</param>
/// <returns>A dictionary containing the data loaded.</returns>
private static IDictionary<string, List<object>> 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<string, List<object>>), _dataContractSerializerSettings);
IDictionary<string, List<Object>> data;
using Stream stream = FileSystem.Current.OpenAppPackageFileAsync(path).Result;
data = serializer.ReadObject(stream) as IDictionary<string, List<Object>>;
return data;
}
}
}