Skip to content

Commit

Permalink
nullables and automated csharp changes
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesroddie authored and martijn00 committed Dec 8, 2020
1 parent 796475d commit 42d71b9
Show file tree
Hide file tree
Showing 23 changed files with 170 additions and 277 deletions.
22 changes: 12 additions & 10 deletions Rg.Plugins.Popup/Animations/MoveAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Task>();

taskList.Add(base.Appearing(content, page));
var taskList = new List<Task>
{
base.Appearing(content, page)
};

if (content != null)
{
Expand Down Expand Up @@ -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<Task>();

taskList.Add(base.Disappearing(content, page));
var taskList = new List<Task>
{
base.Disappearing(content, page)
};

if (content != null)
{
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions Rg.Plugins.Popup/Pages/PopupPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -288,7 +288,7 @@ protected virtual bool OnBackgroundClicked()

#region Internal Methods

internal async void SendBackgroundClick()
internal async Task SendBackgroundClick()
{
BackgroundClicked?.Invoke(this, EventArgs.Empty);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace Rg.Plugins.Popup.Droid.Gestures
{
internal class RgGestureDetectorListener : GestureDetector.SimpleOnGestureListener
{
public event EventHandler<MotionEvent> Clicked;
public event EventHandler<MotionEvent>? 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;
}
}
}
}
25 changes: 10 additions & 15 deletions Rg.Plugins.Popup/Platforms/Android/Impl/PopupPlatformDroid.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using Android.App;
using Android.OS;
Expand All @@ -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
{
Expand All @@ -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);
}
Expand All @@ -53,21 +51,21 @@ 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);
}

#region System Animation

private bool GetIsSystemAnimationEnabled()
private static bool GetIsSystemAnimationEnabled()
{
float animationScale;
var context = Popup.Context;
Expand Down Expand Up @@ -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<bool>();

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

return tcs.Task;
}

#endregion
}
}
}
10 changes: 5 additions & 5 deletions Rg.Plugins.Popup/Platforms/Android/Popup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;

Expand Down Expand Up @@ -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!);
}
}
}
Expand Down
45 changes: 25 additions & 20 deletions Rg.Plugins.Popup/Platforms/Android/Renderers/PopupPageRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Android.App;
using Android.Content;
using Android.Graphics;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -132,15 +137,15 @@ 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();
}

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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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];

Expand Down
Loading

0 comments on commit 42d71b9

Please sign in to comment.