FLEXViewControllerShortcuts.m 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // FLEXViewControllerShortcuts.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 12/12/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXViewControllerShortcuts.h"
  9. #import "FLEXAlert.h"
  10. @interface FLEXViewControllerShortcuts ()
  11. @property (nonatomic, readonly) UIViewController *viewController;
  12. @property (nonatomic, readonly) BOOL viewControllerIsInUse;
  13. @end
  14. @implementation FLEXViewControllerShortcuts
  15. #pragma mark - Internal
  16. - (UIViewController *)viewController {
  17. return self.object;
  18. }
  19. /// A view controller is "in use" if it's view is in a window,
  20. /// or if it belongs to a navigation stack which is in use.
  21. - (BOOL)viewControllerIsInUse {
  22. if (self.viewController.view.window) {
  23. return YES;
  24. }
  25. return self.viewController.navigationController != nil;
  26. }
  27. #pragma mark - Overrides
  28. + (instancetype)forObject:(UIViewController *)viewController {
  29. // These additional rows will appear at the beginning of the shortcuts section.
  30. // The methods below are written in such a way that they will not interfere
  31. // with properties/etc being registered alongside these
  32. return [self forObject:viewController additionalRows:@[@"Push View Controoller"]];
  33. }
  34. - (void (^)(UIViewController *))didSelectRowAction:(NSInteger)row {
  35. if (row == 0) {
  36. return ^(UIViewController *host) {
  37. if (!self.viewControllerIsInUse) {
  38. [host.navigationController pushViewController:self.viewController animated:YES];
  39. } else {
  40. [FLEXAlert
  41. showAlert:@"Cannot Push View Controller"
  42. message:@"This view controller's view is currently in use."
  43. from:host
  44. ];
  45. }
  46. };
  47. }
  48. return [super didSelectRowAction:row];
  49. }
  50. - (UITableViewCellAccessoryType)accessoryTypeForRow:(NSInteger)row {
  51. switch (row) {
  52. case 0:
  53. if (self.viewControllerIsInUse) {
  54. return UITableViewCellAccessoryDisclosureIndicator;
  55. } else {
  56. return UITableViewCellAccessoryNone;
  57. }
  58. default:
  59. return [super accessoryTypeForRow:row];
  60. }
  61. }
  62. @end