Monday 26 January 2015

Local & Roaming Settings

In essence there's two types of data in an application, that is User Data and Application Data. The distinctions being:
  • Application Data: Data relevant to the application, such as app settings, or context specific information such as for example in a bus route application the location of bus stops or the collection of routes.
  • User Data: for example in a fitness application that tracks exercise the exercise would be Application Data, but the values would be User Data, because they're only relevant to the user. 
You could make the distinction that Application data would be the data that would be used by all users, whereas user data is the data relevant to each particular user of the application.

Let's start with our simplest option, and that is local settings, it`s ideal if you`re looking to store a simple key value pair.

private void SaveValue(string Key, Object Value, string ContainerName = "Main")
{
    var roamingSettings = ApplicationData.Current.RoamingSettings;

    if (roamingSettings.Values.ContainsKey(Key))
        roamingSettings.Values.Remove(Key);
    roamingSettings.Values.Add(new KeyValuePair<stringobject>(Key, Value));
}

private object GetStoredValue(string Key, string ContainerName = "Main")
{
    var roamingSettings = ApplicationData.Current.RoamingSettings;

    if (roamingSettings.Values.ContainsKey(Key))
        return roamingSettings.Values[Key];

    return String.Empty;

}


for more complex settings you can utilize containers, now you can think of a container as a dictionary of dictionaries, so you can group settings by key. 

private void SaveValue(string Key, Object Value, string ContainerName="Main")
{
    var localSettings = ApplicationData.Current.LocalSettings.CreateContainer(ContainerName, ApplicationDataCreateDisposition.Always);

    if (localSettings.Values.ContainsKey(Key))
        localSettings.Values.Remove(Key);
    localSettings.Values.Add(new KeyValuePair<string, object>(Key, Value));
}

private object GetStoredValue(string Key, string ContainerName = "Main")
{
           
    var localSettings = ApplicationData.Current.LocalSettings;
    if (localSettings.Containers.ContainsKey(ContainerName) && localSettings.Containers[ContainerName].Values.ContainsKey(Key))
        return localSettings.Containers[ContainerName].Values[Key];

    return String.Empty;

}

and that's about it, a lovely thing is that to modify this to use the roaming settings instead is very straight forward, just swap LocalSettings for RoamingSettings. like so

private void SaveValue(string Key, Object Value, string ContainerName = "Main")
{
    var roamingSettings = ApplicationData.Current.RoamingSettings.CreateContainer(ContainerName, ApplicationDataCreateDisposition.Always);

    if (roamingSettings.Values.ContainsKey(Key))
        roamingSettings.Values.Remove(Key);
    roamingSettings.Values.Add(new KeyValuePair<string, object>(Key, Value));
}

private object GetStoredValue(string Key, string ContainerName = "Main")
{
    var roamingSettings = ApplicationData.Current.RoamingSettings;

    if (roamingSettings.Containers.ContainsKey(ContainerName) && roamingSettings.Containers[ContainerName].Values.ContainsKey(Key))
        return roamingSettings.Containers[ContainerName].Values[Key];

    return String.Empty;

}