Last active
August 29, 2015 14:02
-
-
Save LucasMoffitt/17eb8cdd8545335781ed to your computer and use it in GitHub Desktop.
Basic Windows 8.1 Roaming Settings Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SettingsManager | |
{ | |
public static bool Set<T> (string key, T data) | |
{ | |
try | |
{ | |
ApplicationData.Current.RoamingSettings.Values[key] = data; | |
} | |
catch (Exception) | |
{ | |
try | |
{ | |
ApplicationData.Current.RoamingSettings.Values[key] = data.ToString(); | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
public static T Get<T>(string key) | |
{ | |
try | |
{ | |
return (T) ApplicationData.Current.RoamingSettings.Values[key]; | |
} | |
catch (InvalidCastException) | |
{ | |
try | |
{ | |
return (T)Enum.Parse(typeof(T), ApplicationData.Current.RoamingSettings.Values[key].ToString()); | |
} | |
catch (Exception) | |
{ | |
return default(T); | |
} | |
} | |
catch (Exception) | |
{ | |
return default(T); | |
} | |
} | |
public static bool Delete(string key) | |
{ | |
try | |
{ | |
return ApplicationData.Current.RoamingSettings.Values.Remove(key); | |
} | |
catch (Exception) | |
{ | |
return true; | |
} | |
} | |
public static bool HasKey(string key) | |
{ | |
try | |
{ | |
return ApplicationData.Current.RoamingSettings.Values.ContainsKey(key); | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class SettingsManagerTest | |
{ | |
[TestMethod] | |
public void HasKeyReturnsTrueWhenKeyIsSet() | |
{ | |
const string testKey = "TestKey"; | |
SettingsManager.Set(testKey, "Just some test data"); | |
var hasKey = SettingsManager.HasKey(testKey); | |
Assert.IsTrue(hasKey); | |
} | |
[TestMethod] | |
public void HasKeyReturnsFalseWhenKeyIsDeleted() | |
{ | |
const string testKey = "TestKey"; | |
SettingsManager.Set(testKey, "Just some test data"); | |
var hasKey = SettingsManager.HasKey(testKey); | |
var removeKey = SettingsManager.Delete(testKey); | |
var hasKeyAfterDelete = SettingsManager.HasKey(testKey); | |
Assert.IsTrue(hasKey); | |
Assert.IsTrue(removeKey); | |
Assert.IsFalse(hasKeyAfterDelete); | |
} | |
[TestMethod] | |
public void SettingsManagerCanSaveAndRetrieveString() | |
{ | |
const string testKey = "TestStringKey"; | |
const string testData = "Something to save!"; | |
SettingsManager.Set(testKey, testData); | |
var dataFromSettings = SettingsManager.Get<string>(testKey); | |
Assert.IsFalse(string.IsNullOrEmpty(dataFromSettings)); | |
Assert.AreEqual(testData, dataFromSettings); | |
} | |
[TestMethod] | |
public void SettingsManagerCanSaveAndRetrieveBool() | |
{ | |
const string testKey = "TestBoolKey"; | |
const bool testData = true; | |
SettingsManager.Set(testKey, testData); | |
var dataFromSettings = SettingsManager.Get<bool>(testKey); | |
Assert.AreEqual(testData, dataFromSettings); | |
} | |
[TestMethod] | |
public void SettingsManagerCanSaveAndRetrieveInt() | |
{ | |
const string testKey = "TestIntKey"; | |
const int testData = 1010; | |
SettingsManager.Set(testKey, testData); | |
var dataFromSettings = SettingsManager.Get<int>(testKey); | |
Assert.AreEqual(testData, dataFromSettings); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment