sharee.bike-App/TINKLib/Model/Settings/JsonSettingsDictionary.cs

583 lines
23 KiB
C#
Raw Normal View History

2022-10-17 18:45:38 +02:00
using System;
2021-05-13 20:03:07 +02:00
using System.Collections.Generic;
using System.Linq;
2022-08-30 15:42:25 +02:00
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Serilog.Events;
2021-05-13 20:03:07 +02:00
using TINK.Model.Services.CopriApi.ServerUris;
2022-08-30 15:42:25 +02:00
using TINK.Services.BluetoothLock;
2022-04-10 17:38:34 +02:00
using TINK.Services.Geolocation;
2021-05-13 20:03:07 +02:00
using TINK.Settings;
using TINK.ViewModel.Map;
using TINK.ViewModel.Settings;
namespace TINK.Model.Settings
{
2022-09-06 16:08:19 +02:00
public static class JsonSettingsDictionary
{
/// <summary>Title of the settings file.</summary>
public const string SETTINGSFILETITLE = "Setting.Json";
/// <summary> Key of the app version entry. </summary>
2023-08-31 12:20:06 +02:00
public const string APPVERSIONKEY = "AppVersion";
2022-09-06 16:08:19 +02:00
/// <summary> Key of the app version entry. </summary>
public const string SHOWWHATSNEWKEY = "ShowWhatsNew";
/// <summary> Key of the app version entry. </summary>
public const string EXPIRESAFTER = "ExpiresAfter";
/// <summary> Key of the connect timeout. </summary>
public const string CONNECTTIMEOUT = "ConnectTimeout";
/// <summary> Key of the logging level entry. </summary>
public const string MINLOGGINGLEVELKEY = "MinimumLoggingLevel";
/// <summary> Key of the logging level entry. </summary>
public const string ISREPORTLEVELVERBOSEKEY = "IsReportLevelVerbose";
/// <summary> Key of the center to ... entry. </summary>
public const string CENTERMAPTOCURRENTLOCATION = "CenterMapToCurrentLocation";
2022-10-17 18:45:38 +02:00
/// <summary> Key of the center to ... entry. </summary>
public const string STARTUPSETTINGS = "StartupSettings";
2022-09-06 16:08:19 +02:00
public const string LOGTOEXTERNALFOLDER = "LogToExternalFolder";
public const string THEMEKEY = "Theme";
public const string ISSITECACHINGON = "IsSiteCachingOn";
/// <summary> Gets a nullable value.</summary>
/// <param name="settingsJSON">Dictionary to get value from.</param>
public static T? GetNullableEntry<T>(
string keyName,
2022-10-17 18:45:38 +02:00
IDictionary<string, string> settingsJSON) where T : struct
2022-09-06 16:08:19 +02:00
{
if (!settingsJSON.TryGetValue(keyName, out string boolText)
|| string.IsNullOrEmpty(boolText))
{
// File holds no entry.
return null;
}
return JsonConvert.DeserializeObject<T>(boolText);
}
/// <summary> Gets a value to type class.</summary>
/// <param name="settingsJSON">Dictionary to get value from.</param>
public static T GetEntry<T>(
string keyName,
2022-10-17 18:45:38 +02:00
IDictionary<string, string> settingsJSON,
2022-09-06 16:08:19 +02:00
Func<string, string> legacyValueConverter = null) where T : class
{
if (string.IsNullOrEmpty(keyName)
|| settingsJSON == null
|| !settingsJSON.TryGetValue(keyName, out string valueJSON)
|| string.IsNullOrEmpty(valueJSON))
{
// File holds no entry.
return null;
}
return JsonConvert.DeserializeObject<T>(legacyValueConverter != null
? legacyValueConverter(valueJSON)
: valueJSON);
}
/// <summary> Sets a nullable.</summary>
/// <param name="entry">Entry to add to dictionary.</param>
/// <param name="keyName">Key to use for value. </param>
/// <param name="targetDictionary">Dictionary to set value to.</param>
public static Dictionary<string, string> SetEntry<T>(T entry, string keyName, IDictionary<string, string> targetDictionary)
{
// Set value.
if (targetDictionary == null)
throw new Exception($"Writing entry value {keyName} to dictionary failed. Dictionary must not be null.");
return targetDictionary.Union(new Dictionary<string, string>
{
{ keyName, JsonConvert.SerializeObject(entry) }
}).ToDictionary(key => key.Key, value => value.Value);
}
/// <summary> Sets the timeout to apply when connecting to bluetooth lock.</summary>
/// <param name="targetDictionary">Dictionary to write information to.</param>
/// <param name="connectTimeout">Connect timeout value.</param>
public static Dictionary<string, string> SetConnectTimeout(
this IDictionary<string, string> targetDictionary,
TimeSpan connectTimeout)
{
if (targetDictionary == null)
throw new Exception("Writing conntect timeout info failed. Dictionary must not be null.");
return targetDictionary.Union(new Dictionary<string, string>
{
{ CONNECTTIMEOUT, JsonConvert.SerializeObject(connectTimeout, new JavaScriptDateTimeConverter()) }
}).ToDictionary(key => key.Key, value => value.Value);
}
/// <summary> Sets the uri of the active copri host. </summary>
/// <param name="settingsJSON">Dictionary holding parameters from JSON.</param>
2023-02-22 14:03:35 +01:00
public static Dictionary<string, string> SetCopriHostUri(this IDictionary<string, string> targetDictionary, string p_strNextActiveUriText)
2022-09-06 16:08:19 +02:00
{
2023-02-22 14:03:35 +01:00
if (targetDictionary == null)
2022-09-06 16:08:19 +02:00
throw new Exception("Writing copri host uri to dictionary failed. Dictionary must not be null.");
2023-02-22 14:03:35 +01:00
return targetDictionary.Union(new Dictionary<string, string>
2022-09-06 16:08:19 +02:00
{
{ typeof(CopriServerUriList).ToString(), JsonConvert.SerializeObject(p_strNextActiveUriText) },
}).ToDictionary(key => key.Key, value => value.Value);
}
/// <summary> Gets the timeout to apply when connecting to bluetooth lock.</summary>
/// <param name="settingsJSON">Dictionary to get information from.</param>
/// <returns>Connect timeout value.</returns>
public static TimeSpan? GetConnectTimeout(Dictionary<string, string> settingsJSON)
{
if (!settingsJSON.TryGetValue(CONNECTTIMEOUT, out string connectTimeout)
|| string.IsNullOrEmpty(connectTimeout))
{
// File holds no entry.
return null;
}
return JsonConvert.DeserializeObject<TimeSpan>(connectTimeout, new JavaScriptDateTimeConverter());
}
/// <summary> Gets the logging level.</summary>
/// <param name="settingsJSON">Dictionary to get logging level from.</param>
/// <returns>Logging level</returns>
public static Uri GetCopriHostUri(this IDictionary<string, string> settingsJSON)
{
2023-05-09 08:47:52 +02:00
// Get uri of copri server.
2022-09-06 16:08:19 +02:00
if (!settingsJSON.TryGetValue(typeof(CopriServerUriList).ToString(), out string uriText)
|| string.IsNullOrEmpty(uriText))
{
// File holds no entry.
return null;
}
return JsonConvert.DeserializeObject<Uri>(uriText);
}
/// <summary> Sets the version of the app. </summary>
/// <param name="settingsJSON">Dictionary holding parameters from JSON.</param>
public static Dictionary<string, string> SetAppVersion(
this IDictionary<string, string> targetDictionary,
Version appVersion)
{
if (targetDictionary == null)
throw new Exception("Writing app version to dictionary failed. Dictionary must not be null.");
return targetDictionary.Union(new Dictionary<string, string>
{
2023-08-31 12:20:06 +02:00
{APPVERSIONKEY , JsonConvert.SerializeObject(appVersion, new VersionConverter()) },
2022-09-06 16:08:19 +02:00
}).ToDictionary(key => key.Key, value => value.Value);
}
/// <summary> Gets the app versions.</summary>
/// <param name="settingsJSON">Dictionary to get logging level from.</param>
/// <returns>Logging level</returns>
public static Version GetAppVersion(this IDictionary<string, string> settingsJSON)
{
// Get the version of the app which wrote the settings file.
2023-08-31 12:20:06 +02:00
if (!settingsJSON.TryGetValue(APPVERSIONKEY, out string appVersion)
2022-09-06 16:08:19 +02:00
|| string.IsNullOrEmpty(appVersion))
{
// File holds no entry.
return null;
}
return appVersion.TrimStart().StartsWith("\"")
? JsonConvert.DeserializeObject<Version>(appVersion, new VersionConverter())
: Version.Parse(appVersion); // Format used up to version 3.0.0.115
}
2023-04-19 12:14:14 +02:00
/// <summary> Sets whether polling is on or off and the period if polling is on. </summary>
2022-09-06 16:08:19 +02:00
/// <param name="settingsJSON">Dictionary to write entries to.</param>
2023-02-22 14:03:35 +01:00
public static Dictionary<string, string> SetPollingParameters(
this IDictionary<string, string> targetDictionary,
PollingParameters pollingParameter)
2022-09-06 16:08:19 +02:00
{
2023-02-22 14:03:35 +01:00
if (targetDictionary == null)
2022-09-06 16:08:19 +02:00
throw new Exception("Writing polling parameters to dictionary failed. Dictionary must not be null.");
2023-02-22 14:03:35 +01:00
return targetDictionary.Union(new Dictionary<string, string>
2022-09-06 16:08:19 +02:00
{
2023-02-22 14:03:35 +01:00
{ $"{typeof(PollingParameters).Name}_{typeof(TimeSpan).Name}", JsonConvert.SerializeObject(pollingParameter.Periode) },
{ $"{typeof(PollingParameters).Name}_{typeof(bool).Name}", JsonConvert.SerializeObject(pollingParameter.IsActivated) },
2022-09-06 16:08:19 +02:00
}).ToDictionary(key => key.Key, value => value.Value);
}
2023-04-19 12:14:14 +02:00
/// <summary> Get whether polling is on or off and the period if polling is on. </summary>
2022-09-06 16:08:19 +02:00
/// <param name="settingsJSON">Dictionary holding parameters from JSON.</param>
/// <returns>Polling parameters.</returns>
public static PollingParameters GetPollingParameters(this IDictionary<string, string> settingsJSON)
{
2023-04-19 12:14:14 +02:00
// Check if dictionary contains entry for period.
if (settingsJSON.TryGetValue($"{typeof(PollingParameters).Name}_{typeof(TimeSpan).Name}", out string period)
2022-09-06 16:08:19 +02:00
&& settingsJSON.TryGetValue($"{typeof(PollingParameters).Name}_{typeof(bool).Name}", out string active)
2023-04-19 12:14:14 +02:00
&& !string.IsNullOrEmpty(period)
2022-09-06 16:08:19 +02:00
&& !string.IsNullOrEmpty(active))
{
return new PollingParameters(
2023-04-19 12:14:14 +02:00
JsonConvert.DeserializeObject<TimeSpan>(period),
2022-09-06 16:08:19 +02:00
JsonConvert.DeserializeObject<bool>(active));
}
return null;
}
/// <summary> Saves object to file. </summary>
/// <param name="settingsList">Settings to save.</param>
public static void Serialize(string settingsFileFolder, IDictionary<string, string> settingsList)
{
// Save settings to file.
var l_oText = JsonConvert.SerializeObject(settingsList, Formatting.Indented);
var l_oFolder = settingsFileFolder;
System.IO.File.WriteAllText($"{l_oFolder}{System.IO.Path.DirectorySeparatorChar}{SETTINGSFILETITLE}", l_oText);
}
/// <summary> Gets TINK app settings form xml- file. </summary>
/// <param name="settingsDirectory">Directory to read settings from.</param>
/// <returns>Dictionary of settings.</returns>
public static Dictionary<string, string> Deserialize(string settingsDirectory)
{
2023-02-22 14:03:35 +01:00
var fileName = $"{settingsDirectory}{System.IO.Path.DirectorySeparatorChar}{SETTINGSFILETITLE}";
2022-09-06 16:08:19 +02:00
2023-02-22 14:03:35 +01:00
if (!System.IO.File.Exists(fileName))
2022-09-06 16:08:19 +02:00
{
// File is empty. Nothing to read.
return new Dictionary<string, string>(); ;
}
2023-02-22 14:03:35 +01:00
var jsonFile = System.IO.File.ReadAllText(fileName);
2022-09-06 16:08:19 +02:00
2023-02-22 14:03:35 +01:00
if (string.IsNullOrEmpty(jsonFile))
2022-09-06 16:08:19 +02:00
{
// File is empty. Nothing to read.
return new Dictionary<string, string>();
}
// Load setting file.
2023-02-22 14:03:35 +01:00
return JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonFile);
2022-09-06 16:08:19 +02:00
}
/// <summary> Gets the logging level.</summary>
/// <param name="settingsJSON">Dictionary to get logging level from.</param>
/// <returns>Logging level.</returns>
public static LogEventLevel? GetMinimumLoggingLevel(
Dictionary<string, string> settingsJSON)
{
// Get logging level.
if (!settingsJSON.TryGetValue(MINLOGGINGLEVELKEY, out string level)
|| string.IsNullOrEmpty(level))
{
// File holds no entry.
return null;
}
return (LogEventLevel)int.Parse(JsonConvert.DeserializeObject<string>(level));
}
/// <summary> Gets a value indicating whether report level is verbose or not.</summary>
/// <param name="settingsJSON">Dictionary to get value from.</param>
public static bool? GetIsReportLevelVerbose(Dictionary<string, string> settingsJSON) => GetNullableEntry<bool>(ISREPORTLEVELVERBOSEKEY, settingsJSON);
/// <summary> Sets a value indicating whether report level is verbose or not.</summary>
/// <param name="settingsJSON">Dictionary to get value from.</param>
public static Dictionary<string, string> SetIsReportLevelVerbose(this IDictionary<string, string> targetDictionary, bool isReportLevelVerbose)
=> SetEntry(isReportLevelVerbose, ISREPORTLEVELVERBOSEKEY, targetDictionary);
/// <summary> Sets the logging level.</summary>
/// <param name="settingsJSON">Dictionary to get logging level from.</param>
2023-02-22 14:03:35 +01:00
public static Dictionary<string, string> SetMinimumLoggingLevel(this IDictionary<string, string> targetDictionary, LogEventLevel level)
2022-09-06 16:08:19 +02:00
{
// Set logging level.
2023-02-22 14:03:35 +01:00
if (targetDictionary == null)
2022-09-06 16:08:19 +02:00
throw new Exception("Writing logging level to dictionary failed. Dictionary must not be null.");
2023-02-22 14:03:35 +01:00
return targetDictionary.Union(new Dictionary<string, string>
2022-09-06 16:08:19 +02:00
{
2023-02-22 14:03:35 +01:00
{ MINLOGGINGLEVELKEY, JsonConvert.SerializeObject((int)level) }
2022-09-06 16:08:19 +02:00
}).ToDictionary(key => key.Key, value => value.Value);
}
/// <summary> Gets the version of app when whats new was shown.</summary>
/// <param name="settingsJSON">Dictionary to get logging level from.</param>
/// <returns>Version of the app.</returns>
public static Version GetWhatsNew(Dictionary<string, string> settingsJSON)
{
// Get logging level.
if (!settingsJSON.TryGetValue(SHOWWHATSNEWKEY, out string whatsNewVersion)
|| string.IsNullOrEmpty(whatsNewVersion))
{
// File holds no entry.
return null;
}
return JsonConvert.DeserializeObject<Version>(whatsNewVersion, new VersionConverter());
}
/// <summary> Sets the version of app when whats new was shown.</summary>
/// <param name="settingsJSON">Dictionary to get information from.</param>
2023-02-22 14:03:35 +01:00
public static Dictionary<string, string> SetWhatsNew(this IDictionary<string, string> targetDictionary, Version appVersion)
2022-09-06 16:08:19 +02:00
{
// Set logging level.
2023-02-22 14:03:35 +01:00
if (targetDictionary == null)
2022-09-06 16:08:19 +02:00
throw new Exception("Writing WhatsNew info failed. Dictionary must not be null.");
2023-02-22 14:03:35 +01:00
return targetDictionary.Union(new Dictionary<string, string>
2022-09-06 16:08:19 +02:00
{
2023-02-22 14:03:35 +01:00
{ SHOWWHATSNEWKEY, JsonConvert.SerializeObject(appVersion, new VersionConverter()) }
2022-09-06 16:08:19 +02:00
}).ToDictionary(key => key.Key, value => value.Value);
}
/// <summary> Gets the expires after value.</summary>
/// <param name="settingsJSON">Dictionary to get expries after value from.</param>
/// <returns>Expires after value.</returns>
public static TimeSpan? GetExpiresAfter(Dictionary<string, string> settingsJSON)
{
if (!settingsJSON.TryGetValue(EXPIRESAFTER, out string expiresAfter)
|| string.IsNullOrEmpty(expiresAfter))
{
// File holds no entry.
return null;
}
return JsonConvert.DeserializeObject<TimeSpan>(expiresAfter, new JavaScriptDateTimeConverter());
}
/// <summary> Sets the the expiration time.</summary>
/// <param name="settingsJSON">Dictionary to write information to.</param>
2023-02-22 14:03:35 +01:00
public static Dictionary<string, string> SetExpiresAfter(this IDictionary<string, string> targetDictionary, TimeSpan expiresAfter)
2022-09-06 16:08:19 +02:00
{
2023-02-22 14:03:35 +01:00
if (targetDictionary == null)
2022-09-06 16:08:19 +02:00
throw new Exception("Writing ExpiresAfter info failed. Dictionary must not be null.");
2023-02-22 14:03:35 +01:00
return targetDictionary.Union(new Dictionary<string, string>
2022-09-06 16:08:19 +02:00
{
{ EXPIRESAFTER, JsonConvert.SerializeObject(expiresAfter, new JavaScriptDateTimeConverter()) }
}).ToDictionary(key => key.Key, value => value.Value);
}
/// <summary> Sets the active lock service name. </summary>
/// <param name="targetDictionary">Dictionary holding parameters from JSON.</param>
public static Dictionary<string, string> SetActiveLockService(this IDictionary<string, string> targetDictionary, string activeLockService)
{
if (targetDictionary == null)
throw new Exception("Writing active lock service name to dictionary failed. Dictionary must not be null.");
return targetDictionary.Union(new Dictionary<string, string>
{
{ typeof(ILocksService).Name, activeLockService },
}).ToDictionary(key => key.Key, value => value.Value);
}
/// <summary> Gets the active lock service name.</summary>
/// <param name="settingsJSON">Dictionary to get logging level from.</param>
/// <returns>Active lock service name.</returns>
public static string GetActiveLockService(this IDictionary<string, string> settingsJSON)
{
2023-05-09 08:47:52 +02:00
// Get uri of copri server.
2022-09-06 16:08:19 +02:00
if (!settingsJSON.TryGetValue(typeof(ILocksService).Name, out string activeLockService)
|| string.IsNullOrEmpty(activeLockService))
{
// File holds no entry.
return null;
}
if (activeLockService == "TINK.Services.BluetoothLock.BLE.LockItByScanService")
{
// Name of this service was switched.
return typeof(TINK.Services.BluetoothLock.BLE.LockItByScanServicePolling).FullName;
}
return activeLockService;
}
/// <summary> Sets the active Geolocation service name. </summary>
/// <param name="targetDictionary">Dictionary holding parameters from JSON.</param>
public static Dictionary<string, string> SetActiveGeolocationService(
this IDictionary<string, string> targetDictionary,
string activeGeolocationService)
{
if (targetDictionary == null)
throw new Exception("Writing active geolocation service name to dictionary failed. Dictionary must not be null.");
return targetDictionary.Union(new Dictionary<string, string>
{
2023-04-05 15:02:10 +02:00
{ typeof(IGeolocationService).Name, activeGeolocationService },
2022-09-06 16:08:19 +02:00
}).ToDictionary(key => key.Key, value => value.Value);
}
/// <summary> Gets the active Geolocation service name.</summary>
/// <param name="settingsJSON">Dictionary to get name of geolocation service from.</param>
/// <returns>Active lock service name.</returns>
public static string GetActiveGeolocationService(this IDictionary<string, string> settingsJSON)
{
2023-05-09 08:47:52 +02:00
// Get uri of copri server.
2023-04-05 15:02:10 +02:00
if (!settingsJSON.TryGetValue(typeof(IGeolocationService).Name, out string activeGeolocationService)
2022-09-06 16:08:19 +02:00
|| string.IsNullOrEmpty(activeGeolocationService))
{
// File holds no entry.
return null;
}
return activeGeolocationService;
}
/// <summary> Gets a value indicating whether to center the map to location or not.</summary>
/// <param name="settingsJSON">Dictionary to get value from.</param>
public static bool? GetCenterMapToCurrentLocation(Dictionary<string, string> settingsJSON) => GetNullableEntry<bool>(CENTERMAPTOCURRENTLOCATION, settingsJSON);
/// <summary> Sets a value indicating whether to center the map to location or not.</summary>
/// <param name="settingsJSON">Dictionary to get value from.</param>
public static Dictionary<string, string> SetCenterMapToCurrentLocation(this IDictionary<string, string> targetDictionary, bool centerMapToCurrentLocation)
=> SetEntry(centerMapToCurrentLocation, CENTERMAPTOCURRENTLOCATION, targetDictionary);
/// <summary> Gets whether to store logging data on SD card or not.</summary>
/// <param name="settingsJSON">Dictionary to get value from.</param>
public static bool? GetLogToExternalFolder(Dictionary<string, string> settingsJSON) => GetNullableEntry<bool>(LOGTOEXTERNALFOLDER, settingsJSON);
/// <summary> Gets full class name of active theme.</summary>
/// <param name="settingsJSON">Dictionary to get value from.</param>
public static string GetActiveTheme(Dictionary<string, string> settingsJSON)
=> GetEntry<string>(THEMEKEY, settingsJSON, (value) =>
{
if (value == JsonConvert.SerializeObject(typeof(Themes.Konrad).FullName))
{
return JsonConvert.SerializeObject(TINK.Services.ThemeNS.ThemeSet.Konrad.ToString());
}
else if (value == JsonConvert.SerializeObject(typeof(Themes.LastenradBayern).FullName))
{
return JsonConvert.SerializeObject(TINK.Services.ThemeNS.ThemeSet.LastenradBayern.ToString());
}
else if (value == JsonConvert.SerializeObject(typeof(Themes.ShareeBike).FullName))
{
return JsonConvert.SerializeObject(TINK.Services.ThemeNS.ThemeSet.ShareeBike.ToString());
}
else
{
return value;
}
});
/// <summary> Gets a value indicating whether site caching is on or off.</summary>
/// <param name="settingsJSON">Dictionary to get value from.</param>
public static bool? GetIsSiteCachingOn(Dictionary<string, string> settingsJSON) => GetNullableEntry<bool>(ISSITECACHINGON, settingsJSON);
/// <summary> Sets whether to store logging data on SD card or not.</summary>
2022-10-17 18:45:38 +02:00
/// <param name="settingsJSON">Dictionary to write value to.</param>
2022-09-06 16:08:19 +02:00
public static Dictionary<string, string> SetLogToExternalFolder(this IDictionary<string, string> targetDictionary, bool useSdCard) => SetEntry(useSdCard, LOGTOEXTERNALFOLDER, targetDictionary);
/// <summary> Sets active theme.</summary>
/// <param name="targetDictionary">Dictionary to set value to.</param>
public static Dictionary<string, string> SetActiveTheme(
this IDictionary<string, string> targetDictionary,
string theme)
=> SetEntry(theme, THEMEKEY, targetDictionary);
/// <summary> Sets whether site caching is on or off.</summary>
/// <param name="settingsJSON">Dictionary to get value from.</param>
public static Dictionary<string, string> SetIsSiteCachingOn(this IDictionary<string, string> targetDictionary, bool useSdCard) => SetEntry(useSdCard, ISSITECACHINGON, targetDictionary);
/// <summary> Gets the map page filter. </summary>
/// <param name="settings">Settings objet to load from.</param>
public static IGroupFilterMapPage GetGroupFilterMapPage(this IDictionary<string, string> settings)
{
var keyName = "FilterCollection_MapPageFilter";
if (settings == null || !settings.ContainsKey(keyName))
{
return null;
}
return new GroupFilterMapPage(JsonConvert.DeserializeObject<IDictionary<string, FilterState>>(settings[keyName]));
}
public static IDictionary<string, string> SetGroupFilterMapPage(
this IDictionary<string, string> settings,
2023-02-22 14:03:35 +01:00
IDictionary<string, FilterState> filterCollection)
2022-09-06 16:08:19 +02:00
{
if (settings == null
2023-02-22 14:03:35 +01:00
|| filterCollection == null
|| filterCollection.Count < 1)
2022-09-06 16:08:19 +02:00
{
return settings;
}
2023-02-22 14:03:35 +01:00
settings["FilterCollection_MapPageFilter"] = JsonConvert.SerializeObject(filterCollection);
2022-09-06 16:08:19 +02:00
return settings;
}
/// <summary> Gets the settings filter. </summary>
/// <param name="settings">Settings objet to load from.</param>
public static IGroupFilterSettings GetGoupFilterSettings(this IDictionary<string, string> settings)
{
var keyName = "FilterCollection";
if (settings == null || !settings.ContainsKey(keyName))
{
return null;
}
var legacyFilterCollection = new GroupFilterSettings(JsonConvert.DeserializeObject<IDictionary<string, FilterState>>(settings[keyName]));
// Process legacy entries.
var updatedFilterCollection = legacyFilterCollection.Where(x => x.Key.ToUpper() != "TINK.SMS" && x.Key.ToUpper() != "TINK.COPRI").ToDictionary(x => x.Key, x => x.Value);
if (legacyFilterCollection.Count() <= updatedFilterCollection.Count())
{
// No legacy entries "INK.SMS" or "TINK.COPRI" found.
return legacyFilterCollection;
}
var list = updatedFilterCollection.ToList();
updatedFilterCollection.Add(
"TINK",
legacyFilterCollection.Any(x => x.Key.ToUpper() == "TINK.COPRI") ? legacyFilterCollection.FirstOrDefault(x => x.Key.ToUpper() == "TINK.COPRI").Value : FilterState.Off);
return new GroupFilterSettings(updatedFilterCollection);
}
public static IDictionary<string, string> SetGroupFilterSettings(
this IDictionary<string, string> settings,
2023-02-22 14:03:35 +01:00
IDictionary<string, FilterState> filterCollection)
2022-09-06 16:08:19 +02:00
{
if (settings == null
2023-02-22 14:03:35 +01:00
|| filterCollection == null
|| filterCollection.Count < 1)
2022-09-06 16:08:19 +02:00
{
return settings;
}
2023-02-22 14:03:35 +01:00
settings["FilterCollection"] = JsonConvert.SerializeObject(filterCollection);
2022-09-06 16:08:19 +02:00
return settings;
}
2022-10-17 18:45:38 +02:00
/// <summary>
/// Gets the startup settings from dictionary.
/// </summary>
/// <param name="settings">Settings objet to load from.</param>
public static StartupSettings GetStartupSettings(this IDictionary<string, string> settingsJSON)
=> GetEntry<StartupSettings>(STARTUPSETTINGS, settingsJSON) ?? new StartupSettings();
/// <summary> Sets the startup settings.</summary>
/// <param name="settingsJSON">Dictionary to write value to.</param>
public static IDictionary<string, string> SetStartupSettings(
this IDictionary<string, string> settingsJSON,
IStartupSettings startupSettings)
{
if (settingsJSON == null
|| startupSettings == null)
{
return settingsJSON;
}
settingsJSON[STARTUPSETTINGS] = JsonConvert.SerializeObject(startupSettings);
return settingsJSON;
}
2022-09-06 16:08:19 +02:00
}
2021-05-13 20:03:07 +02:00
}