Skip to content

Commit 5bab7f2

Browse files
author
Kevin Brown
committed
Clean up of objective C code and made JS much simpler too.
1 parent 3d595d3 commit 5bab7f2

File tree

4 files changed

+57
-106
lines changed

4 files changed

+57
-106
lines changed

RNSketch/RNSketch.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77
//
88

99
#import <UIKit/UIKit.h>
10-
11-
@class RCTEventDispatcher;
10+
#import "UIView+React.h"
1211

1312
@interface RNSketch : UIView
1413

15-
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispather NS_DESIGNATED_INITIALIZER;
16-
- (void)setFillColor:(UIColor *)fillColor;
17-
- (void)setStrokeColor:(UIColor *)strokeColor;
18-
- (void)setStrokeThickness:(NSInteger)strokeThickness;
19-
- (void)setClearButtonHidden:(BOOL)hidden;
20-
- (void)setImageType:(NSString *)imageType;
2114
- (void)clearDrawing;
15+
- (NSString *)base64Code;
16+
17+
// Events
18+
@property (nonatomic, copy) RCTBubblingEventBlock onReset;
19+
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
20+
21+
// Properties
22+
@property (nonatomic, strong) UIColor *fillColor;
23+
@property (nonatomic, strong) UIColor *strokeColor;
24+
@property (nonatomic, assign) NSInteger strokeThickness;
25+
@property (nonatomic, assign) BOOL clearButtonHidden;
26+
@property (nonatomic, strong) NSString *imageType;
2227

2328
@end

RNSketch/RNSketch.m

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,26 @@
1515
@implementation RNSketch
1616
{
1717
// Internal
18-
RCTEventDispatcher *_eventDispatcher;
1918
UIButton *_clearButton;
2019
UIBezierPath *_path;
2120
UIImage *_image;
2221
CGPoint _points[5];
2322
uint _counter;
24-
25-
// Configuration settings
26-
UIColor *_fillColor;
27-
UIColor *_strokeColor;
28-
NSString *_imageType;
2923
}
3024

3125

3226
#pragma mark - UIViewHierarchy methods
3327

3428

