Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customization of ProgressHUD through ProgressHUDAppearance #107

Merged
merged 7 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions BTProgressHUD/BTProgressHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ public static void Dismiss()
public static bool 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);
Expand Down
87 changes: 29 additions & 58 deletions BTProgressHUD/ProgressHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@

private static readonly NSObject obj = new();

private UIImage? _errorImage;
private UIImage? _successImage;
private UIImage? _infoImage;
private UIImage? _errorOutlineImage;
private UIImage? _successOutlineImage;
private UIImage? _infoOutlineImage;
private UIImage? _errorOutlineFullImage;
private UIImage? _successOutlineFullImage;
private UIImage? _infoOutlineFullImage;
private UIImage? _errorImage = ProgressHUDAppearance.ErrorImage;
private UIImage? _successImage = ProgressHUDAppearance.SuccessImage;
private UIImage? _infoImage = ProgressHUDAppearance.InfoImage;
private UIImage? _errorOutlineImage = ProgressHUDAppearance.ErrorOutlineImage;
private UIImage? _successOutlineImage = ProgressHUDAppearance.SuccessOutlineImage;
private UIImage? _infoOutlineImage = ProgressHUDAppearance.InfoOutlineImage;
private UIImage? _errorOutlineFullImage = ProgressHUDAppearance.ErrorOutlineFullImage;
private UIImage? _successOutlineFullImage = ProgressHUDAppearance.SuccessOutlineFullImage;
private UIImage? _infoOutlineFullImage = ProgressHUDAppearance.InfoOutlineFullImage;

private MaskType _maskType;
private NSTimer? _fadeoutTimer;
Expand Down Expand Up @@ -100,22 +100,12 @@
BackgroundColor = UIColor.Clear;
Alpha = 0;
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;

SetOSSpecificLookAndFeel();
}

public UIWindow HudWindow { get; private set; }

public static CGRect KeyboardSize { get; private set; } = CGRect.Empty;

public UIColor HudBackgroundColour { get; set; } = UIColor.FromWhiteAlpha(0.0f, 0.8f);
public UIColor HudForegroundColor { get; set; } = UIColor.White;
public UIColor HudStatusShadowColor { get; set; } = UIColor.Black;
public UIColor HudToastBackgroundColor { get; set; } = UIColor.Clear;
public UIFont HudFont { get; set; } = UIFont.BoldSystemFontOfSize(16f);
public UITextAlignment HudTextAlignment { get; set; } = UITextAlignment.Center;
public Ring Ring { get; } = new();

public UIImage ErrorImage
{
get => _errorImage ?? ImageHelper.ErrorImage.Value!;
Expand Down Expand Up @@ -197,10 +187,10 @@

if (OperatingSystem.IsMacCatalystVersionAtLeast(15) || OperatingSystem.IsIOSVersionAtLeast(15))
{
foreach (var scene in UIApplication.SharedApplication.ConnectedScenes)

Check warning on line 190 in BTProgressHUD/ProgressHUD.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'iOS' 11.0 and later, 'maccatalyst' 15.0 and later. 'UIApplication.ConnectedScenes' is only supported on: 'ios' 13.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
{
if (scene is not UIWindowScene windowScene) continue;
window = windowScene.KeyWindow ?? windowScene.Windows?.LastOrDefault();

Check warning on line 193 in BTProgressHUD/ProgressHUD.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'iOS' 11.0 and later, 'maccatalyst' 15.0 and later. 'UIWindowScene.Windows' is only supported on: 'ios' 13.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}
}
else if (OperatingSystem.IsMacCatalystVersionAtLeast(13) || OperatingSystem.IsIOSVersionAtLeast(13))
Expand All @@ -215,9 +205,6 @@

return For(window);
}

public float RingRadius { get; set; } = 14f;
public float RingThickness { get; set; } = 6f;

