Skip to content

Commit

Permalink
Commands: support Float arguments
Browse files Browse the repository at this point in the history
Summary: Support codegen'ing commands with Float arguments.

Reviewed By: mdvacca

Differential Revision: D16785534

fbshipit-source-id: 8174ae40762c1114b87a023cb2b69b2515dc6e23
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Aug 13, 2019
1 parent 724fe11 commit 825e1c0
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 13 deletions.
5 changes: 5 additions & 0 deletions packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ export type CommandsFunctionTypeParamAnnotation = $ReadOnly<{|
export type CommandsTypeAnnotation =
| BooleanTypeAnnotation
| Int32TypeAnnotation
| FloatTypeAnnotation
| StringTypeAnnotation;

export type FloatTypeAnnotation = $ReadOnly<{|
type: 'FloatTypeAnnotation',
|}>;

export type BooleanTypeAnnotation = $ReadOnly<{|
type: 'BooleanTypeAnnotation',
|}>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ function getObjCParamType(param: CommandsFunctionTypeParamAnnotation): string {
switch (param.typeAnnotation.type) {
case 'BooleanTypeAnnotation':
return 'BOOL';
case 'FloatTypeAnnotation':
return 'float';
case 'Int32TypeAnnotation':
return 'NSInteger';
case 'StringTypeAnnotation':
Expand All @@ -121,6 +123,8 @@ function getObjCExpectedKindParamType(
switch (param.typeAnnotation.type) {
case 'BooleanTypeAnnotation':
return '[NSNumber class]';
case 'FloatTypeAnnotation':
return '[NSNumber class]';
case 'Int32TypeAnnotation':
return '[NSNumber class]';
case 'StringTypeAnnotation':
Expand All @@ -137,6 +141,8 @@ function getReadableExpectedKindParamType(
switch (param.typeAnnotation.type) {
case 'BooleanTypeAnnotation':
return 'boolean';
case 'FloatTypeAnnotation':
return 'float';
case 'Int32TypeAnnotation':
return 'number';
case 'StringTypeAnnotation':
Expand All @@ -154,6 +160,8 @@ function getObjCRightHandAssignmentParamType(
switch (param.typeAnnotation.type) {
case 'BooleanTypeAnnotation':
return `[(NSNumber *)arg${index} boolValue]`;
case 'FloatTypeAnnotation':
return `[(NSNumber *)arg${index} floatValue]`;
case 'Int32TypeAnnotation':
return `[(NSNumber *)arg${index} intValue]`;
case 'StringTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ function getCommandArgJavaType(param) {
switch (param.typeAnnotation.type) {
case 'BooleanTypeAnnotation':
return 'getBoolean';
case 'FloatTypeAnnotation':
return 'getFloat';
case 'Int32TypeAnnotation':
return 'getInt';
case 'StringTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ function getCommandArgJavaType(param) {
switch (param.typeAnnotation.type) {
case 'BooleanTypeAnnotation':
return 'boolean';
case 'FloatTypeAnnotation':
return 'float';
case 'Int32TypeAnnotation':
return 'int';
case 'StringTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,12 @@ const COMMANDS: SchemaType = {
type: 'Int32TypeAnnotation',
},
},
{
name: 'y',
typeAnnotation: {
type: 'FloatTypeAnnotation',
},
},
{
name: 'message',
typeAnnotation: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ NS_ASSUME_NONNULL_BEGIN
@protocol RCTCommandNativeComponentViewProtocol <NSObject>
- (void)flashScrollIndicators;
- (void)allTypes:(NSInteger)x message:(NSString *)message animated:(BOOL)animated;
- (void)allTypes:(NSInteger)x y:(float)y message:(NSString *)message animated:(BOOL)animated;
@end
RCT_EXTERN inline void RCTCommandNativeComponentHandleCommand(
Expand All @@ -110,8 +110,8 @@ RCT_EXTERN inline void RCTCommandNativeComponentHandleCommand(
if ([commandName isEqualToString:@\\"allTypes\\"]) {
#if RCT_DEBUG
if ([args count] != 3) {
RCTLogError(@\\"%@ command %@ received %d arguments, expected %d.\\", @\\"CommandNativeComponent\\", commandName, (int)[args count], 3);
if ([args count] != 4) {
RCTLogError(@\\"%@ command %@ received %d arguments, expected %d.\\", @\\"CommandNativeComponent\\", commandName, (int)[args count], 4);
return;
}
#endif
Expand All @@ -126,21 +126,29 @@ if ([commandName isEqualToString:@\\"allTypes\\"]) {
#if RCT_DEBUG
NSObject *arg1 = args[1];
if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSString class], @\\"string\\", @\\"CommandNativeComponent\\", commandName, @\\"2nd\\")) {
if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSNumber class], @\\"float\\", @\\"CommandNativeComponent\\", commandName, @\\"2nd\\")) {
return;
}
#endif
NSString * message = (NSString *)arg1;
float y = [(NSNumber *)arg1 floatValue];
#if RCT_DEBUG
NSObject *arg2 = args[2];
if (!RCTValidateTypeOfViewCommandArgument(arg2, [NSNumber class], @\\"boolean\\", @\\"CommandNativeComponent\\", commandName, @\\"3rd\\")) {
if (!RCTValidateTypeOfViewCommandArgument(arg2, [NSString class], @\\"string\\", @\\"CommandNativeComponent\\", commandName, @\\"3rd\\")) {
return;
}
#endif
BOOL animated = [(NSNumber *)arg2 boolValue];
NSString * message = (NSString *)arg2;
[componentView allTypes:x message:message animated:animated]
#if RCT_DEBUG
NSObject *arg3 = args[3];
if (!RCTValidateTypeOfViewCommandArgument(arg3, [NSNumber class], @\\"boolean\\", @\\"CommandNativeComponent\\", commandName, @\\"4th\\")) {
return;
}
#endif
BOOL animated = [(NSNumber *)arg3 boolValue];
[componentView allTypes:x y:y message:message animated:animated]
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public class CommandNativeComponentManagerDelegate<T extends View, U extends Bas
viewManager.flashScrollIndicators(view);
break;
case \\"allTypes\\":
viewManager.allTypes(view, args.getInt(0), args.getString(1), args.getBoolean(2));
viewManager.allTypes(view, args.getInt(0), args.getFloat(1), args.getString(2), args.getBoolean(3));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import android.view.View;
public interface CommandNativeComponentManagerInterface<T extends View> {
// No props
void flashScrollIndicators(T view);
void allTypes(T view, int x, String message, boolean animated);
void allTypes(T view, int x, float y, String message, boolean animated);
}
",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ export const Commands = {
dispatchCommand(ref, \\"flashScrollIndicators\\", []);
},
allTypes(ref, x, message, animated) {
dispatchCommand(ref, \\"allTypes\\", [x, message, animated]);
allTypes(ref, x, y, message, animated) {
dispatchCommand(ref, \\"allTypes\\", [x, y, message, animated]);
}
};
",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,14 +670,15 @@ const COMMANDS_DEFINED_WITH_ALL_TYPES = `
const codegenNativeCommands = require('codegenNativeCommands');
const codegenNativeComponent = require('codegenNativeComponent');
import type {Int32} from 'CodegenTypes';
import type {Int32, Float} from 'CodegenTypes';
import type {ViewProps} from 'ViewPropTypes';
import type {NativeComponent} from 'codegenNativeComponent';
interface NativeCommands {
+hotspotUpdate: (viewRef: React.Ref<'RCTView'>, x: Int32, y: Int32) => void;
+scrollTo: (
viewRef: React.Ref<'RCTView'>,
x: Float,
y: Int32,
animated: boolean,
) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,12 @@ Object {
"optional": false,
"typeAnnotation": Object {
"params": Array [
Object {
"name": "x",
"typeAnnotation": Object {
"type": "FloatTypeAnnotation",
},
},
Object {
"name": "y",
"typeAnnotation": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ function buildCommandSchema(property, types: TypeMap) {
type: 'Int32TypeAnnotation',
};
break;
case 'Float':
returnType = {
type: 'FloatTypeAnnotation',
};
break;
default:
(type: empty);
throw new Error(
Expand Down

0 comments on commit 825e1c0

Please sign in to comment.