Skip to content

Commit

Permalink
Support string command arguments
Browse files Browse the repository at this point in the history
Summary: Support command arguments that are strings

Reviewed By: JoshuaGross

Differential Revision: D16509728

fbshipit-source-id: 003aba66231d204071d043c01cb0781150d0edb9
  • Loading branch information
elicwhite authored and facebook-github-bot committed Jul 29, 2019
1 parent a174647 commit 0314305
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 11 deletions.
7 changes: 6 additions & 1 deletion packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export type CommandsFunctionTypeParamAnnotation = $ReadOnly<{|

export type CommandsTypeAnnotation =
| BooleanTypeAnnotation
| Int32TypeAnnotation;
| Int32TypeAnnotation
| StringTypeAnnotation;

export type BooleanTypeAnnotation = $ReadOnly<{|
type: 'BooleanTypeAnnotation',
Expand All @@ -32,6 +33,10 @@ export type Int32TypeAnnotation = $ReadOnly<{|
type: 'Int32TypeAnnotation',
|}>;

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

export type ObjectPropertyType =
| $ReadOnly<{|
type: 'BooleanTypeAnnotation',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ function getObjCParamType(param: CommandsFunctionTypeParamAnnotation): string {
return 'BOOL';
case 'Int32TypeAnnotation':
return 'NSInteger';
case 'StringTypeAnnotation':
return 'NSString *';
default:
(param.typeAnnotation.type: empty);
throw new Error('Received invalid param type annotation');
Expand All @@ -121,6 +123,8 @@ function getObjCExpectedKindParamType(
return '[NSNumber class]';
case 'Int32TypeAnnotation':
return '[NSNumber class]';
case 'StringTypeAnnotation':
return '[NSString class]';
default:
(param.typeAnnotation.type: empty);
throw new Error('Received invalid param type annotation');
Expand All @@ -135,6 +139,8 @@ function getReadableExpectedKindParamType(
return 'boolean';
case 'Int32TypeAnnotation':
return 'number';
case 'StringTypeAnnotation':
return 'string';
default:
(param.typeAnnotation.type: empty);
throw new Error('Received invalid param type annotation');
Expand All @@ -150,6 +156,8 @@ function getObjCRightHandAssignmentParamType(
return `[(NSNumber *)arg${index} boolValue]`;
case 'Int32TypeAnnotation':
return `[(NSNumber *)arg${index} intValue]`;
case 'StringTypeAnnotation':
return `(NSString *)arg${index}`;
default:
(param.typeAnnotation.type: empty);
throw new Error('Received invalid param type annotation');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ function getCommandArgJavaType(param) {
return 'getBoolean';
case 'Int32TypeAnnotation':
return 'getInt';
case 'StringTypeAnnotation':
return 'getString';
default:
(param.typeAnnotation.type: empty);
throw new Error('Receieved invalid typeAnnotation');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ function getCommandArgJavaType(param) {
return 'boolean';
case 'Int32TypeAnnotation':
return 'int';
case 'StringTypeAnnotation':
return 'String';
default:
(param.typeAnnotation.type: empty);
throw new Error('Receieved invalid typeAnnotation');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,12 @@ const COMMANDS: SchemaType = {
type: 'Int32TypeAnnotation',
},
},
{
name: 'message',
typeAnnotation: {
type: 'StringTypeAnnotation',
},
},
{
name: 'animated',
typeAnnotation: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ NS_ASSUME_NONNULL_BEGIN
@protocol CommandNativeComponentViewProtocol <NSObject>
- (void)flashScrollIndicators;
- (void)allTypes:(NSInteger)x animated:(BOOL)animated;
- (void)allTypes:(NSInteger)x message:(NSString *)message animated:(BOOL)animated;
@end
RCT_EXTERN inline void CommandNativeComponentHandleCommand(
Expand All @@ -110,8 +110,8 @@ RCT_EXTERN inline void CommandNativeComponentHandleCommand(
if ([commandName isEqualToString:@\\"allTypes\\"]) {
#if RCT_DEBUG
if ([args count] != 2) {
RCTLogError(@\\"%@ command %@ received %d arguments, expected %d.\\", @\\"CommandNativeComponent\\", commandName, (int)[args count], 2);
if ([args count] != 3) {
RCTLogError(@\\"%@ command %@ received %d arguments, expected %d.\\", @\\"CommandNativeComponent\\", commandName, (int)[args count], 3);
return;
}
#endif
Expand All @@ -126,13 +126,21 @@ if ([commandName isEqualToString:@\\"allTypes\\"]) {
#if RCT_DEBUG
NSObject *arg1 = args[1];
if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSNumber class], @\\"boolean\\", @\\"CommandNativeComponent\\", commandName, @\\"2nd\\")) {
if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSString class], @\\"string\\", @\\"CommandNativeComponent\\", commandName, @\\"2nd\\")) {
return;
}
#endif
NSString * message = (NSString *)arg1;
#if RCT_DEBUG
NSObject *arg2 = args[2];
if (!RCTValidateTypeOfViewCommandArgument(arg2, [NSNumber class], @\\"boolean\\", @\\"CommandNativeComponent\\", commandName, @\\"3rd\\")) {
return;
}
#endif
BOOL animated = [(NSNumber *)arg1 boolValue];
BOOL animated = [(NSNumber *)arg2 boolValue];
[componentView allTypes:x animated:animated]
[componentView allTypes:x message:message animated:animated]
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public class CommandNativeComponentViewManagerDelegate<T extends View> {
viewManager.flashScrollIndicators(view);
break;
case \\"allTypes\\":
viewManager.allTypes(view, args.getInt(0), args.getBoolean(1));
viewManager.allTypes(view, args.getInt(0), args.getString(1), args.getBoolean(2));
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 CommandNativeComponentViewManagerInterface<T extends View> {
// No props
void flashScrollIndicators(T view);
void allTypes(T view, int x, boolean animated);
void allTypes(T view, int x, String message, boolean animated);
}
",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ export const Commands = {
);
},
allTypes(ref, x, animated) {
allTypes(ref, x, message, animated) {
UIManager.dispatchViewCommand(
findNodeHandle(ref),
UIManager.getViewManagerConfig(\\"CommandNativeComponent\\").Commands.allTypes,
[x, animated]
[x, message, animated]
);
}
};
Expand Down

0 comments on commit 0314305

Please sign in to comment.