-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRDPConnections.cs
61 lines (48 loc) · 1.72 KB
/
RDPConnections.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Linq;
namespace Flow.Launcher.Plugin.RDP;
internal class RDPConnections
{
private readonly List<string> _connections;
private RDPConnections(IEnumerable<string> connections)
{
_connections = connections.ToList();
}
public static RDPConnections Create(IEnumerable<string> connections) => new(connections);
public IReadOnlyCollection<string> Connections => _connections;
public void Reload(IReadOnlyCollection<string> rdpConnections)
{
var newConnections = rdpConnections.Where(x => !_connections.Contains(x)).ToList();
var oldConnections = _connections.Where(x => !rdpConnections.Contains(x)).ToList();
foreach (var oldConnection in oldConnections)
{
_connections.Remove(oldConnection);
}
_connections.AddRange(newConnections);
}
public void ConnectionWasSelected(string connection)
{
var index = _connections.IndexOf(connection);
if (index == -1)
{
return;
}
_connections.RemoveAt(index);
_connections.Insert(0, connection);
}
public IReadOnlyCollection<(string Connection, int Score)> FindConnections(string querySearch)
{
if (string.IsNullOrWhiteSpace(querySearch))
{
return _connections
.Select(MapToScore)
.ToList();
}
return _connections
.Where(x => x.Contains(querySearch, StringComparison.InvariantCultureIgnoreCase))
.Select(MapToScore)
.ToList();
}
private (string connection, int score) MapToScore(string x, int i) => (connection: x, score: _connections.Count + 1 - i);
}