35-
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
29+
- (instancetype)initWithFrame:(CGRect) frame
3630
{
37-
if ((self = [super init])) {
31+
if ((self = [super initWithFrame:frame])) {
3832
// Internal setup
3933
self.multipleTouchEnabled = NO;
34+
4035
// For borderRadius property to work (CALayer's cornerRadius).
4136
self.layer.masksToBounds = YES;
42-
_eventDispatcher = eventDispatcher;
37+
4338
_path = [UIBezierPath bezierPath];
4439

4540
[self initClearButton];
@@ -48,6 +43,8 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
4843
return self;
4944
}
5045

46+
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
47+
5148
- (void)layoutSubviews
5249
{
5350
[super layoutSubviews];
@@ -93,11 +90,6 @@ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
9390
_counter = 0;
9491
UITouch *touch = [touches anyObject];
9592
_points[0] = [touch locationInView:self];
96-
97-
NSDictionary *bodyEvent = @{
98-
@"target": self.reactTag,
99-
};
100-
[_eventDispatcher sendInputEventWithName:@"onClearPlaceholder" body:bodyEvent];
10193
}
10294

10395
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
@@ -120,12 +112,7 @@ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
120112
[_path removeAllPoints];
121113
_counter = 0;
122114

123-
// Send event
124-
NSDictionary *bodyEvent = @{
125-
@"target": self.reactTag,
126-
@"image": [self drawingToString],
127-
};
128-
[_eventDispatcher sendInputEventWithName:@"topChange" body:bodyEvent];
115+
if (_onChange) _onChange(@{ @"imageData": [self drawingToString]});
129116
}
130117

131118
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
@@ -189,15 +176,21 @@ - (void)drawBitmap
189176

190177
- (NSString *)drawingToString
191178
{
179+
NSString *imageData = nil;
180+
192181
if ([_imageType isEqualToString:@"jpg"]) {
193-
return [UIImageJPEGRepresentation(_image, 1) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
182+
imageData = [UIImageJPEGRepresentation(_image, 1) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
194183
} else if ([_imageType isEqualToString:@"png"]) {
195-
return [UIImagePNGRepresentation(_image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
184+
imageData = [UIImagePNGRepresentation(_image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
185+
} else {
186+
[NSException raise:@"Invalid image type" format:@"%@ is not a valid image type for exporting the drawing.", _imageType];
196187
}
197188

198-
[NSException raise:@"Invalid image type" format:@"%@ is not a valid image type for exporting the drawing.", _imageType];
199-
200-
return nil;
189+
return [[self base64Code] stringByAppendingString:imageData];
190+
}
191+
192+
- (NSString *)base64Code {
193+
return [NSString stringWithFormat:@"data:image/%@;base64,", self.imageType];
201194
}
202195

203196

@@ -215,10 +208,8 @@ - (void)clearDrawing
215208
[self setNeedsDisplay];
216209

217210
// Send event
218-
NSDictionary *bodyEvent = @{
219-
@"target": self.reactTag,
220-
};
221-
[_eventDispatcher sendInputEventWithName:@"onReset" body:bodyEvent];
211+
if (_onReset) _onReset(@{});
212+
if (_onChange) _onChange(@{});
222213
}
223214

224215

@@ -245,4 +236,8 @@ - (void)setImageType:(NSString *)imageType
245236
_imageType = imageType;
246237
}
247238

239+
- (NSString *)getImageType {
240+
return _imageType;
241+
}
242+
248243
@end

RNSketch/RNSketchManager.m

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,18 @@ @implementation RNSketchManager
1919

2020
RCT_EXPORT_MODULE()
2121

22-
#pragma mark - Properties
22+
#pragma mark - Events
2323

24+
RCT_EXPORT_VIEW_PROPERTY(onReset, RCTBubblingEventBlock);
25+
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock);
2426

25-
RCT_CUSTOM_VIEW_PROPERTY(fillColor, UIColor, RNSketch)
26-
{
27-
[view setFillColor:json ? [RCTConvert UIColor:json] : [UIColor whiteColor]];
28-
}
29-
RCT_CUSTOM_VIEW_PROPERTY(strokeColor, UIColor, RNSketch)
30-
{
31-
[view setStrokeColor:json ? [RCTConvert UIColor:json] : [UIColor blackColor]];
32-
}
33-
RCT_CUSTOM_VIEW_PROPERTY(clearButtonHidden, BOOL, RNSketch)
34-
{
35-
[view setClearButtonHidden:json ? [RCTConvert BOOL:json] : NO];
36-
}
37-
RCT_CUSTOM_VIEW_PROPERTY(strokeThickness, NSInteger, RNSketch)
38-
{
39-
[view setStrokeThickness:json ? [RCTConvert NSInteger:json] : 1];
40-
}
41-
RCT_CUSTOM_VIEW_PROPERTY(imageType, NSString, RNSketch)
42-
{
43-
[view setImageType:json ? [RCTConvert NSString:json] : @"jpg"];
44-
}
27+
#pragma mark - Properties
28+
29+
RCT_EXPORT_VIEW_PROPERTY(fillColor, UIColor);
30+
RCT_EXPORT_VIEW_PROPERTY(strokeColor, UIColor);
31+
RCT_EXPORT_VIEW_PROPERTY(clearButtonHidden, BOOL);
32+
RCT_EXPORT_VIEW_PROPERTY(strokeThickness, NSInteger);
33+
RCT_EXPORT_VIEW_PROPERTY(imageType, NSString);
4534

4635
#pragma mark - Lifecycle
4736

@@ -57,32 +46,27 @@ - (instancetype)init
5746
- (UIView *)view
5847
{
5948
if (!self.sketchView) {
60-
self.sketchView = [[RNSketch alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
49+
self.sketchView = [[RNSketch alloc] initWithFrame:CGRectZero];
6150
}
6251

6352
return self.sketchView;
6453
}
6554

66-
#pragma mark - Event types
67-
68-
69-
- (NSArray *)customDirectEventTypes
70-
{
71-
return @[
72-
@"onReset",
73-
@"onClearPlaceholder"
74-
];
75-
}
76-
7755

7856
#pragma mark - Exported methods
7957

80-
8158
RCT_EXPORT_METHOD(saveImage:(NSString *)encodedImage
8259
ofType:(NSString *)imageType
8360
resolve:(RCTPromiseResolveBlock)resolve
8461
reject:(RCTPromiseRejectBlock)reject)
8562
{
63+
// Strip the Base 64 Code out if it's there.
64+
NSString *base64Code = [self.sketchView base64Code];
65+
encodedImage = [encodedImage stringByReplacingOccurrencesOfString:base64Code
66+
withString:@""
67+
options:NULL
68+
range:NSMakeRange(0, [base64Code length])];
69+
8670
// Create image data with base64 source
8771
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:encodedImage options:NSDataBase64DecodingIgnoreUnknownCharacters];
8872
if (!imageData) {

index.ios.js

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export default class Sketch extends React.Component {
2121

2222
static propTypes = {
2323
fillColor: string,
24+
onChange: func,
2425
onReset: func,
25-
onUpdate: func,
2626
clearButtonHidden: bool,
2727
onClearPlaceholder: func,
2828
strokeColor: string,
@@ -33,50 +33,20 @@ export default class Sketch extends React.Component {
3333

3434
static defaultProps = {
3535
fillColor: '#ffffff',
36+
onChange: () => {},
3637
onReset: () => {},
37-
onClearPlaceholder: () => {},
38-
onUpdate: () => {},
3938
clearButtonHidden: false,
4039
strokeColor: '#000000',
4140
strokeThickness: 1,
4241
style: null,
4342
imageType: 'jpg'
4443
};
4544

46-
constructor(props) {
47-
super(props);
48-
this.onReset = this.onReset.bind(this);
49-
this.onClearPlaceholder = this.onClearPlaceholder.bind(this);
50-
this.onUpdate = this.onUpdate.bind(this);
51-
this.getBase64Code = this.getBase64Code.bind(this);
52-
}
53-
54-
onReset() {
55-
this.props.onUpdate(null);
56-
this.props.onReset();
57-
}
58-
59-
getBase64Code() {
60-
return `data:image/${this.props.imageType};base64,`;
61-
}
62-
63-
onClearPlaceholder() {
64-
this.props.onClearPlaceholder();
65-
}
66-
67-
onUpdate(e) {
68-
const { onUpdate, imageType } = this.props;
69-
70-
onUpdate(`${this.getBase64Code()}${e.nativeEvent.image}`);
71-
}
72-
7345
saveImage(image) {
7446
if (typeof image !== 'string') {
7547
return Promise.reject('You need to provide a valid base64 encoded image.');
7648
}
7749

78-
const base64Code = this.getBase64Code();
79-
const src = image.indexOf(base64Code) === 0 ? image.replace(base64Code, '') : image;
8050
return SketchManager.saveImage(src, this.props.imageType);
8151
}
8252

@@ -88,9 +58,6 @@ export default class Sketch extends React.Component {
8858
return (
8959
<RNSketch
9060
{...this.props}
91-
onChange={this.onUpdate}
92-
onReset={this.onReset}
93-
onClearPlaceholder={this.onClearPlaceholder}
9461
style={[styles.base, this.props.style]}
9562
/>
9663
);

0 commit comments

Comments
 (0)