Skip to content

Commit

Permalink
Merge pull request #107 from romerotg/master
Browse files Browse the repository at this point in the history
Fix "VirtualView cannot be null here" on Android when `OnLayout` is fired after the handler is disconnected
  • Loading branch information
LuckyDucko authored Mar 7, 2024
2 parents 87fb74f + 42e60cd commit 86c80de
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public PopupPageHandler(IMauiContext context)

protected override void ConnectHandler(ContentViewGroup platformView)
{
(platformView as PopupPageRenderer).PopupHandler = this;
if (platformView is PopupPageRenderer popupPageRenderer)
popupPageRenderer.PopupHandler = this;
base.ConnectHandler(platformView);
}

Expand All @@ -32,6 +33,8 @@ protected override ContentViewGroup CreatePlatformView()

protected override void DisconnectHandler(ContentViewGroup platformView)
{
if (platformView is PopupPageRenderer popupPageRenderer)
popupPageRenderer.PopupHandler = null;
base.DisconnectHandler(platformView);
}
}
Expand Down
39 changes: 25 additions & 14 deletions Mopups/Mopups.Maui/Platforms/Android/Handler/PopupPageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ protected override void OnLayout(bool changed, int left, int top, int right, int
systemPadding = new Thickness();
}

(PopupHandler.VirtualView as PopupPage)?.SetValue(PopupPage.SystemPaddingProperty, systemPadding);
(PopupHandler.VirtualView as PopupPage)?.SetValue(PopupPage.KeyboardOffsetProperty, keyboardOffset);
(PopupHandler?.VirtualView as PopupPage)?.SetValue(PopupPage.SystemPaddingProperty, systemPadding);
(PopupHandler?.VirtualView as PopupPage)?.SetValue(PopupPage.KeyboardOffsetProperty, keyboardOffset);

if (changed)
(PopupHandler.VirtualView as PopupPage)?.Layout(new Rect(Context.FromPixels(left), Context.FromPixels(top), Context.FromPixels(right), Context.FromPixels(bottom)));
(PopupHandler?.VirtualView as PopupPage)?.Layout(new Rect(Context.FromPixels(left), Context.FromPixels(top), Context.FromPixels(right), Context.FromPixels(bottom)));
else
(PopupHandler.VirtualView as PopupPage)?.ForceLayout();
(PopupHandler?.VirtualView as PopupPage)?.ForceLayout();
base.OnLayout(changed, left, top, right, bottom);
//base.OnLayout(changed, 20, 500, 1080, 2000);
//base.OnLayout(changed, visibleRect.Left, visibleRect.Top, visibleRect.Right, visibleRect.Bottom);
Expand All @@ -137,17 +137,19 @@ protected override void OnAttachedToWindow()
var activity = Platform.CurrentActivity;
var decoreView = activity?.Window?.DecorView;
//activity?.Window?.SetSoftInputMode(SoftInput.AdjustResize);
Context.HideKeyboard(decoreView);
if (decoreView != null)
Context?.HideKeyboard(decoreView);
base.OnAttachedToWindow();
}

protected override void OnDetachedFromWindow()
{
Device.StartTimer(TimeSpan.FromMilliseconds(0), () =>
Application.Current?.Dispatcher.StartTimer(TimeSpan.FromMilliseconds(0), () =>
{
var activity = Platform.CurrentActivity;
var decoreView = activity?.Window?.DecorView;
Context.HideKeyboard(decoreView);
if (decoreView != null)
Context?.HideKeyboard(decoreView);
return false;
});

Expand All @@ -170,7 +172,7 @@ public override bool DispatchTouchEvent(MotionEvent e)
{
return false;
}
if ((PopupHandler.VirtualView as PopupPage).BackgroundInputTransparent)
if ((PopupHandler?.VirtualView is PopupPage popupPage) && popupPage.BackgroundInputTransparent)
{
return base.DispatchTouchEvent(e);
}
Expand All @@ -192,11 +194,11 @@ public override bool OnTouchEvent(MotionEvent e)

_gestureDetector.OnTouchEvent(e);

if ((PopupHandler?.VirtualView as PopupPage).BackgroundInputTransparent)
if ((PopupHandler?.VirtualView is PopupPage popupPage) && popupPage.BackgroundInputTransparent)
{
if ((ChildCount > 0 && !IsInRegion(e.RawX, e.RawY, PopupHandler?.PlatformView.GetChildAt(0)!)) || ChildCount == 0)
{
(PopupHandler?.VirtualView as PopupPage).SendBackgroundClick();
popupPage.SendBackgroundClick();

return false;
}
Expand Down Expand Up @@ -227,11 +229,20 @@ private async void OnBackgroundClick(object sender, MotionEvent e)
if (ChildCount == 0)
return;

var isInRegion = IsInRegion(e.RawX, e.RawY, PopupHandler.PlatformView.GetChildAt(0));

if (!isInRegion)
if (PopupHandler != null)
{
(PopupHandler.VirtualView as PopupPage).SendBackgroundClick();
var child = PopupHandler.PlatformView.GetChildAt(0);

if (child != null)
{
var isInRegion = IsInRegion(e.RawX, e.RawY, child);

if (!isInRegion)
{
if (PopupHandler.VirtualView is PopupPage popupPage)
popupPage.SendBackgroundClick();
}
}
}
}
}

0 comments on commit 86c80de

Please sign in to comment.