Have PresentAlert hide new window when alert is dismissed (#543)
authorE.Z. Hart <hartez@users.noreply.github.com>
Fri, 18 Nov 2016 19:47:52 +0000 (12:47 -0700)
committerSamantha Houts <samantha@teamredwall.com>
Fri, 18 Nov 2016 19:47:52 +0000 (11:47 -0800)
Xamarin.Forms.Platform.iOS/Platform.cs

index 762ccf0..f39856b 100644 (file)
@@ -48,14 +48,7 @@ namespace Xamarin.Forms.Platform.iOS
 
                                if (Forms.IsiOS8OrNewer)
                                {
-                                       var alert = UIAlertController.Create(arguments.Title, arguments.Message, UIAlertControllerStyle.Alert);
-                                       var oldFrame = alert.View.Frame;
-                                       alert.View.Frame = new RectangleF(oldFrame.X, oldFrame.Y, oldFrame.Width, oldFrame.Height - _alertPadding * 2);
-                                       alert.AddAction(UIAlertAction.Create(arguments.Cancel, UIAlertActionStyle.Cancel, a => arguments.SetResult(false)));
-                                       if (arguments.Accept != null)
-                                               alert.AddAction(UIAlertAction.Create(arguments.Accept, UIAlertActionStyle.Default, a => arguments.SetResult(true)));
-
-                                       PresentAlert(alert);
+                                       PresentAlert(arguments);
                                }
                                else
                                {
@@ -82,45 +75,7 @@ namespace Xamarin.Forms.Platform.iOS
 
                                if (Forms.IsiOS8OrNewer)
                                {
-                                       var alert = UIAlertController.Create(arguments.Title, null, UIAlertControllerStyle.ActionSheet);
-
-                                       if (arguments.Cancel != null)
-                                       {
-                                               alert.AddAction(UIAlertAction.Create(arguments.Cancel, UIAlertActionStyle.Cancel, a => arguments.SetResult(arguments.Cancel)));
-                                       }
-
-                                       if (arguments.Destruction != null)
-                                       {
-                                               alert.AddAction(UIAlertAction.Create(arguments.Destruction, UIAlertActionStyle.Destructive, a => arguments.SetResult(arguments.Destruction)));
-                                       }
-
-                                       foreach (var label in arguments.Buttons)
-                                       {
-                                               if (label == null)
-                                                       continue;
-
-                                               var blabel = label;
-                                               alert.AddAction(UIAlertAction.Create(blabel, UIAlertActionStyle.Default, a => arguments.SetResult(blabel)));
-                                       }
-
-                                       if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
-                                       {
-                                               UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
-                                               var observer = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification,
-                                                       n => { alert.PopoverPresentationController.SourceRect = pageRenderer.ViewController.View.Bounds; });
-
-                                               arguments.Result.Task.ContinueWith(t =>
-                                               {
-                                                       NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
-                                                       UIDevice.CurrentDevice.EndGeneratingDeviceOrientationNotifications();
-                                               }, TaskScheduler.FromCurrentSynchronizationContext());
-
-                                               alert.PopoverPresentationController.SourceView = pageRenderer.ViewController.View;
-                                               alert.PopoverPresentationController.SourceRect = pageRenderer.ViewController.View.Bounds;
-                                               alert.PopoverPresentationController.PermittedArrowDirections = 0; // No arrow
-                                       }
-
-                                       PresentAlert(alert);
+                                       PresentAlert(arguments, pageRenderer);
                                }
                                else
                                {
@@ -430,11 +385,90 @@ namespace Xamarin.Forms.Platform.iOS
                        return Page == page || _modals.Contains(page);
                }
 
-               void PresentAlert(UIAlertController alert)
+               // Creates a UIAlertAction which includes a call to hide the presenting UIWindow at the end
+               UIAlertAction CreateActionWithWindowHide(string text, UIAlertActionStyle style, Action setResult, UIWindow window)
+               {
+                       return UIAlertAction.Create(text, style,
+                                       a =>
+                                       {
+                                               setResult();
+                                               window.Hidden = true;
+                                       });
+               }
+
+               void PresentAlert(AlertArguments arguments)
+               {
+                       var window = new UIWindow { BackgroundColor = Color.Transparent.ToUIColor() };
+
+                       var alert = UIAlertController.Create(arguments.Title, arguments.Message, UIAlertControllerStyle.Alert);
+                       var oldFrame = alert.View.Frame;
+                       alert.View.Frame = new RectangleF(oldFrame.X, oldFrame.Y, oldFrame.Width, oldFrame.Height - _alertPadding * 2);
+
+                       if (arguments.Cancel != null)
+                       {
+                               alert.AddAction(CreateActionWithWindowHide(arguments.Cancel, UIAlertActionStyle.Cancel,
+                                       () => arguments.SetResult(false), window));
+                       }
+
+                       if (arguments.Accept != null)
+                       {
+                               alert.AddAction(CreateActionWithWindowHide(arguments.Accept, UIAlertActionStyle.Default,
+                                       () => arguments.SetResult(true), window));
+                       }
+
+                       PresentAlert(window, alert);
+               }
+
+               void PresentAlert(ActionSheetArguments arguments, IVisualElementRenderer pageRenderer)
                {
+                       var alert = UIAlertController.Create(arguments.Title, null, UIAlertControllerStyle.ActionSheet);
                        var window = new UIWindow { BackgroundColor = Color.Transparent.ToUIColor() };
+
+                       if (arguments.Cancel != null)
+                       {
+                               alert.AddAction(CreateActionWithWindowHide(arguments.Cancel, UIAlertActionStyle.Cancel, () => arguments.SetResult(arguments.Cancel), window));
+                       }
+
+                       if (arguments.Destruction != null)
+                       {
+                               alert.AddAction(CreateActionWithWindowHide(arguments.Destruction, UIAlertActionStyle.Destructive, () => arguments.SetResult(arguments.Destruction), window));
+                       }
+
+                       foreach (var label in arguments.Buttons)
+                       {
+                               if (label == null)
+                                       continue;
+
+                               var blabel = label;
+
+                               alert.AddAction(CreateActionWithWindowHide(blabel, UIAlertActionStyle.Default, () => arguments.SetResult(blabel), window));
+                       }
+
+                       if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
+                       {
+                               UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
+                               var observer = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification,
+                                       n => { alert.PopoverPresentationController.SourceRect = pageRenderer.ViewController.View.Bounds; });
+
+                               arguments.Result.Task.ContinueWith(t =>
+                               {
+                                       NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
+                                       UIDevice.CurrentDevice.EndGeneratingDeviceOrientationNotifications();
+                               }, TaskScheduler.FromCurrentSynchronizationContext());
+
+                               alert.PopoverPresentationController.SourceView = pageRenderer.ViewController.View;
+                               alert.PopoverPresentationController.SourceRect = pageRenderer.ViewController.View.Bounds;
+                               alert.PopoverPresentationController.PermittedArrowDirections = 0; // No arrow
+                       }
+
+                       PresentAlert(window, alert);
+               }
+
+               static void PresentAlert(UIWindow window, UIAlertController alert)
+               {
                        window.RootViewController = new UIViewController();
                        window.RootViewController.View.BackgroundColor = Color.Transparent.ToUIColor();
+                       window.WindowLevel = UIWindowLevel.Alert + 1;
                        window.MakeKeyAndVisible();
                        window.RootViewController.PresentViewController(alert, true, null);
                }