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