From 8055de4f2526e1b82f613d4904de8620572ff6c8 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 8 Apr 2019 20:59:19 -0400 Subject: [PATCH] shared name --- .../Services/PreferencesStorageService.cs | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/Core/Services/PreferencesStorageService.cs b/src/Core/Services/PreferencesStorageService.cs index 005c0725c..bfc7e4f86 100644 --- a/src/Core/Services/PreferencesStorageService.cs +++ b/src/Core/Services/PreferencesStorageService.cs @@ -9,15 +9,22 @@ namespace Bit.Core.Services public class PreferencesStorageService : IStorageService { private 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 GetAsync(string key) { var formattedKey = string.Format(_keyFormat, key); - if(!Xamarin.Essentials.Preferences.ContainsKey(formattedKey)) + if(!Xamarin.Essentials.Preferences.ContainsKey(formattedKey, _sharedName)) { return Task.FromResult(default(T)); } @@ -25,37 +32,37 @@ namespace Bit.Core.Services var objType = typeof(T); if(objType == typeof(string)) { - var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(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)); + 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)); + 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)); + 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)); + 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)); + 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)); + var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string), _sharedName); return Task.FromResult(JsonConvert.DeserializeObject(val, _jsonSettings)); } } @@ -71,31 +78,32 @@ namespace Bit.Core.Services var objType = typeof(T); if(objType == typeof(string)) { - Xamarin.Essentials.Preferences.Set(formattedKey, obj as 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); + 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); + 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); + 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); + 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); + Xamarin.Essentials.Preferences.Set(formattedKey, (obj as DateTime?).Value, _sharedName); } else { - Xamarin.Essentials.Preferences.Set(formattedKey, JsonConvert.SerializeObject(obj, _jsonSettings)); + Xamarin.Essentials.Preferences.Set(formattedKey, JsonConvert.SerializeObject(obj, _jsonSettings), + _sharedName); } return Task.FromResult(0); } @@ -103,9 +111,9 @@ namespace Bit.Core.Services public Task RemoveAsync(string key) { var formattedKey = string.Format(_keyFormat, key); - if(Xamarin.Essentials.Preferences.ContainsKey(formattedKey)) + if(Xamarin.Essentials.Preferences.ContainsKey(formattedKey, _sharedName)) { - Xamarin.Essentials.Preferences.Remove(formattedKey); + Xamarin.Essentials.Preferences.Remove(formattedKey, _sharedName); } return Task.FromResult(0); }