Winify – Diff between revs 12 and 21

Subversion Repositories:
Rev:
Show entire fileRegard 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 }
-   74 }
-   75  
-   76 if (e.OldItems != null)
-   77 {
-   78 foreach (var server in e.OldItems.OfType<Server>())
60 } 79 {
61   80 RemoveConnection(server);
-   81 }
-   82 }
-   83 }
-   84  
-   85 private void Connection_GotifyNotification(object sender, GotifyNotificationEventArgs e)
-   86 {
-   87 GotifyNotification?.Invoke(sender, e);
-   88 }
-   89  
-   90 #endregion
-   91  
62 if (e.OldItems != null) 92 #region Private Methods
63 { 93  
Line 64... Line 94...
64 foreach (var server in e.OldItems.OfType<Server>()) 94 private bool RemoveConnection(Server server)
Line 65... Line 95...
65 { 95 {
66 if (!_gotifyConnections.TryRemove(server.Name, out var gotifyConnection)) 96 if (!_gotifyConnections.TryRemove(server.Url, out var gotifyConnection))
67 { 97 {
-   98 return false;
68 continue; 99 }
69 } 100  
Line 70... Line 101...
70   101 gotifyConnection.GotifyNotification -= Connection_GotifyNotification;
71 gotifyConnection.GotifyNotification -= Connection_GotifyNotification; 102  
-   103 gotifyConnection.Stop();
-   104 gotifyConnection.Dispose();
-   105 gotifyConnection = null;
-   106  
72   107 return true;
73 gotifyConnection.Stop(); 108 }
Line 74... Line 109...
74 gotifyConnection.Dispose(); 109  
75 } 110 private void CreateConnection(Server server)
76 } 111 {
77 } 112 var connection = new GotifyConnection();