Skip to content

Commit

Permalink
fix hide & show from being called more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
Adar Porat committed Oct 16, 2015
1 parent af1538f commit b53a9f8
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 70 deletions.
22 changes: 11 additions & 11 deletions KKProgressToolbar/KKProgressToolbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#import <UIKit/UIKit.h>

typedef void(^KKProgressToolbarCompletionHandler)(BOOL success);
typedef void(^KKProgressToolbarCompletionHandler)(BOOL finished);

@class KKProgressToolbar;

Expand All @@ -27,24 +27,24 @@ typedef void(^KKProgressToolbarCompletionHandler)(BOOL success);
/**
* called when the user cancels the action
*/
- (void)didCancelButtonPressed:(KKProgressToolbar*)toolbar;
- (void)didCancelButtonPressed:(KKProgressToolbar *)toolbar;

@end

@interface KKProgressToolbar : UIToolbar {
@private
id <KKProgressToolbarDelegate> __weak _actionDelegate;
UIBarButtonItem* _stopButtonItem;
UIActivityIndicatorView* _activityIndicator;
UILabel* _statusLabel;
UIProgressView* _progressBar;
@private

id <KKProgressToolbarDelegate> __weak _actionDelegate;

UIBarButtonItem *_stopButtonItem;
UIActivityIndicatorView *_activityIndicator;
UILabel *_statusLabel;
UIProgressView *_progressBar;
}

@property (nonatomic, strong) UIBarButtonItem *stopButtonItem;
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicator;
@property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIProgressView *progressBar;

@property (nonatomic, weak) id <KKProgressToolbarDelegate> actionDelegate;
Expand Down
110 changes: 57 additions & 53 deletions KKProgressToolbar/KKProgressToolbar.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,82 +25,86 @@ @interface KKProgressToolbar ()

@implementation KKProgressToolbar

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIBarButtonItem *activityIndicatorItem = [[UIBarButtonItem alloc] initWithCustomView:self.activityIndicator];
self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 200, 20)];
self.statusLabel.font = [UIFont boldSystemFontOfSize:14.0];
self.statusLabel.backgroundColor = [UIColor clearColor];
self.statusLabel.textColor = [UIColor whiteColor];
self.statusLabel.shadowColor = [UIColor blackColor];
self.statusLabel.shadowOffset = CGSizeMake(0, -1);
self.statusLabel.textAlignment = UITextAlignmentCenter;
self.progressBar = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 25, 200, 10)];
UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
[statusView addSubview:self.statusLabel];
[statusView addSubview:self.progressBar];
UIBarButtonItem *statusItem = [[UIBarButtonItem alloc] initWithCustomView:statusView];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.stopButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(didCancelButtonPressed:)];
[self setBackgroundImage:nil
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
self.translucent = YES;
self.barStyle = UIBarStyleBlackTranslucent;
self.items = @[activityIndicatorItem, flexSpace, statusItem, flexSpace, self.stopButtonItem];
}
return self;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIBarButtonItem *activityIndicatorItem = [[UIBarButtonItem alloc] initWithCustomView:self.activityIndicator];

self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 200, 20)];
self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
self.titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel.textColor = [UIColor whiteColor];
self.titleLabel.shadowColor = [UIColor blackColor];
self.titleLabel.shadowOffset = CGSizeMake(0, 0);
self.titleLabel.textAlignment = UITextAlignmentCenter;

self.progressBar = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 25, 200, 10)];

UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
[statusView addSubview:self.titleLabel];
[statusView addSubview:self.progressBar];

UIBarButtonItem *statusItem = [[UIBarButtonItem alloc] initWithCustomView:statusView];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.stopButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(didCancelButtonPressed:)];
self.stopButtonItem.tintColor = [UIColor whiteColor];

[self setBackgroundImage:nil
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];

self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
self.translucent = YES;
self.barStyle = UIBarStyleBlackTranslucent;
self.items = @[activityIndicatorItem, flexSpace, statusItem, flexSpace, self.stopButtonItem];

}
return self;
}

