Browse Source

Add ViewModelBase.ViewModelState and OnInitialLoaded virtual method

pull/324/head
Ionite 1 year ago
parent
commit
d734b0666a
No known key found for this signature in database
  1. 15
      StabilityMatrix.Avalonia/Models/ViewModelState.cs
  2. 53
      StabilityMatrix.Avalonia/ViewModels/Base/ViewModelBase.cs

15
StabilityMatrix.Avalonia/Models/ViewModelState.cs

@ -0,0 +1,15 @@
using System;
namespace StabilityMatrix.Avalonia.Models;
/// <summary>
///
/// </summary>
[Flags]
public enum ViewModelState : uint
{
/// <summary>
/// View Model has been initially loaded
/// </summary>
InitialLoaded = 1 << 0,
}

53
StabilityMatrix.Avalonia/ViewModels/Base/ViewModelBase.cs

@ -2,14 +2,18 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using AsyncAwaitBestPractices; using AsyncAwaitBestPractices;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using JetBrains.Annotations;
using StabilityMatrix.Avalonia.Models; using StabilityMatrix.Avalonia.Models;
namespace StabilityMatrix.Avalonia.ViewModels.Base; namespace StabilityMatrix.Avalonia.ViewModels.Base;
public class ViewModelBase : ObservableValidator, IRemovableListItem public class ViewModelBase : ObservableValidator, IRemovableListItem
{ {
[PublicAPI]
protected ViewModelState ViewModelState { get; private set; }
private WeakEventManager? parentListRemoveRequestedEventManager; private WeakEventManager? parentListRemoveRequestedEventManager;
public event EventHandler ParentListRemoveRequested public event EventHandler ParentListRemoveRequested
{ {
add add
@ -20,24 +24,51 @@ public class ViewModelBase : ObservableValidator, IRemovableListItem
remove => parentListRemoveRequestedEventManager?.RemoveEventHandler(value); remove => parentListRemoveRequestedEventManager?.RemoveEventHandler(value);
} }
protected void RemoveFromParentList() => parentListRemoveRequestedEventManager?.RaiseEvent( protected void RemoveFromParentList() =>
this, EventArgs.Empty, nameof(ParentListRemoveRequested)); parentListRemoveRequestedEventManager?.RaiseEvent(
this,
EventArgs.Empty,
nameof(ParentListRemoveRequested)
);
/// <summary>
/// Called when the view's LoadedEvent is fired.
/// </summary>
public virtual void OnLoaded() public virtual void OnLoaded()
{ {
if (!ViewModelState.HasFlag(ViewModelState.InitialLoaded))
{
ViewModelState |= ViewModelState.InitialLoaded;
OnInitialLoaded();
}
} }
/// <summary>
/// Called the first time the view's LoadedEvent is fired.
/// Sets the <see cref="ViewModelState.InitialLoaded"/> flag.
/// </summary>
protected virtual void OnInitialLoaded() { }
/// <summary>
/// Called asynchronously when the view's LoadedEvent is fired.
/// Runs on the UI thread via Dispatcher.UIThread.InvokeAsync.
/// The view loading will not wait for this to complete.
/// </summary>
public virtual Task OnLoadedAsync() public virtual Task OnLoadedAsync()
{ {
return Task.CompletedTask; return Task.CompletedTask;
} }
public virtual void OnUnloaded() /// <summary>
{ /// Called when the view's UnloadedEvent is fired.
/// </summary>
} public virtual void OnUnloaded() { }
/// <summary>
/// Called asynchronously when the view's UnloadedEvent is fired.
/// Runs on the UI thread via Dispatcher.UIThread.InvokeAsync.
/// The view loading will not wait for this to complete.
/// </summary>
public virtual Task OnUnloadedAsync() public virtual Task OnUnloadedAsync()
{ {
return Task.CompletedTask; return Task.CompletedTask;

Loading…
Cancel
Save