2021-05-13 20:03:07 +02:00
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Linq ;
namespace TINK.Services
{
2022-08-30 15:42:25 +02:00
public class ServicesContainerMutable : IEnumerable < string > , INotifyPropertyChanged , IServicesContainer < string >
2021-05-13 20:03:07 +02:00
{
2022-08-30 15:42:25 +02:00
private readonly Dictionary < string , string > serviceDict ;
2021-05-13 20:03:07 +02:00
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 (
2022-08-30 15:42:25 +02:00
IEnumerable < string > services ,
string activeName )
2021-05-13 20:03:07 +02:00
{
2022-08-30 15:42:25 +02:00
serviceDict = services . Distinct ( ) . ToDictionary ( x = > x ) ;
2021-05-13 20:03:07 +02:00
if ( ! serviceDict . ContainsKey ( activeName ) )
{
2022-08-30 15:42:25 +02:00
throw new ArgumentException ( $"Can not instantiate {typeof(string).Name}- object. Active lock service {activeName} must be contained in [{String.Join(" , ", serviceDict)}]." ) ;
2021-05-13 20:03:07 +02:00
}
Active = serviceDict [ activeName ] ;
}
/// <summary> Active locks service.</summary>
2022-08-30 15:42:25 +02:00
public string Active { get ; private set ; }
2021-05-13 20:03:07 +02:00
/// <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 ) ) ) ;
}
2022-08-30 15:42:25 +02:00
public IEnumerator < string > GetEnumerator ( )
2021-05-13 20:03:07 +02:00
= > serviceDict . Values . GetEnumerator ( ) ;
IEnumerator IEnumerable . GetEnumerator ( )
= > serviceDict . Values . GetEnumerator ( ) ;
}
}