setting with custom defaultsName

This commit is contained in:
Kyle Spearrin 2017-08-29 16:58:02 -04:00
parent fd03c33f4d
commit 00703d1570
2 changed files with 253 additions and 36 deletions

View file

@ -1,8 +1,7 @@
using System; using System;
#if __UNIFIED__
using Foundation; using Foundation;
#else #if __IOS__
using MonoTouch.Foundation; using UIKit;
#endif #endif
using Plugin.Settings.Abstractions; using Plugin.Settings.Abstractions;
@ -11,15 +10,15 @@ namespace Bit.iOS.Core.Services
/// <summary> /// <summary>
/// Main implementation for ISettings /// Main implementation for ISettings
/// </summary> /// </summary>
[Preserve(AllMembers = true)]
public class Settings : ISettings public class Settings : ISettings
{ {
private readonly object locker = new object(); private readonly object locker = new object();
private readonly NSUserDefaults _defaults; private readonly string _defaultsName;
public Settings(string defaultsName) public Settings(string defaultsName)
{ {
_defaults = string.IsNullOrWhiteSpace(defaultsName) ? NSUserDefaults.StandardUserDefaults _defaultsName = defaultsName;
: new NSUserDefaults(defaultsName, NSUserDefaultsType.SuiteName);
} }
/// <summary> /// <summary>
@ -28,14 +27,15 @@ namespace Bit.iOS.Core.Services
/// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam> /// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam>
/// <param name="key">Key for settings</param> /// <param name="key">Key for settings</param>
/// <param name="defaultValue">default value if not set</param> /// <param name="defaultValue">default value if not set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>Value or default</returns> /// <returns>Value or default</returns>
public T GetValueOrDefault<T>(string key, T defaultValue = default(T)) T GetValueOrDefaultInternal<T>(string key, T defaultValue = default(T), string fileName = null)
{ {
lock(locker) lock(locker)
{ {
var defaults = _defaults; var defaults = GetUserDefaults(fileName);
if(defaults.ValueForKey(new NSString(key)) == null) if(defaults[key] == null)
return defaultValue; return defaultValue;
Type typeOf = typeof(T); Type typeOf = typeof(T);
@ -65,18 +65,10 @@ namespace Bit.iOS.Core.Services
value = defaults.StringForKey(key); value = defaults.StringForKey(key);
break; break;
case TypeCode.Int32: case TypeCode.Int32:
#if __UNIFIED__
value = (Int32)defaults.IntForKey(key); value = (Int32)defaults.IntForKey(key);
#else
value = defaults.IntForKey(key);
#endif
break; break;
case TypeCode.Single: case TypeCode.Single:
#if __UNIFIED__
value = (float)defaults.FloatForKey(key);
#else
value = defaults.FloatForKey(key); value = defaults.FloatForKey(key);
#endif
break; break;
case TypeCode.DateTime: case TypeCode.DateTime:
@ -118,7 +110,7 @@ namespace Bit.iOS.Core.Services
} }
else else
{ {
throw new ArgumentException(string.Format("Value of type {0} is not supported.", value.GetType().Name)); throw new ArgumentException($"Value of type {typeCode} is not supported.");
} }
break; break;
@ -134,23 +126,30 @@ namespace Bit.iOS.Core.Services
/// </summary> /// </summary>
/// <param name="key">key to update</param> /// <param name="key">key to update</param>
/// <param name="value">value to set</param> /// <param name="value">value to set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True if added or update and you need to save</returns> /// <returns>True if added or update and you need to save</returns>
public bool AddOrUpdateValue<T>(string key, T value) bool AddOrUpdateValueInternal<T>(string key, T value, string fileName = null)
{ {
if(value == null)
{
Remove(key, fileName);
return true;
}
Type typeOf = typeof(T); Type typeOf = typeof(T);
if(typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable<>)) if(typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable<>))
{ {
typeOf = Nullable.GetUnderlyingType(typeOf); typeOf = Nullable.GetUnderlyingType(typeOf);
} }
var typeCode = Type.GetTypeCode(typeOf); var typeCode = Type.GetTypeCode(typeOf);
return AddOrUpdateValue(key, value, typeCode); return AddOrUpdateValueCore(key, value, typeCode, fileName);
} }
private bool AddOrUpdateValue(string key, object value, TypeCode typeCode) bool AddOrUpdateValueCore(string key, object value, TypeCode typeCode, string fileName)
{ {
lock(locker) lock(locker)
{ {
var defaults = _defaults; var defaults = GetUserDefaults(fileName);
switch(typeCode) switch(typeCode)
{ {
case TypeCode.Decimal: case TypeCode.Decimal:
@ -187,7 +186,7 @@ namespace Bit.iOS.Core.Services
} }
else else
{ {
throw new ArgumentException(string.Format("Value of type {0} is not supported.", value.GetType().Name)); throw new ArgumentException($"Value of type {typeCode} is not supported.");
} }
break; break;
} }
@ -209,15 +208,15 @@ namespace Bit.iOS.Core.Services
/// Removes a desired key from the settings /// Removes a desired key from the settings
/// </summary> /// </summary>
/// <param name="key">Key for setting</param> /// <param name="key">Key for setting</param>
public void Remove(string key) /// <param name="fileName">Name of file for settings to be stored and retrieved </param>
public void Remove(string key, string fileName = null)
{ {
lock(locker) lock(locker)
{ {
var defaults = _defaults; var defaults = GetUserDefaults(fileName);
try try
{ {
var nsString = new NSString(key); if(defaults[key] != null)
if(defaults.ValueForKey(nsString) != null)
{ {
defaults.RemoveObject(key); defaults.RemoveObject(key);
defaults.Synchronize(); defaults.Synchronize();
@ -233,14 +232,21 @@ namespace Bit.iOS.Core.Services
/// <summary> /// <summary>
/// Clear all keys from settings /// Clear all keys from settings
/// </summary> /// </summary>
public void Clear() /// <param name="fileName">Name of file for settings to be stored and retrieved </param>
public void Clear(string fileName = null)
{ {
lock(locker) lock(locker)
{ {
var defaults = _defaults; var defaults = GetUserDefaults(fileName);
try try
{ {
defaults.RemovePersistentDomain(NSBundle.MainBundle.BundleIdentifier); var items = defaults.ToDictionary();
foreach(var item in items.Keys)
{
if(item is NSString nsString)
defaults.RemoveObject(nsString);
}
defaults.Synchronize(); defaults.Synchronize();
} }
catch(Exception ex) catch(Exception ex)
@ -254,16 +260,16 @@ namespace Bit.iOS.Core.Services
/// Checks to see if the key has been added. /// Checks to see if the key has been added.
/// </summary> /// </summary>
/// <param name="key">Key to check</param> /// <param name="key">Key to check</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True if contains key, else false</returns> /// <returns>True if contains key, else false</returns>
public bool Contains(string key) public bool Contains(string key, string fileName = null)
{ {
lock(locker) lock(locker)
{ {
var defaults = _defaults; var defaults = GetUserDefaults(fileName);
try try
{ {
var nsString = new NSString(key); var setting = defaults[key];
var setting = defaults.ValueForKey(nsString);
return setting != null; return setting != null;
} }
catch(Exception ex) catch(Exception ex)
@ -274,5 +280,216 @@ namespace Bit.iOS.Core.Services
return false; return false;
} }
} }
NSUserDefaults GetUserDefaults(string fileName = null)
{
if(string.IsNullOrWhiteSpace(fileName) && !string.IsNullOrWhiteSpace(_defaultsName))
{
return new NSUserDefaults(_defaultsName, NSUserDefaultsType.SuiteName);
}
return string.IsNullOrWhiteSpace(fileName) ?
NSUserDefaults.StandardUserDefaults :
new NSUserDefaults(fileName, NSUserDefaultsType.SuiteName);
}
#region GetValueOrDefault
/// <summary>
/// Gets the current value or the default that you specify.
/// </summary>
/// <param name="key">Key for settings</param>
/// <param name="defaultValue">default value if not set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>Value or default</returns>
public decimal GetValueOrDefault(string key, decimal defaultValue, string fileName = null) =>
GetValueOrDefaultInternal(key, defaultValue, fileName);
/// <summary>
/// Gets the current value or the default that you specify.
/// </summary>
/// <param name="key">Key for settings</param>
/// <param name="defaultValue">default value if not set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>Value or default</returns>
public bool GetValueOrDefault(string key, bool defaultValue, string fileName = null) =>
GetValueOrDefaultInternal(key, defaultValue, fileName);
/// <summary>
/// Gets the current value or the default that you specify.
/// </summary>
/// <param name="key">Key for settings</param>
/// <param name="defaultValue">default value if not set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>Value or default</returns>
public long GetValueOrDefault(string key, long defaultValue, string fileName = null) =>
GetValueOrDefaultInternal(key, defaultValue, fileName);
/// <summary>
/// Gets the current value or the default that you specify.
/// </summary>
/// <param name="key">Key for settings</param>
/// <param name="defaultValue">default value if not set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>Value or default</returns>
public string GetValueOrDefault(string key, string defaultValue, string fileName = null) =>
GetValueOrDefaultInternal(key, defaultValue, fileName);
/// <summary>
/// Gets the current value or the default that you specify.
/// </summary>
/// <param name="key">Key for settings</param>
/// <param name="defaultValue">default value if not set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>Value or default</returns>
public int GetValueOrDefault(string key, int defaultValue, string fileName = null) =>
GetValueOrDefaultInternal(key, defaultValue, fileName);
/// <summary>
/// Gets the current value or the default that you specify.
/// </summary>
/// <param name="key">Key for settings</param>
/// <param name="defaultValue">default value if not set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>Value or default</returns>
public float GetValueOrDefault(string key, float defaultValue, string fileName = null) =>
GetValueOrDefaultInternal(key, defaultValue, fileName);
/// <summary>
/// Gets the current value or the default that you specify.
/// </summary>
/// <param name="key">Key for settings</param>
/// <param name="defaultValue">default value if not set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>Value or default</returns>
public DateTime GetValueOrDefault(string key, DateTime defaultValue, string fileName = null) =>
GetValueOrDefaultInternal(key, defaultValue, fileName);
/// <summary>
/// Gets the current value or the default that you specify.
/// </summary>
/// <param name="key">Key for settings</param>
/// <param name="defaultValue">default value if not set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>Value or default</returns>
public Guid GetValueOrDefault(string key, Guid defaultValue, string fileName = null) =>
GetValueOrDefaultInternal(key, defaultValue, fileName);
/// <summary>
/// Gets the current value or the default that you specify.
/// </summary>
/// <param name="key">Key for settings</param>
/// <param name="defaultValue">default value if not set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>Value or default</returns>
public double GetValueOrDefault(string key, double defaultValue, string fileName = null) =>
GetValueOrDefaultInternal(key, defaultValue, fileName);
#endregion
#region AddOrUpdateValue
/// <summary>
/// Adds or updates the value
/// </summary>
/// <param name="key">Key for settting</param>
/// <param name="value">Value to set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True of was added or updated and you need to save it.</returns>
public bool AddOrUpdateValue(string key, decimal value, string fileName = null) =>
AddOrUpdateValueInternal(key, value, fileName);
/// <summary>
/// Adds or updates the value
/// </summary>
/// <param name="key">Key for settting</param>
/// <param name="value">Value to set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True of was added or updated and you need to save it.</returns>
public bool AddOrUpdateValue(string key, bool value, string fileName = null) =>
AddOrUpdateValueInternal(key, value, fileName);
/// <summary>
/// Adds or updates the value
/// </summary>
/// <param name="key">Key for settting</param>
/// <param name="value">Value to set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True of was added or updated and you need to save it.</returns>
public bool AddOrUpdateValue(string key, long value, string fileName = null) =>
AddOrUpdateValueInternal(key, value, fileName);
/// <summary>
/// Adds or updates the value
/// </summary>
/// <param name="key">Key for settting</param>
/// <param name="value">Value to set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True of was added or updated and you need to save it.</returns>
public bool AddOrUpdateValue(string key, string value, string fileName = null) =>
AddOrUpdateValueInternal(key, value, fileName);
/// <summary>
/// Adds or updates the value
/// </summary>
/// <param name="key">Key for settting</param>
/// <param name="value">Value to set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True of was added or updated and you need to save it.</returns>
public bool AddOrUpdateValue(string key, int value, string fileName = null) =>
AddOrUpdateValueInternal(key, value, fileName);
/// <summary>
/// Adds or updates the value
/// </summary>
/// <param name="key">Key for settting</param>
/// <param name="value">Value to set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True of was added or updated and you need to save it.</returns>
public bool AddOrUpdateValue(string key, float value, string fileName = null) =>
AddOrUpdateValueInternal(key, value, fileName);
/// <summary>
/// Adds or updates the value
/// </summary>
/// <param name="key">Key for settting</param>
/// <param name="value">Value to set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True of was added or updated and you need to save it.</returns>
public bool AddOrUpdateValue(string key, DateTime value, string fileName = null) =>
AddOrUpdateValueInternal(key, value, fileName);
/// <summary>
/// Adds or updates the value
/// </summary>
/// <param name="key">Key for settting</param>
/// <param name="value">Value to set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True of was added or updated and you need to save it.</returns>
public bool AddOrUpdateValue(string key, Guid value, string fileName = null) =>
AddOrUpdateValueInternal(key, value, fileName);
/// <summary>
/// Adds or updates the value
/// </summary>
/// <param name="key">Key for settting</param>
/// <param name="value">Value to set</param>
/// <param name="fileName">Name of file for settings to be stored and retrieved </param>
/// <returns>True of was added or updated and you need to save it.</returns>
public bool AddOrUpdateValue(string key, double value, string fileName = null) =>
AddOrUpdateValueInternal(key, value, fileName);
#endregion
/// <summary>
/// Attempts to open the app settings page.
/// </summary>
/// <returns>true if success, else false and not supported</returns>
public bool OpenAppSettings()
{
#if __IOS__
//Opening settings only open in iOS 8+
if(!UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
return false;
try
{
UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
return true;
}
catch
{
return false;
}
#else
return false;
#endif
}
} }
}
}

View file

@ -91,9 +91,9 @@
<Compile Include="Services\CommonCryptoKeyDerivationService.cs" /> <Compile Include="Services\CommonCryptoKeyDerivationService.cs" />
<Compile Include="Services\HttpService.cs" /> <Compile Include="Services\HttpService.cs" />
<Compile Include="Services\LogService.cs" /> <Compile Include="Services\LogService.cs" />
<Compile Include="Services\Settings.cs" />
<Compile Include="Services\GoogleAnalyticsService.cs" /> <Compile Include="Services\GoogleAnalyticsService.cs" />
<Compile Include="Services\LocalizeService.cs" /> <Compile Include="Services\LocalizeService.cs" />
<Compile Include="Services\Settings.cs" />
<Compile Include="Services\SqlService.cs" /> <Compile Include="Services\SqlService.cs" />
<Compile Include="Utilities\Dialogs.cs" /> <Compile Include="Utilities\Dialogs.cs" />
<Compile Include="Views\ISelectable.cs" /> <Compile Include="Views\ISelectable.cs" />