Winify – Blame information for rev 12

Subversion Repositories:
Rev:
Rev Author Line No. Line
7 office 1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Specialized;
4 using System.Linq;
5 using Servers;
4 office 6  
7 namespace Winify.Gotify
8 {
7 office 9 public class GotifyConnectionManager : IDisposable
4 office 10 {
11 office 11 #region Public Events & Delegates
12  
13 public event EventHandler<GotifyNotificationEventArgs> GotifyNotification;
14  
15 #endregion
16  
4 office 17 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
18  
7 office 19 private readonly ConcurrentDictionary<string, GotifyConnection> _gotifyConnections;
4 office 20  
8 office 21 private readonly global::Servers.Servers _servers;
7 office 22  
4 office 23 #endregion
24  
25 #region Constructors, Destructors and Finalizers
26  
7 office 27 private GotifyConnectionManager()
4 office 28 {
7 office 29 _gotifyConnections = new ConcurrentDictionary<string, GotifyConnection>();
4 office 30 }
31  
8 office 32 public GotifyConnectionManager(global::Servers.Servers servers) : this()
7 office 33 {
34 _servers = servers;
4 office 35  
7 office 36 _servers.Server.CollectionChanged += Server_CollectionChanged;
37 }
4 office 38  
7 office 39 public void Dispose()
4 office 40 {
7 office 41 _servers.Server.CollectionChanged -= Server_CollectionChanged;
4 office 42 }
43  
44 #endregion
45  
7 office 46 #region Event Handlers
4 office 47  
7 office 48 private void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
4 office 49 {
7 office 50 if (e.NewItems != null)
4 office 51 {
7 office 52 foreach (var server in e.NewItems.OfType<Server>())
53 {
54 var connection = new GotifyConnection();
55 connection.GotifyNotification += Connection_GotifyNotification;
56  
12 office 57 connection.Start(server.Username, server.Password, server.Url);
7 office 58 _gotifyConnections.TryAdd(server.Name, connection);
59 }
4 office 60 }
61  
7 office 62 if (e.OldItems != null)
4 office 63 {
7 office 64 foreach (var server in e.OldItems.OfType<Server>())
65 {
66 if (!_gotifyConnections.TryRemove(server.Name, out var gotifyConnection))
67 {
68 continue;
69 }
4 office 70  
7 office 71 gotifyConnection.GotifyNotification -= Connection_GotifyNotification;
72  
73 gotifyConnection.Stop();
74 gotifyConnection.Dispose();
75 }
4 office 76 }
77 }
78  
11 office 79 private void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e)
7 office 80 {
11 office 81 GotifyNotification?.Invoke(sender, e);
7 office 82 }
83  
4 office 84 #endregion
85 }
86 }