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
{
///
/// Master manager - access to the Model.
///
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();
}
///
/// 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;
}
}
}