I just learned about the beauties and wonders of interface
s and I realized how I could fix some code that has been bothering me because of its duplication.
This is my ISettingsProvider
, SettingChangedEventArgs
class, and ApplicationSettingProvider
class, which are in the same file:
public class SettingChangedEventArgs : EventArgs
{
public readonly int NewSetting;
public SettingChangedEventArgs(int newSetting)
{
NewSetting = newSetting;
}
}
public interface ISettingsProvider
{
int GetCurrentSetting();
void SetCurrentSetting(int theme);
event EventHandler<SettingChangedEventArgs> SettingChanged;
}
public abstract class ApplicationSettingProvider : ISettingsProvider
{
public abstract int GetCurrentSetting();
public abstract void SetCurrentSetting(int setting);
public event EventHandler<SettingChangedEventArgs> SettingChanged;
public void OnSettingChanged(int newSetting)
{
var handler = SettingChanged;
if (handler != null)
{
handler(this, new SettingChangedEventArgs(newSetting));
}
}
}
ApplicationThemeProvider
:
public class ApplicationThemeProvider : ApplicationSettingProvider
{
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();
}
}
ApplicationFontSizeProvider
:
public class ApplicationFontSizeProvider : ApplicationSettingProvider
{
public ApplicationFontSizeProvider()
{
ApplicationData.Current.DataChanged += (a, o) =>
{
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
OnSettingChanged((int)ApplicationData.Current.RoamingSettings.Values["SetFontSize"]);
});
};
}
public override int GetCurrentSetting()
{
if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("SetFontSize"))
{
int value = (int)ApplicationData.Current.RoamingSettings.Values["SetFontSize"];
if (value == 0) { return 15; }
else if (value == 1) { return 20; }
else if (value == 2) { return 30; }
else if (value == 15 || value == 20 || value == 30) { return value; }
else { return 20; }
}
return 20;
}
public override void SetCurrentSetting(int fontSize)
{
ApplicationData.Current.RoamingSettings.Values["SetFontSize"] = fontSize;
ApplicationData.Current.SignalDataChanged();
}
}
ApplicationStartupVersionProvider
:
public class ApplicationStartupVersionProvider : ApplicationSettingProvider
{
public ApplicationStartupVersionProvider()
{
ApplicationData.Current.DataChanged += (a, o) =>
{
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
OnSettingChanged((int)ApplicationData.Current.RoamingSettings.Values["StartupVersion"]);
});
};
}
public override int GetCurrentSetting()
{
bool WindowsStore = true;
#if WINDOWS_PHONE_APP
WindowsStore = false;
#endif
if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("StartupVersion"))
{
int value = (int)ApplicationData.Current.RoamingSettings.Values["StartupVersion"];
if (value >= 0 && value <= 2) { return value; }
else { return WindowsStore ? 0 : 1; }
}
else { return WindowsStore ? 0 : 1; }
}
public override void SetCurrentSetting(int version)
{
ApplicationData.Current.RoamingSettings.Values["StartupVersion"] = version;
ApplicationData.Current.SignalDataChanged();
}
}
How else should I improve this code?
ApplicationThemeProvider
could expose a property for getting/setting an enum value of available themes). Those providers would take in anISettingsProvider
and translate their domain-specific values back and forth toint
s for the purpose of reading/writing application settings, but that implementation detail could be hidden from callers. \$\endgroup\$