use formatted key

This commit is contained in:
Kyle Spearrin 2019-03-28 14:01:36 -04:00
parent 86f1874379
commit 9042702c09

View file

@ -19,27 +19,27 @@ namespace Bit.Core.Services
var objType = typeof(T);
if(objType == typeof(string))
{
var val = Xamarin.Essentials.Preferences.Get(key, default(string));
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string));
return Task.FromResult((T)(object)val);
}
else if(objType == typeof(int))
{
var val = Xamarin.Essentials.Preferences.Get(key, default(int));
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(int));
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else if(objType == typeof(long))
{
var val = Xamarin.Essentials.Preferences.Get(key, default(long));
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(long));
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else if(objType == typeof(double))
{
var val = Xamarin.Essentials.Preferences.Get(key, default(double));
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(double));
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else if(objType == typeof(DateTime))
{
var val = Xamarin.Essentials.Preferences.Get(key, default(DateTime));
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(DateTime));
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else
@ -50,31 +50,32 @@ namespace Bit.Core.Services
public Task SaveAsync<T>(string key, T obj)
{
var formattedKey = string.Format(_keyFormat, key);
if(obj == null)
{
return RemoveAsync(key);
return RemoveAsync(formattedKey);
}
var objType = typeof(T);
if(objType == typeof(string))
{
Xamarin.Essentials.Preferences.Set(key, obj as string);
Xamarin.Essentials.Preferences.Set(formattedKey, obj as string);
}
else if(objType == typeof(int))
{
Xamarin.Essentials.Preferences.Set(key, (obj as int?).Value);
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as int?).Value);
}
else if(objType == typeof(long))
{
Xamarin.Essentials.Preferences.Set(key, (obj as long?).Value);
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as long?).Value);
}
else if(objType == typeof(double))
{
Xamarin.Essentials.Preferences.Set(key, (obj as double?).Value);
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as double?).Value);
}
else if(objType == typeof(DateTime))
{
Xamarin.Essentials.Preferences.Set(key, (obj as DateTime?).Value);
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as DateTime?).Value);
}
else
{
@ -85,9 +86,10 @@ namespace Bit.Core.Services
public Task RemoveAsync(string key)
{
if(Xamarin.Essentials.Preferences.ContainsKey(key))
var formattedKey = string.Format(_keyFormat, key);
if(Xamarin.Essentials.Preferences.ContainsKey(formattedKey))
{
Xamarin.Essentials.Preferences.Remove(key);
Xamarin.Essentials.Preferences.Remove(formattedKey);
}
return Task.FromResult(0);
}