FLEXMethodCallingViewController.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // FLEXMethodCallingViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/23/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXMethodCallingViewController.h"
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXFieldEditorView.h"
  11. #import "FLEXObjectExplorerFactory.h"
  12. #import "FLEXObjectExplorerViewController.h"
  13. #import "FLEXArgumentInputView.h"
  14. #import "FLEXArgumentInputViewFactory.h"
  15. @interface FLEXMethodCallingViewController ()
  16. @property (nonatomic, assign) Method method;
  17. @end
  18. @implementation FLEXMethodCallingViewController
  19. - (id)initWithTarget:(id)target method:(Method)method
  20. {
  21. self = [super initWithTarget:target];
  22. if (self) {
  23. self.method = method;
  24. self.title = [self isClassMethod] ? @"Class Method" : @"Method";
  25. }
  26. return self;
  27. }
  28. - (void)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. self.fieldEditorView.fieldDescription = [FLEXRuntimeUtility prettyNameForMethod:self.method isClassMethod:[self isClassMethod]];
  32. NSArray *methodComponents = [FLEXRuntimeUtility prettyArgumentComponentsForMethod:self.method];
  33. NSMutableArray *argumentInputViews = [NSMutableArray array];
  34. unsigned int argumentIndex = kFLEXNumberOfImplicitArgs;
  35. for (NSString *methodComponent in methodComponents) {
  36. FLEXArgumentInputView *inputView = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:NULL];
  37. inputView.backgroundColor = self.view.backgroundColor;
  38. inputView.title = methodComponent;
  39. // Prepopulate the structs that we parse from strings with the default values.
  40. // This shows the intended formatting which would be less clear otherwise.
  41. char *argumentTypeEncoding = method_copyArgumentType(self.method, argumentIndex);
  42. NSValue *defaultValue = [[self class] defaultValueForEncoding:argumentTypeEncoding];
  43. if (defaultValue) {
  44. inputView.inputOutput = [FLEXRuntimeUtility editiableDescriptionForObject:defaultValue];
  45. }
  46. free(argumentTypeEncoding);
  47. [argumentInputViews addObject:inputView];
  48. argumentIndex++;
  49. }
  50. self.fieldEditorView.argumentInputViews = argumentInputViews;
  51. }
  52. - (BOOL)isClassMethod
  53. {
  54. return self.target && self.target == [self.target class];
  55. }
  56. - (NSString *)titleForActionButton
  57. {
  58. return @"Call";
  59. }
  60. - (void)actionButtonPressed:(id)sender
  61. {
  62. [super actionButtonPressed:sender];
  63. NSMutableArray *arguments = [NSMutableArray array];
  64. for (FLEXArgumentInputView *inputView in self.fieldEditorView.argumentInputViews) {
  65. NSString *argumentString = inputView.inputOutput;
  66. if (!argumentString) {
  67. // Use empty string as a placeholder in the array. It will be interpreted as nil.
  68. argumentString = @"";
  69. }
  70. [arguments addObject:argumentString];
  71. }
  72. id returnedObject = [FLEXRuntimeUtility performSelector:method_getName(self.method) onObject:self.target withArguments:arguments error:NULL];
  73. // For non-nil (or void) return types, push an explorer view controller to display the returned object
  74. if (returnedObject) {
  75. FLEXObjectExplorerViewController *explorerViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:returnedObject];
  76. [self.navigationController pushViewController:explorerViewController animated:YES];
  77. }
  78. }
  79. + (NSValue *)defaultValueForEncoding:(const char *)typeEncoding
  80. {
  81. NSValue *value = nil;
  82. if (strcmp(typeEncoding, @encode(CGRect)) == 0) {
  83. value = [NSValue valueWithCGRect:CGRectZero];
  84. } else if (strcmp(typeEncoding, @encode(CGSize)) == 0) {
  85. value = [NSValue valueWithCGPoint:CGPointZero];
  86. } else if (strcmp(typeEncoding, @encode(CGPoint)) == 0) {
  87. value = [NSValue valueWithCGSize:CGSizeZero];
  88. } else if (strcmp(typeEncoding, @encode(CGAffineTransform)) == 0) {
  89. value = [NSValue valueWithCGAffineTransform:CGAffineTransformIdentity];
  90. } else if (strcmp(typeEncoding, @encode(NSRange)) == 0) {
  91. value = [NSValue valueWithRange:NSMakeRange(0, 0)];
  92. } else if (strcmp(typeEncoding, @encode(UIEdgeInsets)) == 0) {
  93. value = [NSValue valueWithUIEdgeInsets:UIEdgeInsetsZero];
  94. } else if (strcmp(typeEncoding, @encode(UIOffset)) == 0) {
  95. value = [NSValue valueWithUIOffset:UIOffsetZero];
  96. }
  97. return value;
  98. }
  99. @end