Files
Wino-Mail/Wino.Core.ViewModels/CoreBaseViewModel.cs
T

58 lines
1.6 KiB
C#
Raw Normal View History

2024-11-10 23:28:25 +01:00
using System;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Navigation;
2025-02-16 11:54:23 +01:00
namespace Wino.Core.ViewModels;
2025-10-25 10:22:35 +02:00
public class CoreBaseViewModel : ObservableRecipient, INavigationAware
2024-11-10 23:28:25 +01:00
{
2025-02-16 11:54:23 +01:00
private IDispatcher _dispatcher;
public IDispatcher Dispatcher
2024-11-10 23:28:25 +01:00
{
2025-02-16 11:54:23 +01:00
get
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
return _dispatcher;
}
set
{
_dispatcher = value;
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
if (value != null)
{
OnDispatcherAssigned();
2024-11-10 23:28:25 +01:00
}
}
2025-02-16 11:54:23 +01:00
}
2024-11-10 23:28:25 +01:00
2025-10-25 10:22:35 +02:00
public virtual void OnNavigatedTo(NavigationMode mode, object parameters)
{
2025-12-27 19:16:24 +01:00
UnregisterRecipients();
RegisterRecipients();
}
2024-11-10 23:28:25 +01:00
2025-10-25 10:22:35 +02:00
public virtual void OnNavigatedFrom(NavigationMode mode, object parameters)
{
UnregisterRecipients();
}
2024-11-10 23:28:25 +01:00
2025-02-16 11:54:23 +01:00
public virtual void OnPageLoaded() { }
2024-11-10 23:28:25 +01:00
2025-02-16 11:54:23 +01:00
public async Task ExecuteUIThread(Action action) => await Dispatcher?.ExecuteOnUIThread(action);
public void ReportUIChange<TMessage>(TMessage message) where TMessage : class, IUIMessage => Messenger.Send(message);
2024-11-10 23:28:25 +01:00
2025-02-16 11:54:23 +01:00
protected virtual void OnDispatcherAssigned() { }
2024-11-10 23:28:25 +01:00
/// <summary>
/// Register message recipients for this view model. Override to register specific message types.
/// </summary>
protected virtual void RegisterRecipients() { }
/// <summary>
/// Unregister message recipients for this view model. Override to unregister specific message types.
/// </summary>
protected virtual void UnregisterRecipients() { }
2024-11-10 23:28:25 +01:00
}