1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-09-25 03:33:01 +02:00
bitwarden-mobile/src/Core/Services/PreferencesStorageService.cs

98 lines
3.4 KiB
C#
Raw Normal View History

2019-03-28 04:44:54 +01:00
using Bit.Core.Abstractions;
using System;
using System.Threading.Tasks;
namespace Bit.Core.Services
{
public class PreferencesStorageService : IStorageService
{
private string _keyFormat = "bwPref:{0}";
public Task<T> GetAsync<T>(string key)
{
var formattedKey = string.Format(_keyFormat, key);
if(!Xamarin.Essentials.Preferences.ContainsKey(formattedKey))
{
return Task.FromResult(default(T));
}
var objType = typeof(T);
if(objType == typeof(string))
{
2019-03-28 19:01:36 +01:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string));
2019-03-28 04:44:54 +01:00
return Task.FromResult((T)(object)val);
}
else if(objType == typeof(int))
{
2019-03-28 19:01:36 +01:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(int));
2019-03-28 04:44:54 +01:00
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else if(objType == typeof(long))
{
2019-03-28 19:01:36 +01:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(long));
2019-03-28 04:44:54 +01:00
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else if(objType == typeof(double))
{
2019-03-28 19:01:36 +01:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(double));
2019-03-28 04:44:54 +01:00
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else if(objType == typeof(DateTime))
{
2019-03-28 19:01:36 +01:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(DateTime));
2019-03-28 04:44:54 +01:00
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else
{
throw new Exception("Unsupported object type for preferences.");
}
}
public Task SaveAsync<T>(string key, T obj)
{
2019-03-28 19:01:36 +01:00
var formattedKey = string.Format(_keyFormat, key);
2019-03-28 04:44:54 +01:00
if(obj == null)
{
2019-03-28 19:01:36 +01:00
return RemoveAsync(formattedKey);
2019-03-28 04:44:54 +01:00
}
var objType = typeof(T);
if(objType == typeof(string))
{
2019-03-28 19:01:36 +01:00
Xamarin.Essentials.Preferences.Set(formattedKey, obj as string);
2019-03-28 04:44:54 +01:00
}
else if(objType == typeof(int))
{
2019-03-28 19:01:36 +01:00
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as int?).Value);
2019-03-28 04:44:54 +01:00
}
else if(objType == typeof(long))
{
2019-03-28 19:01:36 +01:00
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as long?).Value);
2019-03-28 04:44:54 +01:00
}
else if(objType == typeof(double))
{
2019-03-28 19:01:36 +01:00
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as double?).Value);
2019-03-28 04:44:54 +01:00
}
else if(objType == typeof(DateTime))
{
2019-03-28 19:01:36 +01:00
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as DateTime?).Value);
2019-03-28 04:44:54 +01:00
}
else
{
throw new Exception("Unsupported object type for preferences.");
}
return Task.FromResult(0);
}
public Task RemoveAsync(string key)
{
2019-03-28 19:01:36 +01:00
var formattedKey = string.Format(_keyFormat, key);
if(Xamarin.Essentials.Preferences.ContainsKey(formattedKey))
2019-03-28 04:44:54 +01:00
{
2019-03-28 19:01:36 +01:00
Xamarin.Essentials.Preferences.Remove(formattedKey);
2019-03-28 04:44:54 +01:00
}
return Task.FromResult(0);
}
}
}