/// <summary> /// Gets the current value or the default that you specify. /// </summary> /// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam> /// <param name="key">Key for settings</param> /// <param name="defaultValue">default value if not set</param> /// <returns>Value or default</returns> public T GetValueOrDefault <T>(string key, T defaultValue = default(T)) { lock (locker) { Type typeOf = typeof(T); if (typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeOf = Nullable.GetUnderlyingType(typeOf); } object value = null; var typeCode = Type.GetTypeCode(typeOf); switch (typeCode) { case TypeCode.Boolean: value = SharedPreferences.GetBoolean(key, Convert.ToBoolean(defaultValue)); break; case TypeCode.Int64: value = SharedPreferences.GetLong(key, Convert.ToInt64(defaultValue)); break; case TypeCode.String: value = SharedPreferences.GetString(key, Convert.ToString(defaultValue)); break; case TypeCode.Int32: value = SharedPreferences.GetInt(key, Convert.ToInt32(defaultValue)); break; case TypeCode.UInt16: value = SharedPreferences.GetInt(key, Convert.ToUInt16(defaultValue)); break; case TypeCode.Single: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue)); break; case TypeCode.Double: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue)); break; case TypeCode.DateTime: var ticks = SharedPreferences.GetLong(key, -1); if (ticks == -1) { value = defaultValue; } else { value = new DateTime(ticks); } break; } return((null != value) ? (T)value : defaultValue); } }
/// <summary> /// Gets the current value or the default that you specify. /// </summary> /// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam> /// <param name="key">Key for settings</param> /// <param name="defaultValue">default value if not set</param> /// <returns>Value or default</returns> public T GetValueOrDefault <T>(string key, T defaultValue = default(T)) { lock (locker) { Type typeOf = typeof(T); if (typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeOf = Nullable.GetUnderlyingType(typeOf); } object value = null; var typeCode = Type.GetTypeCode(typeOf); bool resave = false; switch (typeCode) { case TypeCode.Decimal: //Android doesn't have decimal in shared prefs so get string and convert var savedDecimal = string.Empty; try { savedDecimal = SharedPreferences.GetString(key, string.Empty); } catch (Java.Lang.ClassCastException cce) { Console.WriteLine("Settings 1.5 change, have to remove key."); try { Console.WriteLine("Attempting to get old value."); savedDecimal = SharedPreferences.GetLong(key, (long)Convert.ToDecimal(defaultValue, System.Globalization.CultureInfo.InvariantCulture)).ToString(); Console.WriteLine("Old value has been parsed and will be updated and saved."); } catch (Java.Lang.ClassCastException cce2) { Console.WriteLine("Could not parse old value, will be lost."); } Remove("key"); resave = true; } if (string.IsNullOrWhiteSpace(savedDecimal)) { value = Convert.ToDecimal(defaultValue, System.Globalization.CultureInfo.InvariantCulture); } else { value = Convert.ToDecimal(savedDecimal, System.Globalization.CultureInfo.InvariantCulture); } if (resave) { AddOrUpdateValue(key, value); } break; case TypeCode.Boolean: value = SharedPreferences.GetBoolean(key, Convert.ToBoolean(defaultValue)); break; case TypeCode.Int64: value = (Int64)SharedPreferences.GetLong(key, (long)Convert.ToInt64(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.String: value = SharedPreferences.GetString(key, Convert.ToString(defaultValue)); break; case TypeCode.Double: //Android doesn't have double, so must get as string and parse. var savedDouble = string.Empty; try { savedDouble = SharedPreferences.GetString(key, string.Empty); } catch (Java.Lang.ClassCastException cce) { Console.WriteLine("Settings 1.5 change, have to remove key."); try { Console.WriteLine("Attempting to get old value."); savedDouble = SharedPreferences.GetLong(key, (long)Convert.ToDouble(defaultValue, System.Globalization.CultureInfo.InvariantCulture)).ToString(); Console.WriteLine("Old value has been parsed and will be updated and saved."); } catch (Java.Lang.ClassCastException cce2) { Console.WriteLine("Could not parse old value, will be lost."); } Remove(key); resave = true; } if (string.IsNullOrWhiteSpace(savedDouble)) { value = Convert.ToDouble(defaultValue, System.Globalization.CultureInfo.InvariantCulture); } else { value = Convert.ToDouble(savedDouble, System.Globalization.CultureInfo.InvariantCulture); } if (resave) { AddOrUpdateValue(key, value); } break; case TypeCode.Int32: value = SharedPreferences.GetInt(key, Convert.ToInt32(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Single: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.DateTime: var ticks = SharedPreferences.GetLong(key, -1); if (ticks == -1) { value = defaultValue; } else { value = new DateTime(ticks); } break; default: if (defaultValue is Guid) { var outGuid = Guid.Empty; Guid.TryParse(SharedPreferences.GetString(key, Guid.Empty.ToString()), out outGuid); value = outGuid; } else { throw new ArgumentException(string.Format("Value of type {0} is not supported.", value.GetType().Name)); } break; } return(null != value ? (T)value : defaultValue); } }
/// <summary> /// Gets the current value or the default that you specify. /// </summary> /// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam> /// <param name="key">Key for settings</param> /// <param name="defaultValue">default value if not set</param> /// <returns>Value or default</returns> public T GetValueOrDefault <T>(string key, T defaultValue = default(T)) { lock (locker) { Type typeOf = typeof(T); if (typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeOf = Nullable.GetUnderlyingType(typeOf); } object value = null; var typeCode = Type.GetTypeCode(typeOf); switch (typeCode) { case TypeCode.Decimal: value = (decimal)SharedPreferences.GetLong(key, (long)Convert.ToDecimal(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Boolean: value = SharedPreferences.GetBoolean(key, Convert.ToBoolean(defaultValue)); break; case TypeCode.Int64: value = (Int64)SharedPreferences.GetLong(key, (long)Convert.ToInt64(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.String: value = SharedPreferences.GetString(key, Convert.ToString(defaultValue)); break; case TypeCode.Double: value = (double)SharedPreferences.GetLong(key, (long)Convert.ToDouble(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Int32: value = SharedPreferences.GetInt(key, Convert.ToInt32(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Single: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.DateTime: var ticks = SharedPreferences.GetLong(key, -1); if (ticks == -1) { value = defaultValue; } else { value = new DateTime(ticks); } break; default: if (defaultValue is Guid) { var outGuid = Guid.Empty; Guid.TryParse(SharedPreferences.GetString(key, Guid.Empty.ToString()), out outGuid); value = outGuid; } else { throw new ArgumentException(string.Format("Value of type {0} is not supported.", value.GetType().Name)); } break; } return(null != value ? (T)value : defaultValue); } }