Skip to content

Commit

Permalink
Merge pull request #252 from adospace/fix-251
Browse files Browse the repository at this point in the history
fix #251
  • Loading branch information
adospace authored Sep 3, 2024
2 parents 667ca93 + 1e42c0a commit 1179e02
Showing 1 changed file with 57 additions and 30 deletions.
87 changes: 57 additions & 30 deletions src/MauiReactor/PageHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,65 @@ namespace MauiReactor
{
internal abstract class PageHost : VisualNode
{
private static readonly HashSet<PageHost> _pageHostCache = [];
private readonly INavigation _navigation;
protected bool _sleeping;
protected bool _unloading;

public PageHost(INavigation navigation)
{
_navigation = navigation;
foreach(var pageHost in _pageHostCache.ToArray())
}

private WeakReference<Microsoft.Maui.Controls.Page>? _containerPage;

public Microsoft.Maui.Controls.Page? ContainerPage
{
get
{
pageHost.CheckUnloading();
if (_containerPage?.TryGetTarget(out var page) == true)
{
return page;
}

return null;
}
private set
{
if (value == null)
{
_containerPage = null;
return;
}

if (_containerPage?.TryGetTarget(out var page) == true)
{
throw new InvalidOperationException();
}

_containerPage = new WeakReference<Microsoft.Maui.Controls.Page>(value);
}
}

public Microsoft.Maui.Controls.Page? ContainerPage { get; private set; }

private void CheckUnloading()
{
if (ContainerPage != null &&
!_navigation.NavigationStack.Contains(ContainerPage) &&
!_navigation.ModalStack.Contains(ContainerPage))
var containerPage = ContainerPage;
if (containerPage == null ||
(!_navigation.NavigationStack.Contains(containerPage) &&
!_navigation.ModalStack.Contains(containerPage)))
{
_unloading = true;
Invalidate();
_pageHostCache.Remove(this);
if (!_unloading)
{
_unloading = true;
Invalidate();
}
}
else if (containerPage != null &&
(
_navigation.NavigationStack.Count > 0 && _navigation.NavigationStack[_navigation.NavigationStack.Count - 1] == containerPage ||
_navigation.ModalStack.Count > 0 && _navigation.ModalStack[_navigation.ModalStack.Count - 1] == containerPage
))
{
Application.Current?.Dispatcher.Dispatch(CheckUnloading);
}
}

Expand All @@ -42,9 +76,9 @@ protected sealed override void OnAddChild(VisualNode widget, BindableObject nati
if (nativeControl is Microsoft.Maui.Controls.Page page)
{
ContainerPage = page;
ContainerPage.SetValue(MauiReactorPageHostBagKey, this);
ContainerPage.Appearing += ComponentPage_Appearing;
ContainerPage.Disappearing += ContainerPage_Disappearing;
page.SetValue(MauiReactorPageHostBagKey, this);
page.Appearing += ComponentPage_Appearing;
page.Disappearing += ContainerPage_Disappearing;
}
else
{
Expand All @@ -55,7 +89,7 @@ protected sealed override void OnAddChild(VisualNode widget, BindableObject nati
private void ContainerPage_Disappearing(object? sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine($"{ContainerPage?.Title} Disappearing");
CheckUnloading();
Application.Current?.Dispatcher.Dispatch(CheckUnloading);
}

private void ComponentPage_Appearing(object? sender, EventArgs e)
Expand All @@ -67,14 +101,15 @@ private void ComponentPage_Appearing(object? sender, EventArgs e)

protected sealed override void OnRemoveChild(VisualNode widget, BindableObject nativeControl)
{
if (ContainerPage != null)
var containerPage = ContainerPage;
if (containerPage != null)
{
System.Diagnostics.Debug.WriteLine($"{ContainerPage.Title} OnRemoveChild");
System.Diagnostics.Debug.WriteLine($"{containerPage.Title} OnRemoveChild");

ContainerPage.SetValue(MauiReactorPageHostBagKey, null);
containerPage.SetValue(MauiReactorPageHostBagKey, null);

ContainerPage.Appearing -= ComponentPage_Appearing;
ContainerPage.Disappearing -= ContainerPage_Disappearing;
containerPage.Appearing -= ComponentPage_Appearing;
containerPage.Disappearing -= ContainerPage_Disappearing;
}

ContainerPage = null;
Expand Down Expand Up @@ -137,7 +172,7 @@ protected virtual Component InitializeComponent(Component component)

public IHostElement Run()
{
_component ??= InitializeComponent(new T());
_component ??= InitializeComponent(TypeLoader.Instance.LoadObject<Component>(typeof(T)));

TypeLoader.Instance.Run();
TypeLoader.Instance.AssemblyChangedEvent?.AddListener(this);
Expand All @@ -155,14 +190,7 @@ public IHostElement Run()
}

public void OnAssemblyChanged()
{
// OnComponentAssemblyChanged();
//}

//private void OnComponentAssemblyChanged(/*object? sender, EventArgs e*/)
//{
//Validate.EnsureNotNull(ReactorApplicationHost.Instance);

{
try
{
var newComponent = TypeLoader.Instance.LoadObject<Component>(typeof(T));
Expand All @@ -187,7 +215,6 @@ public void OnAssemblyChanged()

public void Stop()
{
//TypeLoader.Instance.AssemblyChanged -= OnComponentAssemblyChanged;
TypeLoader.Instance.AssemblyChangedEvent?.RemoveListener(this);
_sleeping = true;
}
Expand Down

0 comments on commit 1179e02

Please sign in to comment.