CAShapeLayer RingLayer
{
Expand All @@ -226,7 +213,8 @@
if (_ringLayer == null)
{
var center = new CGPoint(HudView.Frame.Width / 2, HudView.Frame.Height / 2);
_ringLayer = ShapeHelper.CreateRingLayer(center, RingRadius, RingThickness, Ring.Color);
_ringLayer = ShapeHelper.CreateRingLayer(center, ProgressHUDAppearance.RingRadius,
ProgressHUDAppearance.RingThickness, ProgressHUDAppearance.RingColor);
HudView.Layer.AddSublayer(_ringLayer);
}
return _ringLayer;
Expand All @@ -240,7 +228,8 @@
if (_backgroundRingLayer == null)
{
var center = new CGPoint(HudView.Frame.Width / 2, HudView.Frame.Height / 2);
_backgroundRingLayer = ShapeHelper.CreateRingLayer(center, RingRadius, RingThickness, Ring.BackgroundColor);
_backgroundRingLayer = ShapeHelper.CreateRingLayer(center, ProgressHUDAppearance.RingRadius,
ProgressHUDAppearance.RingThickness, ProgressHUDAppearance.RingBackgroundColor);
_backgroundRingLayer.StrokeEnd = 1;
HudView.Layer.AddSublayer(_backgroundRingLayer);
}
Expand Down Expand Up @@ -269,13 +258,13 @@
var hudView = new UIToolbar
{
Translucent = true,
BarTintColor = HudBackgroundColour,
BackgroundColor = HudBackgroundColour,
BarTintColor = ProgressHUDAppearance.HudBackgroundColor,
BackgroundColor = ProgressHUDAppearance.HudBackgroundColor,
AutoresizingMask =
UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin |
UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin
};
hudView.Layer.CornerRadius = 10;
hudView.Layer.CornerRadius = ProgressHUDAppearance.HudCornerRadius;
hudView.Layer.MasksToBounds = true;

AddSubview(hudView);
Expand All @@ -292,12 +281,12 @@
{
_stringLabel ??= new UILabel
{
BackgroundColor = HudToastBackgroundColor,
BackgroundColor = ProgressHUDAppearance.HudToastBackgroundColor,
AdjustsFontSizeToFitWidth = true,
TextAlignment = HudTextAlignment,
TextAlignment = ProgressHUDAppearance.HudTextAlignment,
BaselineAdjustment = UIBaselineAdjustment.AlignCenters,
TextColor = HudForegroundColor,
Font = HudFont,
TextColor = ProgressHUDAppearance.HudTextColor,
Font = ProgressHUDAppearance.HudFont,
Lines = 0
};

Expand All @@ -321,9 +310,8 @@
BackgroundColor = UIColor.Clear,
UserInteractionEnabled = true
};
_cancelHud.TitleLabel.Font = HudFont;

_cancelHud.SetTitleColor(HudForegroundColor, UIControlState.Normal);
_cancelHud.TitleLabel.Font = ProgressHUDAppearance.HudButtonFont;
_cancelHud.SetTitleColor(ProgressHUDAppearance.HudButtonTextColor, UIControlState.Normal);
UserInteractionEnabled = true;
}

Expand Down Expand Up @@ -364,11 +352,14 @@
{
get
{
_spinnerView ??= new UIActivityIndicatorView(UIActivityIndicatorViewStyle.WhiteLarge)
_spinnerView ??= new UIActivityIndicatorView(
OperatingSystem.IsMacCatalystVersionAtLeast(13, 1) || OperatingSystem.IsIOSVersionAtLeast(13) ?
UIActivityIndicatorViewStyle.Large :

Check warning on line 357 in BTProgressHUD/ProgressHUD.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'iOS' 11.0 and later, 'maccatalyst' 13.0 and later. 'UIActivityIndicatorViewStyle.Large' is only supported on: 'ios' 13.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
UIActivityIndicatorViewStyle.WhiteLarge)
{
HidesWhenStopped = true,
Bounds = new CGRect(0, 0, 37, 37),
Color = HudForegroundColor
Color = ProgressHUDAppearance.RingColor
};

// ReSharper disable once ConditionIsAlwaysTrueOrFalse
Expand All @@ -379,20 +370,6 @@
}
}

