FLEXRuntime+UIKitHelpers.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // FLEXRuntime+UIKitHelpers.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 12/16/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXRuntime+UIKitHelpers.h"
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXPropertyAttributes.h"
  11. #import "FLEXArgumentInputViewFactory.h"
  12. @implementation FLEXProperty (UIKitHelpers)
  13. - (BOOL)isEditable {
  14. if (self.attributes.isReadOnly) {
  15. return self.likelySetterExists;
  16. }
  17. const FLEXTypeEncoding *typeEncoding = self.attributes.typeEncoding.UTF8String;
  18. return [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:nil];
  19. }
  20. - (BOOL)isCallable {
  21. return YES;
  22. }
  23. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  24. if (object_isClass(object) && !self.isClassProperty) {
  25. return UITableViewCellAccessoryNone;
  26. }
  27. // We use .tag to store the cached value of .isEditable that is
  28. // initialized by FLEXObjectExplorer in -reloadMetada
  29. if ([self getPotentiallyUnboxedValue:object]) {
  30. if (self.tag) {
  31. // Editable non-nil value, both
  32. return UITableViewCellAccessoryDetailDisclosureButton;
  33. } else {
  34. // Uneditable non-nil value, chevron only
  35. return UITableViewCellAccessoryDisclosureIndicator;
  36. }
  37. } else {
  38. if (self.tag) {
  39. // Editable nil value, just (i)
  40. return UITableViewCellAccessoryDetailButton;
  41. } else {
  42. // Non-editable nil value, neither
  43. return UITableViewCellAccessoryNone;
  44. }
  45. }
  46. }
  47. @end
  48. @implementation FLEXIvar (UIKitHelpers)
  49. - (BOOL)isEditable {
  50. const FLEXTypeEncoding *typeEncoding = self.typeEncoding.UTF8String;
  51. return [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:typeEncoding currentValue:nil];
  52. }
  53. - (BOOL)isCallable {
  54. return NO;
  55. }
  56. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  57. if (object_isClass(object)) {
  58. return UITableViewCellAccessoryNone;
  59. }
  60. // Could use .isEditable here, but we use .tag for speed since it is cached
  61. if ([self getPotentiallyUnboxedValue:object]) {
  62. if (self.tag) {
  63. // Editable non-nil value, both
  64. return UITableViewCellAccessoryDetailDisclosureButton;
  65. } else {
  66. // Uneditable non-nil value, chevron only
  67. return UITableViewCellAccessoryDisclosureIndicator;
  68. }
  69. } else {
  70. if (self.tag) {
  71. // Editable nil value, just (i)
  72. return UITableViewCellAccessoryDetailButton;
  73. } else {
  74. // Non-editable nil value, neither
  75. return UITableViewCellAccessoryNone;
  76. }
  77. }
  78. }
  79. @end
  80. @implementation FLEXMethodBase (UIKitHelpers)
  81. - (BOOL)isEditable {
  82. return NO;
  83. }
  84. - (BOOL)isCallable {
  85. return NO;
  86. }
  87. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  88. // We shouldn't be using any FLEXMethodBase objects for this
  89. @throw NSInternalInconsistencyException;
  90. return UITableViewCellAccessoryNone;
  91. }
  92. @end
  93. @implementation FLEXMethod (UIKitHelpers)
  94. - (BOOL)isCallable {
  95. return self.signature != nil;
  96. }
  97. - (UITableViewCellAccessoryType)suggestedAccessoryTypeWithTarget:(id)object {
  98. if (self.isInstanceMethod) {
  99. if (object_isClass(object)) {
  100. // Instance method from class, can't call
  101. return UITableViewCellAccessoryNone;
  102. } else {
  103. // Instance method from instance, can call
  104. return UITableViewCellAccessoryDisclosureIndicator;
  105. }
  106. } else {
  107. return UITableViewCellAccessoryDisclosureIndicator;
  108. }
  109. }
  110. @end