Skip to content

Commit

Permalink
made the code more test friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Nov 17, 2022
1 parent 5d8ff0c commit 9636f29
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
32 changes: 32 additions & 0 deletions src/MAUI/TinyMvvm.Maui/TinyDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
namespace TinyMvvm.Maui;

internal static class TinyDispatcher
{
internal static void BeginInvokeOnMainThread(Action action)
{
//Run Task.Run instead, because MainThread is not supported during unit tests.
if (DeviceInfo.Platform == DevicePlatform.Unknown)
{
Task.Run(action);
return;
}

MainThread.BeginInvokeOnMainThread(action);
}

internal static bool IsMainThread
{
get
{
//Always return true, because MainThread is not supported during unit tests.
if (DeviceInfo.Platform == DevicePlatform.Unknown)
{
return true;
}

return MainThread.IsMainThread;
}
}
}

12 changes: 7 additions & 5 deletions src/MAUI/TinyMvvm.Maui/TinyView.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace TinyMvvm;
using TinyMvvm.Maui;

namespace TinyMvvm;

public abstract class TinyView : ContentPage
{
Expand Down Expand Up @@ -28,13 +30,13 @@ async Task InternalInitialize()
}
}

if (MainThread.IsMainThread)
if (TinyDispatcher.IsMainThread)
{
await InternalInitialize();
}
else
{
MainThread.BeginInvokeOnMainThread(async () => await InternalInitialize());
TinyDispatcher.BeginInvokeOnMainThread(async () => await InternalInitialize());
}
}

Expand All @@ -47,7 +49,7 @@ protected override void OnAppearing()

if (BindingContext is ITinyViewModel viewModel)
{
MainThread.BeginInvokeOnMainThread(async () =>
TinyDispatcher.BeginInvokeOnMainThread(async () =>
{
await InternalLock.WaitAsync();

Expand All @@ -71,7 +73,7 @@ protected override void OnDisappearing()

if (BindingContext is ITinyViewModel viewModel)
{
MainThread.BeginInvokeOnMainThread(async () =>
TinyDispatcher.BeginInvokeOnMainThread(async () =>
{
await InternalLock.WaitAsync();

Expand Down
7 changes: 4 additions & 3 deletions src/MAUI/TinyMvvm.Maui/TinyViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Mvvm.ComponentModel;
using TinyMvvm.Maui;

namespace TinyMvvm;

Expand Down Expand Up @@ -42,7 +43,7 @@ public virtual Task OnAppearing()

if (!hasAppeared)
{
MainThread.BeginInvokeOnMainThread(async () => await OnFirstAppear());
TinyDispatcher.BeginInvokeOnMainThread(async () => await OnFirstAppear());
}

hasAppeared = true;
Expand Down Expand Up @@ -152,13 +153,13 @@ public async void ApplyQueryAttributes(IDictionary<string, object> query)
QueryParameters = query;
}

if (MainThread.IsMainThread)
if (TinyDispatcher.IsMainThread)
{
await OnParameterSet();
}
else
{
MainThread.BeginInvokeOnMainThread(async () =>
TinyDispatcher.BeginInvokeOnMainThread(async () =>
{
await OnParameterSet();
});
Expand Down

0 comments on commit 9636f29

Please sign in to comment.