mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-05 10:36:30 +01:00
55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
|
|
namespace ShareeBike.Services
|
|
{
|
|
public class ServicesContainerMutable : IEnumerable<string>, INotifyPropertyChanged, IServicesContainer<string>
|
|
{
|
|
private readonly Dictionary<string, string> serviceDict;
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
/// <summary> Constructs object on startup of app.</summary>
|
|
/// <param name="services">Fixed list of service- objects .</param>
|
|
/// <param name="activeName">LocksService name which is activated on startup of app.</param>
|
|
public ServicesContainerMutable(
|
|
IEnumerable<string> services,
|
|
string activeName)
|
|
{
|
|
serviceDict = services.Distinct().ToDictionary(x => x);
|
|
|
|
if (!serviceDict.ContainsKey(activeName))
|
|
{
|
|
throw new ArgumentException($"Can not instantiate {typeof(string).Name}- object. Active lock service {activeName} must be contained in [{String.Join(",", serviceDict)}].");
|
|
}
|
|
|
|
Active = serviceDict[activeName];
|
|
}
|
|
|
|
/// <summary> Active locks service.</summary>
|
|
public string Active { get; private set; }
|
|
|
|
/// <summary> Sets a lock service as active locks service by name. </summary>
|
|
/// <param name="active">Name of the new locks service.</param>
|
|
public void SetActive(string active)
|
|
{
|
|
if (!serviceDict.ContainsKey(active))
|
|
{
|
|
throw new ArgumentException($"Can not set active lock service {active}. Service must be contained in [{String.Join(",", serviceDict)}].");
|
|
}
|
|
|
|
Active = serviceDict[active];
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Active)));
|
|
}
|
|
|
|
public IEnumerator<string> GetEnumerator()
|
|
=> serviceDict.Values.GetEnumerator();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
=> serviceDict.Values.GetEnumerator();
|
|
}
|
|
}
|