Vereinsmeisterschaften  22aa7800eae54b428d40e835886cefe1fdefdfdf
This is a software that can be used to manage the internal competition of the swimming club Illertissen called "Vereinsmeisterschaften".
Loading...
Searching...
No Matches
WorkspaceSetting.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2
4{
9 public class WorkspaceSetting<T> : ObservableObject, IWorkspaceSetting, IEquatable<WorkspaceSetting<T>>
10 {
11 #region Properties
12
13 private string _key;
17 public string Key
18 {
19 get => _key;
20 set => SetProperty(ref _key, value);
21 }
22
23 private T _value;
27 public T Value
28 {
29 get => _value;
30 set
31 {
32 if (SetProperty(ref _value, value)) // returns true, if the value has changed
33 {
34 OnPropertyChanged(nameof(UntypedValue));
35 OnPropertyChanged(nameof(HasChanged));
36 OnPropertyChanged(nameof(HasDefaultValue));
37 }
38 }
39 }
40
45 public object UntypedValue
46 {
47 get => Value!;
48 set => Value = (T)Convert.ChangeType(value, typeof(T));
49 }
50
51 private T _defaultValue;
55 public T DefaultValue
56 {
57 get => _defaultValue;
58 set
59 {
60 if (SetProperty(ref _defaultValue, value)) // returns true, if the value has changed
61 {
62 OnPropertyChanged(nameof(HasDefaultValue));
63 }
64 }
65 }
66
72
73 private T _snapshotValue;
78 {
79 get => _snapshotValue;
80 private set
81 {
82 if (SetProperty(ref _snapshotValue, value)) // returns true, if the value has changed
83 {
84 OnPropertyChanged(nameof(HasChanged));
85 }
86 }
87 }
88
94
98 public T MinValue { get; }
99
103 public T MaxValue { get; }
104
109 public object UntypedMinValue => MinValue!;
110
115 public object UntypedMaxValue => MaxValue!;
116
120 public Type ValueType => typeof(T);
121
125 public bool HasChanged => !EqualityComparer<T>.Default.Equals(Value, SnapshotValue);
126
130 public bool HasDefaultValue => EqualityComparer<T>.Default.Equals(Value, DefaultValue);
131
132 #endregion
133
134 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
135
136 #region Constructors
137
142 {
143 }
144
152 public WorkspaceSetting(string key, T defaultValue, T minValue = default, T maxValue = default)
153 {
154 Key = key;
155 Value = defaultValue;
156 SnapshotValue = defaultValue;
157 DefaultValue = defaultValue;
158 MinValue = minValue;
159 MaxValue = maxValue;
160 }
161
167 {
168 if (other == null) { return; }
169 Key = other.Key;
170 Value = (other.Value is ICloneable cloneableValue) ? (T)cloneableValue.Clone() : other.Value;
171 DefaultValue = (other.DefaultValue is ICloneable cloneableDefaultValue) ? (T)cloneableDefaultValue.Clone() : other.DefaultValue;
172 SnapshotValue = (other.SnapshotValue is ICloneable cloneableSnapshotValue) ? (T)cloneableSnapshotValue.Clone() : other.SnapshotValue;
173 MinValue = (other.MinValue is ICloneable cloneableMinValue) ? (T)cloneableMinValue.Clone() : other.MinValue;
174 MaxValue = (other.MaxValue is ICloneable cloneableMaxValue) ? (T)cloneableMaxValue.Clone() : other.MaxValue;
175 }
176
177 #endregion
178
179 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
180
181 #region Reset / Snapshot / Default
182
186 public void Reset() => Value = SnapshotValue;
187
192
196 public void SetToDefault() => Value = DefaultValue;
197
198 #endregion
199
200 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
201
202 #region IEquatable, ICloneable, ToString()
203
204 public override bool Equals(object obj)
205 => obj is WorkspaceSetting<T> s && s.Value.Equals(Value);
206
207 public bool Equals(WorkspaceSetting<T> other)
208 => Equals((object)other);
209
210 public override int GetHashCode()
211 => Value.GetHashCode();
212
213 public object Clone()
214 => new WorkspaceSetting<T>(this);
215
216 public override string ToString()
217 => $"{Key}: {Value} (Default: {DefaultValue}, Snapshot: {SnapshotValue})";
218
219 #endregion
220
221 }
222}
void CreateSnapshot()
Save the Value to the SnapshotValue
bool HasDefaultValue
Indicating if the Value equals the DefaultValue.
bool HasChanged
Indicating if the Value and SnapshotValue differ.
void Reset()
Set the Value to the SnapshotValue
void SetToDefault()
Set the Value to the DefaultValue
WorkspaceSetting(string key, T defaultValue, T minValue=default, T maxValue=default)
Create a new WorkspaceSetting<T>
WorkspaceSetting(WorkspaceSetting< T > other)
Copy Constructor.
object UntypedDefaultValue
Default value for the setting.
object UntypedSnapshotValue
Snapshot value for the setting.
Interface for a single workspace setting.