-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d91dd2c
commit ebb5ac3
Showing
19 changed files
with
212 additions
and
96 deletions.
There are no files selected for viewing
20 changes: 6 additions & 14 deletions
20
android/src/main/java/com/newarchtricks/NewArchTricksModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 25 additions & 7 deletions
32
android/src/main/java/com/newarchtricks/NewArchTricksPackage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
package com.newarchtricks | ||
|
||
import com.facebook.react.ReactPackage | ||
import com.facebook.react.TurboReactPackage | ||
import com.facebook.react.bridge.NativeModule | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.module.model.ReactModuleInfo | ||
import com.facebook.react.module.model.ReactModuleInfoProvider | ||
import com.facebook.react.uimanager.ViewManager | ||
|
||
|
||
class NewArchTricksPackage : ReactPackage { | ||
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> { | ||
return listOf(NewArchTricksModule(reactContext)) | ||
} | ||
|
||
class NewArchTricksPackage : TurboReactPackage() { | ||
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> { | ||
return emptyList() | ||
return listOf(NewArchTricksViewManager()) | ||
} | ||
|
||
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? = | ||
if (name == NewArchTricksModule.NAME) { | ||
NewArchTricksModule(reactContext) | ||
} else { | ||
null | ||
} | ||
|
||
override fun getReactModuleInfoProvider() = ReactModuleInfoProvider { | ||
mapOf( | ||
NewArchTricksModule.NAME to ReactModuleInfo( | ||
NewArchTricksModule.NAME, | ||
NewArchTricksModule.NAME, | ||
false, // canOverrideExistingModule | ||
false, // needsEagerInit | ||
false, // isCxxModule | ||
true // isTurboModule | ||
) | ||
) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
android/src/main/java/com/newarchtricks/NewArchTricksViewManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.newarchtricks | ||
|
||
import android.graphics.Color | ||
import android.view.View | ||
import com.facebook.react.module.annotations.ReactModule | ||
import com.facebook.react.uimanager.SimpleViewManager | ||
import com.facebook.react.uimanager.ThemedReactContext | ||
import com.facebook.react.uimanager.annotations.ReactProp | ||
import com.facebook.react.viewmanagers.NewArchTricksViewManagerDelegate | ||
import com.facebook.react.viewmanagers.NewArchTricksViewManagerInterface | ||
|
||
@ReactModule(name = NewArchTricksViewManager.NAME) | ||
class NewArchTricksViewManager : SimpleViewManager<View>(), NewArchTricksViewManagerInterface<View> { | ||
private val delegate = NewArchTricksViewManagerDelegate(this) | ||
|
||
override fun getDelegate() = delegate | ||
override fun getName() = NAME | ||
|
||
override fun createViewInstance(reactContext: ThemedReactContext): View { | ||
return View(reactContext) | ||
} | ||
|
||
@ReactProp(name = "color") | ||
override fun setColor(view: View?, color: String?) { | ||
view!!.setBackgroundColor(Color.parseColor(color)) | ||
} | ||
|
||
companion object { | ||
const val NAME = "NewArchTricksView" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,4 @@ | ||
#import <NewArchTricksSpec/NewArchTricksSpec.h> | ||
|
||
#ifdef RCT_NEW_ARCH_ENABLED | ||
#import "RNNewArchTricksSpec.h" | ||
|
||
@interface NewArchTricks : NSObject <NativeNewArchTricksSpec> | ||
#else | ||
#import <React/RCTBridgeModule.h> | ||
|
||
@interface NewArchTricks : NSObject <RCTBridgeModule> | ||
#endif | ||
|
||
@interface NewArchTricks : NSObject <NativeNewArchTricksModuleSpec> | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
#import "NewArchTricks.h" | ||
|
||
#import <React/RCTBridge+Private.h> | ||
#import <React/RCTBridge.h> | ||
#import <React/RCTBridgeModule.h> | ||
#import <React/RCTUtils.h> | ||
#import <ReactCommon/CallInvoker.h> | ||
#import <ReactCommon/RCTTurboModule.h> | ||
|
||
#import <react/renderer/uimanager/primitives.h> | ||
|
||
@implementation NewArchTricks | ||
RCT_EXPORT_MODULE() | ||
|
||
// Example method | ||
// See // https://reactnative.dev/docs/native-modules-ios | ||
RCT_EXPORT_METHOD(multiply:(double)a | ||
b:(double)b | ||
resolve:(RCTPromiseResolveBlock)resolve | ||
reject:(RCTPromiseRejectBlock)reject) | ||
- (NSNumber *)multiply:(double)a b:(double)b | ||
{ | ||
NSNumber *result = @(a * b); | ||
|
||
resolve(result); | ||
return [[NSNumber alloc] initWithDouble:a * b]; | ||
} | ||
|
||
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params | ||
{ | ||
return std::make_shared<facebook::react::NativeNewArchTricksModuleSpecJSI>(params); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#import <UIKit/UIKit.h> | ||
#import <React/RCTViewComponentView.h> | ||
|
||
@interface NewArchTricksViewComponentView : RCTViewComponentView | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#import "NewArchTricksViewComponentView.h" | ||
|
||
// without this header the provider won't be found by RN | ||
#import <React/RCTFabricComponentsPlugins.h> | ||
|
||
#import <react/renderer/components/NewArchTricksSpec/ComponentDescriptors.h> | ||
#import <react/renderer/components/NewArchTricksSpec/EventEmitters.h> | ||
#import <react/renderer/components/NewArchTricksSpec/Props.h> | ||
#import <react/renderer/components/NewArchTricksSpec/RCTComponentViewHelpers.h> | ||
|
||
using namespace facebook::react; | ||
|
||
@interface NewArchTricksViewComponentView () <RCTNewArchTricksViewViewProtocol> | ||
@end | ||
|
||
@implementation NewArchTricksViewComponentView { | ||
UIView *_customView; | ||
} | ||
|
||
// Needed because of this: https://github.com/facebook/react-native/pull/37274 | ||
+ (void)load | ||
{ | ||
[super load]; | ||
} | ||
|
||
- (instancetype)initWithFrame:(CGRect)frame | ||
{ | ||
if (self = [super initWithFrame:frame]) { | ||
static const auto defaultProps = std::make_shared<const NewArchTricksViewProps>(); | ||
_props = defaultProps; | ||
_customView = [[UIView alloc] initWithFrame:self.bounds]; | ||
|
||
self.contentView = _customView; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- 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]; | ||
} | ||
|
||
#pragma mark - RCTComponentViewProtocol | ||
|
||
+ (ComponentDescriptorProvider)componentDescriptorProvider | ||
{ | ||
return concreteComponentDescriptorProvider<NewArchTricksViewComponentDescriptor>(); | ||
} | ||
|
||
- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps | ||
{ | ||
const auto &newProps = *std::static_pointer_cast<const NewArchTricksViewProps>(props); | ||
|
||
[_customView setBackgroundColor:[self hexStringToColor:[NSString stringWithUTF8String:newProps.color.c_str()]]]; | ||
|
||
[super updateProps:props oldProps:oldProps]; | ||
} | ||
@end | ||
|
||
Class<RCTComponentViewProtocol> NewArchTricksViewCls(void) | ||
{ | ||
return NewArchTricksViewComponentView.class; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#import <React/RCTViewManager.h> | ||
|
||
@interface NewArchTricksViewManager : RCTViewManager | ||
@end | ||
|
||
@implementation NewArchTricksViewManager | ||
|
||
RCT_EXPORT_MODULE(NewArchTricksView) | ||
RCT_EXPORT_VIEW_PROPERTY(color, NSString) | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import NativeNewArchTricksModule from './specs/NativeNewArchTricksModule'; | ||
|
||
export function multiply(a: number, b: number): number { | ||
return NativeNewArchTricksModule.multiply(a, b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import NewArchTricksNativeComponent from './specs/NewArchTricksNativeComponent'; | ||
|
||
export { NewArchTricksNativeComponent as NewArchTricksView }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,2 @@ | ||
import { NativeModules, Platform } from 'react-native'; | ||
|
||
const LINKING_ERROR = | ||
`The package 'react-native-new-arch-tricks' doesn't seem to be linked. Make sure: \n\n` + | ||
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + | ||
'- You rebuilt the app after installing the package\n' + | ||
'- You are not using Expo Go\n'; | ||
|
||
const NewArchTricks = NativeModules.NewArchTricks | ||
? NativeModules.NewArchTricks | ||
: new Proxy( | ||
{}, | ||
{ | ||
get() { | ||
throw new Error(LINKING_ERROR); | ||
}, | ||
} | ||
); | ||
|
||
export function multiply(a: number, b: number): Promise<number> { | ||
return NewArchTricks.multiply(a, b); | ||
} | ||
export { NewArchTricksView } from './NewArchTricksView'; | ||
export { multiply } from './NewArchTricksModule'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { TurboModule } from 'react-native'; | ||
import { TurboModuleRegistry } from 'react-native'; | ||
|
||
export interface Spec extends TurboModule { | ||
multiply(a: number, b: number): number; | ||
} | ||
|
||
export default TurboModuleRegistry.getEnforcing<Spec>('NewArchTricks'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent'; | ||
import type { ViewProps } from 'react-native'; | ||
|
||
interface NativeProps extends ViewProps { | ||
color: string; | ||
} | ||
|
||
export default codegenNativeComponent<NativeProps>('NewArchTricksView'); |