private void SetOSSpecificLookAndFeel()
{
HudBackgroundColour =
(System.OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
? UIColor.SystemBackground.ColorWithAlpha(0.8f) : UIColor.White.ColorWithAlpha(0.8f);
HudForegroundColor =
(System.OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
? UIColor.Label.ColorWithAlpha(0.8f) : UIColor.FromWhiteAlpha(0.0f, 0.8f);
HudStatusShadowColor =
(System.OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
? UIColor.Label.ColorWithAlpha(0.8f) : UIColor.FromWhiteAlpha(200f / 255f, 0.8f);
RingThickness = 1f;
}

public void Show(string? status = null, float progress = -1, MaskType maskType = MaskType.None, double timeoutMs = 1000)
{
obj.InvokeOnMainThread(() => ShowProgressWorker(progress, status, maskType, timeoutMs: timeoutMs));
Expand Down Expand Up @@ -510,9 +487,6 @@
ToastPosition toastPosition = ToastPosition.Center, string? cancelCaption = null, Action? cancelCallback = null,
double timeoutMs = 1000, bool showContinuousProgress = false, UIImage? displayContinuousImage = null)
{
if (TintColor != null)
Ring.ResetStyle(TintColor);

// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (OverlayView.Superview == null)
{
Expand Down Expand Up @@ -557,7 +531,7 @@
}

RingLayer.StrokeEnd = 0.0f;
StartProgressTimer(TimeSpan.FromMilliseconds(Ring.ProgressUpdateInterval));
StartProgressTimer(TimeSpan.FromMilliseconds(ProgressHUDAppearance.RingProgressUpdateInterval));
}
else
{
Expand Down Expand Up @@ -651,7 +625,7 @@
Show(null, -1F, maskType);
}

ImageView.TintColor = HudForegroundColor;
ImageView.TintColor = ProgressHUDAppearance.HudImageTintColor;
ImageView.Image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
ImageView.Hidden = false;
StringLabel.Text = status;
Expand Down Expand Up @@ -762,9 +736,6 @@
UnRegisterNotifications();
NSNotificationCenter.DefaultCenter.RemoveObserver(this);

if (TintColor != null)
Ring.ResetStyle(TintColor);

CancelRingLayerAnimation();
StringLabel.RemoveFromSuperview();
SpinnerView.RemoveFromSuperview();
Expand Down
132 changes: 132 additions & 0 deletions BTProgressHUD/ProgressHUDAppearance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using UIKit;

namespace BigTed;

public static class ProgressHUDAppearance
{
public static UIColor DefaultHudBackgroundColor { get; } =
OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13) ?
UIColor.SystemBackground.ColorWithAlpha(0.8f) :

Check warning on line 10 in BTProgressHUD/ProgressHUDAppearance.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'iOS' 11.0 and later, 'maccatalyst' 13.0 and later. 'UIColor.SystemBackground' is only supported on: 'ios' 13.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 10 in BTProgressHUD/ProgressHUDAppearance.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'iOS' 11.0 and later, 'maccatalyst' 13.0 and later. 'UIColor.SystemBackground' is only supported on: 'ios' 13.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
UIColor.White.ColorWithAlpha(0.8f);

public static UIColor DefaultForegroundColor { get; } =
OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13) ?
UIColor.Label.ColorWithAlpha(0.8f) :

Check warning on line 15 in BTProgressHUD/ProgressHUDAppearance.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'iOS' 11.0 and later, 'maccatalyst' 13.0 and later. 'UIColor.Label' is only supported on: 'ios' 13.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 15 in BTProgressHUD/ProgressHUDAppearance.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'iOS' 11.0 and later, 'maccatalyst' 13.0 and later. 'UIColor.Label' is only supported on: 'ios' 13.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
UIColor.FromWhiteAlpha(0.0f, 0.8f);

public static UIColor DefaultHudToastBackgroundColor { get; } = UIColor.Clear;
public static UIFont DefaultHudFont { get; } = UIFont.BoldSystemFontOfSize(16f);
public static UITextAlignment DefaultHudTextAlignment { get; } = UITextAlignment.Center;

public static UIColor DefaultRingColor = UIColor.SystemBlue;

public static UIColor DefaultRingBackgroundColor =
OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13)
? UIColor.SystemBackground

Check warning on line 26 in BTProgressHUD/ProgressHUDAppearance.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'iOS' 11.0 and later, 'maccatalyst' 13.0 and later. 'UIColor.SystemBackground' is only supported on: 'ios' 13.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 26 in BTProgressHUD/ProgressHUDAppearance.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'iOS' 11.0 and later, 'maccatalyst' 13.0 and later. 'UIColor.SystemBackground' is only supported on: 'ios' 13.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
: DefaultHudBackgroundColor;

public const float DefaultRingRadius = 14f;
public const float DefaultRingThickness = 1f;
public const float DefaultHudCornerRadius = 10f;
public const double DefaultProgressUpdateInterval = 300;

/// <summary>
/// Get or set definite progress indicator ring radius to control its size
/// </summary>
public static float RingRadius { get; set; } = DefaultRingRadius;

/// <summary>
/// Get or set definite progress indicator ring stroke thickness
/// </summary>
public static float RingThickness { get; set; } = DefaultRingThickness;

/// <summary>
/// Get or set update interval for definite progress indicator ring animation
/// </summary>
public static double RingProgressUpdateInterval { get; set; } = DefaultProgressUpdateInterval;

/// <summary>
/// Get or set definite progress indicator ring foreground color
/// </summary>
public static UIColor RingColor { get; set; } = DefaultRingColor;

/// <summary>
/// Get or set definite progress indicator ring background color
/// </summary>
public static UIColor RingBackgroundColor { get; set; } = DefaultRingBackgroundColor;

/// <summary>
/// Get or set hud corner radius
/// </summary>
public static float HudCornerRadius { get; set; } = DefaultHudCornerRadius;

/// <summary>
/// Get or set hud background color
/// </summary>
public static UIColor HudBackgroundColor { get; set; } = DefaultHudBackgroundColor;

/// <summary>
/// Get or set image tint color
/// </summary>
public static UIColor HudImageTintColor { get; set; } = DefaultForegroundColor;

/// <summary>
/// Get or set background color of toast
/// </summary>
public static UIColor HudToastBackgroundColor { get; set; } = DefaultHudToastBackgroundColor;

/// <summary>
/// Get or set font used for texts shown in hud
/// </summary>
public static UIFont HudFont { get; set; } = DefaultHudFont;

/// <summary>
/// Get or set color of texts shown in hud
/// </summary>
public static UIColor HudTextColor { get; set; } = DefaultForegroundColor;

/// <summary>
/// Get or set font to use for cancel button
/// </summary>
public static UIFont HudButtonFont { get; set; } = DefaultHudFont;

/// <summary>
/// Get or set text color on cancel button
/// </summary>
public static UIColor HudButtonTextColor { get; set; } = DefaultForegroundColor;

/// <summary>
/// Get or set alignment on texts shown in hud
/// </summary>
public static UITextAlignment HudTextAlignment { get; set; } = DefaultHudTextAlignment;

public static UIImage? ErrorImage { get; set; }
public static UIImage? SuccessImage { get; set; }
public static UIImage? InfoImage { get; set; }
public static UIImage? ErrorOutlineImage { get; set; }
public static UIImage? SuccessOutlineImage { get; set; }
public static UIImage? InfoOutlineImage { get; set; }
public static UIImage? ErrorOutlineFullImage { get; set; }
public static UIImage? SuccessOutlineFullImage { get; set; }
public static UIImage? InfoOutlineFullImage { get; set; }

public static void ResetToDefaults()
{
RingRadius = DefaultRingRadius;
RingThickness = DefaultRingThickness;
RingProgressUpdateInterval = DefaultProgressUpdateInterval;
RingColor = DefaultRingColor;
RingBackgroundColor = DefaultRingBackgroundColor;

HudCornerRadius = DefaultHudCornerRadius;
HudBackgroundColor = DefaultHudBackgroundColor;
HudImageTintColor = DefaultForegroundColor;
HudToastBackgroundColor = DefaultHudToastBackgroundColor;
HudFont = DefaultHudFont;
HudButtonFont = DefaultHudFont;
HudTextColor = DefaultForegroundColor;
HudButtonTextColor = DefaultForegroundColor;
HudTextAlignment = DefaultHudTextAlignment;
}
}
36 changes: 0 additions & 36 deletions BTProgressHUD/Ring.cs

This file was deleted.

Loading
Loading