Repository navigation
Expand file tree
/
Copy pathPingMonitorGroupSummaryConverter.cs
More file actions
58 lines (52 loc) · 2.2 KB
/
Copy pathPingMonitorGroupSummaryConverter.cs
File metadata and controls
58 lines (52 loc) · 2.2 KB
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
using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using NETworkManager.Models.Network;
namespace NETworkManager.Converters;
/// <summary>
/// Formats the count of hosts up (reachable), down (unreachable) or paused (not running) within
/// a Ping Monitor group, selected via <c>ConverterParameter</c> ("Up", "Down", "Paused" - formats
/// "{count} {label}" using the label bound as the third value - or "PausedVisibility", which
/// instead returns a <see cref="Visibility"/> so the paused count can be hidden while zero).
/// </summary>
/// <remarks>
/// Bound as a <see cref="MultiBinding"/> with the <see cref="CollectionViewGroup"/> as the first
/// value and a per-group change-notification trigger as the second. The second value isn't used
/// directly, it only forces re-evaluation whenever a host's <see cref="IPingMonitorHostStatus"/>
/// changes, since <see cref="CollectionViewGroup"/> itself only raises change notifications for
/// item add/remove, not for property changes on its items.
/// </remarks>
public sealed class PingMonitorGroupSummaryConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length == 0 || values[0] is not CollectionViewGroup group)
return parameter as string == "PausedVisibility" ? Visibility.Collapsed : string.Empty;
var up = 0;
var down = 0;
var paused = 0;
foreach (var host in group.Items.OfType<IPingMonitorHostStatus>())
{
if (!host.IsRunning)
paused++;
else if (host.IsReachable)
up++;
else
down++;
}
return parameter as string switch
{
"Up" => $"{up} {values[2]}",
"Down" => $"{down} {values[2]}",
"Paused" => $"{paused} {values[2]}",
"PausedVisibility" => paused > 0 ? Visibility.Visible : Visibility.Collapsed,
_ => string.Empty
};
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}