Skip to content

Commit

Permalink
added example project
Browse files Browse the repository at this point in the history
  • Loading branch information
JARinteractive authored and JARinteractive committed Jul 25, 2013
1 parent 71885df commit 23cc658
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 13 deletions.
2 changes: 1 addition & 1 deletion example/ESCColorPicker/ESCColorPicker-Info.plist
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.bwater.${PRODUCT_NAME:rfc1034identifier}</string>
<string>com.escappe.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
4 changes: 4 additions & 0 deletions example/ESCColorPicker/ESCColorPickerModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@protocol ESCColorPickerModelObserver

- (void)colorDidChange;
- (void)colorDescriptionDidChangeKeys:(NSArray *)keys values:(NSArray *)values;

@end

Expand All @@ -13,4 +14,7 @@
@property (nonatomic) CGFloat saturation;
@property (nonatomic) CGFloat brightness;

@property (nonatomic) NSArray *colorDescriptionKeys;
@property (nonatomic) NSArray *colorDescriptionValues;

@end
12 changes: 12 additions & 0 deletions example/ESCColorPicker/ESCColorPickerModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@ - (id)init {

- (void)setHue:(CGFloat)hue {
_hue = hue;
[self updateDescription];
[self.escNotifier colorDidChange];
}

- (void)setSaturation:(CGFloat)saturation {
_saturation = saturation;
[self updateDescription];
[self.escNotifier colorDidChange];
}

- (void)setBrightness:(CGFloat)brightness {
_brightness = brightness;
[self updateDescription];
[self.escNotifier colorDidChange];
}

- (void)updateDescription {
self.colorDescriptionKeys = @[@"H", @"S", @"B"];
NSString *formatString = @"%.3f";
self.colorDescriptionValues = @[[NSString stringWithFormat:formatString, self.hue],
[NSString stringWithFormat:formatString, self.saturation],
[NSString stringWithFormat:formatString, self.brightness]];
[self.escNotifier colorDescriptionDidChangeKeys:self.colorDescriptionKeys values:self.colorDescriptionValues];
}

@end
14 changes: 14 additions & 0 deletions example/ESCColorPicker/ESCColorPickerModelTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ - (void)testModelSendsEventForBrightnessChange {
[self.mockObserver verify];
}

- (void)testModelSendsDescriptionEventWhenColorChanges {
[[self.mockObserver expect] colorDescriptionDidChangeKeys:@[@"H", @"S", @"B"] values:@[@"0.500", @"0.300", @"0.400"]];
self.testObject.hue = 0.5;
[self.mockObserver verify];

[[self.mockObserver expect] colorDescriptionDidChangeKeys:@[@"H", @"S", @"B"] values:@[@"0.500", @"0.600", @"0.400"]];
self.testObject.saturation = 0.6;
[self.mockObserver verify];

[[self.mockObserver expect] colorDescriptionDidChangeKeys:@[@"H", @"S", @"B"] values:@[@"0.500", @"0.600", @"0.129"]];
self.testObject.brightness = 0.129;
[self.mockObserver verify];
}

@end
5 changes: 5 additions & 0 deletions example/ESCColorPicker/ESCColorPickerPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ - (instancetype)initWithView:(ESCColorPickerView *)view model:(ESCColorPickerMod
[self.view escAddObserver:self];

[self colorDidChange];
[self.view setColorDescriptionKeys:self.model.colorDescriptionKeys values:self.model.colorDescriptionValues];
}
return self;
}
Expand All @@ -40,4 +41,8 @@ - (void)brightnessDidChange:(CGFloat)brightness {
self.model.brightness = brightness;
}

- (void)colorDescriptionDidChangeKeys:(NSArray *)keys values:(NSArray *)values {
[self.view setColorDescriptionKeys:keys values:values];
}

