FLEXGlobalsEntry.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // FLEXGlobalsEntry.m
  3. // FLEX
  4. //
  5. // Created by Javier Soto on 7/26/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXGlobalsEntry.h"
  9. @implementation FLEXGlobalsEntry
  10. + (instancetype)entryWithEntry:(Class<FLEXGlobalsEntry>)cls row:(FLEXGlobalsRow)row {
  11. BOOL providesVCs = [cls respondsToSelector:@selector(globalsEntryViewController:)];
  12. BOOL providesActions = [cls respondsToSelector:@selector(globalsEntryRowAction:)];
  13. NSParameterAssert(cls);
  14. NSParameterAssert(providesVCs || providesActions);
  15. FLEXGlobalsEntry *entry = [self new];
  16. entry->_entryNameFuture = ^{ return [cls globalsEntryTitle:row]; };
  17. if (providesVCs) {
  18. id action = providesActions ? [cls globalsEntryRowAction:row] : nil;
  19. if (action) {
  20. entry->_rowAction = action;
  21. } else {
  22. entry->_viewControllerFuture = ^{ return [cls globalsEntryViewController:row]; };
  23. }
  24. } else {
  25. entry->_rowAction = [cls globalsEntryRowAction:row];
  26. }
  27. return entry;
  28. }
  29. + (instancetype)entryWithNameFuture:(FLEXGlobalsEntryNameFuture)nameFuture
  30. viewControllerFuture:(FLEXGlobalsEntryViewControllerFuture)viewControllerFuture {
  31. NSParameterAssert(nameFuture);
  32. NSParameterAssert(viewControllerFuture);
  33. FLEXGlobalsEntry *entry = [self new];
  34. entry->_entryNameFuture = [nameFuture copy];
  35. entry->_viewControllerFuture = [viewControllerFuture copy];
  36. return entry;
  37. }
  38. + (instancetype)entryWithNameFuture:(FLEXGlobalsEntryNameFuture)nameFuture
  39. action:(FLEXGlobalsEntryRowAction)rowSelectedAction {
  40. NSParameterAssert(nameFuture);
  41. NSParameterAssert(rowSelectedAction);
  42. FLEXGlobalsEntry *entry = [self new];
  43. entry->_entryNameFuture = [nameFuture copy];
  44. entry->_rowAction = [rowSelectedAction copy];
  45. return entry;
  46. }
  47. @end
  48. @interface FLEXGlobalsEntry (Debugging)
  49. @property (nonatomic, readonly) NSString *name;
  50. @end
  51. @implementation FLEXGlobalsEntry (Debugging)
  52. - (NSString *)name {
  53. return self.entryNameFuture();
  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