Skip to content

Commit

Permalink
Split some styles and make them resettable
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheesebaron committed May 13, 2024
1 parent 21fdab8 commit 4d1adaa
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 88 deletions.
52 changes: 20 additions & 32 deletions BTProgressHUD/ProgressHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ public ProgressHUD(UIWindow window) : base(window.Bounds)

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

public UIColor HudBackgroundColour { get; set; } = ProgressHUDAppearance.HudBackgroundColour;
public UIColor HudForegroundColor { get; set; } = ProgressHUDAppearance.HudForegroundColor;
public UIColor HudToastBackgroundColor { get; set; } = ProgressHUDAppearance.HudToastBackgroundColor;
public UIFont HudFont { get; set; } = ProgressHUDAppearance.HudFont;
public UITextAlignment HudTextAlignment { get; set; } = ProgressHUDAppearance.HudTextAlignment;
public Ring Ring { get; } = new();

public UIImage ErrorImage
{
get => _errorImage ?? ImageHelper.ErrorImage.Value!;
Expand Down Expand Up @@ -212,9 +205,6 @@ public UIImage InfoOutlineFullImage

return For(window);
}

public float RingRadius { get; set; } = ProgressHUDAppearance.RingRadius;
public float RingThickness { get; set; } = ProgressHUDAppearance.RingThickness;

