file360

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 using System;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;

namespace File360
{
    public class AppSettings
    {
        // Our settings
        IsolatedStorageSettings settings;

        //Key Names
        const string SideBarColorKeyName = "SideBarColor";
        const string PageColorKeyName = "PageColor";

        // The default value of our settings
        const string SideBarColorDefault = "{StaticResource PhoneChromeBrush}";
        const string PageColorDefault = "{StaticResource PhoneChromeBrush}";

        public AppSettings()
        {
            settings = IsolatedStorageSettings.ApplicationSettings;
        }

        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (settings[Key] != value)
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }
            return valueChanged;
        }

        public T GetValueOrDefault<T>(string Key, T defaultValue)
        {
            T value;

            // If the key exists, retrieve the value.
            if (settings.Contains(Key))
            {
                value = (T)settings[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            settings.Save();
        }


        /// <summary>
        /// Property to get and set a ListBox Setting Key.
        /// </summary>
        public string SideBarColor
        {
            get
            {
                return GetValueOrDefault<string>(SideBarColorKeyName, SideBarColorDefault);
            }
            set
            {
                if (AddOrUpdateValue(SideBarColorKeyName, value))
                {
                    Save();
                }
            }
        }
        public string PageColor
        {
            get
            {
                return GetValueOrDefault<string>(PageColorKeyName, PageColorDefault);
            }
            set
            {
                if (AddOrUpdateValue(PageColorKeyName, value))
                {
                    Save();
                }
            }
        }
    }
}