ModalPresenter.m 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ModalPresenter.h"
  21. @implementation ModalPresenter
  22. static UIViewController *pickerPopover;
  23. + (void) present:(UIViewController *)controllerToPresent on:(UIViewController *)controller {
  24. [self present:controllerToPresent on:controller fromRect:controller.view.bounds inView:controller.view];
  25. }
  26. + (void) present:(UIViewController *)controllerToPresent on:(UIViewController *)controller fromRect:(CGRect)fromRect inView:(UIView *)inView {
  27. [ModalPresenter present:controllerToPresent on:controller fromRect:fromRect inView:inView completion:nil];
  28. }
  29. + (void) present:(UIViewController *)controllerToPresent on:(UIViewController *)controller fromRect:(CGRect)fromRect inView:(UIView *)inView completion:(void(^)(void))completion {
  30. if ([self shouldPresentInPopover:controllerToPresent]) {
  31. pickerPopover = controllerToPresent;
  32. pickerPopover.modalPresentationStyle = UIModalPresentationPopover;
  33. pickerPopover.popoverPresentationController.sourceView = inView;
  34. pickerPopover.popoverPresentationController.sourceRect = fromRect;
  35. [controller presentViewController:controllerToPresent animated:YES completion:completion];
  36. } else {
  37. [controller presentViewController:controllerToPresent animated:YES completion:completion];
  38. }
  39. }
  40. + (void) present:(UIViewController *)controllerToPresent on:(UIViewController *)controller fromBarButton:(UIBarButtonItem *)barButtonItem {
  41. if ([self shouldPresentInPopover:controllerToPresent]) {
  42. pickerPopover = controllerToPresent;
  43. pickerPopover.modalPresentationStyle = UIModalPresentationPopover;
  44. pickerPopover.popoverPresentationController.barButtonItem = barButtonItem;
  45. dispatch_async(dispatch_get_main_queue(), ^{
  46. [controller presentViewController:controllerToPresent animated:YES completion:nil];
  47. });
  48. } else {
  49. [controller presentViewController:controllerToPresent animated:YES completion:nil];
  50. }
  51. }
  52. + (BOOL) shouldPresentInPopover:(UIViewController *)controller {
  53. if (SYSTEM_IS_IPAD) {
  54. if ([controller isKindOfClass:[UIImagePickerController class]]) {
  55. UIImagePickerController *picker = (UIImagePickerController *)controller;
  56. return (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary);
  57. } else {
  58. return YES;
  59. }
  60. }
  61. return NO;
  62. }
  63. + (void) dismissPresentedControllerOn:(UIViewController *)presentingViewController animated:(BOOL)animated {
  64. [self dismissPresentedControllerOn:presentingViewController animated:YES completion:nil];
  65. }
  66. + (void) dismissPresentedControllerOn:(UIViewController *)presentingViewController animated:(BOOL)animated completion:(void (^)(void))completion {
  67. if (pickerPopover) {
  68. [pickerPopover dismissViewControllerAnimated:YES completion:completion];
  69. } else {
  70. [presentingViewController dismissViewControllerAnimated:YES completion:completion];
  71. }
  72. pickerPopover = nil;
  73. }
  74. @end