summaryrefslogtreecommitdiff
path: root/windows/WinUI/NetworksPage.xaml.cs
blob: 39a2fefc937c69a374782ce7a801ab65b16bcec5 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WinUI
{
    /// <summary>
    /// Interaction logic for NetworksPage.xaml
    /// </summary>
    public partial class NetworksPage : UserControl
    {
        public NetworksPage()
        {
            InitializeComponent();
        }

        public void setNetworks(List<ZeroTierNetwork> networks)
        {
            if (networks == null)
            {
                this.wrapPanel.Children.Clear();
                return;
            }

            foreach (ZeroTierNetwork network in networks)
            {
                NetworkInfoView view = ChildWithNetwork(network);
                if (view != null)
                {
                    view.SetNetworkInfo(network);
                }
                else
                {
                    wrapPanel.Children.Add(
                        new NetworkInfoView(
                            network));
                }
            }

            // remove networks we're no longer joined to.
            List<ZeroTierNetwork> tmpList = GetNetworksFromChildren();
            foreach (ZeroTierNetwork n in networks)
            {
                if (tmpList.Contains(n))
                {
                    tmpList.Remove(n);
                }
            }

            foreach (ZeroTierNetwork n in tmpList)
            {
                NetworkInfoView view = ChildWithNetwork(n);
                if (view != null)
                {
                    wrapPanel.Children.Remove(view);
                }
            }
        }

        private NetworkInfoView ChildWithNetwork(ZeroTierNetwork network)
        {
            List<NetworkInfoView> list = wrapPanel.Children.OfType<NetworkInfoView>().ToList();
           
            foreach (NetworkInfoView view in list)
            {
                if (view.HasNetwork(network))
                {
                    return view;
                }
            }

            return null;
        }

        private List<ZeroTierNetwork> GetNetworksFromChildren()
        {
            List<ZeroTierNetwork> networks = new List<ZeroTierNetwork>(wrapPanel.Children.Count);

            List<NetworkInfoView> list = wrapPanel.Children.OfType<NetworkInfoView>().ToList();
            foreach (NetworkInfoView n in list)
            {
                networks.Add(n.network);
            }

            return networks;
        }
    }
}