mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 15:16:34 +01:00
81 lines
3.2 KiB
C#
81 lines
3.2 KiB
C#
|
using Serilog;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
namespace TINK.ViewModel.Settings
|
|||
|
{
|
|||
|
/// <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;
|
|||
|
|
|||
|
/// <summary> Holds the dictionary which maps services to service display texts.</summary>
|
|||
|
private IDictionary<string, string> ServiceToText { get; }
|
|||
|
|
|||
|
/// <summary> Holds the dictionary which maps service display texts to services.</summary>
|
|||
|
private IDictionary<string, string> TextToService { get; }
|
|||
|
|
|||
|
/// <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)}].");
|
|||
|
|
|||
|
ServiceToText = services.Distinct().ToDictionary(
|
|||
|
x => x,
|
|||
|
x => serviceToText.ContainsKey(x) ? serviceToText[x] : x);
|
|||
|
|
|||
|
TextToService = ServiceToText.ToDictionary(x => x.Value, x => x.Key);
|
|||
|
Active = active;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> Fired whenever active service changes.</summary>
|
|||
|
public event PropertyChangedEventHandler PropertyChanged;
|
|||
|
|
|||
|
/// <summary> Holds active service.</summary>
|
|||
|
public string Active
|
|||
|
{
|
|||
|
get => active;
|
|||
|
set
|
|||
|
{
|
|||
|
if (active == value)
|
|||
|
return;
|
|||
|
|
|||
|
active = value;
|
|||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Active)));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> List of display texts of services.</summary>
|
|||
|
public IList<string> ServicesTextList => ServiceToText.Select(x => x.Value).OrderBy(x => x).ToList();
|
|||
|
|
|||
|
/// <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.");
|
|||
|
}
|
|||
|
|
|||
|
Active = TextToService[value];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|