You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.4 KiB

  1. //
  2. // FLEXArgumentInputJSONObjectView.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/15/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXArgumentInputJSONObjectView.h"
  9. #import "FLEXRuntimeUtility.h"
  10. @implementation FLEXArgumentInputJSONObjectView
  11. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
  12. {
  13. self = [super initWithArgumentTypeEncoding:typeEncoding];
  14. if (self) {
  15. // Start with the numbers and punctuation keyboard since quotes, curly braces, or
  16. // square brackets are likely to be the first characters type for the JSON.
  17. self.inputTextView.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
  18. self.targetSize = FLEXArgumentInputViewSizeLarge;
  19. }
  20. return self;
  21. }
  22. - (void)setInputValue:(id)inputValue
  23. {
  24. self.inputTextView.text = [FLEXRuntimeUtility editableJSONStringForObject:inputValue];
  25. }
  26. - (id)inputValue
  27. {
  28. return [FLEXRuntimeUtility objectValueFromEditableJSONString:self.inputTextView.text];
  29. }
  30. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
  31. {
  32. // Must be object type.
  33. BOOL supported = type && type[0] == '@';
  34. if (supported) {
  35. if (value) {
  36. // If there's a current value, it must be serializable to JSON
  37. supported = [FLEXRuntimeUtility editableJSONStringForObject:value] != nil;
  38. } else {
  39. // Otherwise, see if we have more type information than just 'id'.
  40. // If we do, make sure the encoding is something serializable to JSON.
  41. // Properties and ivars keep more detailed type encoding information than method arguments.
  42. if (strcmp(type, @encode(id)) != 0) {
  43. BOOL isJSONSerializableType = NO;
  44. // Note: we can't use @encode(NSString) here because that drops the string information and just goes to @encode(id).
  45. isJSONSerializableType = isJSONSerializableType || strcmp(type, FLEXEncodeClass(NSString)) == 0;
  46. isJSONSerializableType = isJSONSerializableType || strcmp(type, FLEXEncodeClass(NSNumber)) == 0;
  47. isJSONSerializableType = isJSONSerializableType || strcmp(type, FLEXEncodeClass(NSArray)) == 0;
  48. isJSONSerializableType = isJSONSerializableType || strcmp(type, FLEXEncodeClass(NSDictionary)) == 0;
  49. supported = isJSONSerializableType;
  50. }
  51. }
  52. }
  53. return supported;
  54. }
  55. @end