public void Write(string key, object value, SettingsStrategy strategy) { var type = value.GetType(); var editor = LocalSettings.Edit(); if (type == typeof(int)) { editor.PutInt(key, (int)value); } else if (type == typeof(long)) { editor.PutLong(key, (long)value); } else if (type == typeof(float)) { editor.PutFloat(key, (float)value); } else if (type == typeof(bool)) { editor.PutBoolean(key, (bool)value); } else { editor.PutString(key, (string)value); } editor.Commit(); }
public void Write <T>(string key, T value, SettingsStrategy strategy = SettingsStrategy.Local) { var type = value.GetType(); var editor = LocalSettings.Edit(); var valueObj = (object)value; if (type == typeof(int)) { editor.PutInt(key, (int)valueObj); } else if (type == typeof(long)) { editor.PutLong(key, (long)valueObj); } else if (type == typeof(float)) { editor.PutFloat(key, (float)valueObj); } else if (type == typeof(bool)) { editor.PutBoolean(key, (bool)valueObj); } else if (type == typeof(string)) { editor.PutString(key, (string)valueObj); } else { var json = JsonConvert.SerializeObject(value); editor.PutString(key, json); } editor.Commit(); }
public T Read <T>(string key, T otherwise, SettingsStrategy strategy = SettingsStrategy.Local) { var container = Container(strategy); var type = typeof(T); var defaultObject = (object)otherwise; object returnValue; if (type == typeof(int)) { returnValue = container.GetInt(key, (int)defaultObject); } else if (type == typeof(long)) { returnValue = container.GetLong(key, (long)defaultObject); } else if (type == typeof(float)) { returnValue = container.GetFloat(key, (float)defaultObject); } else if (type == typeof(bool)) { returnValue = container.GetBoolean(key, (bool)defaultObject); } else if (type == typeof(string)) { returnValue = container.GetString(key, (string)defaultObject); } else { var json = container.GetString(key, null); returnValue = JsonConvert.DeserializeObject <T>(json); } return((T)returnValue); }
public void Write <T>(string key, T value, SettingsStrategy strategy = SettingsStrategy.Local) { var settings = Container(strategy); if (IsPrimitive(typeof(T))) { try { settings[key] = value; } catch { // ignored } } else { var json = Serialize(value); try { settings[key] = json; } catch { // too big, fallback to file var file = AsyncHelper.RunSync(() => StorageHelper.CreateFileAsync(string.Format(FileFallback, key), option: CreationCollisionOption.ReplaceExisting)); using (var stream = AsyncHelper.RunSync(() => file.OpenStreamForWriteAsync())) { var bytes = Encoding.UTF8.GetBytes(json); stream.Write(bytes, 0, bytes.Length); settings[key] = string.Format(FileFallback, key); } } } }
public T Read <T>(string key, T defaultValue, SettingsStrategy strategy) { //strategy is ignored, Android doesn't have a roaming settings option var type = typeof(T); var defaultObject = (object)defaultValue; object returnValue; if (type == typeof(int)) { returnValue = LocalSettings.GetInt(key, (int)defaultObject); } else if (type == typeof(long)) { returnValue = LocalSettings.GetLong(key, (long)defaultObject); } else if (type == typeof(float)) { returnValue = LocalSettings.GetFloat(key, (float)defaultObject); } else if (type == typeof(bool)) { returnValue = LocalSettings.GetBoolean(key, (bool)defaultObject); } else { returnValue = LocalSettings.GetString(key, (string)defaultObject); } return((T)returnValue); }
public void Remove(string key, SettingsStrategy strategy) { //Try to get the settings value if (GetContainerFromStrategy(strategy).Values.ContainsKey(key)) { GetContainerFromStrategy(strategy).Values.Remove(key); } }
public void Remove(string key, SettingsStrategy strategy = SettingsStrategy.Local) { var settings = Container(strategy); if (settings.ContainsKey(key)) { settings.Remove(key); } }
private static ApplicationDataContainer GetContainerFromStrategy(SettingsStrategy location) { switch (location) { case SettingsStrategy.Roaming: return(ApplicationData.Current.RoamingSettings); default: return(ApplicationData.Current.LocalSettings); } }
public void Write(string key, object value, SettingsStrategy strategy) { if (GetContainerFromStrategy(strategy).Values.ContainsKey(key)) { GetContainerFromStrategy(strategy).Values[key] = value; } else { GetContainerFromStrategy(strategy).Values.Add(key, value); } }
public void Write <T>(string key, T value, SettingsStrategy strategy = SettingsStrategy.Local) { var settings = Container(strategy); if (IsPrimitive(typeof(T))) { try { settings[key] = value.ToString(); } catch { settings[key] = string.Empty; } } else { var json = Serialize(value); try { settings[key] = json; } catch { settings[key] = string.Empty; } } }
private static ISettings GetSettingsContainer(SettingsStrategy strategy) { switch (strategy) { case SettingsStrategy.Local: return(new ApplicationContainerSettings(ApplicationData.Current.LocalSettings)); case SettingsStrategy.Roaming: return(new ApplicationContainerSettings(ApplicationData.Current.RoamingSettings)); case SettingsStrategy.Temp: return(new ApplicationFileSettings(ApplicationTempSettings.TempSettingsFolder)); default: throw new ArgumentException("unknown settings strategy", nameof(strategy)); } }
public T Read <T>(string key, T defaultValue, SettingsStrategy strategy) { object obj; //Try to get the settings value if (GetContainerFromStrategy(strategy).Values.TryGetValue(key, out obj)) { try { //Try casting it return((T)obj); } catch { // ignored } } return(defaultValue); }
public T Read <T>(string key, T otherwise, SettingsStrategy strategy = SettingsStrategy.Local) { var settings = Container(strategy); if (!settings.ContainsKey(key)) { return(otherwise); } try { var o = settings[key]; if (IsPrimitive(typeof(T))) { return((T)o); } var json = o.ToString(); var fallback = string.Format(FileFallback, key); if (json == fallback) { var file = AsyncHelper.RunSync(() => StorageHelper.GetFileAsync(fallback)); using (var stream = AsyncHelper.RunSync(() => file.OpenStreamForReadAsync())) { var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); json = Encoding.UTF8.GetString(bytes, 0, bytes.Length); } AsyncHelper.RunSync(() => file.DeleteAsync().AsTask()); } return(Deserialize <T>(json)); } catch { return(otherwise); } }
public T Read <T>(string key, T otherwise, SettingsStrategy strategy = SettingsStrategy.Local) { var settings = Container(strategy); if (!settings.ContainsKey(key)) { return(otherwise); } try { object untyped; if (settings.TryGetValue(key, out untyped)) { if (untyped == null) { return(otherwise); } } else { return(otherwise); } // attempt to deserialize if (IsPrimitive(typeof(T))) { var parse = typeof(T).GetRuntimeMethod(SettingsHelper.parse, new[] { typeof(string) }); return((T)parse.Invoke(null, new[] { untyped })); } else { var json = untyped.ToString(); return(Deserialize <T>(json)); } } catch { return(otherwise); } }
public T Read <T>(string key, T otherwise, SettingsStrategy strategy = SettingsStrategy.Local) { var settings = Container(strategy); if (!settings.ContainsKey(key)) { return(otherwise); } try { if (IsPrimitive(typeof(T))) { var value = settings[key].ToString(); var parse = typeof(T).GetRuntimeMethod(SettingsHelper.parse, new[] { typeof(string) }); return((T)parse.Invoke(null, new[] { value })); } else { var json = settings[key].ToString(); return(Deserialize <T>(json)); } } catch { return(otherwise); } }
public void Write <T>(string key, T value, SettingsStrategy strategy = SettingsStrategy.Local) { }
public void Remove(string key, SettingsStrategy strategy = SettingsStrategy.Local) { }
public IPropertySet Container(SettingsStrategy strategy) { return((strategy == SettingsStrategy.Local) ? ApplicationData.Current.LocalSettings.Values : ApplicationData.Current.RoamingSettings.Values); }
public bool Exists(string key, SettingsStrategy strategy = SettingsStrategy.Local) { var settings = Container(strategy); return(settings.ContainsKey(key)); }
public T Read <T>(string key, T otherwise, SettingsStrategy strategy = SettingsStrategy.Local) { return(otherwise); }
public static bool Exist(string key, SettingsStrategy strategy) { return(GetSettingsContainer(strategy).Exist(key)); }
public static bool Remove(string key, SettingsStrategy strategy) { return(GetSettingsContainer(strategy).Remove(key)); }
public static void Write <T>(string key, T value, SettingsStrategy strategy) { GetSettingsContainer(strategy).Write(key, value); }
public bool Exists(string key, SettingsStrategy strategy = SettingsStrategy.Local) { return(false); }
public static T Read <T>(string key, SettingsStrategy strategy) { return(GetSettingsContainer(strategy).Read <T>(key)); }
private ISharedPreferences Container(SettingsStrategy strategy) { return(strategy == SettingsStrategy.Local ? LocalSettings : RoamingSettings); }
public bool Exists(string key, SettingsStrategy strategy = SettingsStrategy.Local) { return(Container(strategy).Contains(key)); }
public void Remove(string key, SettingsStrategy strategy = SettingsStrategy.Local) { Container(strategy).Edit().Remove(key); }
public SettingsService Container(SettingsStrategy strategy) { return((strategy == SettingsStrategy.Local) ? Local : Roaming); }
public T Read <T>(string key, SettingsStrategy strategy) { return(Read(key, default(T), strategy)); }