mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2025-06-22 05:47:28 +02:00
Initial version.
This commit is contained in:
parent
193aaa1a56
commit
b72c67a53e
228 changed files with 25924 additions and 0 deletions
75
TINKLib/Model/Settings/GroupFilterSettings.cs
Normal file
75
TINKLib/Model/Settings/GroupFilterSettings.cs
Normal file
|
@ -0,0 +1,75 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TINK.Model;
|
||||
using TINK.Model.Connector.Filter;
|
||||
|
||||
namespace TINK.ViewModel.Settings
|
||||
{
|
||||
public class GroupFilterSettings : IGroupFilterSettings
|
||||
{
|
||||
|
||||
public GroupFilterSettings(IDictionary<string, FilterState> filterDictionary = null)
|
||||
{
|
||||
FilterDictionary = filterDictionary ?? new Dictionary<string, FilterState>();
|
||||
|
||||
Filter = filterDictionary != null
|
||||
? (IGroupFilter) new IntersectGroupFilter(FilterDictionary.Where(x => x.Value == FilterState.On).Select(x => x.Key))
|
||||
: new NullGroupFilter();
|
||||
}
|
||||
|
||||
private IDictionary<string, FilterState> FilterDictionary { get; set; }
|
||||
|
||||
private IGroupFilter Filter { get; }
|
||||
|
||||
/// <summary> Performs filtering on response-group. </summary>
|
||||
public IEnumerable<string> DoFilter(IEnumerable<string> filter = null) => Filter.DoFilter(filter);
|
||||
|
||||
public FilterState this[string key] { get => FilterDictionary[key]; set => FilterDictionary[key] = value; }
|
||||
|
||||
public ICollection<string> Keys => FilterDictionary.Keys;
|
||||
|
||||
public ICollection<FilterState> Values => FilterDictionary.Values;
|
||||
|
||||
public int Count => FilterDictionary.Count;
|
||||
|
||||
public bool IsReadOnly => true;
|
||||
|
||||
public void Add(string key, FilterState value)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<string, FilterState> item)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<string, FilterState> item) => FilterDictionary.Contains(item);
|
||||
|
||||
public bool ContainsKey(string key) => FilterDictionary.ContainsKey(key);
|
||||
|
||||
public void CopyTo(KeyValuePair<string, FilterState>[] array, int arrayIndex) => FilterDictionary.CopyTo(array, arrayIndex);
|
||||
|
||||
public IEnumerator<KeyValuePair<string, FilterState>> GetEnumerator() => FilterDictionary.GetEnumerator();
|
||||
|
||||
public bool Remove(string key)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<string, FilterState> item)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public bool TryGetValue(string key, out FilterState value) => FilterDictionary.TryGetValue(key, out value);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => FilterDictionary.GetEnumerator();
|
||||
}
|
||||
}
|
11
TINKLib/Model/Settings/IGroupFilterSettings.cs
Normal file
11
TINKLib/Model/Settings/IGroupFilterSettings.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
using System.Collections.Generic;
|
||||
using TINK.Model;
|
||||
|
||||
namespace TINK.ViewModel.Settings
|
||||
{
|
||||
public interface IGroupFilterSettings : IDictionary<string, FilterState>
|
||||
{
|
||||
/// <summary> Performs filtering on response-group. </summary>
|
||||
IEnumerable<string> DoFilter(IEnumerable<string> filter = null);
|
||||
}
|
||||
}
|
515
TINKLib/Model/Settings/JsonSettingsDictionary.cs
Normal file
515
TINKLib/Model/Settings/JsonSettingsDictionary.cs
Normal file
|
@ -0,0 +1,515 @@
|
|||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Serilog.Events;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TINK.Model.Connector;
|
||||
using TINK.Services.BluetoothLock;
|
||||
using TINK.Model.Services.CopriApi.ServerUris;
|
||||
using TINK.Model.Services.Geolocation;
|
||||
using TINK.Settings;
|
||||
using TINK.ViewModel.Map;
|
||||
using TINK.ViewModel.Settings;
|
||||
|
||||
namespace TINK.Model.Settings
|
||||
{
|
||||
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>
|
||||
public const string APPVERIONKEY = "AppVersion";
|
||||
|
||||
/// <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 center to ... entry. </summary>
|
||||
public const string CENTERMAPTOCURRENTLOCATION = "CenterMapToCurrentLocation";
|
||||
|
||||
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,
|
||||
Dictionary<string, string> settingsJSON) where T : struct
|
||||
{
|
||||
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,
|
||||
Dictionary<string, string> settingsJSON) where T : class
|
||||
{
|
||||
if (!settingsJSON.TryGetValue(keyName, out string boolText)
|
||||
|| string.IsNullOrEmpty(boolText))
|
||||
{
|
||||
// File holds no entry.
|
||||
return null;
|
||||
}
|
||||
|
||||
return JsonConvert.DeserializeObject<T>(boolText);
|
||||
}
|
||||
|
||||
/// <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="p_oSettingsJSON">Dictionary holding parameters from JSON.</param>
|
||||
public static Dictionary<string, string> SetCopriHostUri(this IDictionary<string, string> p_oTargetDictionary, string p_strNextActiveUriText)
|
||||
{
|
||||
if (p_oTargetDictionary == null)
|
||||
throw new Exception("Writing copri host uri to dictionary failed. Dictionary must not be null.");
|
||||
|
||||
return p_oTargetDictionary.Union(new Dictionary<string, string>
|
||||
{
|
||||
{ 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="p_oSettingsJSON">Dictionary to get information from.</param>
|
||||
/// <returns>Connect timeout value.</returns>
|
||||
public static TimeSpan? GetConnectTimeout(Dictionary<string, string> p_oSettingsJSON)
|
||||
{
|
||||
if (!p_oSettingsJSON.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)
|
||||
{
|
||||
// Get uri of corpi server.
|
||||
if (!settingsJSON.TryGetValue(typeof(CopriServerUriList).ToString(), out string uriText)
|
||||
|| string.IsNullOrEmpty(uriText))
|
||||
{
|
||||
// File holds no entry.
|
||||
return null;
|
||||
}
|
||||
|
||||
if (uriText.ToUpper().ToUpper().Contains("copri-bike.de".ToUpper()))
|
||||
return new Uri(CopriServerUriList.SHAREE_DEVEL);
|
||||
|
||||
if (uriText.ToUpper().ToUpper().Contains("copri.eu".ToUpper()))
|
||||
return new Uri(CopriServerUriList.SHAREE_LIVE);
|
||||
|
||||
return JsonConvert.DeserializeObject<Uri>(uriText);
|
||||
}
|
||||
|
||||
/// <summary> Sets the version of the app. </summary>
|
||||
/// <param name="p_oSettingsJSON">Dictionary holding parameters from JSON.</param>
|
||||
public static Dictionary<string, string> SetAppVersion(this IDictionary<string, string> p_oTargetDictionary, Version p_strAppVersion)
|
||||
{
|
||||
if (p_oTargetDictionary == null)
|
||||
throw new Exception("Writing copri host uri to dictionary failed. Dictionary must not be null.");
|
||||
|
||||
return p_oTargetDictionary.Union(new Dictionary<string, string>
|
||||
{
|
||||
{APPVERIONKEY , JsonConvert.SerializeObject(p_strAppVersion, new VersionConverter()) },
|
||||
}).ToDictionary(key => key.Key, value => value.Value);
|
||||
}
|
||||
|
||||
/// <summary> Gets the app versions.</summary>
|
||||
/// <param name="p_oSettingsJSON">Dictionary to get logging level from.</param>
|
||||
/// <returns>Logging level</returns>
|
||||
public static Version GetAppVersion(this IDictionary<string, string> p_oSettingsJSON)
|
||||
{
|
||||
// Get the version of the app which wrote the settings file.
|
||||
if (!p_oSettingsJSON.TryGetValue(APPVERIONKEY, out string l_oAppVersion)
|
||||
|| string.IsNullOrEmpty(l_oAppVersion))
|
||||
{
|
||||
// File holds no entry.
|
||||
return null;
|
||||
}
|
||||
|
||||
return l_oAppVersion.TrimStart().StartsWith("\"")
|
||||
? JsonConvert.DeserializeObject<Version>(l_oAppVersion, new VersionConverter())
|
||||
: Version.Parse(l_oAppVersion); // Format used up to version 3.0.0.115
|
||||
}
|
||||
|
||||
|
||||
/// <summary> Sets whether polling is on or off and the periode if polling is on. </summary>
|
||||
/// <param name="p_oSettingsJSON">Dictionary to write entries to.</param>
|
||||
public static Dictionary<string, string> SetPollingParameters(this IDictionary<string, string> p_oTargetDictionary, PollingParameters p_oPollingParameter)
|
||||
{
|
||||
if (p_oTargetDictionary == null)
|
||||
throw new Exception("Writing polling parameters to dictionary failed. Dictionary must not be null.");
|
||||
|
||||
return p_oTargetDictionary.Union(new Dictionary<string, string>
|
||||
{
|
||||
{ $"{typeof(PollingParameters).Name}_{typeof(TimeSpan).Name}", JsonConvert.SerializeObject(p_oPollingParameter.Periode) },
|
||||
{ $"{typeof(PollingParameters).Name}_{typeof(bool).Name}", JsonConvert.SerializeObject(p_oPollingParameter.IsActivated) },
|
||||
}).ToDictionary(key => key.Key, value => value.Value);
|
||||
}
|
||||
|
||||
/// <summary> Get whether polling is on or off and the periode if polling is on. </summary>
|
||||
/// <param name="p_oSettingsJSON">Dictionary holding parameters from JSON.</param>
|
||||
/// <returns>Polling parameters.</returns>
|
||||
public static PollingParameters GetPollingParameters(this IDictionary<string, string> p_oSettingsJSON)
|
||||
{
|
||||
// Check if dictionary contains entry for periode.
|
||||
if (p_oSettingsJSON.TryGetValue($"{typeof(PollingParameters).Name}_{typeof(TimeSpan).Name}", out string l_strPeriode)
|
||||
&& p_oSettingsJSON.TryGetValue($"{typeof(PollingParameters).Name}_{typeof(bool).Name}", out string l_strIsActive)
|
||||
&& !string.IsNullOrEmpty(l_strPeriode)
|
||||
&& !string.IsNullOrEmpty(l_strIsActive))
|
||||
{
|
||||
return new PollingParameters(
|
||||
JsonConvert.DeserializeObject<TimeSpan>(l_strPeriode),
|
||||
JsonConvert.DeserializeObject<bool>(l_strIsActive));
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary> Saves object to file. </summary>
|
||||
/// <param name="p_SettingsList">Settings to save.</param>
|
||||
public static void Serialize(string p_strSettingsFileFolder, IDictionary<string, string> p_SettingsList)
|
||||
{
|
||||
// Save settings to file.
|
||||
var l_oText = JsonConvert.SerializeObject(p_SettingsList, Formatting.Indented);
|
||||
var l_oFolder = p_strSettingsFileFolder;
|
||||
System.IO.File.WriteAllText($"{l_oFolder}{System.IO.Path.DirectorySeparatorChar}{SETTINGSFILETITLE}", l_oText);
|
||||
}
|
||||
|
||||
/// <summary> Gets TINK app settings form xml- file. </summary>
|
||||
/// <param name="p_strSettingsDirectory">Directory to read settings from.</param>
|
||||
/// <returns>Dictionary of settings.</returns>
|
||||
public static Dictionary<string, string> Deserialize(string p_strSettingsDirectory)
|
||||
{
|
||||
var l_oFileName = $"{p_strSettingsDirectory}{System.IO.Path.DirectorySeparatorChar}{SETTINGSFILETITLE}";
|
||||
|
||||
if (!System.IO.File.Exists(l_oFileName))
|
||||
{
|
||||
// File is empty. Nothing to read.
|
||||
return new Dictionary<string, string>(); ;
|
||||
}
|
||||
|
||||
var l_oJSONFile = System.IO.File.ReadAllText(l_oFileName);
|
||||
|
||||
if (string.IsNullOrEmpty(l_oJSONFile))
|
||||
{
|
||||
// File is empty. Nothing to read.
|
||||
return new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
// Load setting file.
|
||||
return JsonConvert.DeserializeObject<Dictionary<string, string>>(l_oJSONFile);
|
||||
}
|
||||
|
||||
/// <summary> Gets the logging level.</summary>
|
||||
/// <param name="p_oSettingsJSON">Dictionary to get logging level from.</param>
|
||||
/// <returns>Logging level.</returns>
|
||||
public static LogEventLevel? GetMinimumLoggingLevel(
|
||||
Dictionary<string, string> p_oSettingsJSON)
|
||||
{
|
||||
// Get logging level.
|
||||
if (!p_oSettingsJSON.TryGetValue(MINLOGGINGLEVELKEY, out string l_strLevel)
|
||||
|| string.IsNullOrEmpty(l_strLevel))
|
||||
{
|
||||
// File holds no entry.
|
||||
return null;
|
||||
}
|
||||
|
||||
return (LogEventLevel)int.Parse(JsonConvert.DeserializeObject<string>(l_strLevel));
|
||||
}
|
||||
|
||||
/// <summary> Sets the logging level.</summary>
|
||||
/// <param name="p_oSettingsJSON">Dictionary to get logging level from.</param>
|
||||
public static Dictionary<string, string> SetMinimumLoggingLevel(this IDictionary<string, string> p_oTargetDictionary, LogEventLevel p_oLevel)
|
||||
{
|
||||
// Set logging level.
|
||||
if (p_oTargetDictionary == null)
|
||||
throw new Exception("Writing logging level to dictionary failed. Dictionary must not be null.");
|
||||
|
||||
return p_oTargetDictionary.Union(new Dictionary<string, string>
|
||||
{
|
||||
{ MINLOGGINGLEVELKEY, JsonConvert.SerializeObject((int)p_oLevel) }
|
||||
}).ToDictionary(key => key.Key, value => value.Value);
|
||||
}
|
||||
|
||||
/// <summary> Gets the version of app when whats new was shown.</summary>
|
||||
/// <param name="p_oSettingsJSON">Dictionary to get logging level from.</param>
|
||||
/// <returns>Version of the app.</returns>
|
||||
public static Version GetWhatsNew(Dictionary<string, string> p_oSettingsJSON)
|
||||
{
|
||||
// Get logging level.
|
||||
if (!p_oSettingsJSON.TryGetValue(SHOWWHATSNEWKEY, out string l_strWhatsNewVersion)
|
||||
|| string.IsNullOrEmpty(l_strWhatsNewVersion))
|
||||
{
|
||||
// File holds no entry.
|
||||
return null;
|
||||
}
|
||||
|
||||
return JsonConvert.DeserializeObject<Version>(l_strWhatsNewVersion, new VersionConverter());
|
||||
}
|
||||
|
||||
/// <summary> Sets the version of app when whats new was shown.</summary>
|
||||
/// <param name="p_oSettingsJSON">Dictionary to get information from.</param>
|
||||
public static Dictionary<string, string> SetWhatsNew(this IDictionary<string, string> p_oTargetDictionary, Version p_oAppVersion)
|
||||
{
|
||||
// Set logging level.
|
||||
if (p_oTargetDictionary == null)
|
||||
throw new Exception("Writing WhatsNew info failed. Dictionary must not be null.");
|
||||
|
||||
return p_oTargetDictionary.Union(new Dictionary<string, string>
|
||||
{
|
||||
{ SHOWWHATSNEWKEY, JsonConvert.SerializeObject(p_oAppVersion, new VersionConverter()) }
|
||||
}).ToDictionary(key => key.Key, value => value.Value);
|
||||
}
|
||||
|
||||
/// <summary> Gets the expires after value.</summary>
|
||||
/// <param name="p_oSettingsJSON">Dictionary to get expries after value from.</param>
|
||||
/// <returns>Expires after value.</returns>
|
||||
public static TimeSpan? GetExpiresAfter(Dictionary<string, string> p_oSettingsJSON)
|
||||
{
|
||||
if (!p_oSettingsJSON.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="p_oSettingsJSON">Dictionary to write information to.</param>
|
||||
public static Dictionary<string, string> SetExpiresAfter(this IDictionary<string, string> p_oTargetDictionary, TimeSpan expiresAfter)
|
||||
{
|
||||
if (p_oTargetDictionary == null)
|
||||
throw new Exception("Writing ExpiresAfter info failed. Dictionary must not be null.");
|
||||
|
||||
return p_oTargetDictionary.Union(new Dictionary<string, string>
|
||||
{
|
||||
{ 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)
|
||||
{
|
||||
// Get uri of corpi server.
|
||||
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>
|
||||
{
|
||||
{ typeof(IGeolocation).Name, activeGeolocationService },
|
||||
}).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)
|
||||
{
|
||||
// Get uri of corpi server.
|
||||
if (!settingsJSON.TryGetValue(typeof(IGeolocation).Name, out string activeGeolocationService)
|
||||
|| 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="p_oSettingsJSON">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);
|
||||
|
||||
/// <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>
|
||||
/// <param name="p_oSettingsJSON">Dictionary to get value from.</param>
|
||||
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="p_oSettingsJSON">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 l_oKeyName = "FilterCollection_MapPageFilter";
|
||||
if (settings == null || !settings.ContainsKey(l_oKeyName))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new GroupFilterMapPage(JsonConvert.DeserializeObject<IDictionary<string, FilterState>>(settings[l_oKeyName]));
|
||||
}
|
||||
|
||||
public static IDictionary<string, string> SetGroupFilterMapPage(
|
||||
this IDictionary<string, string> settings,
|
||||
IDictionary<string, FilterState> p_oFilterCollection)
|
||||
{
|
||||
if (settings == null
|
||||
|| p_oFilterCollection == null
|
||||
|| p_oFilterCollection.Count < 1)
|
||||
{
|
||||
return settings;
|
||||
}
|
||||
|
||||
settings["FilterCollection_MapPageFilter"] = JsonConvert.SerializeObject(p_oFilterCollection);
|
||||
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 l_oKeyName = "FilterCollection";
|
||||
if (settings == null || !settings.ContainsKey(l_oKeyName))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var legacyFilterCollection = new GroupFilterSettings(JsonConvert.DeserializeObject<IDictionary<string, FilterState>>(settings[l_oKeyName]));
|
||||
|
||||
// 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())
|
||||
return legacyFilterCollection;
|
||||
var list = updatedFilterCollection.ToList();
|
||||
|
||||
updatedFilterCollection.Add(
|
||||
FilterHelper.FILTERTINKGENERAL,
|
||||
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,
|
||||
IDictionary<string, FilterState> p_oFilterCollection)
|
||||
{
|
||||
if (settings == null
|
||||
|| p_oFilterCollection == null
|
||||
|| p_oFilterCollection.Count < 1)
|
||||
{
|
||||
return settings;
|
||||
}
|
||||
|
||||
settings["FilterCollection"] = JsonConvert.SerializeObject(p_oFilterCollection);
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
}
|
100
TINKLib/Model/Settings/PollingParameters.cs
Normal file
100
TINKLib/Model/Settings/PollingParameters.cs
Normal file
|
@ -0,0 +1,100 @@
|
|||
using System;
|
||||
|
||||
namespace TINK.Settings
|
||||
{
|
||||
/// <summary> Holds polling parameters.</summary>
|
||||
public sealed class PollingParameters : IEquatable<PollingParameters>
|
||||
{
|
||||
/// <summary> Holds default polling parameters. </summary>
|
||||
public static PollingParameters Default { get; } = new PollingParameters(
|
||||
new TimeSpan(0, 0, 0, 10 /*secs*/, 0),// Default polling interval.
|
||||
true);
|
||||
|
||||
/// <summary> Holds polling parameters which represent polling off (empty polling object). </summary>
|
||||
public static PollingParameters NoPolling { get; } = new PollingParameters(
|
||||
TimeSpan.MaxValue,// Very long intervall which should never be used because polling IsActivated property is set to false.
|
||||
false);
|
||||
|
||||
/// <summary> Constructs a polling parameter object. </summary>
|
||||
/// <param name="p_oPeriode">Polling periode.</param>
|
||||
/// <param name="p_bIsActivated">True if polling is activated.</param>
|
||||
public PollingParameters(TimeSpan p_oPeriode, bool p_bIsActivated)
|
||||
{
|
||||
Periode = p_oPeriode; // Can not be null because is a struct.
|
||||
IsActivated = p_bIsActivated;
|
||||
}
|
||||
|
||||
/// <summary>Holds the polling periode.</summary>
|
||||
public TimeSpan Periode { get; }
|
||||
|
||||
/// <summary> Holds value whether polling is activated or not.</summary>
|
||||
public bool IsActivated { get; }
|
||||
|
||||
/// <summary> Checks equallity.</summary>
|
||||
/// <param name="other">Object to compare with.</param>
|
||||
/// <returns>True if objects are equal.</returns>
|
||||
public bool Equals(PollingParameters other)
|
||||
{
|
||||
return this == other;
|
||||
}
|
||||
|
||||
/// <summary> Checks equallity.</summary>
|
||||
/// <param name="obj">Object to compare with.</param>
|
||||
/// <returns>True if objects are equal.</returns>
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
var l_oParameters = obj as PollingParameters;
|
||||
if (l_oParameters == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return this == l_oParameters;
|
||||
}
|
||||
|
||||
/// <summary> Gets the has code of object.</summary>
|
||||
/// <returns></returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Periode.GetHashCode() ^ IsActivated.GetHashCode();
|
||||
}
|
||||
|
||||
/// <summary> Gets the string representation of the object.</summary>
|
||||
/// <returns></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Polling is on={IsActivated}, polling interval={Periode.TotalSeconds}[sec].";
|
||||
}
|
||||
|
||||
/// <summary>Defines equality of thwo polling parameter objects.</summary>
|
||||
/// <param name="p_oSource">First object to compare.</param>
|
||||
/// <param name="p_oTarget">Second object to compare.</param>
|
||||
/// <returns>True if objects are equal</returns>
|
||||
public static bool operator ==(PollingParameters p_oSource, PollingParameters p_oTarget)
|
||||
{
|
||||
if (p_oSource is null && p_oTarget is null)
|
||||
{
|
||||
// Both object are null
|
||||
return true;
|
||||
}
|
||||
|
||||
if (p_oSource is null ^ p_oTarget is null)
|
||||
{
|
||||
// Only one object is null.
|
||||
return false;
|
||||
}
|
||||
|
||||
return p_oSource.Periode == p_oTarget.Periode
|
||||
&& p_oSource.IsActivated == p_oTarget.IsActivated;
|
||||
}
|
||||
|
||||
/// <summary>Defines equality of thwo polling parameter objects.</summary>
|
||||
/// <param name="p_oSource">First object to compare.</param>
|
||||
/// <param name="p_oTarget">Second object to compare.</param>
|
||||
/// <returns>True if objects are equal</returns>
|
||||
public static bool operator !=(PollingParameters p_oSource, PollingParameters p_oTarget)
|
||||
{
|
||||
return (p_oSource == p_oTarget) == false;
|
||||
}
|
||||
}
|
||||
}
|
104
TINKLib/Model/Settings/Settings.cs
Normal file
104
TINKLib/Model/Settings/Settings.cs
Normal file
|
@ -0,0 +1,104 @@
|
|||
using Serilog.Events;
|
||||
using System;
|
||||
using TINK.Model.Services.Geolocation;
|
||||
using TINK.Services.BluetoothLock;
|
||||
using TINK.Services.CopriApi.ServerUris;
|
||||
using TINK.Settings;
|
||||
using TINK.ViewModel.Map;
|
||||
using TINK.ViewModel.Settings;
|
||||
|
||||
|
||||
namespace TINK.Model.Settings
|
||||
{
|
||||
/// <summary> Holds settings which are persisted.</summary>
|
||||
public class Settings
|
||||
{
|
||||
public const LogEventLevel DEFAULTLOGGINLEVEL = LogEventLevel.Error;
|
||||
|
||||
// Default value of the expires after entry. Controls the expiration time of the cache values.
|
||||
private TimeSpan DEFAULTEXPIRESAFTER = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary> Constructs settings object. </summary>
|
||||
/// <param name="groupFilterMapPage">filter which is applied on the map view. Either TINK or Konrad stations are displayed.</param>
|
||||
/// <param name="groupFilterSettings"></param>
|
||||
/// <param name="activeUri"></param>
|
||||
/// <param name="pollingParameters"></param>
|
||||
/// <param name="minimumLogEventLevel">Minimum logging level to be applied.</param>
|
||||
/// <param name="expiresAfter">Holds the expires after value.</param>
|
||||
/// <param name="activeLockService">Gets the name of the lock service to use.</param>
|
||||
/// <param name="connectTimeout">Timeout to apply when connecting to bluetooth lock</param>
|
||||
/// <param name="activeTheme">Full class name of active app theme.</param>
|
||||
public Settings(
|
||||
IGroupFilterMapPage groupFilterMapPage = null,
|
||||
IGroupFilterSettings groupFilterSettings = null,
|
||||
Uri activeUri = null,
|
||||
PollingParameters pollingParameters = null,
|
||||
LogEventLevel? minimumLogEventLevel = null,
|
||||
TimeSpan? expiresAfter = null,
|
||||
string activeLockService = null,
|
||||
TimeSpan? connectTimeout = null,
|
||||
string activeGeolocationService = null,
|
||||
bool? centerMapToCurrentLocation = null,
|
||||
bool? logToExternalFolder = null,
|
||||
bool? isSiteCachingOn = null,
|
||||
string activeTheme = null)
|
||||
{
|
||||
GroupFilterMapPage = groupFilterMapPage ?? GroupFilterHelper.GetMapPageFilterDefaults;
|
||||
GroupFilterSettings = groupFilterSettings ?? GroupFilterHelper.GetSettingsFilterDefaults;
|
||||
ActiveUri = GetActiveUri(activeUri);
|
||||
PollingParameters = pollingParameters ?? PollingParameters.Default;
|
||||
MinimumLogEventLevel = minimumLogEventLevel ?? DEFAULTLOGGINLEVEL;
|
||||
ExpiresAfter = expiresAfter ?? DEFAULTEXPIRESAFTER;
|
||||
ActiveLockService = activeLockService ?? LocksServicesContainerMutable.DefaultLocksservice;
|
||||
ConnectTimeout = connectTimeout ?? new TimeSpan(0, 0, TimeOutProvider.DEFAULT_BLUETOOTHCONNECT_TIMEOUTSECONDS); // Try one sec. to connect.
|
||||
ActiveGeolocationService = activeGeolocationService ?? typeof(LastKnownGeolocationService).Name;
|
||||
CenterMapToCurrentLocation = centerMapToCurrentLocation ?? GetCenterMapToCurrentLocation(activeUri);
|
||||
LogToExternalFolder = logToExternalFolder ?? false;
|
||||
IsSiteCachingOn = isSiteCachingOn ?? true;
|
||||
ActiveTheme = activeTheme ?? typeof(Themes.ShareeBike).FullName;
|
||||
}
|
||||
|
||||
/// <summary> Holds the filter which is applied on the map view. Either TINK or Konrad stations are displayed. </summary>
|
||||
public IGroupFilterMapPage GroupFilterMapPage { get; }
|
||||
|
||||
/// <summary> Holds the filters loaded from settings. </summary>
|
||||
public IGroupFilterSettings GroupFilterSettings { get; }
|
||||
|
||||
/// <summary> Holds the uri to connect to. </summary>
|
||||
public Uri ActiveUri { get; }
|
||||
|
||||
/// <summary> Holds the polling parameters. </summary>
|
||||
public PollingParameters PollingParameters { get; }
|
||||
|
||||
/// <summary> Gets the minimum logging level. </summary>
|
||||
public LogEventLevel MinimumLogEventLevel { get; }
|
||||
|
||||
/// <summary> Gets the expires after value.</summary>
|
||||
public TimeSpan ExpiresAfter { get; }
|
||||
|
||||
/// <summary> Gets the lock service to use.</summary>
|
||||
public string ActiveLockService { get; private set; }
|
||||
|
||||
/// <summary> Timeout to apply when connecting to bluetooth lock.</summary>
|
||||
public TimeSpan ConnectTimeout { get; }
|
||||
|
||||
/// <summary> Gets the geolocation service to use.</summary>
|
||||
public string ActiveGeolocationService { get; }
|
||||
|
||||
public bool CenterMapToCurrentLocation { get; }
|
||||
|
||||
public bool LogToExternalFolder { get; }
|
||||
|
||||
public bool IsSiteCachingOn { get; }
|
||||
|
||||
public string ActiveTheme { get; }
|
||||
|
||||
public static Uri GetActiveUri(Uri activeUri) => activeUri ?? Services.CopriApi.ServerUris.CopriServerUriList.DefaultActiveUri;
|
||||
|
||||
public static bool GetCenterMapToCurrentLocation(Uri activeUri)
|
||||
{
|
||||
// TINK does not require acess to current location. Deactivate center map to current location for this reason.
|
||||
return !GetActiveUri(activeUri).Host.GetIsCopri();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue