using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Serilog;
using Xamarin.Essentials;
namespace TINK.Model.User.Account
{
public class Store : IStore
{
/// Holds id of the debug level key.
private const string KEY_DEBUGLEVEL = "DebugLevel";
/// Holds the id of the session.
private const string KEY_SESSIONCOOKIE = "SessionCookie";
/// Holds id of the mail address key.
private const string KEY_MAILADDRESS = "MailAddress";
/// Holds key for flag is agb acknowledged.
private const string KEY_ISAGBACKNOWLEDGED = "IsAgbAcknowledged";
public IAccount Delete(IAccount account)
{
SecureStorage.RemoveAll();
return new EmptyAccount();
}
public async Task Load()
{
var mail = string.Empty;
var isAgbAcknowledged = Account.DEFAULTISAGBACKNOWLEDGED;
var sessionCookie = string.Empty;
var debugLevel = Permissions.None;
try
{
mail = await SecureStorage.GetAsync(KEY_MAILADDRESS);
bool.TryParse(await SecureStorage.GetAsync(KEY_ISAGBACKNOWLEDGED), out isAgbAcknowledged);
sessionCookie = await SecureStorage.GetAsync(KEY_SESSIONCOOKIE);
Enum.TryParse(await SecureStorage.GetAsync(KEY_DEBUGLEVEL), out debugLevel);
}
catch (Exception exception)
{
Log.ForContext().Error("Loading account from store failed. {Exception}", exception);
}
return new Account(
mail,
string.Empty,
isAgbAcknowledged,
sessionCookie,
new List(),
debugLevel);
}
public async Task Save(IAccount mailAndPwd)
{
try
{
await SecureStorage.SetAsync(KEY_MAILADDRESS, mailAndPwd.Mail);
await SecureStorage.SetAsync(KEY_ISAGBACKNOWLEDGED, mailAndPwd.IsAgbAcknowledged.ToString());
await SecureStorage.SetAsync(KEY_SESSIONCOOKIE, mailAndPwd.SessionCookie);
await SecureStorage.SetAsync(KEY_DEBUGLEVEL, mailAndPwd.DebugLevel.ToString());
}
catch (Exception exception)
{
Log.ForContext().Error("Saving account from store failed. {Exception}", exception);
}
}
}
}