1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-23 03:33:59 +00:00

storage services

This commit is contained in:
Kyle Spearrin
2019-03-27 23:44:54 -04:00
parent a88f799372
commit 21777602f6
10 changed files with 381 additions and 18 deletions

View File

@@ -0,0 +1,11 @@
using System.Threading.Tasks;
namespace Bit.Core.Abstractions
{
public interface IStorageService
{
Task<T> GetAsync<T>(string key);
Task SaveAsync<T>(string key, T obj);
Task RemoveAsync(string key);
}
}

View File

@@ -1,8 +0,0 @@
using System;
namespace Bit.Core
{
public class Class1
{
}
}

View File

@@ -11,4 +11,10 @@
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LiteDB" Version="4.1.4" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Xamarin.Essentials" Version="1.1.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,56 @@
using Bit.Core.Abstractions;
using LiteDB;
using Newtonsoft.Json;
using System.Linq;
using System.Threading.Tasks;
namespace Bit.Core.Services
{
public class LiteDbStorageService : IStorageService
{
private LiteCollection<JsonItem> _collection;
public LiteDbStorageService(string dbPath)
{
var db = new LiteDatabase($"filename={dbPath}");
_collection = db.GetCollection<JsonItem>("json_items");
}
public Task<T> GetAsync<T>(string key)
{
var item = _collection.Find(i => i.Id == key).FirstOrDefault();
if(item == null)
{
return Task.FromResult(default(T));
}
return Task.FromResult(JsonConvert.DeserializeObject<T>(item.Value));
}
public Task SaveAsync<T>(string key, T obj)
{
var data = JsonConvert.SerializeObject(obj);
_collection.Upsert(new JsonItem(key, data));
return Task.FromResult(0);
}
public Task RemoveAsync(string key)
{
_collection.Delete(i => i.Id == key);
return Task.FromResult(0);
}
private class JsonItem
{
public JsonItem() { }
public JsonItem(string key, string value)
{
Id = key;
Value = value;
}
public string Id { get; set; }
public string Value { get; set; }
}
}
}

View File

@@ -0,0 +1,95 @@
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))
{
var val = Xamarin.Essentials.Preferences.Get(key, default(string));
return Task.FromResult((T)(object)val);
}
else if(objType == typeof(int))
{
var val = Xamarin.Essentials.Preferences.Get(key, default(int));
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else if(objType == typeof(long))
{
var val = Xamarin.Essentials.Preferences.Get(key, default(long));
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else if(objType == typeof(double))
{
var val = Xamarin.Essentials.Preferences.Get(key, default(double));
return Task.FromResult((T)Convert.ChangeType(val, objType));
}
else if(objType == typeof(DateTime))
{
var val = Xamarin.Essentials.Preferences.Get(key, default(DateTime));
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)
{
return RemoveAsync(key);
}
var objType = typeof(T);
if(objType == typeof(string))
{
Xamarin.Essentials.Preferences.Set(key, obj as string);
}
else if(objType == typeof(int))
{
Xamarin.Essentials.Preferences.Set(key, (obj as int?).Value);
}
else if(objType == typeof(long))
{
Xamarin.Essentials.Preferences.Set(key, (obj as long?).Value);
}
else if(objType == typeof(double))
{
Xamarin.Essentials.Preferences.Set(key, (obj as double?).Value);
}
else if(objType == typeof(DateTime))
{
Xamarin.Essentials.Preferences.Set(key, (obj as DateTime?).Value);
}
else
{
throw new Exception("Unsupported object type for preferences.");
}
return Task.FromResult(0);
}
public Task RemoveAsync(string key)
{
if(Xamarin.Essentials.Preferences.ContainsKey(key))
{
Xamarin.Essentials.Preferences.Remove(key);
}
return Task.FromResult(0);
}
}
}