Fixed #20: Refactor the code

This commit is contained in:
Tito Ciuro
2012-09-29 13:59:54 -07:00
parent 785b42caa1
commit 8a0f1a1ee4
9 changed files with 115 additions and 160 deletions

View File

@@ -22,7 +22,6 @@
59A782461562F5F80001F08D /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 59A782441562F5F70001F08D /* MainStoryboard.storyboard */; };
59A782491562F5F80001F08D /* WBViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A782481562F5F80001F08D /* WBViewController.m */; };
59A782591562F8750001F08D /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 59A782581562F8750001F08D /* QuartzCore.framework */; };
746CBCFC15709E8300B844B1 /* WBBaseNoticeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 746CBCFB15709E8300B844B1 /* WBBaseNoticeView.m */; };
746CBD001570A1D100B844B1 /* WBErrorNoticeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 746CBCFF1570A1D100B844B1 /* WBErrorNoticeView.m */; };
746CBD081570A8FA00B844B1 /* WBSuccessNoticeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 746CBD071570A8FA00B844B1 /* WBSuccessNoticeView.m */; };
748A6A59157D0B74003C7655 /* WBGrayGradientView.m in Sources */ = {isa = PBXBuildFile; fileRef = 748A6A58157D0B74003C7655 /* WBGrayGradientView.m */; };
@@ -54,8 +53,6 @@
59A782471562F5F80001F08D /* WBViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WBViewController.h; sourceTree = "<group>"; };
59A782481562F5F80001F08D /* WBViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WBViewController.m; sourceTree = "<group>"; };
59A782581562F8750001F08D /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
746CBCFA15709E8300B844B1 /* WBBaseNoticeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WBBaseNoticeView.h; sourceTree = "<group>"; };
746CBCFB15709E8300B844B1 /* WBBaseNoticeView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WBBaseNoticeView.m; sourceTree = "<group>"; };
746CBCFE1570A1D100B844B1 /* WBErrorNoticeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WBErrorNoticeView.h; sourceTree = "<group>"; };
746CBCFF1570A1D100B844B1 /* WBErrorNoticeView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WBErrorNoticeView.m; sourceTree = "<group>"; };
746CBD011570A65D00B844B1 /* WBNoticeView_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WBNoticeView_Private.h; sourceTree = "<group>"; };
@@ -167,8 +164,6 @@
746CBD051570A88800B844B1 /* Private */ = {
isa = PBXGroup;
children = (
746CBCFA15709E8300B844B1 /* WBBaseNoticeView.h */,
746CBCFB15709E8300B844B1 /* WBBaseNoticeView.m */,
592BDDE715645BEE00B78820 /* WBNoticeView.h */,
592BDDE815645BEE00B78820 /* WBNoticeView.m */,
746CBD011570A65D00B844B1 /* WBNoticeView_Private.h */,
@@ -258,7 +253,6 @@
59A782491562F5F80001F08D /* WBViewController.m in Sources */,
592BDDEC15645BEE00B78820 /* UILabel+WBExtensions.m in Sources */,
592BDDED15645BEE00B78820 /* WBNoticeView.m in Sources */,
746CBCFC15709E8300B844B1 /* WBBaseNoticeView.m in Sources */,
746CBD001570A1D100B844B1 /* WBErrorNoticeView.m in Sources */,
746CBD081570A8FA00B844B1 /* WBSuccessNoticeView.m in Sources */,
7492FA45157C8CD700FB834C /* WBBlueGradientView.m in Sources */,

View File

@@ -1,29 +0,0 @@
//
// WBBaseNoticeView.h
// NoticeView
//
// Created by Tito Ciuro on 5/25/12.
// Copyright (c) 2012 Tito Ciuro. All rights reserved.
//
#import "WBNoticeView.h"
@interface WBBaseNoticeView : WBNoticeView
- (id)initWithView:(UIView *)theView title:(NSString *)theTitle; // throws NSInvalidArgumentException is view is nil.
- (void)show;
+ (void)raiseIfObjectIsNil:(id)object named:(NSString *)name;
@property (nonatomic, readwrite) WBNoticeViewType noticeType;
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) NSString *title; // default: @"Unknown Error"
@property (nonatomic, readwrite) CGFloat duration; // default: 0.5
@property (nonatomic, readwrite) CGFloat delay; // default: 2.0
@property (nonatomic, readwrite) CGFloat alpha; // default: 1.0
@property (nonatomic, readwrite) CGFloat originY; // default: 0.0
@end

View File

@@ -1,56 +0,0 @@
//
// WBBaseNoticeView.m
// NoticeView
//
// Created by Tito Ciuro on 5/25/12.
// Copyright (c) 2012 Tito Ciuro. All rights reserved.
//
#import "WBBaseNoticeView.h"
@implementation WBBaseNoticeView
- (id)initWithView:(UIView *)theView title:(NSString *)theTitle
{
[WBBaseNoticeView raiseIfObjectIsNil:theView named:@"view"];
if (self = [super init]) {
self.view = theView;
self.title = theTitle;
}
return self;
}
- (void)show
{
// Subclasses need to override this method...
[self doesNotRecognizeSelector:_cmd];
}
+ (void)raiseIfObjectIsNil:(id)object named:(NSString *)name
{
if (nil == object) {
// If the name has not been supplied, name it generically
if (nil == name) name = @"<name not supplied>";
// Log the stack trace
NSLog(@"%@", [NSThread callStackSymbols]);
[[NSException exceptionWithName:NSInvalidArgumentException
reason:[NSString stringWithFormat:@"*** -[%@ %@]: '%@' cannot be nil.", [self class], NSStringFromSelector(_cmd), name]
userInfo:nil]raise];
}
}
#pragma mark - Properties
@synthesize noticeType;
@synthesize view;
@synthesize title;
@synthesize duration;
@synthesize delay;
@synthesize alpha;
@synthesize originY;
@end

View File

@@ -6,9 +6,9 @@
// Copyright (c) 2012 Tito Ciuro. All rights reserved.
//
#import "WBBaseNoticeView.h"
#import "WBNoticeView.h"
@interface WBErrorNoticeView : WBBaseNoticeView
@interface WBErrorNoticeView : WBNoticeView
+ (WBErrorNoticeView *)errorNoticeInView:(UIView *)view title:(NSString *)title message:(NSString *)message;

View File

@@ -16,8 +16,22 @@ typedef enum {
WBNoticeViewTypeSticky
} WBNoticeViewType;
@property (nonatomic, readwrite) WBNoticeViewType noticeType;
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) NSString *title; // default: @"Unknown Error"
@property (nonatomic, readwrite) CGFloat duration; // default: 0.5
@property (nonatomic, readwrite) CGFloat delay; // default: 2.0
@property (nonatomic, readwrite) CGFloat alpha; // default: 1.0
@property (nonatomic, readwrite) CGFloat originY; // default: 0.0
+ (WBNoticeView *)defaultManager;
- (id)initWithView:(UIView *)theView title:(NSString *)theTitle; // throws NSInvalidArgumentException is view is nil.
- (void)show; // Must be implemented in the subclasses, or else it'll raise an exception.
// Error notice methods
- (void)showErrorNoticeInView:(UIView *)view

