Skip to content

Instantly share code, notes, and snippets.

@jakesays-old
Created October 20, 2014 23:52
Show Gist options
  • Save jakesays-old/2e14785d85610e179aeb to your computer and use it in GitHub Desktop.
Save jakesays-old/2e14785d85610e179aeb to your computer and use it in GitHub Desktop.
Simple app settings class
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
using System.Windows.Media;
using FirstFloor.ModernUI.Presentation;
namespace MegaTestBuilder
{
internal static class AppSettings
{
[Serializable]
internal sealed class AppSettingsData : ISerializable
{
public Rect? MainWindowLocation { get; set; }
public string LastConnectionString { get; set; }
public string LastGeneratedTestSetDirectory { get; set; }
public string LastRawDataDirectory { get; set; }
public string LastTemplateDirectory { get; set; }
public bool? LastBuildProject { get; set; }
public string LastNamespace { get; set; }
public string LastQuery { get; set; }
public string LastOutputDirectory { get; set; }
public string AccentColor { get; set; }
public FontSize? FontSize { get; set; }
public Uri ThemeSource { get; set; }
public AppSettingsData()
{
}
private static IEnumerable<TItem> ToEnumerable<TItem>(IEnumerator source)
{
while (source.MoveNext())
{
yield return (TItem) source.Current;
}
}
private AppSettingsData(SerializationInfo info, StreamingContext context)
{
var items = ToEnumerable<SerializationEntry>(info.GetEnumerator())
.ToDictionary(k => k.Name, v => v.Value);
foreach (var prop in typeof (AppSettingsData).GetProperties())
{
object value;
if (items.TryGetValue(prop.Name, out value))
{
prop.SetValue(this, value);
}
}
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
foreach (var prop in typeof(AppSettingsData).GetProperties())
{
info.AddValue(prop.Name, prop.GetValue(this), prop.PropertyType);
}
}
}
private static AppSettingsData _instance;
private static readonly object _guard = new object();
public static AppSettingsData Instance
{
get
{
if (_instance == null)
{
Initialize();
}
return _instance;
}
}
private const string SettingsFileName = ".MegaTestBuilder.AppSettings";
enum SaveState
{
Save,
Defer,
Abandon
}
private static SaveState _state;
struct SaveDeferer : IDisposable
{
public void Dispose()
{
if (_state == SaveState.Defer)
{
_state = SaveState.Save;
Save();
}
}
}
public static IDisposable DeferSave()
{
_state = SaveState.Defer;
return new SaveDeferer();
}
public static void AbandonSave()
{
_state = SaveState.Abandon;
}
public static void Save()
{
if (_instance == null ||
_state != SaveState.Save)
{
return;
}
lock (_guard)
{
var instance = _instance;
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
SettingsFileName);
using (var output = File.OpenWrite(path))
{
var serializer = new BinaryFormatter();
serializer.Serialize(output, instance);
}
}
}
private static void Initialize()
{
if (_instance == null)
{
lock (_guard)
{
if (_instance != null)
{
return;
}
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
SettingsFileName);
if (File.Exists(path))
{
try
{
using (var settings = File.OpenRead(path))
{
var serializer = new BinaryFormatter();
_instance = serializer.Deserialize(settings) as AppSettingsData;
}
return;
}
catch (Exception)
{
File.Delete(path);
//ignore errors - start with clean settings
}
}
_instance = new AppSettingsData();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment