Skip to content

Commit

Permalink
Merge pull request #104 from redth-org/bugfix/gh-103-null-window
Browse files Browse the repository at this point in the history
Fix null Window in `For` and `ForDefaultWindow`
  • Loading branch information
Cheesebaron authored Sep 13, 2023
2 parents 2833abe + ef9bda9 commit 58efc84
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 67 deletions.
48 changes: 24 additions & 24 deletions BTProgressHUD/BTProgressHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ public static class BTProgressHUD
{
public static void Show(string? status = null, float progress = -1, MaskType maskType = MaskType.None)
{
ProgressHUD.ForDefaultWindow().Show(status, progress, maskType);
ProgressHUD.ForDefaultWindow()?.Show(status, progress, maskType);
}

public static void Show(string cancelCaption, Action cancelCallback, string? status = null, float progress = -1, MaskType maskType = MaskType.None)
{
ProgressHUD.ForDefaultWindow().Show(cancelCaption, cancelCallback, status, progress, maskType);
ProgressHUD.ForDefaultWindow()?.Show(cancelCaption, cancelCallback, status, progress, maskType);
}

public static void ShowContinuousProgress(string? status = null, MaskType maskType = MaskType.None)
{
ProgressHUD.ForDefaultWindow().ShowContinuousProgress(status, maskType);
ProgressHUD.ForDefaultWindow()?.ShowContinuousProgress(status, maskType);
}

public static void ShowToast(string status, bool showToastCentered = false, double timeoutMs = 1000)
Expand All @@ -28,63 +28,63 @@ public static void ShowToast(string status, bool showToastCentered = false, doub

public static void ShowToast(string status, ToastPosition toastPosition = ToastPosition.Center, double timeoutMs = 1000)
{
ProgressHUD.ForDefaultWindow().ShowToast(status, MaskType.None, toastPosition, timeoutMs);
ProgressHUD.ForDefaultWindow()?.ShowToast(status, MaskType.None, toastPosition, timeoutMs);
}

public static void ShowToast(string status, MaskType maskType = MaskType.None, bool showToastCentered = true, double timeoutMs = 1000)
{
ProgressHUD.ForDefaultWindow().ShowToast(status, maskType, showToastCentered ? ToastPosition.Center : ToastPosition.Bottom, timeoutMs);
ProgressHUD.ForDefaultWindow()?.ShowToast(status, maskType, showToastCentered ? ToastPosition.Center : ToastPosition.Bottom, timeoutMs);
}

public static void SetStatus(string status)
{
ProgressHUD.ForDefaultWindow().SetStatus(status);
ProgressHUD.ForDefaultWindow()?.SetStatus(status);
}

public static void ShowSuccessWithStatus(string status, MaskType maskType = MaskType.None, double timeoutMs = 1000, ImageStyle imageStyle = ImageStyle.Default)
{
ProgressHUD.ForDefaultWindow().ShowSuccessWithStatus(status, maskType, timeoutMs, imageStyle);
ProgressHUD.ForDefaultWindow()?.ShowSuccessWithStatus(status, maskType, timeoutMs, imageStyle);
}

public static void ShowErrorWithStatus(string status, MaskType maskType = MaskType.None, double timeoutMs = 1000, ImageStyle imageStyle = ImageStyle.Default)
{
ProgressHUD.ForDefaultWindow().ShowErrorWithStatus(status, maskType, timeoutMs, imageStyle);
ProgressHUD.ForDefaultWindow()?.ShowErrorWithStatus(status, maskType, timeoutMs, imageStyle);
}

public static void ShowInfoWithStatus(string status, MaskType maskType = MaskType.None, double timeoutMs = 1000, ImageStyle imageStyle = ImageStyle.Default)
{
ProgressHUD.ForDefaultWindow().ShowInfoWithStatus(status, maskType, timeoutMs, imageStyle);
ProgressHUD.ForDefaultWindow()?.ShowInfoWithStatus(status, maskType, timeoutMs, imageStyle);
}

public static void ShowImage(UIImage image, string? status, MaskType maskType = MaskType.None, double timeoutMs = 1000)
{
ProgressHUD.ForDefaultWindow().ShowImage(image, status, maskType, timeoutMs);
ProgressHUD.ForDefaultWindow()?.ShowImage(image, status, maskType, timeoutMs);
}

public static void Dismiss()
{
ProgressHUD.ForDefaultWindow().Dismiss();
ProgressHUD.ForDefaultWindow()?.Dismiss();
}

public static bool IsVisible
=> ProgressHUD.ForDefaultWindow().IsVisible;
=> ProgressHUD.ForDefaultWindow()?.IsVisible ?? false;




public static void Show(UIWindow forWindow, string? status = null, float progress = -1, MaskType maskType = MaskType.None)
{
ProgressHUD.For(forWindow).Show(status, progress, maskType);
ProgressHUD.For(forWindow)?.Show(status, progress, maskType);
}

public static void Show(UIWindow forWindow, string cancelCaption, Action cancelCallback, string? status = null, float progress = -1, MaskType maskType = MaskType.None)
{
ProgressHUD.For(forWindow).Show(cancelCaption, cancelCallback, status, progress, maskType);
ProgressHUD.For(forWindow)?.Show(cancelCaption, cancelCallback, status, progress, maskType);
}

public static void ShowContinuousProgress(UIWindow forWindow, string? status = null, MaskType maskType = MaskType.None)
{
ProgressHUD.For(forWindow).ShowContinuousProgress(status, maskType);
ProgressHUD.For(forWindow)?.ShowContinuousProgress(status, maskType);
}

public static void ShowToast(UIWindow forWindow, string status, bool showToastCentered = false, double timeoutMs = 1000)
Expand All @@ -94,46 +94,46 @@ public static void ShowToast(UIWindow forWindow, string status, bool showToastCe

public static void ShowToast(UIWindow forWindow, string status, ToastPosition toastPosition = ToastPosition.Center, double timeoutMs = 1000)
{
ProgressHUD.For(forWindow).ShowToast(status, MaskType.None, toastPosition, timeoutMs);
ProgressHUD.For(forWindow)?.ShowToast(status, MaskType.None, toastPosition, timeoutMs);
}

public static void ShowToast(UIWindow forWindow, string status, MaskType maskType = MaskType.None, bool showToastCentered = true, double timeoutMs = 1000)
{
ProgressHUD.For(forWindow).ShowToast(status, maskType, showToastCentered ? ToastPosition.Center : ToastPosition.Bottom, timeoutMs);
ProgressHUD.For(forWindow)?.ShowToast(status, maskType, showToastCentered ? ToastPosition.Center : ToastPosition.Bottom, timeoutMs);
}

public static void SetStatus(UIWindow forWindow, string status)
{
ProgressHUD.For(forWindow).SetStatus(status);
ProgressHUD.For(forWindow)?.SetStatus(status);
}

public static void ShowSuccessWithStatus(UIWindow forWindow, string status, MaskType maskType = MaskType.None, double timeoutMs = 1000, ImageStyle imageStyle = ImageStyle.Default)
{
ProgressHUD.For(forWindow).ShowSuccessWithStatus(status, maskType, timeoutMs, imageStyle);
ProgressHUD.For(forWindow)?.ShowSuccessWithStatus(status, maskType, timeoutMs, imageStyle);
}

public static void ShowErrorWithStatus(UIWindow forWindow, string status, MaskType maskType = MaskType.None, double timeoutMs = 1000, ImageStyle imageStyle = ImageStyle.Default)
{
ProgressHUD.For(forWindow).ShowErrorWithStatus(status, maskType, timeoutMs, imageStyle);
ProgressHUD.For(forWindow)?.ShowErrorWithStatus(status, maskType, timeoutMs, imageStyle);
}

public static void ShowInfoWithStatus(UIWindow forWindow, string status, MaskType maskType = MaskType.None, double timeoutMs = 1000, ImageStyle imageStyle = ImageStyle.Default)
{
ProgressHUD.For(forWindow).ShowInfoWithStatus(status, maskType, timeoutMs, imageStyle);
ProgressHUD.For(forWindow)?.ShowInfoWithStatus(status, maskType, timeoutMs, imageStyle);
}

public static void ShowImage(UIWindow forWindow, UIImage image, string? status, MaskType maskType = MaskType.None, double timeoutMs = 1000)
{
ProgressHUD.For(forWindow).ShowImage(image, status, maskType, timeoutMs);
ProgressHUD.For(forWindow)?.ShowImage(image, status, maskType, timeoutMs);
}

public static void Dismiss(UIWindow forWindow)
{
ProgressHUD.For(forWindow).Dismiss();
ProgressHUD.For(forWindow)?.Dismiss();
}

public static bool GetIsVisible(UIWindow forWindow)
=> ProgressHUD.For(forWindow).IsVisible;
=> ProgressHUD.For(forWindow)?.IsVisible ?? false;
}
}

50 changes: 7 additions & 43 deletions BTProgressHUD/ProgressHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using CoreAnimation;
using CoreGraphics;
using Foundation;
Expand Down Expand Up @@ -175,11 +174,11 @@ public UIImage InfoOutlineFullImage

static Dictionary<NativeHandle, ProgressHUD> windowHuds = new ();

public static ProgressHUD For(UIWindow window)
public static ProgressHUD? For(UIWindow? window)
{
ProgressHUD? hud = null;

window.InvokeOnMainThread(() =>
window?.InvokeOnMainThread(() =>
{
var handle = window.Handle;

Expand All @@ -189,26 +188,19 @@ public static ProgressHUD For(UIWindow window)
hud = windowHuds[handle];
});

return hud!;
return hud;
}

public static ProgressHUD ForDefaultWindow()
public static ProgressHUD? ForDefaultWindow()
{
UIWindow? window = null;

if (OperatingSystem.IsMacCatalystVersionAtLeast(15) || OperatingSystem.IsIOSVersionAtLeast(15))
{
foreach (var scene in UIApplication.SharedApplication.ConnectedScenes)
{
if (scene is UIWindowScene windowScene)
{
window = windowScene.KeyWindow;

if (window is null)
{
window = windowScene?.Windows?.LastOrDefault();
}
}
if (scene is not UIWindowScene windowScene) continue;
window = windowScene.KeyWindow ?? windowScene.Windows?.LastOrDefault();
}
}
else if (OperatingSystem.IsMacCatalystVersionAtLeast(13) || OperatingSystem.IsIOSVersionAtLeast(13))
Expand All @@ -221,7 +213,7 @@ public static ProgressHUD ForDefaultWindow()
?? UIApplication.SharedApplication.Windows?.LastOrDefault();
}

return For(window!);
return For(window);
}

public float RingRadius { get; set; } = 14f;
Expand Down Expand Up @@ -1149,33 +1141,5 @@ public static bool IsLandscape(UIInterfaceOrientation orientation) =>

public static bool IsPortrait(UIInterfaceOrientation orientation) =>
orientation is UIInterfaceOrientation.Portrait or UIInterfaceOrientation.PortraitUpsideDown;

//private static UIWindow? GetActiveWindow()
//{
// if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
// {
// var scene = UIApplication.SharedApplication.ConnectedScenes.ToArray()
// .OfType<UIWindowScene>()
// .FirstOrDefault(s =>
// s.ActivationState == UISceneActivationState.ForegroundActive || // scene in foreground or
// s.Windows.Any(w => w.IsKeyWindow)); // current shown window

// if (scene != null)
// return scene.Windows.FirstOrDefault(w => w.IsKeyWindow);
// }

// var windows = UIApplication.SharedApplication.Windows;
// var window = windows.LastOrDefault(w => w.WindowLevel == UIWindowLevel.Normal && !w.Hidden && w.IsKeyWindow);

// // As a last resort, use the first window.
// // In iOS 15, showing the HUD while the app is moving to the foreground sometimes
// // leads to this method getting called in a condition where
// // UIWindowScene.ActivationState == UISceneActivationStateForegroundInactive
// // and there is no window with IsKeyWindow == true
// if (window == null)
// window = windows.FirstOrDefault(w => w.WindowLevel == UIWindowLevel.Normal);

// return window ?? throw new Exception("Could not find active window");
//}
}
}

0 comments on commit 58efc84

Please sign in to comment.