Skip to content

Commit 1ba688a

Browse files
committed
feat: create objc part
1 parent 9d0a89e commit 1ba688a

File tree

7 files changed

+199
-0
lines changed

7 files changed

+199
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef Utils_h
2+
#define Utils_h
3+
4+
@interface Utils : NSObject
5+
+ hexStringToColor:(NSString *)stringToConvert;
6+
@end
7+
8+
#endif /* Utils_h */
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#import <Foundation/Foundation.h>
2+
#import "Utils.h"
3+
#import <UIKit/UIKit.h>
4+
5+
@implementation Utils
6+
7+
+ hexStringToColor:(NSString *)stringToConvert
8+
{
9+
NSString *noHashString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""];
10+
NSScanner *stringScanner = [NSScanner scannerWithString:noHashString];
11+
12+
unsigned hex;
13+
if (![stringScanner scanHexInt:&hex]) return nil;
14+
int r = (hex >> 16) & 0xFF;
15+
int g = (hex >> 8) & 0xFF;
16+
int b = (hex) & 0xFF;
17+
18+
return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f];
19+
}
20+
21+
+ (id)alloc {
22+
[NSException raise:@"Cannot be instantiated!" format:@"Static class 'Utils' cannot be instantiated!"];
23+
return nil;
24+
}
25+
26+
@end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<% if (project.cpp) { -%>
2+
#ifdef __cplusplus
3+
#import "<%- project.identifier -%>.h"
4+
#endif
5+
<% } -%>
6+
7+
#ifdef RCT_NEW_ARCH_ENABLED
8+
#import "RN<%- project.name -%>Spec.h"
9+
10+
@interface <%- project.name -%> : NSObject <Native<%- project.name -%>Spec>
11+
#else
12+
#import <React/RCTBridgeModule.h>
13+
14+
@interface <%- project.name -%> : NSObject <RCTBridgeModule>
15+
#endif
16+
17+
@end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#import "<%- project.name -%>.h"
2+
3+
@implementation <%- project.name -%>
4+
5+
RCT_EXPORT_MODULE()
6+
7+
<% if (project.arch === 'legacy' || project.arch === 'mixed') { -%>
8+
// Example method
9+
// See // https://reactnative.dev/docs/native-modules-ios
10+
RCT_EXPORT_METHOD(multiply:(double)a
11+
b:(double)b
12+
resolve:(RCTPromiseResolveBlock)resolve
13+
reject:(RCTPromiseRejectBlock)reject)
14+
{
15+
<% if (project.cpp) { -%>
16+
NSNumber *result = @(<%- project.package_cpp -%>::multiply(a, b));
17+
<% } else { -%>
18+
NSNumber *result = @(a * b);
19+
<% } -%>
20+
21+
resolve(result);
22+
}
23+
24+
<% } -%>
25+
<% if (project.arch === 'new' || project.arch === 'mixed') { -%>
26+
// Don't compile this code when we build for the old architecture.
27+
#ifdef RCT_NEW_ARCH_ENABLED
28+
<% if (project.arch === 'new') { -%>
29+
- (NSNumber *)multiply:(double)a b:(double)b {
30+
<% if (project.cpp) { -%>
31+
NSNumber *result = @(<%- project.package_cpp -%>::multiply(a, b));
32+
<% } else { -%>
33+
NSNumber *result = @(a * b);
34+
<% } -%>
35+
36+
return result;
37+
}
38+
39+
<% } -%>
40+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
41+
(const facebook::react::ObjCTurboModule::InitParams &)params
42+
{
43+
return std::make_shared<facebook::react::Native<%- project.name -%>SpecJSI>(params);
44+
}
45+
#endif
46+
<% } -%>
47+
48+
@end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This guard prevent this file to be compiled in the old architecture.
2+
#ifdef RCT_NEW_ARCH_ENABLED
3+
#import <React/RCTViewComponentView.h>
4+
#import <UIKit/UIKit.h>
5+
6+
#ifndef <%- project.name -%>ViewNativeComponent_h
7+
#define <%- project.name -%>ViewNativeComponent_h
8+
9+
NS_ASSUME_NONNULL_BEGIN
10+
11+
@interface <%- project.name -%>View : RCTViewComponentView
12+
@end
13+
14+
NS_ASSUME_NONNULL_END
15+
16+
#endif /* <%- project.name -%>ViewNativeComponent_h */
17+
#endif /* RCT_NEW_ARCH_ENABLED */
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifdef RCT_NEW_ARCH_ENABLED
2+
#import "<%- project.name -%>View.h"
3+
4+
#import <react/renderer/components/RN<%- project.name -%>ViewSpec/ComponentDescriptors.h>
5+
#import <react/renderer/components/RN<%- project.name -%>ViewSpec/EventEmitters.h>
6+
#import <react/renderer/components/RN<%- project.name -%>ViewSpec/Props.h>
7+
#import <react/renderer/components/RN<%- project.name -%>ViewSpec/RCTComponentViewHelpers.h>
8+
9+
#import "RCTFabricComponentsPlugins.h"
10+
#import "Utils.h"
11+
12+
using namespace facebook::react;
13+
14+
@interface <%- project.name -%>View () <RCT<%- project.name -%>ViewViewProtocol>
15+
16+
@end
17+
18+
@implementation <%- project.name -%>View {
19+
UIView * _view;
20+
}
21+
22+
+ (ComponentDescriptorProvider)componentDescriptorProvider
23+
{
24+
return concreteComponentDescriptorProvider<<%- project.name -%>ViewComponentDescriptor>();
25+
}
26+
27+
- (instancetype)initWithFrame:(CGRect)frame
28+
{
29+
if (self = [super initWithFrame:frame]) {
30+
static const auto defaultProps = std::make_shared<const <%- project.name -%>ViewProps>();
31+
_props = defaultProps;
32+
33+
_view = [[UIView alloc] init];
34+
35+
self.contentView = _view;
36+
}
37+
38+
return self;
39+
}
40+
41+
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
42+
{
43+
const auto &oldViewProps = *std::static_pointer_cast<<%- project.name -%>ViewProps const>(_props);
44+
const auto &newViewProps = *std::static_pointer_cast<<%- project.name -%>ViewProps const>(props);
45+
46+
if (oldViewProps.color != newViewProps.color) {
47+
NSString * colorToConvert = [[NSString alloc] initWithUTF8String: newViewProps.color.c_str()];
48+
[_view setBackgroundColor: [Utils hexStringToColor:colorToConvert]];
49+
}
50+
51+
[super updateProps:props oldProps:oldProps];
52+
}
53+
54+
Class<RCTComponentViewProtocol> <%- project.name -%>ViewCls(void)
55+
{
56+
return <%- project.name -%>View.class;
57+
}
58+
59+
@end
60+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#import <React/RCTViewManager.h>
2+
#import <React/RCTUIManager.h>
3+
#import "RCTBridge.h"
4+
#import "Utils.h"
5+
6+
@interface <%- project.name -%>ViewManager : RCTViewManager
7+
@end
8+
9+
@implementation <%- project.name -%>ViewManager
10+
11+
RCT_EXPORT_MODULE(<%- project.name -%>View)
12+
13+
- (UIView *)view
14+
{
15+
return [[UIView alloc] init];
16+
}
17+
18+
RCT_CUSTOM_VIEW_PROPERTY(color, NSString, UIView)
19+
{
20+
[view setBackgroundColor: [Utils hexStringToColor:json]];
21+
}
22+
23+
@end

0 commit comments

Comments
 (0)