HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Settings, setting settings, and settings handlers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andsettingsettingshandlers

Problem

So, back in this question, I built a set of settings. Now, I am trying to clean the entire system up.

This is the abstract base class:

public class SettingChangedEventArgs : EventArgs
{
    public int NewSetting { get; private set; }

    public SettingChangedEventArgs(int newSetting)
    {
        NewSetting = newSetting;
    }
}

public abstract class ApplicationSettingsProvider
{
    public abstract int GetCurrentSetting();
    public abstract void SetCurrentSetting(int setting);

    public event EventHandler SettingChanged;
    public void OnSettingChanged(int newSetting)
    {
        var handler = SettingChanged;
        if (handler != null)
        {
            handler(this, new SettingChangedEventArgs(newSetting));
        }
    }
}


Here is a setting built on it:

public class ApplicationThemeProvider : ApplicationSettingsProvider
{
    public ApplicationThemeProvider()
    {
        ApplicationData.Current.DataChanged += (a, o) =>
        {
            CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                OnSettingChanged((int)ApplicationData.Current.RoamingSettings.Values["Theme"]);
            });
        };
    }

    public override int GetCurrentSetting()
    {
        if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("Theme"))
        {
            int value = (int)ApplicationData.Current.RoamingSettings.Values["Theme"];
            return (value >= 0 && value <= 2) ? value : 0;
        }
        return 0;
    }

    public override void SetCurrentSetting(int theme)
    {
        ApplicationData.Current.RoamingSettings.Values["Theme"] = theme;
        ApplicationData.Current.SignalDataChanged();
    }
}


I create an instance of the setting like this:

```
private static ApplicationSettingsProvider _ThemeProvider = new ApplicationThemeProvider();
private static HomePageVM Data = new HomePageVM(_ThemeProvider);
private static Settings settings = new

Solution


  • ThemeToLBStyleConverter and ThemeToLBIStyleConverter have identical code, save for the strings in the square brackets. They should both be subclasses of something else, with the strings as members.



  • In ApplicationThemeProvider, you should use .TryGetValue() rather than .ContainsKey() and a lookup.

Context

StackExchange Code Review Q#88916, answer score: 5

Revisions (0)

No revisions yet.