Skip to content

Commit 58a17ec

Browse files
committed
Add methods to serialize BlockchainExplorerTree.
1 parent 46f15aa commit 58a17ec

File tree

5 files changed

+110
-9
lines changed

5 files changed

+110
-9
lines changed

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainExplorerToolWindowControl.xaml.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,15 @@ private async void NewNetworkCmdExecuted(object sender, ExecutedRoutedEventArgs
132132
{
133133
return;
134134
}
135-
var n = new Network(rpcurl.Text, BigInteger.Parse(chainid.Text));
136-
var t = tree.SelectedItem.AddChild(BlockchainInfoKind.Network, name.Text, n);
135+
var t = tree.SelectedItem.AddChild(BlockchainInfoKind.Network, name.Text);
137136
var endpoints = t.AddChild(BlockchainInfoKind.Folder, "Endpoints");
138-
endpoints.AddChild(BlockchainInfoKind.Endpoint, rpcurl.Text);
137+
endpoints.AddChild(BlockchainInfoKind.Endpoint, rpcurl.Text);
138+
if (!tree.RootItem.Save("BlockchainExplorerTree", out var ex))
139+
{
140+
#if !IS_VSIX
141+
System.Windows.MessageBox.Show("Error saving tree data: " + ex?.Message);
142+
#endif
143+
}
139144
}
140145
catch (Exception ex)
141146
{
@@ -196,6 +201,11 @@ private async void NewEndpointCmd_Executed(object sender, ExecutedRoutedEventArg
196201
var uri = new Uri(rpcurl.Text);
197202
var endpoints = tree.SelectedItem.GetChild("Endpoints", BlockchainInfoKind.Folder);
198203
endpoints.AddChild(BlockchainInfoKind.Endpoint, uri.ToString(), uri);
204+
if (!tree.RootItem.Save("BlockchainExplorerTree", out var ex))
205+
{
206+
System.Windows.MessageBox.Show("Error saving tree data: " + ex?.Message);
207+
}
208+
199209
}
200210
catch (Exception ex)
201211
{

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainExplorerTree.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Windows.Input;
44
using System.Windows.Controls;
5+
using System.Linq;
56
using System.Windows.Media.Imaging;
67

78
using Hardcodet.Wpf.GenericTreeView;
@@ -19,10 +20,12 @@ public class BlockchainExplorerTree : TreeViewBase<BlockchainInfo>
1920
public static RoutedCommand NewEndpointCmd { get; } = new RoutedCommand();
2021

2122
public static RoutedCommand DeleteEndpointCmd { get; } = new RoutedCommand();
23+
24+
public BlockchainInfo RootItem => Items?.First();
2225
#endregion
2326

24-
#region Methods
25-
public override string GetItemKey(BlockchainInfo item) => ((item.Parent?.Name) ?? "Root") + "_" + item.Kind + "_" + item.Name;
27+
#region Overriden Members
28+
public override string GetItemKey(BlockchainInfo item) => item.Key;
2629

2730
public override ICollection<BlockchainInfo> GetChildItems(BlockchainInfo parent) => parent.Children;
2831

@@ -46,6 +49,5 @@ protected override TreeViewItem CreateTreeViewItem(BlockchainInfo data)
4649
return item;
4750
}
4851
#endregion
49-
50-
}
52+
}
5153
}

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainViewModel.cs

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
using System.Collections.ObjectModel;
55
using System.Collections.Specialized;
66
using System.ComponentModel;
7+
using System.IO;
78
using System.Linq;
89
using System.Net.Http;
910
using System.Text;
1011

12+
using Newtonsoft.Json;
13+
using Newtonsoft.Json.Serialization;
1114
using Stratis.DevEx;
1215
using Stratis.DevEx.Ethereum.Explorers;
1316

