-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathCoreBaseViewModel.cs
55 lines (46 loc) · 2.02 KB
/
CoreBaseViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Navigation;
using Wino.Messaging.UI;
namespace Wino.Core.ViewModels
{
public class CoreBaseViewModel : ObservableRecipient,
INavigationAware,
IRecipient<AccountCreatedMessage>,
IRecipient<AccountRemovedMessage>,
IRecipient<AccountUpdatedMessage>
{
private IDispatcher _dispatcher;
public IDispatcher Dispatcher
{
get
{
return _dispatcher;
}
set
{
_dispatcher = value;
if (value != null)
{
OnDispatcherAssigned();
}
}
}
public virtual void OnNavigatedTo(NavigationMode mode, object parameters) { IsActive = true; }
public virtual void OnNavigatedFrom(NavigationMode mode, object parameters) { IsActive = false; }
public virtual void OnPageLoaded() { }
public async Task ExecuteUIThread(Action action) => await Dispatcher?.ExecuteOnUIThread(action);
public void ReportUIChange<TMessage>(TMessage message) where TMessage : class, IUIMessage => Messenger.Send(message);
protected virtual void OnDispatcherAssigned() { }
protected virtual void OnAccountCreated(MailAccount createdAccount) { }
protected virtual void OnAccountRemoved(MailAccount removedAccount) { }
protected virtual void OnAccountUpdated(MailAccount updatedAccount) { }
void IRecipient<AccountCreatedMessage>.Receive(AccountCreatedMessage message) => OnAccountCreated(message.Account);
void IRecipient<AccountRemovedMessage>.Receive(AccountRemovedMessage message) => OnAccountRemoved(message.Account);
void IRecipient<AccountUpdatedMessage>.Receive(AccountUpdatedMessage message) => OnAccountUpdated(message.Account);
}
}