| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ModificationExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using MyNet.Observable.Behaviors; |
| | | 8 | | |
| | | 9 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 10 | | namespace MyNet.Observable; |
| | | 11 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 12 | | |
| | | 13 | | public static class ModificationExtensions |
| | | 14 | | { |
| | | 15 | | extension(ObservableObject owner) |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Determines whether the observable object has been modified since it was last marked as clean. This method ch |
| | | 19 | | /// </summary> |
| | | 20 | | /// <returns>True if the object has been modified; otherwise, false.</returns> |
| | | 21 | | public bool IsModified() |
| | | 22 | | { |
| | 18 | 23 | | if (owner.Behaviors.TryGet<ModificationTrackingBehavior>(out var tracking)) |
| | 0 | 24 | | tracking.EnsureInitialStateAttached(); |
| | | 25 | | |
| | 18 | 26 | | return owner is IModificationAware { IsModified: true } |
| | 18 | 27 | | || (owner.Behaviors.TryGet<IModificationTrackingBehavior>(out var behavior) && behavior.IsModified); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Resets the modification state when a <see cref="ModificationTrackingBehavior"/> is registered. |
| | | 32 | | /// </summary> |
| | | 33 | | public void ResetIsModified() |
| | | 34 | | { |
| | 0 | 35 | | if (owner.Behaviors.TryGet<IModificationTrackingBehavior>(out var behavior)) |
| | 0 | 36 | | behavior.ResetModified(); |
| | 0 | 37 | | else if (owner is IModificationAware modificationAware) |
| | 0 | 38 | | modificationAware.ResetModified(); |
| | 0 | 39 | | } |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | |