support more object types by using JSON strings

This commit is contained in:
Kyle Spearrin 2019-04-08 20:48:18 -04:00
parent 24b4073616
commit 992cf033f2
2 changed files with 10 additions and 8 deletions

View file

@ -1,4 +1,5 @@
using Bit.Core.Abstractions;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
@ -49,7 +50,8 @@ namespace Bit.Core.Services
}
else
{
throw new Exception("Unsupported object type for preferences.");
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string));
return Task.FromResult(JsonConvert.DeserializeObject<T>(val));
}
}
@ -88,7 +90,7 @@ namespace Bit.Core.Services
}
else
{
throw new Exception("Unsupported object type for preferences.");
Xamarin.Essentials.Preferences.Set(formattedKey, JsonConvert.SerializeObject(obj));
}
return Task.FromResult(0);
}

View file

@ -1,4 +1,5 @@
using Bit.Core.Abstractions;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
@ -10,16 +11,16 @@ namespace Bit.Core.Services
public async Task<T> GetAsync<T>(string key)
{
var formattedKey = string.Format(_keyFormat, key);
var val = await Xamarin.Essentials.SecureStorage.GetAsync(formattedKey);
var objType = typeof(T);
if(objType == typeof(string))
{
var formattedKey = string.Format(_keyFormat, key);
var val = await Xamarin.Essentials.SecureStorage.GetAsync(formattedKey);
return (T)(object)val;
}
else
{
throw new Exception("Unsupported object type for secure storage.");
return JsonConvert.DeserializeObject<T>(val);
}
}
@ -30,16 +31,15 @@ namespace Bit.Core.Services
await RemoveAsync(key);
return;
}
var formattedKey = string.Format(_keyFormat, key);
var objType = typeof(T);
if(objType == typeof(string))
{
var formattedKey = string.Format(_keyFormat, key);
await Xamarin.Essentials.SecureStorage.SetAsync(formattedKey, obj as string);
}
else
{
throw new Exception("Unsupported object type for secure storage.");
await Xamarin.Essentials.SecureStorage.SetAsync(formattedKey, JsonConvert.SerializeObject(obj));
}
}