<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Serialization;
using static System.Math;

using WlanProfileViewer.Common;

namespace WlanProfileViewer.Models
{
	/// <summary>
	/// This application's settings
	/// </summary>
	public class Settings : BindableBase
	{
		public static Settings Current { get; } = new Settings();

		public WindowPlacement.WINDOWPLACEMENT? Placement { get; set; }

		#region Settings

		private const int _intervalMin = 4;
		private const int _intervalMax = 16;

		public int AutoReloadInterval
		{
			get { return _autoReloadInterval; }
			set
			{
				var clamped = Max(Min(value, _intervalMax), _intervalMin);
				SetProperty(ref _autoReloadInterval, clamped);
			}
		}
		private int _autoReloadInterval = 8; // Default

		#endregion

		#region Load/Save

		public static bool IsLoaded { get; private set; }

		private const string _settingsFileName = "settings.xml";
		private static readonly string _settingsFilePath = Path.Combine(FolderPathAppData, _settingsFileName);

		public static void Load()
		{
			try
			{
				if (File.Exists(_settingsFilePath))
				{
					using (var fs = new FileStream(_settingsFilePath, FileMode.Open, FileAccess.Read))
					{
						var serializer = new XmlSerializer(typeof(Settings));
						var loaded = (Settings)serializer.Deserialize(fs);

						typeof(Settings)
							.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
							.Where(x => x.CanWrite)
							.ToList()
							.ForEach(x => x.SetValue(Current, x.GetValue(loaded)));
					}
				}
			}
			catch (Exception ex)
			{
				throw new Exception("Failed to load settings.", ex);
			}

			IsLoaded = true;
		}

		public static void Save()
		{
			if (!IsLoaded)
				return;

			try
			{
				PrepareFolderAppData();

				using (var fs = new FileStream(_settingsFilePath, FileMode.Create, FileAccess.Write))
				{
					var serializer = new XmlSerializer(typeof(Settings));
					serializer.Serialize(fs, Current);
				}
			}
			catch (Exception ex)
			{<commit_msg>Fixed InvalidOperationException when loading settings file<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;
using System.Xml.Serialization;
using static System.Math;

using WlanProfileViewer.Common;

namespace WlanProfileViewer.Models
{
	/// <summary>
	/// This application's settings
	/// </summary>
	public class Settings : BindableBase
	{
		public static Settings Current { get; } = new Settings();

		public WindowPlacement.WINDOWPLACEMENT? Placement { get; set; }

		#region Settings

		private const int _intervalMin = 4;
		private const int _intervalMax = 16;

		public int AutoReloadInterval
		{
			get { return _autoReloadInterval; }
			set
			{
				var clamped = Max(Min(value, _intervalMax), _intervalMin);
				SetProperty(ref _autoReloadInterval, clamped);
			}
		}
		private int _autoReloadInterval = 8; // Default

		#endregion

		#region Load/Save

		public static bool IsLoaded { get; private set; }

		private const string _settingsFileName = "settings.xml";
		private static readonly string _settingsFilePath = Path.Combine(FolderPathAppData, _settingsFileName);

		public static void Load()
		{
			IsLoaded = true;

			if (!File.Exists(_settingsFilePath))
				return;

			try
			{
				using (var fs = new FileStream(_settingsFilePath, FileMode.Open, FileAccess.Read))
				{
					var serializer = new XmlSerializer(typeof(Settings));
					var loaded = (Settings)serializer.Deserialize(fs);

					typeof(Settings)
						.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
						.Where(x => x.CanWrite)
						.ToList()
						.ForEach(x => x.SetValue(Current, x.GetValue(loaded)));
				}
			}
			catch (InvalidOperationException ex) when (ex.InnerException is XmlException)
			{
				// Ignore broken settings file.
			}
			catch (Exception ex)
			{
				throw new Exception("Failed to load settings.", ex);
			}
		}

		public static void Save()
		{
			if (!IsLoaded)
				return;

			try
			{
				PrepareFolderAppData();

				using (var fs = new FileStream(_settingsFilePath, FileMode.Create, FileAccess.Write))
				{
					var serializer = new XmlSerializer(typeof(Settings));
					serializer.Serialize(fs, Current);
				}
			}
			catch (Exception ex)
			{
{'fraction_non_alphanumeric': 0.10142700329308452, 'fraction_numerical': 0.001756311745334797, 'mean_word_length': 3.5153617443012886, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5199004', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1338, 'n_words': 355}