RCTComponentViewHelpers.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <Foundation/Foundation.h>
  8. #import <React/RCTDefines.h>
  9. #import <React/RCTLog.h>
  10. NS_ASSUME_NONNULL_BEGIN
  11. @protocol RCTScrollViewProtocol <NSObject>
  12. - (void)flashScrollIndicators;
  13. - (void)scrollTo:(double)x y:(double)y animated:(BOOL)animated;
  14. - (void)scrollToEnd:(BOOL)animated;
  15. @end
  16. RCT_EXTERN inline void
  17. RCTScrollViewHandleCommand(id<RCTScrollViewProtocol> componentView, NSString const *commandName, NSArray const *args)
  18. {
  19. if ([commandName isEqualToString:@"flashScrollIndicators"]) {
  20. #if RCT_DEBUG
  21. if ([args count] != 0) {
  22. RCTLogError(
  23. @"%@ command %@ received %d arguments, expected %d.", @"ScrollView", commandName, (int)[args count], 1);
  24. return;
  25. }
  26. #endif
  27. [componentView flashScrollIndicators];
  28. return;
  29. }
  30. if ([commandName isEqualToString:@"scrollTo"]) {
  31. #if RCT_DEBUG
  32. if ([args count] != 3) {
  33. RCTLogError(
  34. @"%@ command %@ received %d arguments, expected %d.", @"ScrollView", commandName, (int)[args count], 3);
  35. return;
  36. }
  37. #endif
  38. NSObject *arg0 = args[0];
  39. #if RCT_DEBUG
  40. if (!RCTValidateTypeOfViewCommandArgument(arg0, [NSNumber class], @"float", @"ScrollView", commandName, @"1st")) {
  41. return;
  42. }
  43. #endif
  44. NSObject *arg1 = args[1];
  45. #if RCT_DEBUG
  46. if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSNumber class], @"float", @"ScrollView", commandName, @"2nd")) {
  47. return;
  48. }
  49. #endif
  50. NSObject *arg2 = args[2];
  51. #if RCT_DEBUG
  52. if (!RCTValidateTypeOfViewCommandArgument(arg2, [NSNumber class], @"boolean", @"ScrollView", commandName, @"3rd")) {
  53. return;
  54. }
  55. #endif
  56. double x = [(NSNumber *)arg0 doubleValue];
  57. double y = [(NSNumber *)arg1 doubleValue];
  58. BOOL animated = [(NSNumber *)arg2 boolValue];
  59. [componentView scrollTo:x y:y animated:animated];
  60. return;
  61. }
  62. if ([commandName isEqualToString:@"scrollToEnd"]) {
  63. #if RCT_DEBUG
  64. if ([args count] != 1) {
  65. RCTLogError(
  66. @"%@ command %@ received %d arguments, expected %d.", @"ScrollView", commandName, (int)[args count], 1);
  67. return;
  68. }
  69. #endif
  70. NSObject *arg0 = args[0];
  71. #if RCT_DEBUG
  72. if (!RCTValidateTypeOfViewCommandArgument(arg0, [NSNumber class], @"boolean", @"ScrollView", commandName, @"1st")) {
  73. return;
  74. }
  75. #endif
  76. BOOL animated = [(NSNumber *)arg0 boolValue];
  77. [componentView scrollToEnd:animated];
  78. return;
  79. }
  80. }
  81. NS_ASSUME_NONNULL_END