-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathViewModelBase.cs
63 lines (54 loc) · 2.3 KB
/
ViewModelBase.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
56
57
58
59
60
61
62
63
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.ComponentModel;
namespace MvvmScarletToolkit.Observables
{
/// <summary>
/// BaseViewModel that serves as service aggregate and caches <see cref="INotifyPropertyChanged"/> EventArgs
/// </summary>
public abstract class ViewModelBase : ObservableRecipient, IDisposable
{
protected readonly IObservableBusyStack BusyStack;
protected readonly IScarletCommandBuilder CommandBuilder;
protected readonly IScarletCommandManager CommandManager;
protected readonly IScarletDispatcher Dispatcher;
protected readonly IExitService Exit;
protected readonly IScarletEventManager<INotifyPropertyChanged, PropertyChangedEventArgs> WeakEventManager;
private bool _isBusy;
[Bindable(true, BindingDirection.OneWay)]
public bool IsBusy
{
get { return _isBusy; }
protected set { SetProperty(ref _isBusy, value); }
}
protected bool IsDisposed { get; private set; }
protected ViewModelBase(IScarletCommandBuilder commandBuilder)
: base((commandBuilder?.Messenger)!)
{
CommandBuilder = commandBuilder ?? throw new ArgumentNullException(nameof(commandBuilder));
Dispatcher = commandBuilder.Dispatcher ?? throw new ArgumentNullException(nameof(IScarletCommandBuilder.Dispatcher));
CommandManager = commandBuilder.CommandManager ?? throw new ArgumentNullException(nameof(IScarletCommandBuilder.CommandManager));
Exit = commandBuilder.Exit ?? throw new ArgumentNullException(nameof(IScarletCommandBuilder.Exit));
WeakEventManager = commandBuilder.WeakEventManager ?? throw new ArgumentNullException(nameof(IScarletCommandBuilder.WeakEventManager));
BusyStack = new ObservableBusyStack((hasItems) => IsBusy = hasItems);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (IsDisposed)
{
return;
}
if (disposing)
{
BusyStack.Dispose();
IsActive = false;
}
IsDisposed = true;
}
}
}