-
Notifications
You must be signed in to change notification settings - Fork 64
/
ProgressHUD.cs
1077 lines (903 loc) · 31.1 KB
/
ProgressHUD.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// BTProgressHUD - port of SVProgressHUD
//
// https://github.com/nicwise/BTProgressHUD
//
// Ported by Nic Wise -
// Copyright 2013 Nic Wise. MIT license.
//
// SVProgressHUD.m
//
// Created by Sam Vermette on 27.03.11.
// Copyright 2011 Sam Vermette. All rights reserved.
//
// https://github.com/samvermette/SVProgressHUD
//
// Version 1.6.1
using System;
using System.Collections.Generic;
#if __UNIFIED__
using UIKit;
using Foundation;
using CoreAnimation;
using CoreGraphics;
using ObjCRuntime;
#else
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.CoreAnimation;
using MonoTouch.CoreGraphics;
using MonoTouch.ObjCRuntime;
using nfloat = System.Single;
using System.Drawing;
using CGRect = global::System.Drawing.RectangleF;
using CGPoint = global::System.Drawing.PointF;
using CGSize = global::System.Drawing.SizeF;
#endif
namespace BigTed
{
public class ProgressHUD : UIView
{
static Class clsUIPeripheralHostView = null;
static Class clsUIKeyboard = null;
static Class clsUIInputSetContainerView = null;
static Class clsUIInputSetHostView = null;
static ProgressHUD ()
{
//initialize static fields used for input view detection
var ptrUIPeripheralHostView = Class.GetHandle("UIPeripheralHostView");
if (ptrUIPeripheralHostView != IntPtr.Zero)
clsUIPeripheralHostView = new Class (ptrUIPeripheralHostView);
var ptrUIKeyboard = Class.GetHandle("UIKeyboard");
if (ptrUIKeyboard != IntPtr.Zero)
clsUIKeyboard = new Class (ptrUIKeyboard);
var ptrUIInputSetContainerView = Class.GetHandle("UIInputSetContainerView");
if (ptrUIInputSetContainerView != IntPtr.Zero)
clsUIInputSetContainerView = new Class (ptrUIInputSetContainerView);
var ptrUIInputSetHostView = Class.GetHandle("UIInputSetHostView");
if (ptrUIInputSetHostView != IntPtr.Zero)
clsUIInputSetHostView = new Class (ptrUIInputSetHostView);
}
public ProgressHUD () : this (UIScreen.MainScreen.Bounds)
{
}
public ProgressHUD (CGRect frame) : base (frame)
{
UserInteractionEnabled = false;
BackgroundColor = UIColor.Clear;
Alpha = 0;
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
SetOSSpecificLookAndFeel ();
}
public void SetOSSpecificLookAndFeel ()
{
if (IsiOS7ForLookAndFeel) {
HudBackgroundColour = UIColor.White.ColorWithAlpha (0.8f);
HudForegroundColor = UIColor.FromWhiteAlpha (0.0f, 0.8f);
HudStatusShadowColor = UIColor.FromWhiteAlpha (200f / 255f, 0.8f);
_ringThickness = 1f;
} else {
HudBackgroundColour = UIColor.FromWhiteAlpha (0.0f, 0.8f);
HudForegroundColor = UIColor.White;
HudStatusShadowColor = UIColor.Black;
_ringThickness = 6f;
}
}
public enum MaskType
{
None = 1,
Clear,
Black,
Gradient
}
public enum ToastPosition
{
Bottom = 1,
Center,
Top
}
public UIColor HudBackgroundColour = UIColor.FromWhiteAlpha (0.0f, 0.8f);
public UIColor HudForegroundColor = UIColor.White;
public UIColor HudStatusShadowColor = UIColor.Black;
public UIColor HudToastBackgroundColor = UIColor.Clear;
public UIFont HudFont = UIFont.BoldSystemFontOfSize (16f);
public UITextAlignment HudTextAlignment = UITextAlignment.Center;
public Ring Ring = new Ring ();
static NSObject obj = new NSObject ();
public void Show (string status = null, float progress = -1, MaskType maskType = MaskType.None, double timeoutMs = 1000)
{
obj.InvokeOnMainThread (() => ShowProgressWorker (progress, status, maskType, timeoutMs: timeoutMs));
}
public void Show (string cancelCaption, Action cancelCallback, string status = null,
float progress = -1, MaskType maskType = MaskType.None, double timeoutMs = 1000)
{
// Making cancelCaption optional hides the method via the overload
if (string.IsNullOrEmpty (cancelCaption)) {
cancelCaption = "Cancel";
}
obj.InvokeOnMainThread (() => ShowProgressWorker (progress, status, maskType,
cancelCaption: cancelCaption, cancelCallback: cancelCallback, timeoutMs: timeoutMs));
}
public void ShowContinuousProgress (string status = null, MaskType maskType = MaskType.None, double timeoutMs = 1000, UIImage img = null)
{
obj.InvokeOnMainThread (() => ShowProgressWorker (0, status, maskType, false, ToastPosition.Center, null, null, timeoutMs, true, img));
}
public void ShowContinuousProgressTest (string status = null, MaskType maskType = MaskType.None, double timeoutMs = 1000)
{
obj.InvokeOnMainThread (() => ShowProgressWorker (0, status, maskType, false, ToastPosition.Center, null, null, timeoutMs, true));
}
public void ShowToast (string status, MaskType maskType = MaskType.None, ToastPosition toastPosition = ToastPosition.Center, double timeoutMs = 1000)
{
obj.InvokeOnMainThread (() => ShowProgressWorker (status: status, textOnly: true, toastPosition: toastPosition, timeoutMs: timeoutMs, maskType: maskType));
}
public void SetStatus (string status)
{
obj.InvokeOnMainThread (() => SetStatusWorker (status));
}
public void ShowSuccessWithStatus (string status, double timeoutMs = 1000)
{
ShowImage (SuccessImage, status, timeoutMs);
}
public void ShowErrorWithStatus (string status, double timeoutMs = 1000)
{
ShowImage (ErrorImage, status, timeoutMs);
}
public void ShowImage (UIImage image, string status, double timeoutMs = 1000)
{
obj.InvokeOnMainThread (() => ShowImageWorker (image, status, TimeSpan.FromMilliseconds (timeoutMs)));
}
public void Dismiss ()
{
obj.InvokeOnMainThread (DismissWorker);
}
public UIImage ErrorImage {
get {
return (IsiOS7ForLookAndFeel ? UIImage.FromBundle ("error_7.png") : UIImage.FromBundle ("error.png"));
}
}
public UIImage SuccessImage {
get {
return (IsiOS7ForLookAndFeel ? UIImage.FromBundle ("success_7.png") : UIImage.FromBundle ("success.png"));
}
}
public bool IsVisible {
get {
return Alpha == 1;
}
}
static ProgressHUD sharedHUD = null;
public static ProgressHUD Shared {
get {
if (sharedHUD == null) {
UIApplication.EnsureUIThread ();
sharedHUD = new ProgressHUD (UIScreen.MainScreen.Bounds);
}
return sharedHUD;
}
}
float _ringRadius = 14f;
float _ringThickness = 6f;
MaskType _maskType;
NSTimer _fadeoutTimer;
UIView _overlayView;
UIView _hudView;
UILabel _stringLabel;
UIImageView _imageView;
UIActivityIndicatorView _spinnerView;
UIButton _cancelHud;
NSTimer _progressTimer;
float _progress;
CAShapeLayer _backgroundRingLayer;
CAShapeLayer _ringLayer;
List<NSObject> _eventListeners;
bool _displayContinuousImage;
public float RingRadius {
get { return _ringRadius; }
set { _ringRadius = value; }
}
public float RingThickness {
get { return _ringThickness; }
set { _ringThickness = value; }
}
public override void Draw (CGRect rect)
{
using (var context = UIGraphics.GetCurrentContext ()) {
switch (_maskType) {
case MaskType.Black:
UIColor.FromWhiteAlpha (0f, 0.5f).SetColor ();
context.FillRect (Bounds);
break;
case MaskType.Gradient:
nfloat[] colors = new nfloat[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.75f };
nfloat[] locations = new nfloat[] { 0.0f, 1.0f };
using (var colorSpace = CGColorSpace.CreateDeviceRGB ()) {
using (var gradient = new CGGradient (colorSpace, colors, locations)) {
var center = new CGPoint (Bounds.Size.Width / 2, Bounds.Size.Height / 2);
float radius = Math.Min ((float)Bounds.Size.Width, (float)Bounds.Size.Height);
context.DrawRadialGradient (gradient, center, 0, center, radius, CGGradientDrawingOptions.DrawsAfterEndLocation);
}
}
break;
}
}
}
void ShowProgressWorker (float progress = -1, string status = null, MaskType maskType = MaskType.None, bool textOnly = false,
ToastPosition toastPosition = ToastPosition.Center, string cancelCaption = null, Action cancelCallback = null,
double timeoutMs = 1000, bool showContinuousProgress = false, UIImage displayContinuousImage = null)
{
Ring.ResetStyle(IsiOS7ForLookAndFeel, (IsiOS7ForLookAndFeel ? TintColor : UIColor.White));
if (OverlayView.Superview == null) {
var windows = UIApplication.SharedApplication.Windows;
Array.Reverse (windows);
foreach (UIWindow window in windows) {
if (window.WindowLevel == UIWindowLevel.Normal && !window.Hidden && window.IsKeyWindow) {
window.AddSubview (OverlayView);
break;
}
}
}
if (Superview == null)
OverlayView.AddSubview (this);
_fadeoutTimer = null;
ImageView.Hidden = true;
_maskType = maskType;
_progress = progress;
StringLabel.Text = status;
if (!string.IsNullOrEmpty (cancelCaption)) {
CancelHudButton.SetTitle (cancelCaption, UIControlState.Normal);
CancelHudButton.TouchUpInside += delegate {
Dismiss ();
if (cancelCallback != null) {
obj.InvokeOnMainThread (() => cancelCallback.DynamicInvoke (null));
//cancelCallback.DynamicInvoke(null);
}
};
}
UpdatePosition (textOnly);
if (showContinuousProgress) {
if (displayContinuousImage != null) {
_displayContinuousImage = true;
ImageView.Image = displayContinuousImage;
ImageView.Hidden = false;
}
RingLayer.StrokeEnd = 0.0f;
StartProgressTimer (TimeSpan.FromMilliseconds (Ring.ProgressUpdateInterval));
} else {
if (progress >= 0) {
ImageView.Image = null;
ImageView.Hidden = false;
SpinnerView.StopAnimating ();
RingLayer.StrokeEnd = progress;
} else if (textOnly) {
CancelRingLayerAnimation ();
SpinnerView.StopAnimating ();
} else {
CancelRingLayerAnimation ();
SpinnerView.StartAnimating ();
}
}
bool cancelButtonVisible = _cancelHud != null && _cancelHud.IsDescendantOfView (_hudView);
// intercept user interaction with the underlying view
if (maskType != MaskType.None || cancelButtonVisible) {
OverlayView.UserInteractionEnabled = true;
//AccessibilityLabel = status;
//IsAccessibilityElement = true;
} else {
OverlayView.UserInteractionEnabled = false;
//hudView.IsAccessibilityElement = true;
}
OverlayView.Hidden = false;
this.toastPosition = toastPosition;
PositionHUD (null);
if (Alpha != 1) {
RegisterNotifications ();
HudView.Transform.Scale (1.3f, 1.3f);
if (isClear) {
Alpha = 1f;
HudView.Alpha = 0f;
}
UIView.Animate (0.15f, 0,
UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.CurveEaseOut | UIViewAnimationOptions.BeginFromCurrentState,
delegate {
HudView.Transform.Scale ((float)1 / 1.3f, (float)1f / 1.3f);
if (isClear) {
HudView.Alpha = 1f;
} else {
Alpha = 1f;
}
}, delegate {
//UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, string);
if (textOnly)
StartDismissTimer (TimeSpan.FromMilliseconds (timeoutMs));
});
SetNeedsDisplay ();
}
}
void ShowImageWorker (UIImage image, string status, TimeSpan duration)
{
_progress = -1;
CancelRingLayerAnimation ();
//this should happen when Dismiss is called, but it happens AFTER the animation ends
// so sometimes, the cancel button is left on :(
if (_cancelHud != null) {
_cancelHud.RemoveFromSuperview ();
_cancelHud = null;
}
if (!IsVisible)
Show ();
ImageView.Image = image;
ImageView.Hidden = false;
StringLabel.Text = status;
UpdatePosition ();
SpinnerView.StopAnimating ();
StartDismissTimer (duration);
}
void StartDismissTimer (TimeSpan duration)
{
#if __UNIFIED__
_fadeoutTimer = NSTimer.CreateTimer (duration, timer => DismissWorker ());
#else
_fadeoutTimer = NSTimer.CreateTimer(duration, DismissWorker);
#endif
NSRunLoop.Main.AddTimer (_fadeoutTimer, NSRunLoopMode.Common);
}
void StartProgressTimer (TimeSpan duration)
{
if (_progressTimer == null) {
#if __UNIFIED__
_progressTimer = NSTimer.CreateRepeatingTimer (duration, timer => UpdateProgress ());
#else
_progressTimer = NSTimer.CreateRepeatingTimer(duration, UpdateProgress);
#endif
NSRunLoop.Current.AddTimer (_progressTimer, NSRunLoopMode.Common);
}
}
void StopProgressTimer ()
{
if (_progressTimer != null) {
_progressTimer.Invalidate ();
_progressTimer = null;
}
}
void UpdateProgress ()
{
obj.InvokeOnMainThread (delegate {
if (!_displayContinuousImage) {
ImageView.Image = null;
ImageView.Hidden = false;
}
SpinnerView.StopAnimating ();
if (RingLayer.StrokeEnd > 1) {
RingLayer.StrokeEnd = 0.0f;
} else {
RingLayer.StrokeEnd += 0.1f;
}
});
}
void CancelRingLayerAnimation ()
{
CATransaction.Begin ();
CATransaction.DisableActions = true;
HudView.Layer.RemoveAllAnimations ();
RingLayer.StrokeEnd = 0;
if (RingLayer.SuperLayer != null) {
RingLayer.RemoveFromSuperLayer ();
}
RingLayer = null;
if (BackgroundRingLayer.SuperLayer != null) {
BackgroundRingLayer.RemoveFromSuperLayer ();
}
BackgroundRingLayer = null;
CATransaction.Commit ();
}
CAShapeLayer RingLayer {
get {
if (_ringLayer == null) {
var center = new CGPoint (HudView.Frame.Width / 2, HudView.Frame.Height / 2);
_ringLayer = CreateRingLayer (center, _ringRadius, _ringThickness, Ring.Color);
HudView.Layer.AddSublayer (_ringLayer);
}
return _ringLayer;
}
set { _ringLayer = value; }
}
CAShapeLayer BackgroundRingLayer {
get {
if (_backgroundRingLayer == null) {
var center = new CGPoint (HudView.Frame.Width / 2, HudView.Frame.Height / 2);
_backgroundRingLayer = CreateRingLayer (center, _ringRadius, _ringThickness, Ring.BackgroundColor);
_backgroundRingLayer.StrokeEnd = 1;
HudView.Layer.AddSublayer (_backgroundRingLayer);
}
return _backgroundRingLayer;
}
set { _backgroundRingLayer = value; }
}
CGPoint PointOnCircle (CGPoint center, float radius, float angleInDegrees)
{
float x = radius * (float)Math.Cos (angleInDegrees * Math.PI / 180) + radius;
float y = radius * (float)Math.Sin (angleInDegrees * Math.PI / 180) + radius;
return new CGPoint (x, y);
}
UIBezierPath CreateCirclePath (CGPoint center, float radius, int sampleCount)
{
var smoothedPath = new UIBezierPath ();
CGPoint startPoint = PointOnCircle (center, radius, -90);
smoothedPath.MoveTo (startPoint);
float delta = 360 / sampleCount;
float angleInDegrees = -90;
for (int i = 1; i < sampleCount; i++) {
angleInDegrees += delta;
var point = PointOnCircle (center, radius, angleInDegrees);
smoothedPath.AddLineTo (point);
}
smoothedPath.AddLineTo (startPoint);
return smoothedPath;
}
CAShapeLayer CreateRingLayer (CGPoint center, float radius, float lineWidth, UIColor color)
{
var smoothedPath = CreateCirclePath (center, radius, 72);
var slice = new CAShapeLayer ();
slice.Frame = new CGRect (center.X - radius, center.Y - radius, radius * 2, radius * 2);
slice.FillColor = UIColor.Clear.CGColor;
slice.StrokeColor = color.CGColor;
slice.LineWidth = lineWidth;
slice.LineCap = CAShapeLayer.JoinBevel;
slice.LineJoin = CAShapeLayer.JoinBevel;
slice.Path = smoothedPath.CGPath;
return slice;
}
bool isClear {
get {
return (_maskType == ProgressHUD.MaskType.Clear || _maskType == ProgressHUD.MaskType.None);
}
}
UIView OverlayView {
get {
if (_overlayView == null) {
_overlayView = new UIView (UIScreen.MainScreen.Bounds);
_overlayView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
_overlayView.BackgroundColor = UIColor.Clear;
_overlayView.UserInteractionEnabled = false;
_overlayView.AccessibilityViewIsModal = true;
}
return _overlayView;
}
set { _overlayView = value; }
}
UIView HudView {
get {
if (_hudView == null) {
if (IsiOS7ForLookAndFeel) {
_hudView = new UIToolbar ();
(_hudView as UIToolbar).Translucent = true;
(_hudView as UIToolbar).BarTintColor = HudBackgroundColour;
} else {
_hudView = new UIView ();
}
_hudView.Layer.CornerRadius = 10;
_hudView.Layer.MasksToBounds = true;
_hudView.BackgroundColor = HudBackgroundColour;
_hudView.AutoresizingMask = (UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin |
UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin);
AddSubview (_hudView);
if (_hudView is UIToolbar)
_hudView.LayoutIfNeeded();
}
return _hudView;
}
set { _hudView = value; }
}
UILabel StringLabel {
get {
if (_stringLabel == null) {
_stringLabel = new UILabel ();
_stringLabel.BackgroundColor = HudToastBackgroundColor;
_stringLabel.AdjustsFontSizeToFitWidth = true;
_stringLabel.TextAlignment = HudTextAlignment;
_stringLabel.BaselineAdjustment = UIBaselineAdjustment.AlignCenters;
_stringLabel.TextColor = HudForegroundColor;
_stringLabel.Font = HudFont;
if (!IsiOS7ForLookAndFeel) {
_stringLabel.ShadowColor = HudStatusShadowColor;
_stringLabel.ShadowOffset = new CGSize (0, -1);
}
_stringLabel.Lines = 0;
}
if (_stringLabel.Superview == null) {
HudView.AddSubview (_stringLabel);
}
return _stringLabel;
}
set { _stringLabel = value; }
}
UIButton CancelHudButton {
get {
if (_cancelHud == null) {
_cancelHud = new UIButton ();
_cancelHud.BackgroundColor = UIColor.Clear;
_cancelHud.SetTitleColor (HudForegroundColor, UIControlState.Normal);
_cancelHud.UserInteractionEnabled = true;
_cancelHud.Font = HudFont;
this.UserInteractionEnabled = true;
}
if (_cancelHud.Superview == null) {
HudView.AddSubview (_cancelHud);
// Position the Cancel button at the bottom
/* var hudFrame = HudView.Frame;
var cancelFrame = _cancelHud.Frame;
var x = ((hudFrame.Width - cancelFrame.Width)/2) + 0;
var y = (hudFrame.Height - cancelFrame.Height - 10);
_cancelHud.Frame = new RectangleF(x, y, cancelFrame.Width, cancelFrame.Height);
HudView.SizeToFit();
*/
}
return _cancelHud;
}
set {
_cancelHud = value;
}
}
UIImageView ImageView {
get {
if (_imageView == null) {
_imageView = new UIImageView (new CGRect (0, 0, 28, 28));
}
if (_imageView.Superview == null) {
HudView.AddSubview (_imageView);
}
return _imageView;
}
set { _imageView = value; }
}
UIActivityIndicatorView SpinnerView {
get {
if (_spinnerView == null) {
_spinnerView = new UIActivityIndicatorView (UIActivityIndicatorViewStyle.WhiteLarge);
_spinnerView.HidesWhenStopped = true;
_spinnerView.Bounds = new CGRect (0, 0, 37, 37);
_spinnerView.Color = HudForegroundColor;
}
if (_spinnerView.Superview == null)
HudView.AddSubview (_spinnerView);
return _spinnerView;
}
set { _spinnerView = value; }
}
float VisibleKeyboardHeight {
get {
foreach (var testWindow in UIApplication.SharedApplication.Windows) {
if (testWindow.Class.Handle != Class.GetHandle("UIWindow")) {
foreach (var possibleKeyboard in testWindow.Subviews) {
if ((clsUIPeripheralHostView != null && possibleKeyboard.IsKindOfClass(clsUIPeripheralHostView)) ||
(clsUIKeyboard != null && possibleKeyboard.IsKindOfClass(clsUIKeyboard))) {
// Check that the keyboard is actually on screen
if (possibleKeyboard.Frame.IntersectsWith(testWindow.Frame))
return (float)possibleKeyboard.Bounds.Size.Height;
}
else if (clsUIInputSetContainerView != null && possibleKeyboard.IsKindOfClass(clsUIInputSetContainerView)) {
foreach (var possibleKeyboardSubview in possibleKeyboard.Subviews)
{
if (clsUIInputSetHostView != null && possibleKeyboardSubview.IsKindOfClass(clsUIInputSetHostView))
// Check that the keyboard is actually on screen
if (possibleKeyboardSubview.Frame.IntersectsWith(testWindow.Frame))
return (float)possibleKeyboardSubview.Bounds.Size.Height;
}
}
}
}
}
return 0;
}
}
void DismissWorker ()
{
SetFadeoutTimer (null);
SetProgressTimer (null);
UIView.Animate (0.3, 0, UIViewAnimationOptions.CurveEaseIn | UIViewAnimationOptions.AllowUserInteraction,
delegate {
HudView.Transform.Scale (0.8f, 0.8f);
if (isClear) {
HudView.Alpha = 0f;
} else {
Alpha = 0f;
}
}, delegate {
if (Alpha == 0f || HudView.Alpha == 0f) {
InvokeOnMainThread (delegate {
Alpha = 0f;
HudView.Alpha = 0f;
//Removing observers
UnRegisterNotifications ();
NSNotificationCenter.DefaultCenter.RemoveObserver (this);
Ring.ResetStyle (IsiOS7ForLookAndFeel, (IsiOS7ForLookAndFeel ? TintColor : UIColor.White));
CancelRingLayerAnimation ();
StringLabel.RemoveFromSuperview ();
SpinnerView.RemoveFromSuperview ();
ImageView.RemoveFromSuperview ();
if (_cancelHud != null)
_cancelHud.RemoveFromSuperview ();
StringLabel = null;
SpinnerView = null;
ImageView = null;
_cancelHud = null;
HudView.RemoveFromSuperview ();
HudView = null;
OverlayView.RemoveFromSuperview ();
OverlayView = null;
this.RemoveFromSuperview ();
if (IsiOS7ForLookAndFeel) {
var rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;
if (rootController != null)
rootController.SetNeedsStatusBarAppearanceUpdate ();
}
});
}
});
}
void SetStatusWorker (string status)
{
StringLabel.Text = status;
UpdatePosition ();
}
void RegisterNotifications ()
{
if (_eventListeners == null) {
_eventListeners = new List<NSObject> ();
}
_eventListeners.Add (NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.DidChangeStatusBarOrientationNotification,
PositionHUD));
_eventListeners.Add (NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillHideNotification,
PositionHUD));
_eventListeners.Add (NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.DidHideNotification,
PositionHUD));
_eventListeners.Add (NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillShowNotification,
PositionHUD));
_eventListeners.Add (NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.DidShowNotification,
PositionHUD));
}
void UnRegisterNotifications ()
{
if (_eventListeners != null) {
NSNotificationCenter.DefaultCenter.RemoveObservers (_eventListeners);
_eventListeners.Clear ();
_eventListeners = null;
}
}
void MoveToPoint (CGPoint newCenter, float angle)
{
HudView.Transform = CGAffineTransform.MakeRotation (angle);
HudView.Center = newCenter;
}
ToastPosition toastPosition = ToastPosition.Center;
void PositionHUD (NSNotification notification)
{
nfloat keyboardHeight = 0;
double animationDuration = 0;
Frame = UIScreen.MainScreen.Bounds;
UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
bool ignoreOrientation = UIDevice.CurrentDevice.CheckSystemVersion (8, 0);
if (notification != null) {
var keyboardFrame = UIKeyboard.FrameEndFromNotification (notification);
animationDuration = UIKeyboard.AnimationDurationFromNotification (notification);
if (notification.Name == UIKeyboard.WillShowNotification || notification.Name == UIKeyboard.DidShowNotification) {
if (ignoreOrientation || IsPortrait (orientation))
keyboardHeight = keyboardFrame.Size.Height;
else
keyboardHeight = keyboardFrame.Size.Width;
} else
keyboardHeight = 0;
} else {
keyboardHeight = VisibleKeyboardHeight;
}
CGRect orientationFrame = UIApplication.SharedApplication.KeyWindow.Bounds;
CGRect statusBarFrame = UIApplication.SharedApplication.StatusBarFrame;
if (!ignoreOrientation && IsLandscape (orientation)) {
orientationFrame.Size = new CGSize (orientationFrame.Size.Height, orientationFrame.Size.Width);
statusBarFrame.Size = new CGSize (statusBarFrame.Size.Height, statusBarFrame.Size.Width);
}
var activeHeight = orientationFrame.Size.Height;
if (keyboardHeight > 0)
activeHeight += statusBarFrame.Size.Height * 2;
activeHeight -= keyboardHeight;
nfloat posY = (float)Math.Floor (activeHeight * 0.45);
nfloat posX = orientationFrame.Size.Width / 2;
nfloat textHeight = _stringLabel.Frame.Height / 2 + 40;
switch (toastPosition) {
case ToastPosition.Bottom:
posY = activeHeight - textHeight;
break;
case ToastPosition.Center:
// Already set above
break;
case ToastPosition.Top:
posY = textHeight;
break;
default:
break;
}
CGPoint newCenter;
float rotateAngle;
if (ignoreOrientation) {
rotateAngle = 0.0f;
newCenter = new CGPoint (posX, posY);
} else {
switch (orientation) {
case UIInterfaceOrientation.PortraitUpsideDown:
rotateAngle = (float)Math.PI;
newCenter = new CGPoint (posX, orientationFrame.Size.Height - posY);
break;
case UIInterfaceOrientation.LandscapeLeft:
rotateAngle = (float)(-Math.PI / 2.0f);
newCenter = new CGPoint (posY, posX);
break;
case UIInterfaceOrientation.LandscapeRight:
rotateAngle = (float)(Math.PI / 2.0f);
newCenter = new CGPoint (orientationFrame.Size.Height - posY, posX);
break;
default: // as UIInterfaceOrientationPortrait
rotateAngle = 0.0f;
newCenter = new CGPoint (posX, posY);
break;
}
}
if (notification != null) {
UIView.Animate (animationDuration,
0, UIViewAnimationOptions.AllowUserInteraction, delegate {
MoveToPoint (newCenter, rotateAngle);
}, null);
} else {
MoveToPoint (newCenter, rotateAngle);
}
}
void SetFadeoutTimer (NSTimer newtimer)
{
if (_fadeoutTimer != null) {
_fadeoutTimer.Invalidate ();
_fadeoutTimer = null;
}
if (newtimer != null)
_fadeoutTimer = newtimer;
}
void SetProgressTimer (NSTimer newtimer)
{
StopProgressTimer ();
if (newtimer != null)
_progressTimer = newtimer;
}
void UpdatePosition (bool textOnly = false)
{
nfloat hudWidth = 100f;
nfloat hudHeight = 100f;
nfloat stringWidth = 0f;
nfloat stringHeight = 0f;
nfloat stringHeightBuffer = 20f;
nfloat stringAndImageHeightBuffer = 80f;
CGRect labelRect = new CGRect ();
string @string = StringLabel.Text;
// False if it's text-only
bool imageUsed = (ImageView.Image != null) || (ImageView.Hidden);
if (textOnly) {
imageUsed = false;
}
if (imageUsed) {
hudHeight = stringAndImageHeightBuffer + stringHeight;
} else {
hudHeight = (textOnly ? stringHeightBuffer : stringHeightBuffer + 40);
}
if (!string.IsNullOrEmpty (@string)) {
int lineCount = Math.Min (10, @string.Split ('\n').Length + 1);
if (IsIOS7OrNewer) {
var stringSize = new NSString (@string).GetBoundingRect (new CGSize (200, 30 * lineCount), NSStringDrawingOptions.UsesLineFragmentOrigin,
new UIStringAttributes{ Font = StringLabel.Font },
null);
stringWidth = stringSize.Width;
stringHeight = stringSize.Height;
} else {
var stringSize = new NSString (@string).StringSize (StringLabel.Font, new CGSize (200, 30 * lineCount));
stringWidth = stringSize.Width;
stringHeight = stringSize.Height;
}
hudHeight += stringHeight;
if (stringWidth > hudWidth)
hudWidth = (float)Math.Ceiling (stringWidth / 2) * 2;
float labelRectY = imageUsed ? 66 : 9;
if (hudHeight > 100) {
labelRect = new CGRect (12, labelRectY, hudWidth, stringHeight);
hudWidth += 24;
} else {
hudWidth += 24;
labelRect = new CGRect (0, labelRectY, hudWidth, stringHeight);
}
}
// Adjust for Cancel Button
var cancelRect = new CGRect ();
string @cancelCaption = _cancelHud == null ? null : CancelHudButton.Title (UIControlState.Normal);
if (!string.IsNullOrEmpty (@cancelCaption)) {
const int gap = 20;
if (IsIOS7OrNewer) {
var stringSize = new NSString (@cancelCaption).GetBoundingRect (new CGSize (200, 300), NSStringDrawingOptions.UsesLineFragmentOrigin,
new UIStringAttributes{ Font = StringLabel.Font },
null);
stringWidth = stringSize.Width;
stringHeight = stringSize.Height;
} else {
var stringSize = new NSString (@cancelCaption).StringSize (StringLabel.Font, new CGSize (200, 300));
stringWidth = stringSize.Width;
stringHeight = stringSize.Height;
}
if (stringWidth > hudWidth)
hudWidth = (float)Math.Ceiling (stringWidth / 2) * 2;
// Adjust for label
nfloat cancelRectY = 0f;
if (labelRect.Height > 0) {
cancelRectY = labelRect.Y + labelRect.Height + (nfloat)gap;
} else {
if (string.IsNullOrEmpty (@string)) {
cancelRectY = 76;
} else {
cancelRectY = (imageUsed ? 66 : 9);
}