1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-14 23:33:34 +00:00

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 Bit.Core.Abstractions;
using Newtonsoft.Json;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -49,7 +50,8 @@ namespace Bit.Core.Services
} }
else 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 else
{ {
throw new Exception("Unsupported object type for preferences."); Xamarin.Essentials.Preferences.Set(formattedKey, JsonConvert.SerializeObject(obj));
} }
return Task.FromResult(0); return Task.FromResult(0);
} }

View File

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