bitwarden-mobile/src/App/Services/PreferencesStorageService.cs

137 lines
5.3 KiB
C#
Raw Normal View History

2022-04-26 17:21:17 +02:00
using System;
using System.Threading.Tasks;
using Bit.Core.Abstractions;
using Newtonsoft.Json;
2019-04-09 02:49:48 +02:00
using Newtonsoft.Json.Serialization;
2019-03-28 04:44:54 +01:00
namespace Bit.App.Services
2019-03-28 04:44:54 +01:00
{
public class PreferencesStorageService : IStorageService
{
2019-05-29 21:50:20 +02:00
public static string KeyFormat = "bwPreferencesStorage:{0}";
2022-04-26 17:21:17 +02:00
2019-04-09 02:59:19 +02:00
private readonly string _sharedName;
2019-04-09 02:49:48 +02:00
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
2019-04-09 02:49:48 +02:00
};
2019-03-28 04:44:54 +01:00
2019-04-09 02:59:19 +02:00
public PreferencesStorageService(string sharedName)
{
_sharedName = sharedName;
}
2019-03-28 04:44:54 +01:00
public Task<T> GetAsync<T>(string key)
{
2019-05-29 21:50:20 +02:00
var formattedKey = string.Format(KeyFormat, key);
if (!Xamarin.Essentials.Preferences.ContainsKey(formattedKey, _sharedName))
2019-03-28 04:44:54 +01:00
{
return Task.FromResult(default(T));
}
var objType = typeof(T);
if (objType == typeof(string))
2019-03-28 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string), _sharedName);
2019-03-28 04:44:54 +01:00
return Task.FromResult((T)(object)val);
}
else if (objType == typeof(bool) || objType == typeof(bool?))
2019-04-09 02:33:52 +02:00
{
2019-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(bool), _sharedName);
2019-05-16 18:03:36 +02:00
return Task.FromResult(ChangeType<T>(val));
2019-04-09 02:33:52 +02:00
}
else if (objType == typeof(int) || objType == typeof(int?))
2019-03-28 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(int), _sharedName);
2019-05-16 18:03:36 +02:00
return Task.FromResult(ChangeType<T>(val));
2019-03-28 04:44:54 +01:00
}
else if (objType == typeof(long) || objType == typeof(long?))
2019-03-28 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(long), _sharedName);
2019-05-16 18:03:36 +02:00
return Task.FromResult(ChangeType<T>(val));
2019-03-28 04:44:54 +01:00
}
else if (objType == typeof(double) || objType == typeof(double?))
2019-03-28 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(double), _sharedName);
2019-05-16 18:03:36 +02:00
return Task.FromResult(ChangeType<T>(val));
2019-03-28 04:44:54 +01:00
}
else if (objType == typeof(DateTime) || objType == typeof(DateTime?))
2019-03-28 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(DateTime), _sharedName);
2019-05-16 18:03:36 +02:00
return Task.FromResult(ChangeType<T>(val));
2019-03-28 04:44:54 +01:00
}
else
{
2019-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string), _sharedName);
2019-04-09 02:49:48 +02:00
return Task.FromResult(JsonConvert.DeserializeObject<T>(val, _jsonSettings));
2019-03-28 04:44:54 +01:00
}
}
public Task SaveAsync<T>(string key, T obj)
{
if (obj == null)
2019-03-28 04:44:54 +01:00
{
2019-03-28 19:09:33 +01:00
return RemoveAsync(key);
2019-03-28 04:44:54 +01:00
}
2019-05-29 21:50:20 +02: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 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
Xamarin.Essentials.Preferences.Set(formattedKey, obj as string, _sharedName);
2019-03-28 04:44:54 +01:00
}
else if (objType == typeof(bool) || objType == typeof(bool?))
2019-04-09 02:33:52 +02:00
{
2019-04-09 02:59:19 +02:00
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as bool?).Value, _sharedName);
2019-04-09 02:33:52 +02:00
}
else if (objType == typeof(int) || objType == typeof(int?))
2019-03-28 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as int?).Value, _sharedName);
2019-03-28 04:44:54 +01:00
}
else if (objType == typeof(long) || objType == typeof(long?))
2019-03-28 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as long?).Value, _sharedName);
2019-03-28 04:44:54 +01:00
}
else if (objType == typeof(double) || objType == typeof(double?))
2019-03-28 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as double?).Value, _sharedName);
2019-03-28 04:44:54 +01:00
}
else if (objType == typeof(DateTime) || objType == typeof(DateTime?))
2019-03-28 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as DateTime?).Value, _sharedName);
2019-03-28 04:44:54 +01:00
}
else
{
2019-04-09 02:59:19 +02:00
Xamarin.Essentials.Preferences.Set(formattedKey, JsonConvert.SerializeObject(obj, _jsonSettings),
_sharedName);
2019-03-28 04:44:54 +01:00
}
return Task.FromResult(0);
}
public Task RemoveAsync(string key)
{
2019-05-29 21:50:20 +02:00
var formattedKey = string.Format(KeyFormat, key);
if (Xamarin.Essentials.Preferences.ContainsKey(formattedKey, _sharedName))
2019-03-28 04:44:54 +01:00
{
2019-04-09 02:59:19 +02:00
Xamarin.Essentials.Preferences.Remove(formattedKey, _sharedName);
2019-03-28 04:44:54 +01:00
}
return Task.FromResult(0);
}
2019-05-16 18:03:36 +02:00
private static T ChangeType<T>(object value)
{
var t = typeof(T);
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
2019-05-16 18:03:36 +02:00
{
if (value == null)
2019-05-16 18:03:36 +02:00
{
return default(T);
}
t = Nullable.GetUnderlyingType(t);
}
return (T)Convert.ChangeType(value, t);
}
2019-03-28 04:44:54 +01:00
}
}