1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-13 10:14:16 +01:00
bitwarden-mobile/src/Core/Services/PreferencesStorageService.cs

98 lines
3.6 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
{
2019-03-28 19:09:33 +01:00
private string _keyFormat = "bwPreferencesStorage:{0}";
2019-03-28 04:44:54 +01:00
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);
}
2019-04-09 02:32:12 +02:00
else if(objType == typeof(int) || objType == typeof(int?))
2019-03-28 04:44:54 +01:00
{
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));
}
2019-04-09 02:32:12 +02:00
else if(objType == typeof(long) || objType == typeof(long?))
2019-03-28 04:44:54 +01:00
{
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));
}
2019-04-09 02:32:12 +02:00
else if(objType == typeof(double) || objType == typeof(double?))
2019-03-28 04:44:54 +01:00
{
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));
}
2019-04-09 02:32:12 +02:00
else if(objType == typeof(DateTime) || objType == typeof(DateTime?))
2019-03-28 04:44:54 +01:00
{
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)
{
if(obj == null)
{
2019-03-28 19:09:33 +01:00
return RemoveAsync(key);
2019-03-28 04:44:54 +01:00
}
2019-03-28 19:09:33 +01:00
var formattedKey = string.Format(_keyFormat, key);
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
}
2019-04-09 02:32:12 +02:00
else if(objType == typeof(int) || objType == typeof(int?))
2019-03-28 04:44:54 +01:00
{
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
}
2019-04-09 02:32:12 +02:00
else if(objType == typeof(long) || objType == typeof(long?))
2019-03-28 04:44:54 +01:00
{
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
}
2019-04-09 02:32:12 +02:00
else if(objType == typeof(double) || objType == typeof(double?))
2019-03-28 04:44:54 +01:00
{
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
}
2019-04-09 02:32:12 +02:00
else if(objType == typeof(DateTime) || objType == typeof(DateTime?))
2019-03-28 04:44:54 +01:00
{
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);
}
}
}