diff --git a/Rg.Plugins.Popup/Animations/MoveAnimation.cs b/Rg.Plugins.Popup/Animations/MoveAnimation.cs index 35dcc6a0..1a6bdc0d 100644 --- a/Rg.Plugins.Popup/Animations/MoveAnimation.cs +++ b/Rg.Plugins.Popup/Animations/MoveAnimation.cs @@ -50,11 +50,12 @@ public override void Disposing(View content, PopupPage page) content.TranslationY = _defaultTranslationY; } - public async override Task Appearing(View content, PopupPage page) + public override Task Appearing(View content, PopupPage page) { - var taskList = new List(); - - taskList.Add(base.Appearing(content, page)); + var taskList = new List + { + base.Appearing(content, page) + }; if (content != null) { @@ -83,14 +84,15 @@ public async override Task Appearing(View content, PopupPage page) ShowPage(page); - await Task.WhenAll(taskList); + return Task.WhenAll(taskList); } - public async override Task Disappearing(View content, PopupPage page) + public override Task Disappearing(View content, PopupPage page) { - var taskList = new List(); - - taskList.Add(base.Disappearing(content, page)); + var taskList = new List + { + base.Disappearing(content, page) + }; if (content != null) { @@ -117,7 +119,7 @@ public async override Task Disappearing(View content, PopupPage page) } } - await Task.WhenAll(taskList); + return Task.WhenAll(taskList); } private void UpdateDefaultTranslations(View content) diff --git a/Rg.Plugins.Popup/Converters/TypeConverters/EasingTypeConverter.cs b/Rg.Plugins.Popup/Converters/TypeConverters/EasingTypeConverter.cs index 8d98a8d9..32e0f54e 100644 --- a/Rg.Plugins.Popup/Converters/TypeConverters/EasingTypeConverter.cs +++ b/Rg.Plugins.Popup/Converters/TypeConverters/EasingTypeConverter.cs @@ -11,12 +11,12 @@ public override object ConvertFromInvariantString(string value) { if (value != null) { - FieldInfo fieldInfo = typeof(Easing).GetRuntimeFields()?.FirstOrDefault((fi => + var fieldInfo = typeof(Easing).GetRuntimeFields()?.FirstOrDefault(fi => { if (fi.IsStatic) return fi.Name == value; return false; - })); + }); if (fieldInfo != null) return (Easing)fieldInfo.GetValue(null); } diff --git a/Rg.Plugins.Popup/Pages/PopupPage.cs b/Rg.Plugins.Popup/Pages/PopupPage.cs index 93f51bb3..49afa64f 100644 --- a/Rg.Plugins.Popup/Pages/PopupPage.cs +++ b/Rg.Plugins.Popup/Pages/PopupPage.cs @@ -23,15 +23,15 @@ public class PopupPage : ContentPage #region Internal Properties - internal Task AppearingTransactionTask { get; set; } + internal Task? AppearingTransactionTask { get; set; } - internal Task DisappearingTransactionTask { get; set; } + internal Task? DisappearingTransactionTask { get; set; } #endregion #region Events - public event EventHandler BackgroundClicked; + public event EventHandler? BackgroundClicked; #endregion @@ -128,7 +128,7 @@ public PopupPage() BackgroundColor = Color.FromHex("#80000000"); } - protected override void OnPropertyChanged(string propertyName = null) + protected override void OnPropertyChanged(string? propertyName = null) { base.OnPropertyChanged(propertyName); @@ -288,7 +288,7 @@ protected virtual bool OnBackgroundClicked() #region Internal Methods - internal async void SendBackgroundClick() + internal async Task SendBackgroundClick() { BackgroundClicked?.Invoke(this, EventArgs.Empty); diff --git a/Rg.Plugins.Popup/Platforms/Android/Gestures/RgGestureDetectorListener.cs b/Rg.Plugins.Popup/Platforms/Android/Gestures/RgGestureDetectorListener.cs index 8c10d437..01570c71 100644 --- a/Rg.Plugins.Popup/Platforms/Android/Gestures/RgGestureDetectorListener.cs +++ b/Rg.Plugins.Popup/Platforms/Android/Gestures/RgGestureDetectorListener.cs @@ -5,13 +5,13 @@ namespace Rg.Plugins.Popup.Droid.Gestures { internal class RgGestureDetectorListener : GestureDetector.SimpleOnGestureListener { - public event EventHandler Clicked; + public event EventHandler? Clicked; - public override bool OnSingleTapUp(MotionEvent e) + public override bool OnSingleTapUp(MotionEvent? e) { - Clicked?.Invoke(this, e); + if (e != null) Clicked?.Invoke(this, e); return false; } } -} \ No newline at end of file +} diff --git a/Rg.Plugins.Popup/Platforms/Android/Impl/PopupPlatformDroid.cs b/Rg.Plugins.Popup/Platforms/Android/Impl/PopupPlatformDroid.cs index 124c12f7..591e6d9f 100644 --- a/Rg.Plugins.Popup/Platforms/Android/Impl/PopupPlatformDroid.cs +++ b/Rg.Plugins.Popup/Platforms/Android/Impl/PopupPlatformDroid.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading.Tasks; using Android.App; using Android.OS; @@ -19,9 +19,7 @@ namespace Rg.Plugins.Popup.Droid.Impl [Preserve(AllMembers = true)] internal class PopupPlatformDroid : IPopupPlatform { - private IPopupNavigation PopupNavigationInstance => PopupNavigation.Instance; - - private FrameLayout DecoreView => (FrameLayout)((Activity)Popup.Context).Window.DecorView; + private static FrameLayout? DecoreView => (FrameLayout?)((Activity?)Popup.Context)?.Window?.DecorView; public event EventHandler OnInitialized { @@ -41,7 +39,7 @@ public Task AddAsync(PopupPage page) var renderer = page.GetOrCreateRenderer(); - decoreView.AddView(renderer.View); + decoreView?.AddView(renderer.View); return PostAsync(renderer.View); } @@ -53,13 +51,13 @@ public Task RemoveAsync(PopupPage page) { var element = renderer.Element; - DecoreView.RemoveView(renderer.View); + DecoreView?.RemoveView(renderer.View); renderer.Dispose(); if (element != null) element.Parent = null; - - return PostAsync(DecoreView); + if (DecoreView != null) + return PostAsync(DecoreView); } return Task.FromResult(true); @@ -67,7 +65,7 @@ public Task RemoveAsync(PopupPage page) #region System Animation - private bool GetIsSystemAnimationEnabled() + private static bool GetIsSystemAnimationEnabled() { float animationScale; var context = Popup.Context; @@ -97,21 +95,18 @@ private bool GetIsSystemAnimationEnabled() #region Helpers - Task PostAsync(Android.Views.View nativeView) + private static Task PostAsync(Android.Views.View nativeView) { if (nativeView == null) return Task.FromResult(true); var tcs = new TaskCompletionSource(); - nativeView.Post(() => - { - tcs.SetResult(true); - }); + nativeView.Post(() => tcs.SetResult(true)); return tcs.Task; } #endregion } -} \ No newline at end of file +} diff --git a/Rg.Plugins.Popup/Platforms/Android/Popup.cs b/Rg.Plugins.Popup/Platforms/Android/Popup.cs index cd32d8ea..b88d11b7 100644 --- a/Rg.Plugins.Popup/Platforms/Android/Popup.cs +++ b/Rg.Plugins.Popup/Platforms/Android/Popup.cs @@ -11,13 +11,13 @@ namespace Rg.Plugins.Popup { public static class Popup { - internal static event EventHandler OnInitialized; + internal static event EventHandler? OnInitialized; internal static bool IsInitialized { get; private set; } - internal static Context Context { get; private set; } + internal static Context? Context { get; private set; } - public static void Init(Context context, Bundle bundle) + public static void Init(Context context) { LinkAssemblies(); @@ -27,7 +27,7 @@ public static void Init(Context context, Bundle bundle) OnInitialized?.Invoke(null, EventArgs.Empty); } - public static bool SendBackPressed(Action backPressedHandler = null) + public static bool SendBackPressed(Action? backPressedHandler = null) { var popupNavigationInstance = PopupNavigation.Instance; @@ -58,7 +58,7 @@ private static void LinkAssemblies() if (false.Equals(true)) { var i = new PopupPlatformDroid(); - var r = new PopupPageRenderer(null); + var r = new PopupPageRenderer(null!); } } } diff --git a/Rg.Plugins.Popup/Platforms/Android/Renderers/PopupPageRenderer.cs b/Rg.Plugins.Popup/Platforms/Android/Renderers/PopupPageRenderer.cs index b4e18ade..480d7a8c 100644 --- a/Rg.Plugins.Popup/Platforms/Android/Renderers/PopupPageRenderer.cs +++ b/Rg.Plugins.Popup/Platforms/Android/Renderers/PopupPageRenderer.cs @@ -1,4 +1,4 @@ -using System; +using System; using Android.App; using Android.Content; using Android.Graphics; @@ -59,23 +59,21 @@ protected override void Dispose(bool disposing) protected override void OnLayout(bool changed, int l, int t, int r, int b) { - var activity = (Activity)Context; + var activity = (Activity?)Context; Thickness systemPadding; var keyboardOffset = 0d; - var decoreView = activity.Window.DecorView; - var decoreHeight = decoreView.Height; - var decoreWidht = decoreView.Width; + var decoreView = activity?.Window?.DecorView; var visibleRect = new Android.Graphics.Rect(); - decoreView.GetWindowVisibleDisplayFrame(visibleRect); + decoreView?.GetWindowVisibleDisplayFrame(visibleRect); - if (Build.VERSION.SdkInt >= BuildVersionCodes.M) + if (Build.VERSION.SdkInt >= BuildVersionCodes.M && RootWindowInsets != null) { var screenRealSize = new Android.Graphics.Point(); - activity.WindowManager.DefaultDisplay.GetRealSize(screenRealSize); + activity?.WindowManager?.DefaultDisplay?.GetRealSize(screenRealSize); var windowInsets = RootWindowInsets; var bottomPadding = Math.Min(windowInsets.StableInsetBottom, windowInsets.SystemWindowInsetBottom); @@ -93,13 +91,16 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b) Bottom = Context.FromPixels(bottomPadding) }; } - else + else if (Build.VERSION.SdkInt < BuildVersionCodes.M && decoreView != null) { var screenSize = new Android.Graphics.Point(); - activity.WindowManager.DefaultDisplay.GetSize(screenSize); + activity?.WindowManager?.DefaultDisplay?.GetSize(screenSize); var keyboardHeight = 0d; + var decoreHeight = decoreView.Height; + var decoreWidht = decoreView.Width; + if (visibleRect.Bottom < screenSize.Y) { keyboardHeight = screenSize.Y - visibleRect.Bottom; @@ -114,6 +115,10 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b) Bottom = Context.FromPixels(decoreHeight - visibleRect.Bottom - keyboardHeight) }; } + else + { + systemPadding = new Thickness(); + } CurrentElement.SetValue(PopupPage.SystemPaddingProperty, systemPadding); CurrentElement.SetValue(PopupPage.KeyboardOffsetProperty, keyboardOffset); @@ -132,7 +137,7 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b) protected override void OnAttachedToWindow() { - Context.HideKeyboard(((Activity)Context).Window.DecorView); + Context.HideKeyboard(((Activity?)Context)?.Window?.DecorView); base.OnAttachedToWindow(); } @@ -140,7 +145,7 @@ protected override void OnDetachedFromWindow() { Device.StartTimer(TimeSpan.FromMilliseconds(0), () => { - Popup.Context.HideKeyboard(((Activity)Popup.Context).Window.DecorView); + Popup.Context.HideKeyboard(((Activity?)Popup.Context)?.Window?.DecorView); return false; }); base.OnDetachedFromWindow(); @@ -172,17 +177,17 @@ public override bool DispatchTouchEvent(MotionEvent e) if (_disposed) return false; - View currentFocus1 = ((Activity)Context).CurrentFocus; + View? currentFocus1 = ((Activity?)Context)?.CurrentFocus; if (currentFocus1 is EditText) { - View currentFocus2 = ((Activity)Context).CurrentFocus; + View? currentFocus2 = ((Activity?)Context)?.CurrentFocus; if (currentFocus1 == currentFocus2 && _downPosition.Distance(new Point(e.RawX, e.RawY)) <= Context.ToPixels(20.0) && !(DateTime.UtcNow - _downTime > TimeSpan.FromMilliseconds(200.0))) { - int[] location = new int[2]; + var location = new int[2]; currentFocus1.GetLocationOnScreen(location); - float num1 = e.RawX + currentFocus1.Left - location[0]; - float num2 = e.RawY + currentFocus1.Top - location[1]; + var num1 = e.RawX + currentFocus1.Left - location[0]; + var num2 = e.RawY + currentFocus1.Top - location[1]; if (!new Rectangle(currentFocus1.Left, currentFocus1.Top, currentFocus1.Width, currentFocus1.Height).Contains(num1, num2)) { Context.HideKeyboard(currentFocus1); @@ -210,7 +215,7 @@ public override bool OnTouchEvent(MotionEvent e) if (CurrentElement != null && CurrentElement.BackgroundInputTransparent) { - if (ChildCount > 0 && !IsInRegion(e.RawX, e.RawY, GetChildAt(0)) || ChildCount == 0) + if ((ChildCount > 0 && !IsInRegion(e.RawX, e.RawY, GetChildAt(0)!)) || ChildCount == 0) { CurrentElement.SendBackgroundClick(); return false; @@ -225,14 +230,14 @@ private void OnBackgroundClick(object sender, MotionEvent e) if (ChildCount == 0) return; - var isInRegion = IsInRegion(e.RawX, e.RawY, GetChildAt(0)); + var isInRegion = IsInRegion(e.RawX, e.RawY, GetChildAt(0)!); if (!isInRegion) CurrentElement.SendBackgroundClick(); } // Fix for "CloseWhenBackgroundIsClicked not works on Android with Xamarin.Forms 2.4.0.280" #173 - private bool IsInRegion(float x, float y, View v) + private static bool IsInRegion(float x, float y, View v) { var mCoordBuffer = new int[2]; diff --git a/Rg.Plugins.Popup/Platforms/Ios/Impl/PopupPlatformIos.cs b/Rg.Plugins.Popup/Platforms/Ios/Impl/PopupPlatformIos.cs index c272a769..db236c3b 100644 --- a/Rg.Plugins.Popup/Platforms/Ios/Impl/PopupPlatformIos.cs +++ b/Rg.Plugins.Popup/Platforms/Ios/Impl/PopupPlatformIos.cs @@ -20,10 +20,11 @@ namespace Rg.Plugins.Popup.IOS.Impl internal class PopupPlatformIos : IPopupPlatform { // It's necessary because GC in Xamarin.iOS 13 removes all UIWindow if there are not any references to them. See #459 - readonly List _windows = new List(); + private readonly List _windows = new List(); - bool IsiOS9OrNewer => UIDevice.CurrentDevice.CheckSystemVersion(9, 0); - bool IsiOS13OrNewer => UIDevice.CurrentDevice.CheckSystemVersion(13, 0); + private static bool IsiOS9OrNewer => UIDevice.CurrentDevice.CheckSystemVersion(9, 0); + + private static bool IsiOS13OrNewer => UIDevice.CurrentDevice.CheckSystemVersion(13, 0); public event EventHandler OnInitialized { @@ -35,7 +36,7 @@ public event EventHandler OnInitialized public bool IsSystemAnimationEnabled => true; - public async Task AddAsync(PopupPage page) + public Task AddAsync(PopupPage page) { page.Parent = Application.Current.MainPage; @@ -53,14 +54,15 @@ public async Task AddAsync(PopupPage page) window.BackgroundColor = Color.Transparent.ToUIColor(); window.RootViewController = new PopupPlatformRenderer(renderer); - window.RootViewController.View.BackgroundColor = Color.Transparent.ToUIColor(); + if (window.RootViewController.View != null) + window.RootViewController.View.BackgroundColor = Color.Transparent.ToUIColor(); window.WindowLevel = UIWindowLevel.Normal; window.MakeKeyAndVisible(); if (!IsiOS9OrNewer) window.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height); - await window.RootViewController.PresentViewControllerAsync(renderer.ViewController, false); + return window.RootViewController.PresentViewControllerAsync(renderer.ViewController, false); } public async Task RemoveAsync(PopupPage page) @@ -74,26 +76,30 @@ public async Task RemoveAsync(PopupPage page) if (renderer != null && viewController != null && !viewController.IsBeingDismissed) { - var window = viewController.View.Window; - await window.RootViewController.DismissViewControllerAsync(false); + var window = viewController.View?.Window; DisposeModelAndChildrenRenderers(page); - window.RootViewController.Dispose(); - window.RootViewController = null; page.Parent = null; - window.Hidden = true; - - if (IsiOS13OrNewer && _windows.Contains(window)) - _windows.Remove(window); - - window.Dispose(); - window = null; - + if (window != null) + { + var rvc = window.RootViewController; + if (rvc != null) + { + await rvc.DismissViewControllerAsync(false); + rvc.Dispose(); + } + window.RootViewController = null; + window.Hidden = true; + if (IsiOS13OrNewer && _windows.Contains(window)) + _windows.Remove(window); + window.Dispose(); + window = null; + } if (UIApplication.SharedApplication.KeyWindow.WindowLevel == -1) UIApplication.SharedApplication.KeyWindow.WindowLevel = UIWindowLevel.Normal; } } - void DisposeModelAndChildrenRenderers(VisualElement view) + private static void DisposeModelAndChildrenRenderers(VisualElement view) { IVisualElementRenderer renderer; foreach (VisualElement child in view.Descendants()) @@ -117,10 +123,10 @@ void DisposeModelAndChildrenRenderers(VisualElement view) XFPlatform.SetRenderer(view, null); } - void HandleChildRemoved(object sender, ElementEventArgs e) + private void HandleChildRemoved(object sender, ElementEventArgs e) { var view = e.Element; DisposeModelAndChildrenRenderers((VisualElement)view); } } -} \ No newline at end of file +} diff --git a/Rg.Plugins.Popup/Platforms/Ios/Platform/PopupPlatformRenderer.cs b/Rg.Plugins.Popup/Platforms/Ios/Platform/PopupPlatformRenderer.cs index d964fbfc..e7006621 100644 --- a/Rg.Plugins.Popup/Platforms/Ios/Platform/PopupPlatformRenderer.cs +++ b/Rg.Plugins.Popup/Platforms/Ios/Platform/PopupPlatformRenderer.cs @@ -9,9 +9,9 @@ namespace Rg.Plugins.Popup.IOS.Platform [Register("RgPopupPlatformRenderer")] internal class PopupPlatformRenderer : UIViewController { - private IVisualElementRenderer _renderer; + private IVisualElementRenderer? _renderer; - public IVisualElementRenderer Renderer => _renderer; + public IVisualElementRenderer? Renderer => _renderer; public PopupPlatformRenderer(IVisualElementRenderer renderer) { @@ -53,22 +53,22 @@ public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentat public override UIViewController ChildViewControllerForStatusBarHidden() { - return _renderer.ViewController; + return _renderer?.ViewController!; } public override bool PrefersStatusBarHidden() { - return _renderer.ViewController.PrefersStatusBarHidden(); + return _renderer?.ViewController.PrefersStatusBarHidden() ?? false; } public override UIViewController ChildViewControllerForStatusBarStyle() { - return _renderer.ViewController; + return _renderer?.ViewController!; } public override UIStatusBarStyle PreferredStatusBarStyle() { - return _renderer.ViewController.PreferredStatusBarStyle(); + return (UIStatusBarStyle)(_renderer?.ViewController.PreferredStatusBarStyle())!; } public override bool ShouldAutorotate() diff --git a/Rg.Plugins.Popup/Platforms/Ios/Platform/PopupWindow.cs b/Rg.Plugins.Popup/Platforms/Ios/Platform/PopupWindow.cs index 10167769..87e76ef4 100644 --- a/Rg.Plugins.Popup/Platforms/Ios/Platform/PopupWindow.cs +++ b/Rg.Plugins.Popup/Platforms/Ios/Platform/PopupWindow.cs @@ -20,23 +20,22 @@ public PopupWindow() } - public override UIView HitTest(CGPoint point, UIEvent uievent) + public override UIView HitTest(CGPoint point, UIEvent? uievent) { - var platformRenderer = (PopupPlatformRenderer)RootViewController; - var formsElement = platformRenderer?.Renderer?.Element as PopupPage; + var platformRenderer = (PopupPlatformRenderer?)RootViewController; var renderer = platformRenderer?.Renderer; var hitTestResult = base.HitTest(point, uievent); - if (formsElement == null) + if (!(platformRenderer?.Renderer?.Element is PopupPage formsElement)) return hitTestResult; if (formsElement.InputTransparent) - return null; + return null!; - if (formsElement.BackgroundInputTransparent && renderer.NativeView == hitTestResult) + if (formsElement.BackgroundInputTransparent && renderer?.NativeView == hitTestResult) { formsElement.SendBackgroundClick(); - return null; + return null!; } return hitTestResult; diff --git a/Rg.Plugins.Popup/Platforms/Ios/Popup.cs b/Rg.Plugins.Popup/Platforms/Ios/Popup.cs index 1bc8427d..e97e8078 100644 --- a/Rg.Plugins.Popup/Platforms/Ios/Popup.cs +++ b/Rg.Plugins.Popup/Platforms/Ios/Popup.cs @@ -7,7 +7,7 @@ namespace Rg.Plugins.Popup { public static class Popup { - internal static event EventHandler OnInitialized; + internal static event EventHandler? OnInitialized; internal static bool IsInitialized { get; private set; } diff --git a/Rg.Plugins.Popup/Platforms/Ios/Renderers/PopupPageRenderer.cs b/Rg.Plugins.Popup/Platforms/Ios/Renderers/PopupPageRenderer.cs index 5b831a51..3c1a829b 100644 --- a/Rg.Plugins.Popup/Platforms/Ios/Renderers/PopupPageRenderer.cs +++ b/Rg.Plugins.Popup/Platforms/Ios/Renderers/PopupPageRenderer.cs @@ -15,8 +15,8 @@ namespace Rg.Plugins.Popup.IOS.Renderers public class PopupPageRenderer : PageRenderer { private readonly UIGestureRecognizer _tapGestureRecognizer; - private NSObject _willChangeFrameNotificationObserver; - private NSObject _willHideNotificationObserver; + private NSObject? _willChangeFrameNotificationObserver; + private NSObject? _willHideNotificationObserver; private bool _isDisposed; internal CGRect KeyboardBounds { get; private set; } = CGRect.Empty; @@ -132,12 +132,12 @@ private void KeyBoardUpNotification(NSNotification notifi) private async void KeyBoardDownNotification(NSNotification notifi) { - NSObject duration; - var canAnimated = notifi.UserInfo.TryGetValue(UIKeyboard.AnimationDurationUserInfoKey, out duration); + NSObject duration = null!; + var canAnimated = notifi.UserInfo?.TryGetValue(UIKeyboard.AnimationDurationUserInfoKey, out duration); KeyboardBounds = CGRect.Empty; - if (canAnimated) + if (canAnimated ?? false) { //It is needed that buttons are working when keyboard is opened. See #11 await Task.Delay(70); diff --git a/Rg.Plugins.Popup/Platforms/Mac/Platform/PopupPlatformRenderer.cs b/Rg.Plugins.Popup/Platforms/Mac/Platform/PopupPlatformRenderer.cs index d1c17a46..2fadf91e 100644 --- a/Rg.Plugins.Popup/Platforms/Mac/Platform/PopupPlatformRenderer.cs +++ b/Rg.Plugins.Popup/Platforms/Mac/Platform/PopupPlatformRenderer.cs @@ -9,9 +9,9 @@ namespace Rg.Plugins.Popup.MacOS.Platform [Register("RgPopupPlatformRenderer")] internal class PopupPlatformRenderer : NSViewController { - private IVisualElementRenderer _renderer; + private IVisualElementRenderer? _renderer; - public IVisualElementRenderer Renderer => _renderer; + public IVisualElementRenderer? Renderer => _renderer; public PopupPlatformRenderer(IVisualElementRenderer renderer) { diff --git a/Rg.Plugins.Popup/Platforms/Mac/Popup.cs b/Rg.Plugins.Popup/Platforms/Mac/Popup.cs index e1b6c7b2..b9c1ce12 100644 --- a/Rg.Plugins.Popup/Platforms/Mac/Popup.cs +++ b/Rg.Plugins.Popup/Platforms/Mac/Popup.cs @@ -7,7 +7,7 @@ namespace Rg.Plugins.Popup { public static class Popup { - internal static event EventHandler OnInitialized; + internal static event EventHandler? OnInitialized; internal static bool IsInitialized { get; private set; } diff --git a/Rg.Plugins.Popup/Platforms/Tizen/Popup.cs b/Rg.Plugins.Popup/Platforms/Tizen/Popup.cs index bca35357..00862638 100644 --- a/Rg.Plugins.Popup/Platforms/Tizen/Popup.cs +++ b/Rg.Plugins.Popup/Platforms/Tizen/Popup.cs @@ -1,4 +1,4 @@ -using System; +using System; using Rg.Plugins.Popup.Tizen.Impl; using Rg.Plugins.Popup.Tizen.Renderers; @@ -6,7 +6,7 @@ namespace Rg.Plugins.Popup.Tizen { public static class Popup { - internal static event EventHandler OnInitialized; + internal static event EventHandler? OnInitialized; internal static bool IsInitialized { get; private set; } @@ -27,4 +27,4 @@ private static void LinkAssemblies() } } } -} \ No newline at end of file +} diff --git a/Rg.Plugins.Popup/Platforms/Tizen/Renderers/PopupPageRenderer.cs b/Rg.Plugins.Popup/Platforms/Tizen/Renderers/PopupPageRenderer.cs index 523e2513..856dcfd0 100644 --- a/Rg.Plugins.Popup/Platforms/Tizen/Renderers/PopupPageRenderer.cs +++ b/Rg.Plugins.Popup/Platforms/Tizen/Renderers/PopupPageRenderer.cs @@ -1,4 +1,4 @@ -using System; +using System; using ElmSharp; using Rg.Plugins.Popup.Pages; using Rg.Plugins.Popup.Tizen.Renderers; @@ -12,27 +12,32 @@ namespace Rg.Plugins.Popup.Tizen.Renderers { public class PopupPageRenderer : PageRenderer { - private GestureLayer _gestureLayer; - private EPopup _popup; + private GestureLayer? _gestureLayer; + private EPopup? _popup; public PopupPageRenderer() { RegisterPropertyHandler(Page.TitleProperty, UpdateTitle); } - private PopupPage PopupPage => Element as PopupPage; + private PopupPage PopupPage => (Element as PopupPage)!; private Rect? ContentBound => Platform.GetRenderer(PopupPage.Content)?.NativeView.Geometry; public void ShowPopup() { - _popup.BackButtonPressed += OnBackButtonPressed; - _popup.Show(); + if (_popup != null) + { + _popup.BackButtonPressed += OnBackButtonPressed; + _popup.Show(); + } } - public void ClosePopup() { - _popup.BackButtonPressed -= OnBackButtonPressed; - _popup.Hide(); + if (_popup != null) + { + _popup.BackButtonPressed -= OnBackButtonPressed; + _popup.Hide(); + } } protected override void OnElementChanged(ElementChangedEventArgs e) @@ -82,17 +87,17 @@ protected override void Dispose(bool disposing) private void OnBackButtonPressed(object sender, EventArgs e) { - PopupPage.SendBackgroundClick(); + PopupPage?.SendBackgroundClick(); } private void OnOutsideClicked(object sender, EventArgs e) { - PopupPage.SendBackgroundClick(); + PopupPage?.SendBackgroundClick(); } private void UpdateTitle() { - _popup.SetPartText("title,text", PopupPage.Title); + _popup?.SetPartText("title,text", PopupPage.Title); } } } diff --git a/Rg.Plugins.Popup/Platforms/Uap/Impl/PopupPlatformWinPhone.cs b/Rg.Plugins.Popup/Platforms/Uap/Impl/PopupPlatformWinPhone.cs deleted file mode 100644 index 272775cc..00000000 --- a/Rg.Plugins.Popup/Platforms/Uap/Impl/PopupPlatformWinPhone.cs +++ /dev/null @@ -1,122 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using Rg.Plugins.Popup.Contracts; -using Rg.Plugins.Popup.Pages; -using Rg.Plugins.Popup.Services; -using Rg.Plugins.Popup.Windows.Renderers; -using Rg.Plugins.Popup.WinPhone.Impl; -using Windows.UI.Core; -using Xamarin.Forms; -using Xamarin.Forms.Internals; -using Xamarin.Forms.Platform.UWP; -using XPlatform = Xamarin.Forms.Platform.UWP.Platform; - -[assembly: Dependency(typeof(PopupPlatformWinPhone))] -namespace Rg.Plugins.Popup.WinPhone.Impl -{ - [Preserve(AllMembers = true)] - class PopupPlatformWinPhone : IPopupPlatform - { - private IPopupNavigation PopupNavigationInstance => PopupNavigation.Instance; - - public event EventHandler OnInitialized - { - add => Popup.OnInitialized += value; - remove => Popup.OnInitialized -= value; - } - - public bool IsInitialized => Popup.IsInitialized; - - public bool IsSystemAnimationEnabled => true; - - [Preserve] - public PopupPlatformWinPhone() - { -#if WINDOWS_PHONE_APP - HardwareButtons.BackPressed += OnBackPressed; -#elif WINDOWS_UWP - SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; -#endif - } - -#if WINDOWS_UWP - private async void OnBackRequested(object sender, BackRequestedEventArgs e) -#elif WINDOWS_PHONE_APP - private async void OnBackPressed(object sender, BackPressedEventArgs e) -#endif - { - var lastPopupPage = PopupNavigationInstance.PopupStack.LastOrDefault(); - - if (lastPopupPage != null) - { - var isPrevent = lastPopupPage.DisappearingTransactionTask != null || lastPopupPage.SendBackButtonPressed(); - - if (!isPrevent) - { - e.Handled = true; - await PopupNavigationInstance.PopAsync(); - } - } - } - - public async Task AddAsync(PopupPage page) - { - page.Parent = Application.Current.MainPage; - - var popup = new global::Windows.UI.Xaml.Controls.Primitives.Popup(); - var renderer = (PopupPageRenderer)page.GetOrCreateRenderer(); - - renderer.Prepare(popup); - popup.Child = renderer.ContainerElement; - popup.IsOpen = true; - page.ForceLayout(); - - await Task.Delay(5); - } - - public async Task RemoveAsync(PopupPage page) - { - var renderer = (PopupPageRenderer)page.GetOrCreateRenderer(); - var popup = renderer.Container; - - if (popup != null) - { - renderer.Destroy(); - - Cleanup(page); - page.Parent = null; - popup.Child = null; - popup.IsOpen = false; - } - - await Task.Delay(5); - } - - internal static void Cleanup(VisualElement element) - { - if (element == null) - throw new ArgumentNullException(nameof(element)); - - var elementRenderer = XPlatform.GetRenderer(element); - foreach (Element descendant in element.Descendants()) - { - var child = descendant as VisualElement; - if (child != null) - { - var childRenderer = XPlatform.GetRenderer(child); - if (childRenderer != null) - { - childRenderer.Dispose(); - XPlatform.SetRenderer(child, null); - } - } - } - if (elementRenderer == null) - return; - - elementRenderer.Dispose(); - XPlatform.SetRenderer(element, null); - } - } -} diff --git a/Rg.Plugins.Popup/Platforms/Uap/Popup.cs b/Rg.Plugins.Popup/Platforms/Uap/Popup.cs index 7fccb66d..f7fc5f68 100644 --- a/Rg.Plugins.Popup/Platforms/Uap/Popup.cs +++ b/Rg.Plugins.Popup/Platforms/Uap/Popup.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Reflection; using Rg.Plugins.Popup.Windows.Renderers; -using Rg.Plugins.Popup.WinPhone.Impl; using Xamarin.Forms; using Xamarin.Forms.Internals; @@ -11,7 +10,7 @@ namespace Rg.Plugins.Popup [Preserve(AllMembers = true)] public static class Popup { - internal static event EventHandler OnInitialized; + internal static event EventHandler? OnInitialized; internal static bool IsInitialized { get; private set; } @@ -20,11 +19,10 @@ public static class Popup /// /// Custom assemblies from other libs or your DI implementations and renderers /// All assemblies for - public static IEnumerable GetExtraAssemblies(IEnumerable defaultAssemblies = null) + public static IEnumerable GetExtraAssemblies(IEnumerable? defaultAssemblies = null) { var assemblies = new List { - GetAssembly(), GetAssembly() }; @@ -49,8 +47,6 @@ public static void Init() private static void LinkAssemblies() { - DependencyService.Register(); - if (false.Equals(true)) { var r = new PopupPageRenderer(); diff --git a/Rg.Plugins.Popup/Platforms/Uap/Renderers/PopupPageRenderer.cs b/Rg.Plugins.Popup/Platforms/Uap/Renderers/PopupPageRenderer.cs index ccc51e6f..063f96c9 100644 --- a/Rg.Plugins.Popup/Platforms/Uap/Renderers/PopupPageRenderer.cs +++ b/Rg.Plugins.Popup/Platforms/Uap/Renderers/PopupPageRenderer.cs @@ -21,7 +21,7 @@ public class PopupPageRenderer : PageRenderer { private Rect _keyboardBounds; - internal WinPopup Container { get; private set; } + internal WinPopup? Container { get; private set; } private PopupPage CurrentElement => (PopupPage)Element; @@ -57,7 +57,7 @@ internal void Prepare(WinPopup container) Window.Current.SizeChanged += OnSizeChanged; DisplayInformation.GetForCurrentView().OrientationChanged += OnOrientationChanged; - InputPane inputPane = InputPane.GetForCurrentView(); + var inputPane = InputPane.GetForCurrentView(); inputPane.Showing += OnKeyboardShowing; inputPane.Hiding += OnKeyboardHiding; @@ -71,7 +71,7 @@ internal void Destroy() Window.Current.SizeChanged -= OnSizeChanged; DisplayInformation.GetForCurrentView().OrientationChanged -= OnOrientationChanged; - InputPane inputPane = InputPane.GetForCurrentView(); + var inputPane = InputPane.GetForCurrentView(); inputPane.Showing -= OnKeyboardShowing; inputPane.Hiding -= OnKeyboardHiding; diff --git a/Rg.Plugins.Popup/Platforms/Wpf/Popup.cs b/Rg.Plugins.Popup/Platforms/Wpf/Popup.cs index 7d7a8c1e..8eed567e 100644 --- a/Rg.Plugins.Popup/Platforms/Wpf/Popup.cs +++ b/Rg.Plugins.Popup/Platforms/Wpf/Popup.cs @@ -6,7 +6,7 @@ namespace Rg.Plugins.Popup { public static class Popup { - internal static event EventHandler OnInitialized; + internal static event EventHandler? OnInitialized; internal static bool IsInitialized { get; private set; } diff --git a/Rg.Plugins.Popup/Platforms/Wpf/Renderers/PopupPageRenderer.cs b/Rg.Plugins.Popup/Platforms/Wpf/Renderers/PopupPageRenderer.cs index 3e2b1826..e87b0934 100644 --- a/Rg.Plugins.Popup/Platforms/Wpf/Renderers/PopupPageRenderer.cs +++ b/Rg.Plugins.Popup/Platforms/Wpf/Renderers/PopupPageRenderer.cs @@ -18,7 +18,7 @@ namespace Rg.Plugins.Popup.WPF.Renderers [Preserve(AllMembers = true)] public class PopupPageRenderer : PageRenderer { - internal WinPopup Container { get; private set; } + internal WinPopup? Container { get; private set; } private PopupPage CurrentElement => (PopupPage)Element; @@ -55,14 +55,18 @@ private void Container_Opened(object sender, EventArgs e) internal void Destroy() { - Container.MouseDown -= Container_MouseDown; - Container.Opened -= Container_Opened; - Container = null; + if (Container != null) + { + Container.MouseDown -= Container_MouseDown; + Container.Opened -= Container_Opened; + Container = null; + } if (Application.Current.MainWindow != null) + { Application.Current.MainWindow.SizeChanged -= OnSizeChanged; - if (Application.Current.MainWindow != null) Application.Current.MainWindow.Activated -= OnActivated; + } } private void OnSizeChanged(object sender, SizeChangedEventArgs e) @@ -94,18 +98,22 @@ private void UpdateElementSize() CurrentElement.Layout(rectangle); CurrentElement.BatchCommit(); - Container.VerticalOffset = rectangle.Y; - Container.HorizontalOffset = rectangle.X; + if (Container != null) + { + Container.VerticalOffset = rectangle.Y; + Container.HorizontalOffset = rectangle.X; + } } private void UpdateZOrder() { - _ = SetWindowPos(GetHwnd(Container.Child), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); + if (Container != null) + _ = SetWindowPos(GetHwnd(Container.Child), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); } private static IntPtr GetHwnd(Visual visual) { - HwndSource hwndSource = ((HwndSource)PresentationSource.FromVisual(visual)); + var hwndSource = ((HwndSource)PresentationSource.FromVisual(visual)); if(hwndSource==null) { return IntPtr.Zero; @@ -119,6 +127,5 @@ private static IntPtr GetHwnd(Visual visual) [DllImport("user32", EntryPoint = "SetWindowPos")] private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); - } } diff --git a/Rg.Plugins.Popup/Services/PopupNavigation.cs b/Rg.Plugins.Popup/Services/PopupNavigation.cs index b088419d..ad01d1cd 100644 --- a/Rg.Plugins.Popup/Services/PopupNavigation.cs +++ b/Rg.Plugins.Popup/Services/PopupNavigation.cs @@ -9,7 +9,7 @@ namespace Rg.Plugins.Popup.Services { public static class PopupNavigation { - const string DepractedMethodsText = + private const string DepractedMethodsText = "You should use " + nameof(IPopupNavigation) + " instance from " @@ -19,8 +19,8 @@ public static class PopupNavigation ".\nSee more info: " + Config.MigrationV1_0_xToV1_1_xUrl; - static IPopupNavigation _popupNavigation; - static IPopupNavigation _customNavigation; + private static IPopupNavigation? _popupNavigation; + private static IPopupNavigation? _customNavigation; public static IPopupNavigation Instance { diff --git a/Rg.Plugins.Popup/Services/PopupNavigationImpl.cs b/Rg.Plugins.Popup/Services/PopupNavigationImpl.cs index ea883415..9b26f70b 100644 --- a/Rg.Plugins.Popup/Services/PopupNavigationImpl.cs +++ b/Rg.Plugins.Popup/Services/PopupNavigationImpl.cs @@ -11,19 +11,19 @@ namespace Rg.Plugins.Popup.Services { internal class PopupNavigationImpl : IPopupNavigation { - readonly object _locker = new object(); + private readonly object _locker = new object(); - readonly List _popupStack = new List(); + private readonly List _popupStack = new List(); - public event EventHandler Pushing; + public event EventHandler? Pushing; - public event EventHandler Pushed; + public event EventHandler? Pushed; - public event EventHandler Popping; + public event EventHandler? Popping; - public event EventHandler Popped; + public event EventHandler? Popped; - private IPopupPlatform PopupPlatform + private static IPopupPlatform PopupPlatform { get { @@ -170,14 +170,14 @@ public Task RemovePageAsync(PopupPage page, bool animate = true) // Private - async Task AddAsync(PopupPage page) + private static Task AddAsync(PopupPage page) { - await PopupPlatform.AddAsync(page); + return PopupPlatform.AddAsync(page); } - async Task RemoveAsync(PopupPage page) + private static Task RemoveAsync(PopupPage page) { - await PopupPlatform.RemoveAsync(page); + return PopupPlatform.RemoveAsync(page); } // Internal @@ -190,7 +190,7 @@ internal void RemovePopupFromStack(PopupPage page) #region Animation - bool CanBeAnimated(bool animate) + private static bool CanBeAnimated(bool animate) { return animate && PopupPlatform.IsSystemAnimationEnabled; } @@ -199,7 +199,7 @@ bool CanBeAnimated(bool animate) #region Helpers - Task InvokeThreadSafe(Func action) + private static Task InvokeThreadSafe(Func action) { var tcs = new TaskCompletionSource();