mirror of
https://github.com/bitwarden/mobile
synced 2026-01-09 20:13:18 +00:00
move some xamarin specific services to app proj
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Xamarin.Essentials" Version="1.1.0" />
|
||||
<PackageReference Include="Xamarin.Forms" Version="3.6.0.264807" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
53
src/App/Services/MobileStorageService.cs
Normal file
53
src/App/Services/MobileStorageService.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Bit.Core;
|
||||
using Bit.Core.Abstractions;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class MobileStorageService : IStorageService
|
||||
{
|
||||
private readonly IStorageService _preferencesStorageService;
|
||||
private readonly IStorageService _liteDbStorageService;
|
||||
|
||||
private readonly HashSet<string> _preferenceStorageKeys = new HashSet<string>
|
||||
{
|
||||
Constants.LockOptionKey
|
||||
};
|
||||
|
||||
public MobileStorageService(
|
||||
IStorageService preferenceStorageService,
|
||||
IStorageService liteDbStorageService)
|
||||
{
|
||||
_preferencesStorageService = preferenceStorageService;
|
||||
_liteDbStorageService = liteDbStorageService;
|
||||
}
|
||||
|
||||
public Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
if(_preferenceStorageKeys.Contains(key))
|
||||
{
|
||||
return _preferencesStorageService.GetAsync<T>(key);
|
||||
}
|
||||
return _liteDbStorageService.GetAsync<T>(key);
|
||||
}
|
||||
|
||||
public Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
if(_preferenceStorageKeys.Contains(key))
|
||||
{
|
||||
return _preferencesStorageService.SaveAsync(key, obj);
|
||||
}
|
||||
return _liteDbStorageService.SaveAsync(key, obj);
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
if(_preferenceStorageKeys.Contains(key))
|
||||
{
|
||||
return _preferencesStorageService.RemoveAsync(key);
|
||||
}
|
||||
return _liteDbStorageService.RemoveAsync(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
120
src/App/Services/PreferencesStorageService.cs
Normal file
120
src/App/Services/PreferencesStorageService.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using Bit.Core.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class PreferencesStorageService : IStorageService
|
||||
{
|
||||
private readonly string _keyFormat = "bwPreferencesStorage:{0}";
|
||||
private readonly string _sharedName;
|
||||
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
public PreferencesStorageService(string sharedName)
|
||||
{
|
||||
_sharedName = sharedName;
|
||||
}
|
||||
|
||||
public Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
if(!Xamarin.Essentials.Preferences.ContainsKey(formattedKey, _sharedName))
|
||||
{
|
||||
return Task.FromResult(default(T));
|
||||
}
|
||||
|
||||
var objType = typeof(T);
|
||||
if(objType == typeof(string))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string), _sharedName);
|
||||
return Task.FromResult((T)(object)val);
|
||||
}
|
||||
else if(objType == typeof(bool) || objType == typeof(bool?))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(bool), _sharedName);
|
||||
return Task.FromResult((T)Convert.ChangeType(val, objType));
|
||||
}
|
||||
else if(objType == typeof(int) || objType == typeof(int?))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(int), _sharedName);
|
||||
return Task.FromResult((T)Convert.ChangeType(val, objType));
|
||||
}
|
||||
else if(objType == typeof(long) || objType == typeof(long?))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(long), _sharedName);
|
||||
return Task.FromResult((T)Convert.ChangeType(val, objType));
|
||||
}
|
||||
else if(objType == typeof(double) || objType == typeof(double?))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(double), _sharedName);
|
||||
return Task.FromResult((T)Convert.ChangeType(val, objType));
|
||||
}
|
||||
else if(objType == typeof(DateTime) || objType == typeof(DateTime?))
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(DateTime), _sharedName);
|
||||
return Task.FromResult((T)Convert.ChangeType(val, objType));
|
||||
}
|
||||
else
|
||||
{
|
||||
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string), _sharedName);
|
||||
return Task.FromResult(JsonConvert.DeserializeObject<T>(val, _jsonSettings));
|
||||
}
|
||||
}
|
||||
|
||||
public Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
if(obj == null)
|
||||
{
|
||||
return RemoveAsync(key);
|
||||
}
|
||||
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
var objType = typeof(T);
|
||||
if(objType == typeof(string))
|
||||
{
|
||||
Xamarin.Essentials.Preferences.Set(formattedKey, obj as string, _sharedName);
|
||||
}
|
||||
else if(objType == typeof(bool) || objType == typeof(bool?))
|
||||
{
|
||||
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as bool?).Value, _sharedName);
|
||||
}
|
||||
else if(objType == typeof(int) || objType == typeof(int?))
|
||||
{
|
||||
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as int?).Value, _sharedName);
|
||||
}
|
||||
else if(objType == typeof(long) || objType == typeof(long?))
|
||||
{
|
||||
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as long?).Value, _sharedName);
|
||||
}
|
||||
else if(objType == typeof(double) || objType == typeof(double?))
|
||||
{
|
||||
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as double?).Value, _sharedName);
|
||||
}
|
||||
else if(objType == typeof(DateTime) || objType == typeof(DateTime?))
|
||||
{
|
||||
Xamarin.Essentials.Preferences.Set(formattedKey, (obj as DateTime?).Value, _sharedName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Xamarin.Essentials.Preferences.Set(formattedKey, JsonConvert.SerializeObject(obj, _jsonSettings),
|
||||
_sharedName);
|
||||
}
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
if(Xamarin.Essentials.Preferences.ContainsKey(formattedKey, _sharedName))
|
||||
{
|
||||
Xamarin.Essentials.Preferences.Remove(formattedKey, _sharedName);
|
||||
}
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
56
src/App/Services/SecureStorageService.cs
Normal file
56
src/App/Services/SecureStorageService.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Bit.Core.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class SecureStorageService : IStorageService
|
||||
{
|
||||
private readonly string _keyFormat = "bwSecureStorage:{0}";
|
||||
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
public async Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
var val = await Xamarin.Essentials.SecureStorage.GetAsync(formattedKey);
|
||||
if(typeof(T) == typeof(string))
|
||||
{
|
||||
return (T)(object)val;
|
||||
}
|
||||
else
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(val, _jsonSettings);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
if(obj == null)
|
||||
{
|
||||
await RemoveAsync(key);
|
||||
return;
|
||||
}
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
if(typeof(T) == typeof(string))
|
||||
{
|
||||
await Xamarin.Essentials.SecureStorage.SetAsync(formattedKey, obj as string);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Xamarin.Essentials.SecureStorage.SetAsync(formattedKey,
|
||||
JsonConvert.SerializeObject(obj, _jsonSettings));
|
||||
}
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
var formattedKey = string.Format(_keyFormat, key);
|
||||
Xamarin.Essentials.SecureStorage.Remove(formattedKey);
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user