4
4
using System . Collections . ObjectModel ;
5
5
using System . Collections . Specialized ;
6
6
using System . ComponentModel ;
7
+ using System . IO ;
7
8
using System . Linq ;
8
9
using System . Net . Http ;
9
10
using System . Text ;
10
11
12
+ using Newtonsoft . Json ;
13
+ using Newtonsoft . Json . Serialization ;
11
14
using Stratis . DevEx ;
12
15
using Stratis . DevEx . Ethereum . Explorers ;
13
16
@@ -43,6 +46,7 @@ public BlockchainInfo(BlockchainInfoKind kind, string name, BlockchainInfo paren
43
46
public BlockchainInfo Parent { get ; set ; }
44
47
45
48
public object Data { get ; set ; }
49
+ [ JsonProperty ( ItemReferenceLoopHandling = ReferenceLoopHandling . Serialize ) ]
46
50
public ObservableCollection < BlockchainInfo > Children = new ObservableCollection < BlockchainInfo > ( ) ;
47
51
#endregion
48
52
@@ -54,6 +58,10 @@ public BlockchainInfo AddChild(BlockchainInfoKind kind, string name, object data
54
58
return info ;
55
59
}
56
60
61
+ public override int GetHashCode ( ) => Key . GetHashCode ( ) ;
62
+
63
+ public override bool Equals ( object obj ) => obj is BlockchainInfo bi ? Key == bi . Key : false ;
64
+
57
65
public void DeleteChild ( BlockchainInfo child ) => Children . Remove ( child ) ;
58
66
59
67
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
62
70
63
71
public IEnumerable < BlockchainInfo > GetChildren ( BlockchainInfoKind kind ) => Children . Where ( c => c . Kind == kind ) ;
64
72
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
+ }
66
120
#endregion
67
121
}
68
122
@@ -71,7 +125,7 @@ public class BlockchainViewModel : INotifyPropertyChanged
71
125
#region Constructors
72
126
public BlockchainViewModel ( )
73
127
{
74
- Objects = CreateInitialTreeData ( ) ;
128
+ Objects = LoadTreeData ( ) ;
75
129
}
76
130
#endregion
77
131
@@ -157,6 +211,19 @@ public static ObservableCollection<BlockchainInfo> CreateInitialTreeData()
157
211
//data.Add(testnet);
158
212
return data ;
159
213
}
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
+ }
160
227
#endregion
161
228
162
229
#region Events
@@ -171,4 +238,22 @@ public static ObservableCollection<BlockchainInfo> CreateInitialTreeData()
171
238
public event PropertyChangedEventHandler PropertyChanged ;
172
239
#endregion
173
240
}
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
+ */
174
259
}
0 commit comments