public sealed class RunicEditSession<T> : INotifyPropertyChanged, IDisposable The editing state of a form in a ViewModel of any MVVM framework: a baseline snapshot of the edited values, whether the current values differ from it (RunicEditSession.IsDirty, with change notification), RunicEditSession.Accept after a save and RunicEditSession.Revert to discard. It pairs with LeaveConfirmation: new LeaveConfirmation(() => session.IsDirty, confirm, session.Revert).
Type parameters
TA snapshot of the edited values with value equality, such as a record or a tuple of immutable values:
(Title, Body)orNoteDraft(Title, Body). A snapshot that holds a mutable object, such as aList<string>or a mutable class, never becomes dirty: the baseline and the current snapshot hold the same instance, so a change of it is a change of both. Capture a copy ([.. Tags]) and pass a structuralcomparer, or capture immutable values (ImmutableArray<string>with a comparer that compares its items).
Remarks
With an owner, the session compares again whenever the owner raises INotifyPropertyChanged.PropertyChanged, so a ReactiveUI WhenAnyValue(vm => vm.Session.IsDirty), a CommunityToolkit binding and a WPF binding follow it. Without one, call RunicEditSession.Refresh after a change. Use it on the ViewModel's thread (its model turn), as the ViewModel's own properties. Dispose it to stop following the owner.
Example
Session = new RunicEditSession<(string Title, string Body)>(this,
capture: () => (Title, Body),
restore: saved => (Title, Body) = saved);
async Task SaveAsync(CancellationToken token)
{
var submitted = (Title, Body); // what this save writes
await _store.SaveAsync(submitted, token);
Session.Accept(submitted); // not Accept(): see below
}After an await, use RunicEditSession.Accept with the values the save wrote. The user may have typed while the save ran, and RunicEditSession.Accept would take those edits as saved, so they would no longer be dirty and a leave confirmation would let them be lost. Accept(submitted) keeps them dirty.
Constructors
RunicEditSession
public RunicEditSession(INotifyPropertyChanged? owner, Func<T> capture, Action<T> restore, IEqualityComparer<T>? comparer = null) Creates a session whose baseline is the current values.
Parameters
ownerThe ViewModel whose property changes may change the values, or
nullto callRunicEditSession.Refresh.captureReturns a snapshot of the edited values.
restoreSets the edited values from a snapshot (
RunicEditSession.Revert).comparerCompares snapshots;
EqualityComparer.Defaultby default.
Properties
Baseline
public T Baseline { get; } The values as last accepted: when the session was created, or by RunicEditSession.Accept.
Current
public T Current { get; } The current values.
IsDirty
public bool IsDirty { get; } Whether the current values differ from RunicEditSession.Baseline.
Methods
Accept
public void Accept() Makes the current values the baseline, such as after a save: the session is clean.
Accept
public void Accept(T baseline) Makes baseline the baseline, such as the values a save wrote or a load returned, and compares the current values with it.
Dispose
public void Dispose() Stops following the owner.
Refresh
public void Refresh() Compares the current values with the baseline again, and raises a change of RunicEditSession.IsDirty if it changed.
Revert
public void Revert() Restores the baseline, discarding the changes: the session is clean.
Events
PropertyChanged
public event PropertyChangedEventHandler? PropertyChanged Inherits documentation from INotifyPropertyChanged.PropertyChanged.