-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetworkPage.xaml.cs
More file actions
76 lines (63 loc) · 2.08 KB
/
NetworkPage.xaml.cs
File metadata and controls
76 lines (63 loc) · 2.08 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using EntglDb.Core.Network;
using EntglDb.Network;
using Microsoft.Maui.Controls;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
namespace EntglDb.Test.Maui;
public partial class NetworkPage : ContentPage
{
private readonly IEntglDbNode _node;
private readonly IPeerNodeConfigurationProvider _configProvider;
public ObservableCollection<PeerInfoViewModel> Peers { get; } = new();
public ICommand RefreshPeersCommand { get; }
public NetworkPage(IEntglDbNode node, IPeerNodeConfigurationProvider configProvider)
{
InitializeComponent();
_node = node;
_configProvider = configProvider;
RefreshPeersCommand = new Command(RefreshPeers);
BindingContext = this;
}
protected override async void OnAppearing()
{
base.OnAppearing();
await LoadNodeInfo();
RefreshPeers();
// Auto-refresh every 5 seconds while on this page
Dispatcher.StartTimer(TimeSpan.FromSeconds(5), () =>
{
if (!this.IsLoaded) return false;
RefreshPeers();
return true;
});
}
private async Task LoadNodeInfo()
{
var config = await _configProvider.GetConfiguration();
NodeIdLabel.Text = $"ID: {config.NodeId}";
AddressLabel.Text = $"Address: {_node.Address}";
}
private void RefreshPeers()
{
var peers = _node.Discovery.GetActivePeers().ToList();
// Simple sync for updating UI
Peers.Clear();
foreach (var p in peers)
{
Peers.Add(new PeerInfoViewModel
{
NodeId = p.NodeId,
Address = p.Address.ToString(),
LastSeen = p.LastSeen.LocalDateTime
});
}
PeersRefreshView.IsRefreshing = false;
}
}
public class PeerInfoViewModel
{
public string NodeId { get; set; }
public string Address { get; set; }
public DateTime LastSeen { get; set; }
}