- (void)show:(BOOL)animated completion:(void (^)(BOOL finished))completion {

if (self.showHandler == nil) {
self.showHandler = completion;

self.stopButtonItem.enabled = YES;
[self.activityIndicator startAnimating];

[UIView animateWithDuration:0.4 animations:^{
self.frame = CGRectMake(0, self.superview.bounds.size.height - 44, self.superview.bounds.size.width, 44);
self.frame = CGRectMake(0, self.superview.bounds.size.height - 44, self.superview.bounds.size.width, 44);
} completion:^(BOOL finished) {
if (self.showHandler!=nil) {
self.showHandler(YES);
}
self.showHandler = nil;
if (self.showHandler != nil) {
self.showHandler(YES);
}
self.showHandler = nil;
}];
}
}

- (void)hide:(BOOL)animated completion:(void (^)(BOOL finished))completion {

if (self.hideHandler == nil) {
self.hideHandler = completion;

self.stopButtonItem.enabled = NO;
[self.activityIndicator stopAnimating];

[UIView animateWithDuration:0.4 delay:1.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
self.frame = CGRectMake(0, self.superview.bounds.size.height, self.superview.bounds.size.width, 44);
self.frame = CGRectMake(0, self.superview.bounds.size.height, self.superview.bounds.size.width, 44);
} completion:^(BOOL finished) {
if (self.hideHandler!=nil) {
self.hideHandler(YES);
}
self.hideHandler = nil;
if (self.hideHandler != nil) {
self.hideHandler(YES);
}
self.hideHandler = nil;
}];
}
}

- (void)didCancelButtonPressed:(id*)sender {
if ([_actionDelegate respondsToSelector:@selector(didCancelButtonPressed:)]) {
[_actionDelegate performSelector:@selector(didCancelButtonPressed:) withObject:self];
}
- (void)didCancelButtonPressed:(id *)sender {
if ([_actionDelegate respondsToSelector:@selector(didCancelButtonPressed:)]) {
[_actionDelegate performSelector:@selector(didCancelButtonPressed:) withObject:self];
}
}


Expand Down
32 changes: 28 additions & 4 deletions example/KKProgressToolbarDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
53C05CE815F70B8600D12E27 /* KKProgressToolbar */ = {
isa = PBXGroup;
children = (
531D477116C6C691008DC7BF /* KKProgressToolbar.m */,
531D477216C6C691008DC7BF /* KKProgressToolbar.h */,
531D477116C6C691008DC7BF /* KKProgressToolbar.m */,
);
name = KKProgressToolbar;
sourceTree = "<group>";
Expand Down Expand Up @@ -143,7 +143,7 @@
53C05CB515F70B7100D12E27 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0440;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = "Kosher Penguin LLC";
};
buildConfigurationList = 53C05CB815F70B7100D12E27 /* Build configuration list for PBXProject "KKProgressToolbarDemo" */;
Expand Down Expand Up @@ -223,25 +223,37 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand All @@ -251,16 +263,26 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
Expand All @@ -276,6 +298,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "KKProgressToolbarDemo/KKProgressToolbarDemo-Prefix.pch";
INFOPLIST_FILE = "KKProgressToolbarDemo/KKProgressToolbarDemo-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "com.kosherpenguin.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -287,6 +310,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "KKProgressToolbarDemo/KKProgressToolbarDemo-Prefix.pch";
INFOPLIST_FILE = "KKProgressToolbarDemo/KKProgressToolbarDemo-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "com.kosherpenguin.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.kosherpenguin.${PRODUCT_NAME:rfc1034identifier}</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
2 changes: 1 addition & 1 deletion example/KKProgressToolbarDemo/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ - (void)didCancelButtonPressed:(KKProgressToolbar *)toolbar {

- (IBAction)startUILoading {

self.statusToolbar.statusLabel.text = @"Loading from server...";
self.statusToolbar.titleLabel.text = @"Loading from server...";
[self.statusToolbar show:YES completion:^(BOOL finished) {

}];
Expand Down

0 comments on commit b53a9f8

Please sign in to comment.