Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ObservableContract.cs
1using System.Collections.ObjectModel;
2using System.Collections.Specialized;
3using System.ComponentModel;
4using CommunityToolkit.Mvvm.ComponentModel;
7
9{
15 public class ObservableContract : ObservableObject, IDisposable
16 {
17
18 #region Constructors and Destructor
19
20 public ObservableContract(Contract contract)
21 {
22 this.Contract = contract;
23 this.Parameters.CollectionChanged += this.Parameters_CollectionChanged;
24 this.Roles.CollectionChanged += this.Roles_CollectionChanged;
25 }
26
27 #endregion
28
29 #region Initialization
30
35 public static async Task<ObservableContract> CreateAsync(Contract contract)
36 {
37 ObservableContract ContractWrapper = new(contract);
38 await ContractWrapper.InitializeAsync();
39 return ContractWrapper;
40 }
41
45 public async Task InitializeAsync()
46 {
47 this.Category = await ContractModel.GetCategory(this.Contract) ?? string.Empty;
48
49 foreach (Parameter Param in this.Contract.Parameters ?? Enumerable.Empty<Parameter>())
50 {
51 ObservableParameter ObservableParam = await ObservableParameter.CreateAsync(Param, this.Contract);
52 this.Parameters.Add(ObservableParam);
53 }
54
55 foreach (Role Role in this.Contract.Roles ?? Enumerable.Empty<Role>())
56 {
59 this.Roles.Add(ObservableRole);
60 }
61 if (this.IsTemplate)
62 {
63 foreach (Part part in this.Contract.Parts ?? Enumerable.Empty<Part>())
64 {
65 ObservableRole? Role = this.Roles.FirstOrDefault(r => r.Name == part.Role);
66 if (Role is not null)
67 await Role.AddPart(part);
68 }
69 }
70 else
71 {
72 foreach (ClientSignature signature in this.Contract.ClientSignatures ?? Enumerable.Empty<ClientSignature>())
73 {
74 ObservableRole? Role = this.Roles.FirstOrDefault(r => r.Name == signature.Role);
75 if (Role is not null)
76 await Role.AddPart(signature);
77 }
78 }
79
80 }
81
82 #endregion
83
84 #region Collection Changed Handlers
85
86 private void Parameters_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
87 {
88 if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems is not null)
89 {
90 foreach (ObservableParameter param in e.NewItems)
91 {
92 param.PropertyChanged += this.Parameter_OnPropertyChanged;
93 }
94 }
95 else if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems is not null)
96 {
97 foreach (ObservableParameter param in e.OldItems)
98 {
99 param.PropertyChanged -= this.Parameter_OnPropertyChanged;
100 }
101 }
102 }
103
104 private void Roles_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
105 {
106 if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems is not null)
107 {
108 foreach (ObservableRole role in e.NewItems)
109 {
110 role.PropertyChanged += this.Role_OnPropertyChanged;
111 role.Parts.CollectionChanged += this.Parts_CollectionChanged;
112 }
113 }
114 else if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems is not null)
115 {
116 foreach (ObservableRole role in e.OldItems)
117 {
118 role.PropertyChanged -= this.Role_OnPropertyChanged;
119 role.Parts.CollectionChanged -= this.Parts_CollectionChanged;
120 }
121 }
122 }
123
124 #endregion
125
126 #region Property Changed Handlers
127
128 public event NotifyCollectionChangedEventHandler? PartChanged;
129 private void Parts_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
130 {
131 this.PartChanged?.Invoke(sender, e);
132 }
133
137 public event PropertyChangedEventHandler? ParameterChanged;
138
139 private void Parameter_OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
140 {
141 this.ParameterChanged?.Invoke(sender, e);
142 }
143
147 public event PropertyChangedEventHandler? RoleChanged;
148
149 private void Role_OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
150 {
151 this.RoleChanged?.Invoke(sender, e);
152 }
153
154 #endregion
155
156 #region Properties
157
161 public Contract Contract { get; }
162
166 public ObservableCollection<ObservableParameter> Parameters => this.parameters;
167 private readonly ObservableCollection<ObservableParameter> parameters = [];
168
172 public ObservableCollection<ObservableRole> Roles => this.roles;
173 private readonly ObservableCollection<ObservableRole> roles = [];
174
178 public string Category
179 {
180 get => this.category;
181 private set => this.SetProperty(ref this.category, value);
182 }
183 private string category = string.Empty;
184
189
194
198 public bool IsTemplate => this.Contract.PartsMode == ContractParts.TemplateOnly;
199
204
208 public string ContractId => this.Contract.ContractId;
209
213 public string TemplateId => this.Contract.TemplateId;
214
215 #endregion
216 private bool disposed = false;
217
218 #region IDisposable Support
219 public void Dispose()
220 {
221 this.Dispose(true);
222 GC.SuppressFinalize(this);
223 }
224
225 protected virtual void Dispose(bool disposing)
226 {
227 if (this.disposed)
228 return;
229
230 if (disposing)
231 {
232 // Unsubscribe from the event to prevent memory leaks
233 this.Parameters.CollectionChanged -= this.Parameters_CollectionChanged;
234 this.Roles.CollectionChanged -= this.Roles_CollectionChanged;
235 }
236
237 this.disposed = true;
238 }
239 #endregion
240 }
241}
static async Task< string?> GetCategory(Contract Contract)
Gets the category of a contract
An observable object that wraps a Contract object. This allows for easier binding in the UI....
bool CanActAsTemplate
Whether the contract can act as a template.
static async Task< ObservableContract > CreateAsync(Contract contract)
Creates a new instance of ObservableContract and initializes the roles and parameters.
ContractVisibility Visibility
The visibility of the contract.
ObservableCollection< ObservableParameter > Parameters
The parameters of the contract.
async Task InitializeAsync()
Initializes the contract data, such as category and parameters.
PropertyChangedEventHandler? RoleChanged
Occurs when a property value of any role changes.
PropertyChangedEventHandler? ParameterChanged
Occurs when a property value of any parameter changes.
ObservableCollection< ObservableRole > Roles
The roles of the contract.
An observable object that wraps a Waher.Networking.XMPP.Contracts.Parameter object....
static async Task< ObservableParameter > CreateAsync(Parameter parameter, Contract contract)
Creates a new instance of ObservableParameter based on the type of the parameter.
An observable object that wraps a Waher.Networking.XMPP.Contracts.Role object. This allows for easier...
async Task InitializeAsync(Contract contract)
Initializes the role in regards to a contract. E.g Sets the description of the role,...
Represents a digital signature on a contract.
string Role
Role of the legal identity in the contract.
Contains the definition of a contract
Definition: Contract.cs:22
Parameter[] Parameters
Defined parameters for the smart contract.
Definition: Contract.cs:267
ClientSignature[] ClientSignatures
Client signatures of the contract.
Definition: Contract.cs:309
bool CanActAsTemplate
If the contract can act as a template for other contracts.
Definition: Contract.cs:336
Role[] Roles
Roles defined in the smart contract.
Definition: Contract.cs:240
ContractState State
Contract state
Definition: Contract.cs:121
string ContractId
Contract identity
Definition: Contract.cs:65
Part[] Parts
Defined parts for the smart contract.
Definition: Contract.cs:258
ContractVisibility Visibility
Contrat Visibility
Definition: Contract.cs:184
string TemplateId
Contract identity of template, if one was used to create the contract.
Definition: Contract.cs:93
Abstract base class for contractual parameters
Definition: Parameter.cs:17
Class defining a part in a contract
Definition: Part.cs:30
string Role
Role of the part in the contract
Definition: Part.cs:57
Class defining a role
Definition: Role.cs:7
ContractParts
How the parts of the contract are defined.
Definition: Part.cs:9
ContractVisibility
Visibility types for contracts.
Definition: Enumerations.cs:56
ContractState
Recognized contract states
Definition: Enumerations.cs:7