CAShapeLayer RingLayer
{
Expand All @@ -223,7 +213,8 @@ CAShapeLayer RingLayer
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 @@ -237,7 +228,8 @@ CAShapeLayer RingLayer
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 @@ -266,13 +258,13 @@ UIView HudView
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 @@ -289,12 +281,12 @@ UILabel StringLabel
{
_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 @@ -318,9 +310,8 @@ UIButton CancelHudButton
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 @@ -361,11 +352,14 @@ UIActivityIndicatorView SpinnerView
{
get
{
_spinnerView ??= new UIActivityIndicatorView(UIActivityIndicatorViewStyle.WhiteLarge)
_spinnerView ??= new UIActivityIndicatorView(
OperatingSystem.IsMacCatalystVersionAtLeast(13, 1) || OperatingSystem.IsIOSVersionAtLeast(13) ?
UIActivityIndicatorViewStyle.Large :
UIActivityIndicatorViewStyle.WhiteLarge)
{
HidesWhenStopped = true,
Bounds = new CGRect(0, 0, 37, 37),
Color = HudForegroundColor
Color = ProgressHUDAppearance.RingColor
};

// ReSharper disable once ConditionIsAlwaysTrueOrFalse
Expand Down Expand Up @@ -493,9 +487,6 @@ private void ShowProgressWorker(
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 @@ -540,7 +531,7 @@ private void ShowProgressWorker(
}

RingLayer.StrokeEnd = 0.0f;
StartProgressTimer(TimeSpan.FromMilliseconds(Ring.ProgressUpdateInterval));
StartProgressTimer(TimeSpan.FromMilliseconds(ProgressHUDAppearance.RingProgressUpdateInterval));
}
else
{
Expand Down Expand Up @@ -634,7 +625,7 @@ private void ShowImageWorker(UIImage image, string? status, MaskType maskType, T
Show(null, -1F, maskType);
}

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

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

CancelRingLayerAnimation();
StringLabel.RemoveFromSuperview();
SpinnerView.RemoveFromSuperview();
Expand Down
103 changes: 101 additions & 2 deletions BTProgressHUD/ProgressHUDAppearance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace BigTed;

public static class ProgressHUDAppearance
{
public static UIColor DefaultHudBackgroundColour { get; } =
public static UIColor DefaultHudBackgroundColor { get; } =
OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13) ?
UIColor.SystemBackground.ColorWithAlpha(0.8f) :
UIColor.White.ColorWithAlpha(0.8f);
Expand All @@ -15,20 +15,100 @@ public static class ProgressHUDAppearance
UIColor.Label.ColorWithAlpha(0.8f) :
UIColor.FromWhiteAlpha(0.0f, 0.8f);

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

public static UIColor DefaultHudButtonTextColor { get; } =
OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13) ?
UIColor.Label.ColorWithAlpha(0.8f) :
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
: UIColor.White;

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;

public static UIColor HudBackgroundColour { get; set; } = DefaultHudBackgroundColour;
/// <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 HudForegroundColor { get; set; } = DefaultHudForegroundColor;

/// <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; } = DefaultHudTextColor;

/// <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; } = DefaultHudButtonTextColor;

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

public static UIImage? ErrorImage { get; set; }
Expand All @@ -40,4 +120,23 @@ public static class ProgressHUDAppearance
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;
HudForegroundColor = DefaultHudForegroundColor;
HudToastBackgroundColor = DefaultHudToastBackgroundColor;
HudFont = DefaultHudFont;
HudButtonFont = DefaultHudFont;
HudTextColor = DefaultHudTextColor;
HudButtonTextColor = DefaultHudButtonTextColor;
HudTextAlignment = DefaultHudTextAlignment;
}
}
36 changes: 0 additions & 36 deletions BTProgressHUD/Ring.cs

This file was deleted.

41 changes: 25 additions & 16 deletions BTProgressHUDDemo/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Windows.Input;
using BigTed;
using Foundation;
using UIKit;

namespace BTProgressHUDDemo2
{
Expand All @@ -12,13 +13,18 @@ public class MainViewModel

public MainViewModel()
{
this.Items = new[]
{
Items =
[
Create("Dismiss", () =>
{
BTProgressHUD.Dismiss();
}, false),
Create("Show", () => BTProgressHUD.Show(), true),
Create("Cancel problem 3", () => BTProgressHUD.Show("Cancel", () => KillAfter(), "Cancel and text"), false),
Create("Cancel problem 2", () => BTProgressHUD.Show("Cancel", () => KillAfter()), false),
Create("Cancel problem", () => BTProgressHUD.Show("Cancel", () => KillAfter(), "This is a multilinetext\nSome more text\n more text\n and again more text"), false),
Create("Show Message", () => BTProgressHUD.Show("Processing your image", -1, MaskType.Black), false),
Create("Show Message", () =>
BTProgressHUD.Show("Processing your image", 10, MaskType.Black), true),
Create("Show Success", () =>
{
BTProgressHUD.ShowSuccessWithStatus("Great success!");
Expand Down Expand Up @@ -68,12 +74,6 @@ public MainViewModel()
{
BTProgressHUD.ShowToast("Hello from the toast", false, 1000);
}, false),

Create("Dismiss", () =>
{
BTProgressHUD.Dismiss();
}, false),

Create("Progress", () =>
{
progress = 0;
Expand All @@ -98,12 +98,21 @@ public MainViewModel()
});
NSRunLoop.Current.AddTimer(timer, NSRunLoopMode.Common);
}, false),

Create("Dismiss", () =>
{
BTProgressHUD.Dismiss();
}, false)
};
Create("Customize", () =>
{
ProgressHUDAppearance.RingColor = UIColor.Green;
ProgressHUDAppearance.RingBackgroundColor = UIColor.Brown;

ProgressHUDAppearance.HudBackgroundColor = UIColor.Yellow;
ProgressHUDAppearance.HudTextColor = UIColor.Purple;
ProgressHUDAppearance.HudButtonTextColor = UIColor.Orange;
ProgressHUDAppearance.HudCornerRadius = 2;
ProgressHUDAppearance.HudTextAlignment = UITextAlignment.Left;
ProgressHUDAppearance.HudTextColor = UIColor.Cyan;
ProgressHUDAppearance.HudToastBackgroundColor = UIColor.Blue;
}, false),
Create("Reset Customization", ProgressHUDAppearance.ResetToDefaults, false)
];
}


Expand Down Expand Up @@ -147,4 +156,4 @@ public record CommandItem(
string Text,
ICommand Command
);
}
}
Loading

0 comments on commit 4d1adaa

Please sign in to comment.