Always load default settings when deserializing SettingsRepository.

- VERBESSERT: Bei Updates werden neue eingebaute Stile jetzt immer berücksichtigt.
This commit is contained in:
Daniel Kraus 2015-08-20 21:51:31 +02:00
parent 7eaabb7f13
commit 3fcb6c1af5

View File

@ -72,7 +72,9 @@ namespace zaaReloaded2.Controller
stream.Write(bytes, 0, bytes.Length); stream.Write(bytes, 0, bytes.Length);
stream.Position = 0; stream.Position = 0;
SoapFormatter serializer = new SoapFormatter(); SoapFormatter serializer = new SoapFormatter();
return serializer.Deserialize(stream) as SettingsRepository; SettingsRepository repo = serializer.Deserialize(stream) as SettingsRepository;
repo.LoadDefaults();
return repo;
} }
} }
@ -113,7 +115,7 @@ namespace zaaReloaded2.Controller
public SettingsRepository() public SettingsRepository()
{ {
SettingsList = new List<Settings>(); SettingsList = new List<Settings>();
CreateDefault(); Reset();
} }
#endregion #endregion
@ -181,23 +183,46 @@ namespace zaaReloaded2.Controller
/// Resets the Settings contained in this SettingsRepository /// Resets the Settings contained in this SettingsRepository
/// to the default set of settings. /// to the default set of settings.
/// </summary> /// </summary>
private void CreateDefault() private void Reset()
{ {
SettingsList.Clear(); SettingsList.Clear();
LoadDefaults();
}
/// <summary>
/// Loads default, built-in settings, replacing existing default
/// settings that might have been persisted.
/// </summary>
private void LoadDefaults()
{
Assembly myAssembly = this.GetType().Assembly; Assembly myAssembly = this.GetType().Assembly;
SoapFormatter deserializer = new SoapFormatter(); SoapFormatter deserializer = new SoapFormatter();
Stream stream = myAssembly.GetManifestResourceStream("zaaReloaded2.Defaults.ward.zaaReloaded"); Stream stream = myAssembly.GetManifestResourceStream("zaaReloaded2.Defaults.ward.zaaReloaded");
Settings settings = deserializer.Deserialize(stream) as Settings; Settings settings = deserializer.Deserialize(stream) as Settings;
settings.Name = Properties.Settings.Default.SettingsNameWard + BUILTIN_LABEL; settings.Name = Properties.Settings.Default.SettingsNameWard + BUILTIN_LABEL;
settings.Uid = Guid.Parse(DEFAULT_SETTINGS_1_UID); settings.Uid = Guid.Parse(DEFAULT_SETTINGS_1_UID);
SettingsList.Add(settings); ReplaceOrAdd(settings);
stream = myAssembly.GetManifestResourceStream("zaaReloaded2.Defaults.clinic.zaaReloaded"); stream = myAssembly.GetManifestResourceStream("zaaReloaded2.Defaults.clinic.zaaReloaded");
settings = deserializer.Deserialize(stream) as Settings; settings = deserializer.Deserialize(stream) as Settings;
settings.Name = Properties.Settings.Default.SettingsNameClinic + BUILTIN_LABEL; settings.Name = Properties.Settings.Default.SettingsNameClinic + BUILTIN_LABEL;
settings.Uid = Guid.Parse(DEFAULT_SETTINGS_2_UID); settings.Uid = Guid.Parse(DEFAULT_SETTINGS_2_UID);
SettingsList.Add(settings); ReplaceOrAdd(settings);
}
private void ReplaceOrAdd(Settings settings)
{
Settings existing = SettingsList.FirstOrDefault(s => s.Uid == settings.Uid);
if (existing == null)
{
SettingsList.Add(settings);
}
else
{
int index = SettingsList.IndexOf(existing);
SettingsList.RemoveAt(index);
SettingsList.Insert(index, settings);
}
} }
#endregion #endregion