sharee.bike-App/TINKLib/ViewModel/Settings/ServicesViewModel.cs

81 lines
2.7 KiB
C#
Raw Normal View History

2022-08-30 15:42:25 +02:00
using System;
2021-05-13 20:03:07 +02:00
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
2022-08-30 15:42:25 +02:00
using Serilog;
2021-05-13 20:03:07 +02:00
namespace TINK.ViewModel.Settings
{
2022-09-06 16:08:19 +02:00
/// <summary> ViewModel for an active service plus a list of services which are not active.</summary>
/// <remarks>
/// Example for services are lock services, geolocation services, ...,
/// </remarks>
public class ServicesViewModel : INotifyPropertyChanged
{
/// <summary> Active service. </summary>
private string active;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Holds the dictionary which maps services to service display texts.</summary>
private IDictionary<string, string> ServiceToText { get; }
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Holds the dictionary which maps service display texts to services.</summary>
private IDictionary<string, string> TextToService { get; }
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>Constructs view model ensuring consistency. </summary>
/// <param name="services">List of available services.</param>
/// <param name="serviceToText"> Dictionary holding display text for services as values.</param>
/// <param name="active">Active service.</param>
public ServicesViewModel(
IEnumerable<string> services,
IDictionary<string, string> serviceToText,
string active)
{
if (!services.Contains(active))
throw new ArgumentException($"Can not instantiate {typeof(ServicesViewModel).Name}- object. Active lock service {active} must be contained in [{String.Join(",", services)}].");
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
ServiceToText = services.Distinct().ToDictionary(
x => x,
x => serviceToText.ContainsKey(x) ? serviceToText[x] : x);
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
TextToService = ServiceToText.ToDictionary(x => x.Value, x => x.Key);
Active = active;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Fired whenever active service changes.</summary>
public event PropertyChangedEventHandler PropertyChanged;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Holds active service.</summary>
public string Active
{
get => active;
set
{
if (active == value)
return;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
active = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Active)));
}
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> List of display texts of services.</summary>
public IList<string> ServicesTextList => ServiceToText.Select(x => x.Value).OrderBy(x => x).ToList();
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Active locks service.</summary>
public string ActiveText
{
get => ServiceToText[Active];
set
{
if (!TextToService.ContainsKey(value))
{
Log.ForContext<ServicesViewModel>().Error($"Can not set service {value} to services view model. List of services {{{string.Join(";", TextToService)}}} does not hold machting element.");
throw new ArgumentException($"Can not set service {value} to services view model. List of services {{{string.Join(";", TextToService)}}} does not hold machting element.");
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
Active = TextToService[value];
}
}
}
2021-05-13 20:03:07 +02:00
}