@@ -43,6 +46,7 @@ public BlockchainInfo(BlockchainInfoKind kind, string name, BlockchainInfo paren
4346
public BlockchainInfo Parent { get; set; }
4447

4548
public object Data { get; set; }
49+
[JsonProperty(ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
4650
public ObservableCollection<BlockchainInfo> Children = new ObservableCollection<BlockchainInfo>();
4751
#endregion
4852

@@ -54,6 +58,10 @@ public BlockchainInfo AddChild(BlockchainInfoKind kind, string name, object data
5458
return info;
5559
}
5660

61+
public override int GetHashCode() => Key.GetHashCode();
62+
63+
public override bool Equals(object obj) => obj is BlockchainInfo bi ? Key == bi.Key : false;
64+
5765
public void DeleteChild(BlockchainInfo child) => Children.Remove(child);
5866

5967
public void DeleteChild(string name, BlockchainInfoKind kind) => Children.Remove(GetChild(name, kind));
@@ -62,7 +70,53 @@ public BlockchainInfo AddChild(BlockchainInfoKind kind, string name, object data
6270

6371
public IEnumerable<BlockchainInfo> GetChildren(BlockchainInfoKind kind) => Children.Where(c => c.Kind == kind);
6472

65-
public IEnumerable<BlockchainInfo> GetEndPoints() => GetChildren(BlockchainInfoKind.Endpoint);
73+
public IEnumerable<BlockchainInfo> GetEndPoints() => GetChildren(BlockchainInfoKind.Endpoint);
74+
75+
public string Key => ((this.Parent?.Name) ?? "Root") + "_" + this.Kind + "_" + this.Name;
76+
77+
public bool Save(string path, out Exception e )
78+
{
79+
try
80+
{
81+
var json = JsonConvert.SerializeObject(this, new JsonSerializerSettings()
82+
{
83+
PreserveReferencesHandling = PreserveReferencesHandling.Objects
84+
85+
});
86+
#if !IS_VSIX
87+
File.WriteAllText(Path.Combine(Runtime.AssemblyLocation, path + ".json"), json);
88+
#endif
89+
e = null;
90+
return true;
91+
}
92+
catch (Exception ex)
93+
{
94+
e = ex;
95+
return false;
96+
}
97+
}
98+
99+
public static BlockchainInfo Load(string path, out Exception e)
100+
{
101+
try
102+
{
103+
#if !IS_VSIX
104+
if (!File.Exists(Path.Combine(Runtime.AssemblyLocation, path + ".json")))
105+
{
106+
e = null;
107+
return null;
108+
}
109+
var b = JsonConvert.DeserializeObject<BlockchainInfo>(File.ReadAllText(Path.Combine(Runtime.AssemblyLocation, path + ".json")));
110+
e = null;
111+
return b;
112+
#endif
113+
}
114+
catch (Exception ex)
115+
{
116+
e = ex;
117+
return null;
118+
}
119+
}
66120
#endregion
67121
}
68122

@@ -71,7 +125,7 @@ public class BlockchainViewModel : INotifyPropertyChanged
71125
#region Constructors
72126
public BlockchainViewModel()
73127
{
74-
Objects = CreateInitialTreeData();
128+
Objects = LoadTreeData();
75129
}
76130
#endregion
77131

@@ -157,6 +211,19 @@ public static ObservableCollection<BlockchainInfo> CreateInitialTreeData()
157211
//data.Add(testnet);
158212
return data;
159213
}
214+
215+
public static ObservableCollection<BlockchainInfo> LoadTreeData()
216+
{
217+
var b = BlockchainInfo.Load("BlockchainExplorerTree", out var e);
218+
if (b == null)
219+
{
220+
return CreateInitialTreeData();
221+
}
222+
else
223+
{
224+
return new ObservableCollection<BlockchainInfo> { b };
225+
}
226+
}
160227
#endregion
161228

162229
#region Events
@@ -171,4 +238,22 @@ public static ObservableCollection<BlockchainInfo> CreateInitialTreeData()
171238
public event PropertyChangedEventHandler PropertyChanged;
172239
#endregion
173240
}
241+
242+
/*
243+
public class BlockchainInfoReferenceResolver : IReferenceResolver
244+
{
245+
public BlockchainInfoReferenceResolver() { }
246+
247+
protected Dictionary<string, BlockchainInfo> references = new Dictionary<string, BlockchainInfo>();
248+
249+
public void AddReference(object context, string reference, object value)
250+
{
251+
if (value is BlockchainInfo bi)
252+
{
253+
references.Add(reference, value);
254+
}
255+
}
256+
257+
}
258+
*/
174259
}

tests/Stratis.DevEx.Wpf.Test/Stratis.DevEx.Wpf.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
<Reference Include="Gu.Wpf.NumericInput, Version=0.5.5.0, Culture=neutral, PublicKeyToken=e6e2e1b32408e0fd, processorArchitecture=MSIL">
3939
<HintPath>..\..\src\packages\Gu.Wpf.NumericInput.0.5.5\lib\net45\Gu.Wpf.NumericInput.dll</HintPath>
4040
</Reference>
41+
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
42+
<HintPath>..\..\src\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
43+
</Reference>
4144
<Reference Include="System" />
4245
<Reference Include="System.Data" />
4346
<Reference Include="System.Numerics" />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Gu.Wpf.NumericInput" version="0.5.5" targetFramework="net472" />
4+
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net472" />
45
<package id="WPF-UI" version="4.0.2" targetFramework="net472" />
56
<package id="WPF-UI.Abstractions" version="4.0.2" targetFramework="net472" />
67
</packages>

0 commit comments

Comments
 (0)