1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-12 10:04:43 +01:00
bitwarden-mobile/src/App/Services/PreferencesStorageService.cs

121 lines
4.8 KiB
C#
Raw Normal View History

2019-03-28 04:44:54 +01:00
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
using System;
using System.Threading.Tasks;
namespace Bit.App.Services
2019-03-28 04:44:54 +01:00
{
public class PreferencesStorageService : IStorageService
{
2019-04-09 03:23:16 +02:00
private readonly string _keyFormat = "bwPreferencesStorage:{0}";
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()
};
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)
{
var formattedKey = string.Format(_keyFormat, key);
2019-04-09 02:59:19 +02:00
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-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);
}
2019-04-09 02:33:52 +02:00
else if(objType == typeof(bool) || objType == typeof(bool?))
{
2019-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(bool), _sharedName);
2019-04-09 02:33:52 +02:00
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
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-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(int), _sharedName);
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-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(long), _sharedName);
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-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(double), _sharedName);
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-04-09 02:59:19 +02:00
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(DateTime), _sharedName);
2019-03-28 04:44:54 +01:00
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
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 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-04-09 02:59:19 +02:00
Xamarin.Essentials.Preferences.Set(formattedKey, obj as string, _sharedName);
2019-03-28 04:44:54 +01:00
}
2019-04-09 02:33:52 +02:00
else if(objType == typeof(bool) || objType == typeof(bool?))
{
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
}
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-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
}
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-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
}
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-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
}
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-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-03-28 19:01:36 +01:00
var formattedKey = string.Format(_keyFormat, key);
2019-04-09 02:59:19 +02:00
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);
}
}
}