Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rotorgames committed Jan 25, 2019
2 parents 499b827 + adf403f commit 5075f18
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 46 deletions.
29 changes: 25 additions & 4 deletions src/Rg.Plugins.Popup.Droid/Impl/PopupPlatformDroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public event EventHandler OnInitialized

public bool IsSystemAnimationEnabled => GetIsSystemAnimationEnabled();

public async Task AddAsync(PopupPage page)
public Task AddAsync(PopupPage page)
{
var decoreView = DecoreView;

Expand All @@ -43,10 +43,10 @@ public async Task AddAsync(PopupPage page)

decoreView.AddView(renderer.View);

await Task.Delay(5);
return PostAsync(renderer.View);
}

public async Task RemoveAsync(PopupPage page)
public Task RemoveAsync(PopupPage page)
{
var renderer = page.GetOrCreateRenderer();
if (renderer != null)
Expand All @@ -58,9 +58,11 @@ public async Task RemoveAsync(PopupPage page)

if(element != null)
element.Parent = null;

return PostAsync(DecoreView);
}

await Task.Delay(5);
return Task.FromResult(true);
}

#region System Animation
Expand Down Expand Up @@ -92,5 +94,24 @@ private bool GetIsSystemAnimationEnabled()
}

#endregion

#region Helpers

Task PostAsync(Android.Views.View nativeView)
{
if (nativeView == null)
return Task.FromResult(true);

var tcs = new TaskCompletionSource<bool>();

nativeView.Post(() =>
{
tcs.SetResult(true);
});

return tcs.Task;
}

#endregion
}
}
1 change: 0 additions & 1 deletion src/Rg.Plugins.Popup.Droid/Renderers/PopupPageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ public override bool DispatchTouchEvent(MotionEvent e)
if (!new Rectangle(currentFocus1.Left, currentFocus1.Top, currentFocus1.Width, currentFocus1.Height).Contains(num1, num2))
{
Context.HideKeyboard(currentFocus1);
RequestFocus();
currentFocus1.ClearFocus();
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/Rg.Plugins.Popup.IOS/Impl/PopupPlatformIos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public async Task AddAsync(PopupPage page)

page.DescendantRemoved += HandleChildRemoved;

if(UIApplication.SharedApplication.KeyWindow.WindowLevel == UIWindowLevel.Normal)
UIApplication.SharedApplication.KeyWindow.WindowLevel = -1;

var renderer = page.GetOrCreateRenderer();

var window = new PopupWindow
Expand All @@ -53,7 +56,6 @@ public async Task AddAsync(PopupPage page)
}

await window.RootViewController.PresentViewControllerAsync(renderer.ViewController, false);
await Task.Delay(5);
}

public async Task RemoveAsync(PopupPage page)
Expand All @@ -74,9 +76,11 @@ public async Task RemoveAsync(PopupPage page)
window.RootViewController = null;
page.Parent = null;
window.Hidden = true;
}
window.Dispose();

await Task.Delay(5);
if (UIApplication.SharedApplication.KeyWindow.WindowLevel == -1)
UIApplication.SharedApplication.KeyWindow.WindowLevel = UIWindowLevel.Normal;
}
}

private void DisposeModelAndChildrenRenderers(VisualElement view)
Expand Down
2 changes: 2 additions & 0 deletions src/Rg.Plugins.Popup/Pages/PopupPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class PopupPage : ContentPage

#region Internal Properties

internal bool IsBeingAppeared { get; set; }

internal bool IsBeingDismissed { get; set; }

#endregion
Expand Down
112 changes: 74 additions & 38 deletions src/Rg.Plugins.Popup/Services/PopupNavigationImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,66 +41,84 @@ private async void OnInitialized(object sender, EventArgs e)
await PopAllAsync(false);
}

public async Task PushAsync(PopupPage page, bool animate = true)
public Task PushAsync(PopupPage page, bool animate = true)
{
animate = CanBeAnimated(animate);

if (animate)
{
page.PreparingAnimation();
await AddAsync(page);
await Task.Delay(10);
await page.AppearingAnimation();
}
else
return InvokeThreadSafty(async () =>
{
await AddAsync(page);
}
if (page.IsBeingAppeared)
return;
page.IsBeingAppeared = true;
animate = CanBeAnimated(animate);
if (animate)
{
page.PreparingAnimation();
await AddAsync(page);
await page.AppearingAnimation();
}
else
{
await AddAsync(page);
}
page.IsBeingAppeared = false;
});
}

public Task PopAsync(bool animate = true)
{
animate = CanBeAnimated(animate);
return InvokeThreadSafty(async () =>
{
animate = CanBeAnimated(animate);
if (PopupStack.Count == 0)
throw new IndexOutOfRangeException("There is not page in PopupStack");
if (!PopupStack.Any())
throw new IndexOutOfRangeException("There is not page in PopupStack");
return RemovePageAsync(PopupStack.Last(), animate);
await RemovePageAsync(PopupStack.Last(), animate);
});
}

public async Task PopAllAsync(bool animate = true)
public Task PopAllAsync(bool animate = true)
{
animate = CanBeAnimated(animate);
return InvokeThreadSafty(async () =>
{
animate = CanBeAnimated(animate);
var popupTasks = _popupStack.ToList().Select(page => RemovePageAsync(page, animate));
if (!PopupStack.Any())
throw new IndexOutOfRangeException("There is not page in PopupStack");
await Task.WhenAll(popupTasks);
var popupTasks = PopupStack.ToList().Select(page => RemovePageAsync(page, animate));
await Task.WhenAll(popupTasks);
});
}

public async Task RemovePageAsync(PopupPage page, bool animate = true)
public Task RemovePageAsync(PopupPage page, bool animate = true)
{
if (page == null)
throw new NullReferenceException("Page can not be null");

if(page.IsBeingDismissed)
return;
return InvokeThreadSafty(async () =>
{
if (page == null)
throw new NullReferenceException("Page can not be null");
animate = CanBeAnimated(animate);
if (page.IsBeingDismissed)
return;
page.IsBeingDismissed = true;
animate = CanBeAnimated(animate);
if (animate)
await page.DisappearingAnimation();
page.IsBeingDismissed = true;
await RemoveAsync(page);
await Task.Delay(50);
if (animate)
await page.DisappearingAnimation();
if (animate)
page.DisposingAnimation();
await RemoveAsync(page);
page.IsBeingDismissed = false;
if (animate)
page.DisposingAnimation();
await Task.Delay(5);
page.IsBeingDismissed = false;
});
}

// Private
Expand All @@ -113,8 +131,8 @@ private async Task AddAsync(PopupPage page)

private async Task RemoveAsync(PopupPage page)
{
_popupStack.Remove(page);
await PopupPlatform.RemoveAsync(page);
_popupStack.Remove(page);
}

// Internal
Expand All @@ -133,5 +151,23 @@ private bool CanBeAnimated(bool animate)
}

#endregion

#region Helpers

Task InvokeThreadSafty(Func<Task> action)

This comment has been minimized.

Copy link
@jkozh

jkozh Jan 25, 2019

InvokeThreadSafEty

This comment has been minimized.

Copy link
@rotorgames

rotorgames Jan 25, 2019

Author Owner

Thank you for your attention. I'm going to rename it to InvokeThreadSafe. It was a mistake.

{
var tcs = new TaskCompletionSource<bool>();

Device.BeginInvokeOnMainThread(async () =>
{
await action.Invoke();
tcs.SetResult(true);
});

return tcs.Task;
}

#endregion
}
}

0 comments on commit 5075f18

Please sign in to comment.