TBKeyPathToolbar.m 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // TBKeyPathToolbar.m
  3. // TBTweakViewController
  4. //
  5. // Created by Tanner on 6/11/17.
  6. // Copyright © 2017 Tanner Bennett. All rights reserved.
  7. //
  8. #import "TBKeyPathToolbar.h"
  9. #import "TBKeyPathTokenizer.h"
  10. @interface TBKeyPathToolbar ()
  11. @property (nonatomic, copy) TBToolbarAction tapHandler;
  12. @end
  13. @implementation TBKeyPathToolbar
  14. + (instancetype)toolbarWithHandler:(TBToolbarAction)tapHandler {
  15. TBKeyPath *emptyKeyPath = [TBKeyPathTokenizer tokenizeString:@""];
  16. NSArray *buttons = [self buttonsForKeyPath:emptyKeyPath handler:tapHandler];
  17. TBKeyPathToolbar *me = [self toolbarWithButtons:buttons];
  18. me.tapHandler = tapHandler;
  19. return me;
  20. }
  21. + (NSArray<TBToolbarButton*> *)buttonsForKeyPath:(TBKeyPath *)keyPath handler:(TBToolbarAction)handler {
  22. NSMutableArray *buttons = [NSMutableArray array];
  23. TBToken *lastKey = nil;
  24. BOOL lastKeyIsMethod = NO;
  25. if (keyPath.methodKey) {
  26. lastKey = keyPath.methodKey;
  27. lastKeyIsMethod = YES;
  28. } else {
  29. lastKey = keyPath.classKey ?: keyPath.bundleKey;
  30. }
  31. switch (lastKey.options) {
  32. case TBWildcardOptionsNone:
  33. case TBWildcardOptionsAny:
  34. if (lastKeyIsMethod) {
  35. if (!keyPath.instanceMethods) {
  36. [buttons addObject:[TBToolbarButton buttonWithTitle:@"-" action:handler]];
  37. [buttons addObject:[TBToolbarButton buttonWithTitle:@"+" action:handler]];
  38. }
  39. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*" action:handler]];
  40. } else {
  41. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*." action:handler]];
  42. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*" action:handler]];
  43. [buttons addObject:[TBToolbarButton buttonWithTitle:@"." action:handler]];
  44. }
  45. break;
  46. default: {
  47. if (lastKey.options & TBWildcardOptionsPrefix) {
  48. if (lastKeyIsMethod) {
  49. if (lastKey.string.length) {
  50. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*" action:handler]];
  51. }
  52. } else {
  53. if (lastKey.string.length) {
  54. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*." action:handler]];
  55. }
  56. [buttons addObject:[TBToolbarButton buttonWithTitle:@"." action:handler]];
  57. }
  58. }
  59. else if (lastKey.options & TBWildcardOptionsSuffix) {
  60. if (!lastKeyIsMethod) {
  61. [buttons addObject:[TBToolbarButton buttonWithTitle:@"*." action:handler]];
  62. [buttons addObject:[TBToolbarButton buttonWithTitle:@"." action:handler]];
  63. }
  64. }
  65. }
  66. }
  67. return buttons;
  68. }
  69. - (void)setKeyPath:(TBKeyPath *)keyPath animated:(BOOL)animated {
  70. NSArray *buttons = [TBKeyPathToolbar buttonsForKeyPath:keyPath handler:self.tapHandler];
  71. [self setButtons:buttons animated:animated];
  72. }
  73. @end