| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.ComponentModel; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | using System.Text.Json.Serialization; |
| | | 6 | | |
| | | 7 | | namespace SwiftCollections.Observable; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents a property of type <typeparamref name="TValue"/> that raises a <see cref="PropertyChanged"/> event when i |
| | | 11 | | /// </summary> |
| | | 12 | | /// <typeparam name="TValue">The type of the value being observed.</typeparam> |
| | | 13 | | [Serializable] |
| | | 14 | | [MemoryPackable] |
| | | 15 | | public partial class SwiftObservableProperty<TValue> : INotifyPropertyChanged |
| | | 16 | | { |
| | | 17 | | #region Fields |
| | | 18 | | |
| | | 19 | | internal int Index; |
| | | 20 | | |
| | | 21 | | [JsonInclude] |
| | | 22 | | [MemoryPackInclude] |
| | | 23 | | private TValue _value; |
| | | 24 | | |
| | | 25 | | #endregion |
| | | 26 | | |
| | | 27 | | #region Events |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Raised whenever the property's value changes. |
| | | 31 | | /// </summary> |
| | | 32 | | public event PropertyChangedEventHandler PropertyChanged; |
| | | 33 | | |
| | | 34 | | #endregion |
| | | 35 | | |
| | | 36 | | #region Constructors |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new instance of the <see cref="SwiftObservableProperty{TValue}"/> class with the default value of |
| | | 40 | | /// </summary> |
| | | 41 | | [MemoryPackConstructor] |
| | 303168 | 42 | | public SwiftObservableProperty() { } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the <see cref="SwiftObservableProperty{TValue}"/> class with the specified initial |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="value">The initial value of the property.</param> |
| | 23 | 48 | | public SwiftObservableProperty(TValue value) |
| | 23 | 49 | | { |
| | 23 | 50 | | _value = value; |
| | 23 | 51 | | } |
| | | 52 | | |
| | | 53 | | #endregion |
| | | 54 | | |
| | | 55 | | #region Properties |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets or sets the property's value. Raises the <see cref="PropertyChanged"/> event when the value changes. |
| | | 59 | | /// </summary> |
| | | 60 | | [JsonIgnore] |
| | | 61 | | [MemoryPackIgnore] |
| | | 62 | | public TValue Value |
| | | 63 | | { |
| | 103084 | 64 | | get => _value; |
| | | 65 | | set |
| | 1031 | 66 | | { |
| | 1031 | 67 | | if (!Equals(_value, value)) |
| | 1030 | 68 | | { |
| | 1030 | 69 | | _value = value; |
| | 1030 | 70 | | OnPropertyChanged(); |
| | 1030 | 71 | | } |
| | 1031 | 72 | | } |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | #endregion |
| | | 76 | | |
| | | 77 | | #region Methods |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Raises the <see cref="PropertyChanged"/> event with the specified property name. |
| | | 81 | | /// </summary> |
| | | 82 | | protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) |
| | 1030 | 83 | | { |
| | 1030 | 84 | | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| | 1030 | 85 | | } |
| | | 86 | | |
| | | 87 | | #endregion |
| | | 88 | | } |