using System;
using System.Threading.Tasks;
using AsyncAwaitBestPractices;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using JetBrains.Annotations;
using CommunityToolkit.Mvvm.Input;
using StabilityMatrix.Avalonia.Models;
namespace StabilityMatrix.Avalonia.ViewModels.Base;
public partial class ViewModelBase : ObservableValidator, IRemovableListItem
{
[PublicAPI]
protected ViewModelState ViewModelState { get; private set; }
private WeakEventManager? parentListRemoveRequestedEventManager;
public event EventHandler ParentListRemoveRequested
{
add
{
parentListRemoveRequestedEventManager ??= new WeakEventManager();
parentListRemoveRequestedEventManager.AddEventHandler(value);
}
remove => parentListRemoveRequestedEventManager?.RemoveEventHandler(value);
}
[RelayCommand]
protected void RemoveFromParentList() =>
parentListRemoveRequestedEventManager?.RaiseEvent(
this,
EventArgs.Empty,
nameof(ParentListRemoveRequested)
);
///
/// Called when the view's LoadedEvent is fired.
///
public virtual void OnLoaded()
{
if (!ViewModelState.HasFlag(ViewModelState.InitialLoaded))
{
ViewModelState |= ViewModelState.InitialLoaded;
OnInitialLoaded();
Dispatcher.UIThread.InvokeAsync(OnInitialLoadedAsync).SafeFireAndForget();
}
}
///
/// Called the first time the view's LoadedEvent is fired.
/// Sets the flag.
///
protected virtual void OnInitialLoaded() { }
///
/// 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.
///
public virtual Task OnLoadedAsync() => Task.CompletedTask;
///
/// Called the first time the view's LoadedEvent is fired.
/// Sets the flag.
///
protected virtual Task OnInitialLoadedAsync() => Task.CompletedTask;
///
/// Called when the view's UnloadedEvent is fired.
///
public virtual void OnUnloaded() { }
///
/// 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.
///
public virtual Task OnUnloadedAsync() => Task.CompletedTask;
}