@end
15 changes: 15 additions & 0 deletions example/ESCColorPicker/ESCColorPickerPresenterTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ - (void)testWhenPresenterIsCreatedValuesAreSetOnView {
CGFloat expectedHue = 0.41;
CGFloat expectedSaturation = 0.29;
CGFloat expectedBrightness = 0.53;
NSArray *expectedKeys = @[@"key1"];
NSArray *expectedValues = @[@"stringValue"];
id mockView = [OCMockObject niceMockForClass:[ESCColorPickerView class]];
id mockModel = [OCMockObject niceMockForClass:[ESCColorPickerModel class]];
[[[mockModel stub] andReturnValue:@(expectedHue)] hue];
[[[mockModel stub] andReturnValue:@(expectedSaturation)] saturation];
[[[mockModel stub] andReturnValue:@(expectedBrightness)] brightness];
[[mockView expect] setHue:expectedHue saturation:expectedSaturation brightness:expectedBrightness];
[[[mockModel stub] andReturn:expectedKeys] colorDescriptionKeys];
[[[mockModel stub] andReturn:expectedValues] colorDescriptionValues];
[[mockView expect] setColorDescriptionKeys:expectedKeys values:expectedValues];

(void)[[ESCColorPickerPresenter alloc] initWithView:mockView model:mockModel];

Expand Down Expand Up @@ -83,4 +88,14 @@ - (void)testWhenBrightnessChangeEventFiresThenBrightnessIsSetOnModel {
[self.mockModel verify];
}

- (void)testWhenDescriptionChangeEventFiresThenDescriptionKeysAndValuesAreSetOnView {
NSArray *expectedKeys = @[@"key1", @"key2"];
NSArray *expectedValues = @[@"5.0", @"stringValue"];
[[self.mockView expect] setColorDescriptionKeys:expectedKeys values:expectedValues];

[[self.mockModel escNotifier] colorDescriptionDidChangeKeys:expectedKeys values:expectedValues];

[self.mockView verify];
}

@end
1 change: 1 addition & 0 deletions example/ESCColorPicker/ESCColorPickerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
@interface ESCColorPickerView : UIView<ESCObservable>

- (void)setHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness;
- (void)setColorDescriptionKeys:(NSArray *)keys values:(NSArray *)values;

@end
33 changes: 25 additions & 8 deletions example/ESCColorPicker/ESCColorPickerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

@interface ESCColorPickerView()<ESCObservableInternal>

@property (nonatomic) UIView *colorView;
@property (nonatomic) ESCHueWheel *hueWheel;
@property (nonatomic) ESCGradientSlider *saturationSlider;
@property (nonatomic) ESCGradientSlider *brightnessSlider;
@property (nonatomic) UILabel *colorLabel;

@end

Expand All @@ -33,8 +33,10 @@ - (instancetype)initWithFrame:(CGRect)frame {
[self.brightnessSlider escAddObserver:self forSelector:@selector(sliderValueDidChange:) forwardingToSelector:@selector(brightnessDidChange:)];
[self addSubview:self.brightnessSlider];

//self.colorView = [[UIView alloc] init];
//[self addSubview:self.colorView];
self.colorLabel = [[UILabel alloc] init];
self.colorLabel.backgroundColor = [UIColor clearColor];
self.colorLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.colorLabel];
}
return self;
}
Expand All @@ -48,11 +50,10 @@ - (void)layoutSubviews {
self.brightnessSlider.frame = CGRectMake(CGRectGetMinX(contentRect), CGRectGetMaxY(contentRect) - sliderHeight, CGRectGetWidth(contentRect), sliderHeight);
self.saturationSlider.frame = CGRectMake(CGRectGetMinX(contentRect), CGRectGetMinY(self.brightnessSlider.frame) - PADDING - sliderHeight, CGRectGetWidth(contentRect), sliderHeight);


self.colorView.frame = CGRectMake(CGRectGetMinX(self.bounds), CGRectGetMinY(contentRect), CGRectGetWidth(self.bounds), 80.0);

CGFloat hueWheelSide = CGRectGetWidth(contentRect) - 40.0;
self.hueWheel.frame = CGRectMake(CGRectGetMidX(contentRect) - hueWheelSide / 2.0, CGRectGetMinY(self.saturationSlider.frame) - PADDING - hueWheelSide, hueWheelSide, hueWheelSide);

self.colorLabel.frame = CGRectMake(CGRectGetMinX(contentRect), CGRectGetMinY(contentRect), CGRectGetWidth(contentRect), CGRectGetMinY(self.hueWheel.frame) - CGRectGetMinY(contentRect));
}

- (void)saturationDidChange:(CGFloat)saturation {
Expand All @@ -76,8 +77,6 @@ - (void)setHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)b
[self.brightnessSlider setEndColor:[UIColor colorWithHue:hue saturation:saturation brightness:1.0 alpha:1.0]];
[self.brightnessSlider setSliderValue:brightness];

self.colorView.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1.0];

[self.hueWheel setHue:hue saturation:saturation brightness:brightness];
}

Expand All @@ -91,4 +90,22 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
return hitView;
}

- (void)setColorDescriptionKeys:(NSArray *)keys values:(NSArray *)values {
NSMutableAttributedString *colorString = [[NSMutableAttributedString alloc] init];

NSDictionary *keyAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:25.0], NSForegroundColorAttributeName : [UIColor colorWithWhite:0.5 alpha:1.0]};
NSDictionary *valueAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:25.0], NSForegroundColorAttributeName : [UIColor colorWithWhite:0.95 alpha:1.0]};

NSInteger i = 0;
for (NSString *key in keys) {
NSAttributedString *keyString = [[NSAttributedString alloc] initWithString:key attributes:keyAttributes];
[colorString appendAttributedString:keyString];
NSAttributedString *valueString = [[NSAttributedString alloc] initWithString:[values[i] description] attributes:valueAttributes];
[colorString appendAttributedString:valueString];
[colorString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
i++;
}
self.colorLabel.attributedText = colorString;
}

@end
5 changes: 1 addition & 4 deletions example/ESCColorPicker/ESCHueWheel.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ - (id)initWithFrame:(CGRect)frame {
NSMutableArray *colorSwatchViews = [NSMutableArray arrayWithCapacity:numberOfSwatches];
for (NSInteger i = 0; i < numberOfSwatches; i++) {
UIView *colorSwatchView = [[UIView alloc] init];
colorSwatchView.backgroundColor = [UIColor orangeColor];
//colorSwatchView.layer.edgeAntialiasingMask = kCALayerBottomEdge | kCALayerLeftEdge | kCALayerRightEdge | kCALayerTopEdge;
colorSwatchView.layer.shouldRasterize = YES;
colorSwatchView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
[self.wheel addSubview:colorSwatchView];
[colorSwatchViews addObject:colorSwatchView];
[self.wheel addSubview:colorSwatchView];
}
self.colorSwatchViews = colorSwatchViews;


self.wheelCenterBorder = [[UIView alloc] init];
self.wheelCenterBorder.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
self.wheelCenterBorder.layer.shadowColor = [UIColor blackColor].CGColor;
Expand Down

0 comments on commit 23cc658

Please sign in to comment.