Skip to content

Commit 6c4bd53

Browse files
committed
Cleanup of the new horizontal bar functionality.
- added class prefix to the bar class, to minimize the chance for namespace collisions - exposed and documented the MBBarProgressView interface - removed some non-crucial methods to simplify the API - adjusted the default values for some attributers - cleaned up the code style, so it matches the rest of the class - added proper ARC macros - using KVO instead of accessor overrides for UI update triggering
1 parent d3c6f30 commit 6c4bd53

File tree

2 files changed

+68
-95
lines changed

2 files changed

+68
-95
lines changed

MBProgressHUD.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,34 @@ typedef void (^MBProgressHUDCompletionBlock)();
451451
@property (nonatomic, assign, getter = isAnnular) BOOL annular;
452452

453453
@end
454+
455+
456+
/**
457+
* A flat bar progress view.
458+
*/
459+
@interface MBBarProgressView : UIView
460+
461+
/**
462+
* Progress (0.0 to 1.0)
463+
*/
464+
@property (nonatomic, assign) float progress;
465+
466+
/**
467+
* Bar border line color.
468+
* Defaults to white [UIColor whiteColor].
469+
*/
470+
@property (nonatomic, MB_STRONG) UIColor *lineColor;
471+
472+
/**
473+
* Bar background color.
474+
* Defaults to clear [UIColor clearColor];
475+
*/
476+
@property (nonatomic, MB_STRONG) UIColor *progressRemainingColor;
477+
478+
/**
479+
* Bar progress color.
480+
* Defaults to white [UIColor whiteColor].
481+
*/
482+
@property (nonatomic, MB_STRONG) UIColor *progressColor;
483+
484+
@end

MBProgressHUD.m

Lines changed: 37 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,6 @@
2828
static const CGFloat kLabelFontSize = 16.f;
2929
static const CGFloat kDetailsLabelFontSize = 12.f;
3030

31-
@interface FlatProgressView : UIView
32-
33-
@property (nonatomic, readwrite) float minValue;
34-
@property (nonatomic, readwrite) float maxValue;
35-
@property (nonatomic, readwrite) float progress;
36-
@property (nonatomic, strong) UIColor *lineColor;
37-
@property (nonatomic, strong) UIColor *progressRemainingColor;
38-
@property (nonatomic, strong) UIColor *progressColor;
39-
40-
-(void)setNewRect:(CGRect)newFrame;
41-
42-
@end
4331

4432
@interface MBProgressHUD ()
4533

