-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy path{%- project.name %}View.mm
More file actions
71 lines (52 loc) · 2.14 KB
/
{%- project.name %}View.mm
File metadata and controls
71 lines (52 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#import "<%- project.name -%>View.h"
#import "generated/<%- project.name -%>ViewSpec/ComponentDescriptors.h"
#import "generated/<%- project.name -%>ViewSpec/EventEmitters.h"
#import "generated/<%- project.name -%>ViewSpec/Props.h"
#import "generated/<%- project.name -%>ViewSpec/RCTComponentViewHelpers.h"
#import "RCTFabricComponentsPlugins.h"
using namespace facebook::react;
@interface <%- project.name -%>View () <RCT<%- project.name -%>ViewViewProtocol>
@end
@implementation <%- project.name -%>View {
UIView * _view;
}
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<<%- project.name -%>ViewComponentDescriptor>();
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const <%- project.name -%>ViewProps>();
_props = defaultProps;
_view = [[UIView alloc] init];
self.contentView = _view;
}
return self;
}
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &oldViewProps = *std::static_pointer_cast<<%- project.name -%>ViewProps const>(_props);
const auto &newViewProps = *std::static_pointer_cast<<%- project.name -%>ViewProps const>(props);
if (oldViewProps.color != newViewProps.color) {
NSString * colorToConvert = [[NSString alloc] initWithUTF8String: newViewProps.color.c_str()];
[_view setBackgroundColor:[self hexStringToColor:colorToConvert]];
}
[super updateProps:props oldProps:oldProps];
}
Class<RCTComponentViewProtocol> <%- project.name -%>ViewCls(void)
{
return <%- project.name -%>View.class;
}
- hexStringToColor:(NSString *)stringToConvert
{
NSString *noHashString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""];
NSScanner *stringScanner = [NSScanner scannerWithString:noHashString];
unsigned hex;
if (![stringScanner scanHexInt:&hex]) return nil;
int r = (hex >> 16) & 0xFF;
int g = (hex >> 8) & 0xFF;
int b = (hex) & 0xFF;
return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f];
}
@end