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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using UVtools.Core.Objects;
namespace UVtools.Core.Managers
{
public class MaterialManager : BindableBase, IList<Material>
{
#region Settings
public static string FilePath;
#endregion
#region Singleton
private static Lazy<MaterialManager> _instanceHolder =
new(() => new MaterialManager());
/// <summary>
/// Instance of <see cref="UserSettings"/> (singleton)
/// </summary>
public static MaterialManager Instance => _instanceHolder.Value;
//public static List<Operation> Operations => _instance.Operations;
#endregion
#region Members
private RangeObservableCollection<Material> _materials = new();
#endregion
#region Properties
public RangeObservableCollection<Material> Materials
{
get => _materials;
set => RaiseAndSetIfChanged(ref _materials, value);
}
/// <summary>
/// Gets the total number of bottles in stock
/// </summary>
public int BottlesInStock => this.Sum(material => material.BottlesInStock);
/// <summary>
/// Gets the total number of bottles ever owned
/// </summary>
public int OwnedBottles => this.Sum(material => material.OwnedBottles);
/// <summary>
/// Gets the total of consumed volume in milliliters
/// </summary>
public decimal ConsumedVolume => this.Sum(material => material.ConsumedVolume);
/// <summary>
/// Gets the total of consumed volume in liters
/// </summary>
public decimal ConsumedVolumeLiters => this.Sum(material => material.ConsumedVolumeLiters);
/// <summary>
/// Gets the total volume in stock in milliliters
/// </summary>
public decimal VolumeInStock => this.Sum(material => material.VolumeInStock);
/// <summary>
/// Gets the total volume in stock in liters
/// </summary>
public decimal VolumeInStockLiters => VolumeInStock / 1000;
/// <summary>
/// Gets the total costs
/// </summary>
public decimal TotalCost => this.Sum(material => material.TotalCost);
/// <summary>
/// Gets the total print time in hours
/// </summary>
public double PrintTime => this.Sum(material => material.PrintTime);
/// <summary>
/// Gets the total print time
/// </summary>
public TimeSpan PrintTimeSpan => TimeSpan.FromHours(PrintTime);
#endregion
#region Constructor
private MaterialManager()
{
}
#endregion
#region Methods
public Material this[int index]
{
get => _materials[index];
set => _materials[index] = value;
}
public Material this[uint index]
{
get => _materials[(int) index];
set => _materials[(int) index] = value;
}
public Material this[Material material]
{
get
{
var index = IndexOf(material);
return index < 0 ? this[index] : null;
}
set
{
var index = IndexOf(material);
if(index >= 0) this[index] = value;
}
}
public IEnumerator<Material> GetEnumerator() => _materials.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void Add(Material item)
{
_materials.Add(item);
RaisePropertiesChanged();
}
public void Add(Material item, bool save)
{
Add(item);
if (save) Save();
}
public void Clear()
{
_materials.Clear();
RaisePropertiesChanged();
}
public void Clear(bool save)
{
Clear();
if(save) Save();
}
public bool Contains(Material item) => _materials.Contains(item);
public void CopyTo(Material[] array, int arrayIndex)
{
_materials.CopyTo(array, arrayIndex);
RaisePropertiesChanged();
}
public bool Remove(Material item)
{
if (_materials.Remove(item))
{
RaisePropertiesChanged();
return true;
}
return false;
}
public void Remove(Material item, bool save)
{
Remove(item);
if (save) Save();
}
public void RemoveRange(IEnumerable<Material> collection)
{
_materials.RemoveRange(collection);
}
public int Count => _materials.Count;
public bool IsReadOnly => false;
public int IndexOf(Material item) => _materials.IndexOf(item);
public void Insert(int index, Material item)
{
_materials.Insert(index, item);
RaisePropertiesChanged();
}
public void Insert(int index, Material item, bool save)
{
Insert(index, item);
if (save) Save();
}
public void RemoveAt(int index)
{
_materials.RemoveAt(index);
RaisePropertiesChanged();
}
public void RemoveAt(int index, bool save)
{
RemoveAt(index);
if (save) Save();
}
public void RaisePropertiesChanged()
{
RaisePropertyChanged(nameof(BottlesInStock));
RaisePropertyChanged(nameof(OwnedBottles));
RaisePropertyChanged(nameof(ConsumedVolume));
RaisePropertyChanged(nameof(ConsumedVolumeLiters));
RaisePropertyChanged(nameof(VolumeInStock));
RaisePropertyChanged(nameof(VolumeInStockLiters));
RaisePropertyChanged(nameof(TotalCost));
RaisePropertyChanged(nameof(PrintTime));
RaisePropertyChanged(nameof(PrintTimeSpan));
RaisePropertyChanged(nameof(Count));
}
/// <summary>
/// Load settings from file
/// </summary>
public static void Load()
{
if (string.IsNullOrWhiteSpace(FilePath) || !File.Exists(FilePath))
{
return;
}
var serializer = new XmlSerializer(Instance.GetType());
try
{
using var myXmlReader = new StreamReader(FilePath);
var instance = (MaterialManager)serializer.Deserialize(myXmlReader);
_instanceHolder = new Lazy<MaterialManager>(() => instance);
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}
/// <summary>
/// Save settings to file
/// </summary>
public static void Save()
{
var serializer = new XmlSerializer(Instance.GetType());
try
{
using var myXmlWriter = new StreamWriter(FilePath);
serializer.Serialize(myXmlWriter, Instance);
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}
public void SortByName()
{
_materials.Sort((material, material1) => string.Compare(material.Name, material1.Name, StringComparison.Ordinal));
}
#endregion
}
}
|