using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using SharedSettingsAbstraction.Extensions; namespace SharedSettingsAbstraction.Setting { /// /// Key Value Pair /// /// Value Type public class SettingsKey { public string key { get; private set; } private T value; public string Description { get; set; } public string Title { get; set; } private string _preferenceName; private T _defaultValue; public List SpinnerOptions { get; set; } /// /// Setup key /// /// public SettingsKey(string _key, string preferenceName, T defaultValue) { key = _key; _preferenceName = preferenceName; _defaultValue = defaultValue; } /// /// Gets the setting /// /// context /// public T getSetting(Context con ) { var shared = con.GetSharedPreferences(_preferenceName, FileCreationMode.WorldReadable); try { value = (T)shared.All.Where(x => x.Key == key).FirstOrDefault().Value; if (value == null) SetSetting(con, _defaultValue); } catch (Exception) { value = (T)_defaultValue; } return value; } /// /// Set the setting with a new setting /// /// /// /// public SettingsKey SetSetting(Context con, T val) { var shared = con.GetSharedPreferences(_preferenceName, FileCreationMode.WorldWriteable); var edit = shared.Edit(); edit.SaveObject(key, val); edit.Commit(); value = val; return this; } } }