Winify – Diff between revs 12 and 21

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 12 Rev 21
Line 1... Line 1...
1 using System; 1 using System;
2 using System.Collections.Concurrent; 2 using System.Collections.Concurrent;
3 using System.Collections.Specialized; 3 using System.Collections.Specialized;
-   4 using System.ComponentModel;
4 using System.Linq; 5 using System.Linq;
5 using Servers; 6 using Servers;
Line 6... Line 7...
6   7  
7 namespace Winify.Gotify 8 namespace Winify.Gotify
Line 32... Line 33...
32 public GotifyConnectionManager(global::Servers.Servers servers) : this() 33 public GotifyConnectionManager(global::Servers.Servers servers) : this()
33 { 34 {
34 _servers = servers; 35 _servers = servers;
Line 35... Line 36...
35   36  
-   37 _servers.Server.CollectionChanged += Server_CollectionChanged;
36 _servers.Server.CollectionChanged += Server_CollectionChanged; 38 _servers.Server.ListChanged += Server_ListChanged;
Line 37... Line 39...
37 } 39 }
38   40  
39 public void Dispose() 41 public void Dispose()
-   42 {
40 { 43 _servers.Server.CollectionChanged -= Server_CollectionChanged;
Line 41... Line 44...
41 _servers.Server.CollectionChanged -= Server_CollectionChanged; 44 _servers.Server.ListChanged -= Server_ListChanged;
Line 42... Line 45...
42 } 45 }
Line -... Line 46...
-   46  
-   47 #endregion
-   48  
-   49 #region Event Handlers
-   50  
-   51 private void Server_ListChanged(object sender, ListChangedEventArgs e)
-   52 {
-   53 if (e.ListChangedType != ListChangedType.ItemChanged)
-   54 {
-   55 return;
-   56 }
-   57  
-   58 var server = _servers.Server[e.NewIndex];
-   59  
-   60 if (RemoveConnection(server))
43   61 {
44 #endregion 62 CreateConnection(server);
45   63 }
46 #region Event Handlers 64 }
47   65  
48 private void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 66 private void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
49 { 67 {
50 if (e.NewItems != null) -  
51 { -  
52 foreach (var server in e.NewItems.OfType<Server>()) -  
53 { -  
54 var connection = new GotifyConnection(); 68 if (e.NewItems != null)
55 connection.GotifyNotification += Connection_GotifyNotification; 69 {
Line 56... Line 70...
56   70 foreach (var server in e.NewItems.OfType<Server>())
57 connection.Start(server.Username, server.Password, server.Url); 71 {
58 _gotifyConnections.TryAdd(server.Name, connection); 72 CreateConnection(server);
59 } 73 }
60 } -  
61   -  
62 if (e.OldItems != null) -  
63 { -  
64 foreach (var server in e.OldItems.OfType<Server>()) -  
65 { -  
66 if (!_gotifyConnections.TryRemove(server.Name, out var gotifyConnection)) -  
67 { -  
68 continue; 74 }
69 } 75  
70   76 if (e.OldItems != null)
71 gotifyConnection.GotifyNotification -= Connection_GotifyNotification; 77 {
Line 72... Line 78...
72   78 foreach (var server in e.OldItems.OfType<Server>())
73 gotifyConnection.Stop(); 79 {
74 gotifyConnection.Dispose(); 80 RemoveConnection(server);
75 } 81 }
Line 76... Line 82...
76 } 82 }
-   83 }
-   84  
-   85 private void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e)
-   86 {
-   87 GotifyNotification?.Invoke(sender, e);
-   88 }
-   89  
-   90 #endregion
-   91  
-   92 #region Private Methods
-   93  
-   94 private bool RemoveConnection(Server server)
-   95 {
-   96 if (!_gotifyConnections.TryRemove(server.Url, out var gotifyConnection))
-   97 {
-   98 return false;
-   99 }
-   100  
-   101 gotifyConnection.GotifyNotification -= Connection_GotifyNotification;
-   102  
-   103 gotifyConnection.Stop();
-   104 gotifyConnection.Dispose();
-   105 gotifyConnection = null;
-   106  
-   107 return true;
-   108 }
-   109  
-   110 private void CreateConnection(Server server)
-   111 {
77 } 112 var connection = new GotifyConnection();
78   113 connection.GotifyNotification += Connection_GotifyNotification;
79 private void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e) 114