Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/SharedMauiCoreLibrary/Models/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using AndreasReitberger.Shared.Core.Interfaces;
#if DEBUG
using System.Diagnostics;
#endif

namespace AndreasReitberger.Shared.Core
{
Expand All @@ -14,25 +17,38 @@ public partial class ViewModelBase : ViewModelCoreBase, IViewModelBase
public ViewModelBase() : base()
{
Dispatcher = DispatcherProvider.Current.GetForCurrentThread();
#if DEBUG
Debug.WriteLine($"[ViewModelBase] Default Ctor called: {Dispatcher}");
#endif
}
public ViewModelBase(IDispatcher? dispatcher) : base()
{
Dispatcher = dispatcher;
#if DEBUG
Debug.WriteLine($"[ViewModelBase] Ctor with dispatcher parameter called: {Dispatcher}");
#endif
}
public ViewModelBase(IDispatcher? dispatcher, IServiceProvider? provider) : base(provider: provider)
{
Dispatcher = dispatcher;
Provider = provider;
#if DEBUG
Debug.WriteLine($"[ViewModelBase] Ctor with dispatcher and provider parameter called: {Dispatcher}, {Provider}");
#endif
}

#endregion

#region Methods
public void SetBusy(bool isBusy, IDispatcher? dispatcher)
{
dispatcher ??= Dispatcher;
// Only dispatch if needed
if (dispatcher is not null && dispatcher?.IsDispatchRequired is true)
if (dispatcher is not null && dispatcher.IsDispatchRequired is true)
{
#if DEBUG
Debug.WriteLine($"[ViewModelBase] SetBusy dispatching on ThreadId: {Environment.CurrentManagedThreadId}");
#endif
dispatcher.Dispatch(() =>
{
if (isBusy)
Expand All @@ -44,6 +60,9 @@ public void SetBusy(bool isBusy, IDispatcher? dispatcher)
// Update on the MainThread
else
{
#if DEBUG
Debug.WriteLine($"[ViewModelBase] SetBusy executing on MainThreadId: {Environment.CurrentManagedThreadId}");
#endif
if (isBusy)
IsBusyCounter++;
else
Expand All @@ -55,9 +74,13 @@ public void SetBusy(bool isBusy, IDispatcher? dispatcher)

public async Task SetBusyAsync(bool isBusy, IDispatcher? dispatcher)
{
dispatcher ??= Dispatcher;
// Only dispatch if needed
if (dispatcher is not null && dispatcher?.IsDispatchRequired is true)
if (dispatcher is not null && dispatcher.IsDispatchRequired)
{
#if DEBUG
Debug.WriteLine($"[ViewModelBase] SetBusyAsync dispatching on ThreadId: {Environment.CurrentManagedThreadId}");
#endif
await dispatcher.DispatchAsync(() =>
{
if (isBusy)
Expand All @@ -69,6 +92,9 @@ await dispatcher.DispatchAsync(() =>
// Update on the MainThread
else
{
#if DEBUG
Debug.WriteLine($"[ViewModelBase] SetBusyAsync executing on MainThreadId: {Environment.CurrentManagedThreadId}");
#endif
if (isBusy)
IsBusyCounter++;
else
Expand Down
Loading