@@ -479,12 +467,9 @@ - (void)updateIndicators {
479467
[self addSubview:indicator];
480468
}
481469
else if (mode == MBProgressHUDModeDeterminateHorizontalBar) {
470+
// Update to bar determinate indicator
482471
[indicator removeFromSuperview];
483-
FlatProgressView* flatProgressView = [[FlatProgressView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 120.0f, 20.0f)];
484-
flatProgressView.progressColor = [UIColor whiteColor];
485-
flatProgressView.progressRemainingColor = [UIColor clearColor];
486-
flatProgressView.lineColor = [UIColor whiteColor];
487-
self.indicator = MB_AUTORELEASE(flatProgressView);
472+
self.indicator = MB_AUTORELEASE([[MBBarProgressView alloc] init]);
488473
[self addSubview:indicator];
489474
}
490475
else if (mode == MBProgressHUDModeDeterminate || mode == MBProgressHUDModeAnnularDeterminate) {
@@ -885,39 +870,39 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
885870

886871
@end
887872

888-
@implementation FlatProgressView
889873

890-
- (id)initWithFrame:(CGRect)frame
891-
{
874+
@implementation MBBarProgressView
875+
876+
- (id)init {
877+
return [self initWithFrame:CGRectMake(.0f, .0f, 120.0f, 20.0f)];
878+
}
879+
880+
- (id)initWithFrame:(CGRect)frame {
892881
self = [super initWithFrame:frame];
893-
if (self)
894-
{
895-
_minValue = 0;
896-
_maxValue = 1;
897-
_progress = 0;
898-
self.backgroundColor = [UIColor clearColor];
882+
if (self) {
883+
_progress = 0.f;
899884
_lineColor = [UIColor whiteColor];
900-
_progressColor = [UIColor darkGrayColor];
901-
_progressRemainingColor = [UIColor lightGrayColor];
885+
_progressColor = [UIColor whiteColor];
886+
_progressRemainingColor = [UIColor clearColor];
887+
self.backgroundColor = [UIColor clearColor];
888+
self.opaque = NO;
889+
[self registerForKVO];
902890
}
903891
return self;
904892
}
905893

906-
- (void)dealloc
907-
{
908-
894+
895+
- (void)dealloc {
896+
[self unregisterFromKVO];
909897
#if !__has_feature(objc_arc)
910-
911898
[_lineColor release];
912899
[_progressColor release];
913900
[_progressRemainingColor release];
914-
[super dealloc];
901+
[super dealloc];
915902
#endif
916-
917903
}
918904

919-
- (void)drawRect:(CGRect)rect
920-
{
905+
- (void)drawRect:(CGRect)rect {
921906
CGContextRef context = UIGraphicsGetCurrentContext();
922907

923908
// setup properties
@@ -949,11 +934,10 @@ - (void)drawRect:(CGRect)rect
949934
// setup to draw progress color
950935
CGContextSetFillColorWithColor(context, [_progressColor CGColor]);
951936
radius = radius - 2;
952-
float amount = (self.progress/(self.maxValue - self.minValue)) * (rect.size.width);
937+
float amount = self.progress * rect.size.width;
953938

954939
// if progress is in the middle area
955-
if (amount >= radius + 4 && amount <= (rect.size.width - radius - 4))
956-
{
940+
if (amount >= radius + 4 && amount <= (rect.size.width - radius - 4)) {
957941
// top
958942
CGContextMoveToPoint(context, 4, rect.size.height/2);
959943
CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
@@ -970,8 +954,7 @@ - (void)drawRect:(CGRect)rect
970954
}
971955

972956
// progress is in the right arc
973-
else if (amount > radius + 4)
974-
{
957+
else if (amount > radius + 4) {
975958
float x = amount - (rect.size.width - radius - 4);
976959

977960
// top
@@ -996,8 +979,7 @@ - (void)drawRect:(CGRect)rect
996979
}
997980

998981
// progress is in the left arc
999-
else if (amount < radius + 4 && amount > 0)
1000-
{
982+
else if (amount < radius + 4 && amount > 0) {
1001983
// top
1002984
CGContextMoveToPoint(context, 4, rect.size.height/2);
1003985
CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
@@ -1012,65 +994,25 @@ - (void)drawRect:(CGRect)rect
1012994
}
1013995
}
1014996

1015-
-(void)setNewRect:(CGRect)newFrame
1016-
{
1017-
self.frame = newFrame;
1018-
[self setNeedsDisplay];
1019-
}
1020-
1021-
-(void)setMinValue:(float)newMin
1022-
{
1023-
_minValue = newMin;
1024-
[self setNeedsDisplay];
1025-
}
1026-
1027-
-(void)setMaxValue:(float)newMax
1028-
{
1029-
_maxValue = newMax;
1030-
[self setNeedsDisplay];
1031-
}
997+
#pragma mark - KVO
1032998

1033-
-(void)setProgress:(float)newValue
1034-
{
1035-
_progress = newValue;
1036-
[self setNeedsDisplay];
999+
- (void)registerForKVO {
1000+
for (NSString *keyPath in [self observableKeypaths]) {
1001+
[self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];
1002+
}
10371003
}
10381004

1039-
-(void)setLineColor:(UIColor *)newColor
1040-
{
1041-
1042-
#if !__has_feature(objc_arc)
1043-
1044-
[_lineColor release];
1045-
1046-
#endif
1047-
1048-
_lineColor = newColor;
1049-
[self setNeedsDisplay];
1005+
- (void)unregisterFromKVO {
1006+
for (NSString *keyPath in [self observableKeypaths]) {
1007+
[self removeObserver:self forKeyPath:keyPath];
1008+
}
10501009
}
10511010

1052-
-(void)setProgressColor:(UIColor *)newColor
1053-
{
1054-
#if !__has_feature(objc_arc)
1055-
1056-
[_progressColor release];
1057-
1058-
#endif
1059-
1060-
_progressColor = newColor;
1061-
[self setNeedsDisplay];
1011+
- (NSArray *)observableKeypaths {
1012+
return [NSArray arrayWithObjects:@"lineColor", @"progressRemainingColor", @"progressColor", @"progress", nil];
10621013
}
10631014

1064-
-(void)setProgressRemainingColor:(UIColor *)newColor
1065-
{
1066-
1067-
#if !__has_feature(objc_arc)
1068-
1069-
[_progressRemainingColor release];
1070-
1071-
#endif
1072-
1073-
_progressRemainingColor = newColor;
1015+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
10741016
[self setNeedsDisplay];
10751017
}
10761018

0 commit comments

Comments
 (0)