Skip to content

Commit

Permalink
Prefer local configuration if present over the configuration in the a…
Browse files Browse the repository at this point in the history
…ppdata
  • Loading branch information
steffen-wilke committed Oct 6, 2020
1 parent 75c5c8d commit d623f0f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Soloplan.WhatsON.GUI/Configuration/AboutPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void ExportButtonClick(object sender, RoutedEventArgs e)

private string GetConfigFileFilter()
{
return $"{Properties.Resources.JsonFilesFilterName}|*.{SerializationHelper.Instance.ConfigFileExtension}";
return $"{Properties.Resources.JsonFilesFilterName}|*.{SerializationHelper.ConfigFileExtension}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public bool Import(ApplicationConfiguration appConfiguration)

private string GetProjectsInterchangeFileFilter()
{
return $"{Properties.Resources.JsonFilesFilterName}|*.{SerializationHelper.Instance.ConfigFileExtension}";
return $"{Properties.Resources.JsonFilesFilterName}|*.{SerializationHelper.ConfigFileExtension}";
}

/// <summary>
Expand Down
42 changes: 25 additions & 17 deletions src/Soloplan.WhatsON/SerializationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace Soloplan.WhatsON
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
Expand All @@ -26,36 +26,44 @@ namespace Soloplan.WhatsON
public sealed class SerializationHelper
{
/// <summary>
/// The <see cref="Lazy{T}"/> instance for the singleton.
/// The configuration file extension.
/// </summary>
static readonly Lazy<SerializationHelper> lazy = new Lazy<SerializationHelper>(() => new SerializationHelper());
public const string ConfigFileExtension = "json";

/// <summary>
/// The instance of the <see cref="SerializationHelper"/>.
/// Logger instance used by this class.
/// </summary>
public static SerializationHelper Instance => lazy.Value;
private static readonly Logger log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType?.ToString());

/// <summary>
/// The configuration file extension.
/// The <see cref="Lazy{T}"/> instance for the singleton.
/// </summary>
public readonly string ConfigFileExtension = "json";
private static readonly Lazy<SerializationHelper> lazy = new Lazy<SerializationHelper>(() => new SerializationHelper());

/// <summary>
/// The configuration folder path, without the back slash.
/// Initializes a new instance of the <see cref="SerializationHelper"/> class.
/// </summary>
public string ConfigFolder;

public string ConfigFile => this.ConfigFolder + "\\configuration." + this.ConfigFileExtension;

public SerializationHelper()
{
this.ConfigFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\WhatsOn";
var localFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var localConfigFolder = localFolder != null ? Path.Combine(localFolder, "config") : null;
var appDataConfigFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\WhatsOn";

// prefer local configuration if present over the configuration in the appdata
this.ConfigFolder = localConfigFolder != null && Directory.Exists(localConfigFolder) ? localConfigFolder : appDataConfigFolder;
}

/// <summary>
/// Logger instance used by this class.
/// Gets the instance of the <see cref="SerializationHelper"/>.
/// </summary>
private static readonly Logger log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType?.ToString());
public static SerializationHelper Instance => lazy.Value;

/// <summary>
/// Gets the configuration folder path, without the back slash.
/// </summary>
public string ConfigFolder { get; private set; }

public string ConfigFile => this.ConfigFolder + "\\configuration." + ConfigFileExtension;

/// <summary>
/// Saves the configuration to specified file.
Expand Down Expand Up @@ -86,8 +94,8 @@ public static T Load<T>(string file)

public ApplicationConfiguration LoadConfiguration()
{
log.Debug("Loading configuration form file {file}", ConfigFile);
var result = Load<ApplicationConfiguration>(ConfigFile);
log.Debug("Loading configuration form file {file}", this.ConfigFile);
var result = Load<ApplicationConfiguration>(this.ConfigFile);
this.HandleConfigurationErrors(result);
return result;
}
Expand Down

0 comments on commit d623f0f

Please sign in to comment.