-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Support for C# Hot Reload (#232)
* Add CommunityToolkitMetadataUpdateHandler * Include `Type[]` in `ReloadApplication` event * Add `HotReloadHandler` * `dotnet format` * Change `Type[]` -> `IReadOnlyList<Type>` * Update HotReloadHandler.cs * Update HotReloadHandler.cs * Update CommunityToolkit.Maui.Markup.csproj * Update CommunityToolkitMetadataUpdateHandler.cs * Update samples/CommunityToolkit.Maui.Markup.Sample/HotReloadHandler.cs Co-authored-by: Vladislav Antonyuk <[email protected]> * Add `ICommunityToolkitHotReloadHandler.cs` * Add `Window` support * Replace `#if DEBUG` with `[Conditional("DEBUG")]` * Reference `IServiceProvider` via `Application.Current?.Handler.MauiContext` * Goto correct shell route * Leverage overloaded method * Change `Debug.WriteLine` -> `Trace.WriteLine` * Add Null Coalescing Operator to `Handler` --------- Co-authored-by: Vladislav Antonyuk <[email protected]>
- Loading branch information
1 parent
c555e90
commit d1825b6
Showing
6 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
samples/CommunityToolkit.Maui.Markup.Sample/HotReloadHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace CommunityToolkit.Maui.Markup.Sample; | ||
|
||
class HotReloadHandler : ICommunityToolkitHotReloadHandler | ||
{ | ||
public async void OnHotReload(IReadOnlyList<Type> types) | ||
{ | ||
if (Application.Current?.Windows is null) | ||
{ | ||
Trace.WriteLine($"{nameof(HotReloadHandler)} Failed: {nameof(Application)}.{nameof(Application.Current)}.{nameof(Application.Current.Windows)} is null"); | ||
return; | ||
} | ||
|
||
foreach (var window in Application.Current.Windows) | ||
{ | ||
if (window.Page is not Page currentPage) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var type in types) | ||
{ | ||
if (type.IsSubclassOf(typeof(Page))) | ||
{ | ||
if (window.Page is AppShell shell) | ||
{ | ||
if (shell.CurrentPage is Page visiblePage | ||
&& visiblePage.GetType() == type) | ||
{ | ||
var currentPageShellRoute = AppShell.GetRoute(type); | ||
|
||
await currentPage.Dispatcher.DispatchAsync(async () => | ||
{ | ||
await shell.GoToAsync(currentPageShellRoute, false); | ||
shell.Navigation.RemovePage(visiblePage); | ||
}); | ||
|
||
break; | ||
} | ||
} | ||
else | ||
{ | ||
if (TryGetModalStackPage(window, out var modalPage)) | ||
{ | ||
await currentPage.Dispatcher.DispatchAsync(async () => | ||
{ | ||
await currentPage.Navigation.PopModalAsync(false); | ||
await currentPage.Navigation.PushModalAsync(modalPage, false); | ||
}); | ||
} | ||
else | ||
{ | ||
await currentPage.Dispatcher.DispatchAsync(async () => | ||
{ | ||
await currentPage.Navigation.PopAsync(false); | ||
await currentPage.Navigation.PushAsync(modalPage, false); | ||
}); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
static bool TryGetModalStackPage(Window window, [NotNullWhen(true)] out Page? page) | ||
{ | ||
page = window.Navigation.ModalStack.LastOrDefault(); | ||
return page is not null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/CommunityToolkit.Maui.Markup/CommunityToolkitMetadataUpdateHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[assembly: System.Reflection.Metadata.MetadataUpdateHandler(typeof(CommunityToolkit.Maui.Markup.CommunityToolkitMetadataUpdateHandler))] | ||
namespace CommunityToolkit.Maui.Markup; | ||
|
||
static class CommunityToolkitMetadataUpdateHandler | ||
{ | ||
static readonly WeakEventManager reloadApplicationEventHandler = new(); | ||
|
||
public static event EventHandler<IReadOnlyList<Type>> ReloadApplication | ||
{ | ||
add => reloadApplicationEventHandler.AddEventHandler(value); | ||
remove => reloadApplicationEventHandler.RemoveEventHandler(value); | ||
} | ||
|
||
[System.Diagnostics.Conditional("DEBUG")] | ||
static void UpdateApplication(Type[]? types) | ||
{ | ||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. | ||
reloadApplicationEventHandler.HandleEvent(null, types?.ToList() ?? Enumerable.Empty<Type>(), nameof(ReloadApplication)); | ||
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/CommunityToolkit.Maui.Markup/ICommunityToolkitHotReloadHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace CommunityToolkit.Maui.Markup; | ||
|
||
/// <summary> | ||
/// Interface for handling C# Hot Reload requests | ||
/// </summary> | ||
public interface ICommunityToolkitHotReloadHandler | ||
{ | ||
/// <summary> | ||
/// Executes when C# Hot Reload is invoked | ||
/// </summary> | ||
/// <param name="types">A <see cref="IReadOnlyList{Type}"/> contianing the types that have been changed in code.</param> | ||
void OnHotReload(IReadOnlyList<Type> types); | ||
} |