Skip to content

Commit

Permalink
add Presented event
Browse files Browse the repository at this point in the history
From DeviceTests, there's a case where the DialogFragment is shown, but the animation event isn't fired, maybe it's dismissed before the animation completes. So adding a handler that will inform that Dialog is shown after 500ms, avoid a dead-lock
  • Loading branch information
pictos committed Jul 25, 2024
1 parent 2dedb42 commit 3facb22
Showing 1 changed file with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Android.Runtime;
using Android.Views;
using System.Runtime.CompilerServices;
using System.Threading;
using Android.Views.Animations;
using AndroidX.Fragment.App;
using AAnimation = Android.Views.Animations.Animation;
Expand Down Expand Up @@ -201,37 +202,49 @@ async Task PresentModal(Page modal, bool animated)
var fragmentManager = WindowMauiContext.GetFragmentManager();
var dialogFragmentId = AView.GenerateViewId().ToString();
_modals.Push(dialogFragmentId);
dialogFragment.Show(fragmentManager, dialogFragmentId);


if (animated)
{
NavAnimationInProgress = true;
dialogFragment!.AnimationEnded += OnAnimationEnded;
dialogFragment.AnimationEnded += OnAnimationEnded;
dialogFragment.Presented += OnPresented;

dialogFragment.Show(fragmentManager, dialogFragmentId);

void OnPresented(object? sender, EventArgs e)
{
dialogFragment.AnimationEnded -= OnAnimationEnded;
NavAnimationInProgress = false;
animationCompletionSource.SetResult(true);
}

await animationCompletionSource.Task;
}
else
{

dialogFragment.Show(fragmentManager, dialogFragmentId);
NavAnimationInProgress = false;
animationCompletionSource.TrySetResult(true);
}

void OnAnimationEnded(object? sender, EventArgs e)
{
dialogFragment!.AnimationEnded -= OnAnimationEnded;
dialogFragment.AnimationEnded -= OnAnimationEnded;
NavAnimationInProgress = false;
animationCompletionSource.SetResult(true);
}
}

internal class ModalFragment : DialogFragment
internal sealed class ModalFragment : DialogFragment
{
Page _modal;
IMauiContext _mauiWindowContext;
NavigationRootManager? _navigationRootManager;
static readonly ColorDrawable TransparentColorDrawable = new(AColor.Transparent);

public event EventHandler? AnimationEnded;
public event EventHandler? Presented;


public bool IsAnimated { get; internal set; }
Expand Down Expand Up @@ -285,6 +298,9 @@ public override void OnStart()
int height = ViewGroup.LayoutParams.MatchParent;
dialog.Window.SetLayout(width, height);


var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(700));
var tcs = new TaskCompletionSource();
if (IsAnimated)
{
var animation = AnimationUtils.LoadAnimation(_mauiWindowContext.Context, Resource.Animation.nav_modal_default_enter_anim)!;
Expand All @@ -293,14 +309,24 @@ public override void OnStart()
animation.AnimationEnd += OnAnimationEnded;
}

tcs.Task.WaitAsync(cts.Token).ContinueWith(t =>
{
cts.Dispose();
Presented?.Invoke(this, EventArgs.Empty);

}, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, TaskScheduler.FromCurrentSynchronizationContext());

void OnAnimationEnded(object? sender, AAnimation.AnimationEndEventArgs e)
{
if (sender is not AAnimation animation)
{
return;
}

tcs.TrySetResult();

animation.AnimationEnd -= OnAnimationEnded;
cts.Dispose();
AnimationEnded?.Invoke(this, EventArgs.Empty);
}
}
Expand Down

0 comments on commit 3facb22

Please sign in to comment.