View File

@@ -17,15 +17,12 @@
@interface WBNoticeView ()
@property(nonatomic, strong) UIView *noticeView;
@property(nonatomic, strong) UIView *gradientView;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UILabel *messageLabel;
@property(nonatomic, assign) CGFloat _duration;
@property(nonatomic, assign) CGFloat _delay;
@property(nonatomic, assign) CGFloat _alpha;
@property(nonatomic, assign) CGFloat _hiddenYOrigin;
@property(nonatomic, strong) WBNoticeView *_currentNotice;
@property(nonatomic, assign) CGFloat hiddenYOrigin;
@property(nonatomic, strong) WBNoticeView *currentNotice;
- (void)_showErrorNoticeInView:(UIView *)view
title:(NSString *)title
@@ -69,12 +66,18 @@
@implementation WBNoticeView
@synthesize noticeView, titleLabel, messageLabel;
@synthesize _duration;
@synthesize _delay;
@synthesize _alpha;
@synthesize _hiddenYOrigin;
@synthesize _currentNotice;
@synthesize gradientView = _gradientView;
@synthesize titleLabel = _titleLabel;
@synthesize messageLabel = _messageLabel;
@synthesize hiddenYOrigin = _hiddenYOrigin;
@synthesize currentNotice = _currentNotice;
@synthesize noticeType = _noticeType;
@synthesize view = _view;
@synthesize title = _title;
@synthesize duration = _duration;
@synthesize delay = _delay;
@synthesize alpha = _alpha;
@synthesize originY = _originY;
+ (WBNoticeView *)defaultManager
{
@@ -87,6 +90,24 @@
return __sWBNoticeView;
}
- (id)initWithView:(UIView *)theView title:(NSString *)theTitle
{
[WBNoticeView _raiseIfObjectIsNil:theView named:@"view"];
if (self = [super init]) {
self.view = theView;
self.title = theTitle;
}
return self;
}
- (void)show
{
// Subclasses need to override this method...
[self doesNotRecognizeSelector:_cmd];
}
#pragma mark - Error Notice Methods
- (void)showErrorNoticeInView:(UIView *)view
@@ -215,7 +236,7 @@
alpha:(float)alpha
yOrigin:(CGFloat)origin
{
if (nil == self.noticeView) {
if (nil == self.gradientView) {
// Set default values if needed
if (nil == title) title = @"Unknown Error";
@@ -309,34 +330,34 @@
float hiddenYOrigin = -noticeViewHeight - 20.0;
// Make and add the notice view
self.noticeView = [[WBRedGradientView alloc]initWithFrame:CGRectMake(0.0, hiddenYOrigin, viewWidth, noticeViewHeight + 10.0)];
[view addSubview:self.noticeView];
self.gradientView = [[WBRedGradientView alloc]initWithFrame:CGRectMake(0.0, hiddenYOrigin, viewWidth, noticeViewHeight + 10.0)];
[view addSubview:self.gradientView];
// Make and add the icon view
UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake(10.0, 10.0, 20.0, 30.0)];
iconView.image = [UIImage imageWithContentsOfFile:noticeIconImageName];
iconView.contentMode = UIViewContentModeScaleAspectFit;
iconView.alpha = 0.8;
[self.noticeView addSubview:iconView];
[self.gradientView addSubview:iconView];
// Add the title label
[self.noticeView addSubview:self.titleLabel];
[self.gradientView addSubview:self.titleLabel];
// Add the message label
[self.noticeView addSubview:self.messageLabel];
[self.gradientView addSubview:self.messageLabel];
// Add the drop shadow to the notice view
CALayer *noticeLayer = self.noticeView.layer;
CALayer *noticeLayer = self.gradientView.layer;
noticeLayer.shadowColor = [[UIColor blackColor]CGColor];
noticeLayer.shadowOffset = CGSizeMake(0.0, 3);
noticeLayer.shadowOpacity = 0.50;
noticeLayer.masksToBounds = NO;
noticeLayer.shouldRasterize = YES;
self._duration = duration;
self._delay = delay;
self._alpha = alpha;
self._hiddenYOrigin = hiddenYOrigin;
self.duration = duration;
self.delay = delay;
self.alpha = alpha;
self.hiddenYOrigin = hiddenYOrigin;
[self displayNoticeOfType:WBNoticeViewTypeError duration:duration delay:delay origin:origin hiddenYOrigin:hiddenYOrigin alpha:alpha];
}
@@ -380,31 +401,31 @@
hiddenYOrigin = -noticeViewHeight - 20.0;
// Make and add the notice view
self.noticeView = [[WBBlueGradientView alloc]initWithFrame:CGRectMake(0.0, hiddenYOrigin, viewWidth, noticeViewHeight + 10.0)];
[view addSubview:self.noticeView];
self.gradientView = [[WBBlueGradientView alloc]initWithFrame:CGRectMake(0.0, hiddenYOrigin, viewWidth, noticeViewHeight + 10.0)];
[view addSubview:self.gradientView];
// Make and add the icon view
UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake(10.0, 10.0, 20.0, 30.0)];
iconView.image = [UIImage imageWithContentsOfFile:noticeIconImageName];
iconView.contentMode = UIViewContentModeScaleAspectFit;
iconView.alpha = 0.8;
[self.noticeView addSubview:iconView];
[self.gradientView addSubview:iconView];
// Add the title label
[self.noticeView addSubview:self.titleLabel];
[self.gradientView addSubview:self.titleLabel];
// Add the drop shadow to the notice view
CALayer *noticeLayer = self.noticeView.layer;
CALayer *noticeLayer = self.gradientView.layer;
noticeLayer.shadowColor = [[UIColor blackColor]CGColor];
noticeLayer.shadowOffset = CGSizeMake(0.0, 3);
noticeLayer.shadowOpacity = 0.50;
noticeLayer.masksToBounds = NO;
noticeLayer.shouldRasterize = YES;
self._duration = duration;
self._delay = delay;
self._alpha = alpha;
self._hiddenYOrigin = hiddenYOrigin;
self.duration = duration;
self.delay = delay;
self.alpha = alpha;
self.hiddenYOrigin = hiddenYOrigin;
[self displayNoticeOfType:WBNoticeViewTypeSuccess duration:duration delay:delay origin:origin hiddenYOrigin:hiddenYOrigin alpha:alpha];
}
@@ -453,12 +474,12 @@
hiddenYOrigin = -noticeViewHeight - 20.0;
// Make and add the notice view
self.noticeView = [[WBGrayGradientView alloc]initWithFrame:CGRectMake(0.0, hiddenYOrigin, viewWidth, 32)];
[view addSubview:self.noticeView];
self.gradientView = [[WBGrayGradientView alloc]initWithFrame:CGRectMake(0.0, hiddenYOrigin, viewWidth, 32)];
[view addSubview:self.gradientView];
// Center the message in the middle of the notice
frame = self.titleLabel.frame;
frame.origin.x = (self.noticeView.frame.size.width - frame.size.width) / 2;
frame.origin.x = (self.gradientView.frame.size.width - frame.size.width) / 2;
self.titleLabel.frame = frame;
// Make and add the icon view
@@ -468,13 +489,13 @@
iconView.image = [UIImage imageWithContentsOfFile:noticeIconImageName];
iconView.contentMode = UIViewContentModeScaleAspectFit;
iconView.alpha = 0.8;
[self.noticeView addSubview:iconView];
[self.gradientView addSubview:iconView];
// Add the title label
[self.noticeView addSubview:self.titleLabel];
[self.gradientView addSubview:self.titleLabel];
// Add the drop shadow to the notice view
CALayer *noticeLayer = self.noticeView.layer;
CALayer *noticeLayer = self.gradientView.layer;
noticeLayer.shadowColor = [[UIColor blackColor]CGColor];
noticeLayer.shadowOffset = CGSizeMake(0.0, 3);
noticeLayer.shadowOpacity = 0.50;
@@ -482,18 +503,18 @@
noticeLayer.shouldRasterize = YES;
// Add an invisible button that responds to a manual dismiss
self._currentNotice = self;
frame = self.noticeView.frame;
self.currentNotice = self;
frame = self.gradientView.frame;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
frame.origin.x = frame.origin.y = 0.0;
button.frame = frame;
[button addTarget:self._currentNotice action:@selector(dismissStickyNotice:) forControlEvents:UIControlEventTouchUpInside];
[self.noticeView addSubview:button];
[button addTarget:self.currentNotice action:@selector(dismissStickyNotice:) forControlEvents:UIControlEventTouchUpInside];
[self.gradientView addSubview:button];
self._duration = duration;
self._delay = delay;
self._alpha = alpha;
self._hiddenYOrigin = hiddenYOrigin;
self.duration = duration;
self.delay = delay;
self.alpha = alpha;
self.hiddenYOrigin = hiddenYOrigin;
[self displayNoticeOfType:WBNoticeViewTypeSticky duration:duration delay:delay origin:origin hiddenYOrigin:hiddenYOrigin alpha:alpha];
}
@@ -504,10 +525,10 @@
{
// Go ahead, display it
[UIView animateWithDuration:duration animations:^ {
CGRect newFrame = self.noticeView.frame;
CGRect newFrame = self.gradientView.frame;
newFrame.origin.y = origin;
self.noticeView.frame = newFrame;
self.noticeView.alpha = alpha;
self.gradientView.frame = newFrame;
self.gradientView.alpha = alpha;
} completion:^ (BOOL finished) {
if (finished) {
// if it's not sticky, hide it automatically
@@ -522,9 +543,9 @@
- (void)dismissNoticeOfType:(WBNoticeViewType)noticeType duration:(CGFloat)duration delay:(CGFloat)delay hiddenYOrigin:(CGFloat)hiddenYOrigin
{
[UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionCurveEaseOut animations:^ {
CGRect newFrame = self.noticeView.frame;
CGRect newFrame = self.gradientView.frame;
newFrame.origin.y = hiddenYOrigin;
self.noticeView.frame = newFrame;
self.gradientView.frame = newFrame;
} completion:^ (BOOL finished) {
if (finished) {
// Cleanup
@@ -536,18 +557,18 @@
- (IBAction)dismissStickyNotice:(id)sender
{
// Triggered manually by the sticky notice
[self dismissNoticeOfType:WBNoticeViewTypeSticky duration:self._duration delay:self._delay hiddenYOrigin:self._hiddenYOrigin];
[self dismissNoticeOfType:WBNoticeViewTypeSticky duration:self.duration delay:self.delay hiddenYOrigin:self.hiddenYOrigin];
}
#pragma mark -
- (void)cleanup
{
[self.noticeView removeFromSuperview];
self.noticeView = nil;
[self.gradientView removeFromSuperview];
self.gradientView = nil;
self.titleLabel = nil;
self.messageLabel = nil;
self._currentNotice = nil;
self.currentNotice = nil;
}
- (void)dealloc
@@ -555,4 +576,21 @@
[self cleanup];
}
#pragma mark -
+ (void)_raiseIfObjectIsNil:(id)object named:(NSString *)name
{
if (nil == object) {
// If the name has not been supplied, name it generically
if (nil == name) name = @"<name not supplied>";
// Log the stack trace
NSLog(@"%@", [NSThread callStackSymbols]);
[[NSException exceptionWithName:NSInvalidArgumentException
reason:[NSString stringWithFormat:@"*** -[%@ %@]: '%@' cannot be nil.", [self class], NSStringFromSelector(_cmd), name]
userInfo:nil]raise];
}
}
@end

View File

@@ -13,6 +13,8 @@
@interface WBNoticeView (Private)
+ (void)_raiseIfObjectIsNil:(id)object named:(NSString *)name;
- (void)_showNoticeOfType:(WBNoticeViewType)noticeType
view:(UIView *)view
title:(NSString *)title
@@ -22,14 +24,6 @@
alpha:(float)alpha
yOrigin:(CGFloat)origin;
- (void)_showErrorNoticeInView:(UIView *)view
title:(NSString *)title
message:(NSString *)message
duration:(float)duration
delay:(float)delay
alpha:(float)alpha
yOrigin:(CGFloat)origin;
@end
#endif

View File

@@ -6,9 +6,9 @@
// Copyright (c) 2012 Tito Ciuro. All rights reserved.
//
#import "WBBaseNoticeView.h"
#import "WBNoticeView.h"
@interface WBStickyNoticeView : WBBaseNoticeView
@interface WBStickyNoticeView : WBNoticeView
+ (WBStickyNoticeView *)stickyNoticeInView:(UIView *)view title:(NSString *)title;

View File

@@ -6,9 +6,9 @@
// Copyright (c) 2012 Tito Ciuro. All rights reserved.
//
#import "WBBaseNoticeView.h"
#import "WBNoticeView.h"
@interface WBSuccessNoticeView : WBBaseNoticeView
@interface WBSuccessNoticeView : WBNoticeView
+ (WBSuccessNoticeView *)successNoticeInView:(UIView *)view title:(NSString *)title;