FLEXGlobalsEntry.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // FLEXGlobalsEntry.m
  3. // FLEX
  4. //
  5. // Created by Javier Soto on 7/26/14.
  6. // Copyright (c) 2014 f. All rights reserved.
  7. //
  8. #import "FLEXGlobalsEntry.h"
  9. @implementation FLEXGlobalsEntry
  10. + (instancetype)entryWithEntry:(Class<FLEXGlobalsEntry>)cls row:(FLEXGlobalsRow)row
  11. {
  12. BOOL providesVCs = [cls respondsToSelector:@selector(globalsEntryViewController:)];
  13. BOOL providesActions = [cls respondsToSelector:@selector(globalsEntryRowAction:)];
  14. NSParameterAssert(cls);
  15. NSParameterAssert(providesVCs || providesActions);
  16. FLEXGlobalsEntry *entry = [self new];
  17. entry->_entryNameFuture = ^{ return [cls globalsEntryTitle:row]; };
  18. if (providesVCs) {
  19. id action = providesActions ? [cls globalsEntryRowAction:row] : nil;
  20. if (action) {
  21. entry->_rowAction = action;
  22. } else {
  23. entry->_viewControllerFuture = ^{ return [cls globalsEntryViewController:row]; };
  24. }
  25. } else {
  26. entry->_rowAction = [cls globalsEntryRowAction:row];
  27. }
  28. return entry;
  29. }
  30. + (instancetype)entryWithNameFuture:(FLEXGlobalsEntryNameFuture)nameFuture
  31. viewControllerFuture:(FLEXGlobalsTableViewControllerViewControllerFuture)viewControllerFuture
  32. {
  33. NSParameterAssert(nameFuture);
  34. NSParameterAssert(viewControllerFuture);
  35. FLEXGlobalsEntry *entry = [self new];
  36. entry->_entryNameFuture = [nameFuture copy];
  37. entry->_viewControllerFuture = [viewControllerFuture copy];
  38. return entry;
  39. }
  40. + (instancetype)entryWithNameFuture:(FLEXGlobalsEntryNameFuture)nameFuture
  41. action:(FLEXGlobalsTableViewControllerRowAction)rowSelectedAction
  42. {
  43. NSParameterAssert(nameFuture);
  44. NSParameterAssert(rowSelectedAction);
  45. FLEXGlobalsEntry *entry = [self new];
  46. entry->_entryNameFuture = [nameFuture copy];
  47. entry->_rowAction = [rowSelectedAction copy];
  48. return entry;
  49. }
  50. #pragma mark FLEXPatternMatching
  51. - (BOOL)matches:(NSString *)query
  52. {
  53. return [self.entryNameFuture() localizedCaseInsensitiveContainsString:query];
  54. }
  55. @end
  56. #pragma mark - flex_concreteGlobalsEntry
  57. @implementation NSObject (FLEXGlobalsEntry)
  58. + (FLEXGlobalsEntry *)flex_concreteGlobalsEntry:(FLEXGlobalsRow)row {
  59. if ([self conformsToProtocol:@protocol(FLEXGlobalsEntry)]) {
  60. return [FLEXGlobalsEntry entryWithEntry:self row:row];
  61. }
  62. return nil;
  63. }
  64. @end