PHP 7.4.33
Preview: RCTModalHostView.m Size: 6.86 KB
/var/www/uibuilder.cmshelp.dk/httpdocs/node_modules/react-native/React/Views/RCTModalHostView.m
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "RCTModalHostView.h"

#import <UIKit/UIKit.h>

#import "RCTAssert.h"
#import "RCTBridge.h"
#import "RCTModalHostViewController.h"
#import "RCTTouchHandler.h"
#import "RCTUIManager.h"
#import "RCTUtils.h"
#import "UIView+React.h"

@implementation RCTModalHostView {
  __weak RCTBridge *_bridge;
  BOOL _isPresented;
  RCTModalHostViewController *_modalViewController;
  RCTTouchHandler *_touchHandler;
  UIView *_reactSubview;
  UIInterfaceOrientation _lastKnownOrientation;
  RCTDirectEventBlock _onRequestClose;
}

RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : coder)

- (instancetype)initWithBridge:(RCTBridge *)bridge
{
  if ((self = [super initWithFrame:CGRectZero])) {
    _bridge = bridge;
    _modalViewController = [RCTModalHostViewController new];
    UIView *containerView = [UIView new];
    containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _modalViewController.view = containerView;
    _touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge];
    _isPresented = NO;

    __weak typeof(self) weakSelf = self;
    _modalViewController.boundsDidChangeBlock = ^(CGRect newBounds) {
      [weakSelf notifyForBoundsChange:newBounds];
    };
  }

  return self;
}

- (void)notifyForBoundsChange:(CGRect)newBounds
{
  if (_reactSubview && _isPresented) {
    [_bridge.uiManager setSize:newBounds.size forView:_reactSubview];
    [self notifyForOrientationChange];
  }
}

- (void)setOnRequestClose:(RCTDirectEventBlock)onRequestClose
{
  _onRequestClose = onRequestClose;
}

- (void)presentationControllerDidAttemptToDismiss:(UIPresentationController *)controller
{
  if (_onRequestClose != nil) {
    _onRequestClose(nil);
  }
}

- (void)notifyForOrientationChange
{
  if (!_onOrientationChange) {
    return;
  }

  UIInterfaceOrientation currentOrientation = RCTKeyWindow().windowScene.interfaceOrientation;
  if (currentOrientation == _lastKnownOrientation) {
    return;
  }
  _lastKnownOrientation = currentOrientation;

  BOOL isPortrait = currentOrientation == UIInterfaceOrientationPortrait ||
      currentOrientation == UIInterfaceOrientationPortraitUpsideDown;
  NSDictionary *eventPayload = @{
    @"orientation" : isPortrait ? @"portrait" : @"landscape",
  };
  _onOrientationChange(eventPayload);
}

- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
{
  RCTAssert(_reactSubview == nil, @"Modal view can only have one subview");
  [super insertReactSubview:subview atIndex:atIndex];
  [_touchHandler attachToView:subview];

  [_modalViewController.view insertSubview:subview atIndex:0];
  _reactSubview = subview;
}

- (void)removeReactSubview:(UIView *)subview
{
  RCTAssert(subview == _reactSubview, @"Cannot remove view other than modal view");
  // Superclass (category) removes the `subview` from actual `superview`.
  [super removeReactSubview:subview];
  [_touchHandler detachFromView:subview];
  _reactSubview = nil;
}

- (void)didUpdateReactSubviews
{
  // Do nothing, as subview (singular) is managed by `insertReactSubview:atIndex:`
}

- (void)dismissModalViewController
{
  if (_isPresented) {
    [_delegate dismissModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
    _isPresented = NO;
    [self setVisible:NO];
  }
}

- (void)didMoveToWindow
{
  [super didMoveToWindow];

  // In the case where there is a LayoutAnimation, we will be reinserted into the view hierarchy but only for aesthetic
  // purposes. In such a case, we should NOT represent the <Modal>.
  if (!self.userInteractionEnabled && ![self.superview.reactSubviews containsObject:self]) {
    return;
  }

  [self ensurePresentedOnlyIfNeeded];
}

- (void)didMoveToSuperview
{
  [super didMoveToSuperview];
  [self ensurePresentedOnlyIfNeeded];
}

- (void)invalidate
{
  dispatch_async(dispatch_get_main_queue(), ^{
    [self dismissModalViewController];
  });
}

- (BOOL)isTransparent
{
  return _modalViewController.modalPresentationStyle == UIModalPresentationOverFullScreen;
}

- (BOOL)hasAnimationType
{
  return ![self.animationType isEqualToString:@"none"];
}

- (void)setVisible:(BOOL)visible
{
  if (_visible != visible) {
    _visible = visible;
    [self ensurePresentedOnlyIfNeeded];
  }
}

- (void)ensurePresentedOnlyIfNeeded
{
  BOOL shouldBePresented = !_isPresented && _visible && self.window;
  if (shouldBePresented) {
    RCTAssert(self.reactViewController, @"Can't present modal view controller without a presenting view controller");

    _modalViewController.supportedInterfaceOrientations = [self supportedOrientationsMask];

    if ([self.animationType isEqualToString:@"fade"]) {
      _modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    } else if ([self.animationType isEqualToString:@"slide"]) {
      _modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    }
    if (self.presentationStyle != UIModalPresentationNone) {
      _modalViewController.modalPresentationStyle = self.presentationStyle;
    }

    _modalViewController.presentationController.delegate = self;

    [_delegate presentModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
    _isPresented = YES;
  }

  BOOL shouldBeHidden = _isPresented && (!_visible || !self.superview);
  if (shouldBeHidden) {
    [self dismissModalViewController];
  }
}

- (void)setTransparent:(BOOL)transparent
{
  if (self.isTransparent != transparent) {
    return;
  }

  _modalViewController.modalPresentationStyle =
      transparent ? UIModalPresentationOverFullScreen : UIModalPresentationFullScreen;
}

- (UIInterfaceOrientationMask)supportedOrientationsMask
{
  if (_supportedOrientations.count == 0) {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
      return UIInterfaceOrientationMaskAll;
    } else {
      return UIInterfaceOrientationMaskPortrait;
    }
  }

  UIInterfaceOrientationMask supportedOrientations = 0;
  for (NSString *orientation in _supportedOrientations) {
    if ([orientation isEqualToString:@"portrait"]) {
      supportedOrientations |= UIInterfaceOrientationMaskPortrait;
    } else if ([orientation isEqualToString:@"portrait-upside-down"]) {
      supportedOrientations |= UIInterfaceOrientationMaskPortraitUpsideDown;
    } else if ([orientation isEqualToString:@"landscape"]) {
      supportedOrientations |= UIInterfaceOrientationMaskLandscape;
    } else if ([orientation isEqualToString:@"landscape-left"]) {
      supportedOrientations |= UIInterfaceOrientationMaskLandscapeLeft;
    } else if ([orientation isEqualToString:@"landscape-right"]) {
      supportedOrientations |= UIInterfaceOrientationMaskLandscapeRight;
    }
  }
  return supportedOrientations;
}

@end

Directory Contents

Dirs: 3 × Files: 59
Name Size Perms Modified Actions
- drwxr-xr-x 2025-03-28 11:04:43
Edit Download
- drwxr-xr-x 2025-03-28 11:04:43
Edit Download
- drwxr-xr-x 2025-03-28 11:04:43
Edit Download
283 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
444 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
418 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.51 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
446 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
880 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
332 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.01 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
20.67 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
372 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.74 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.80 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
20.24 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
463 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
640 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
380 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
5.49 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
306 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
683 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
3.52 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
286 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.96 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.70 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
18.54 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
2.29 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.57 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.78 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
6.86 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
446 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.85 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
997 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
3.87 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
399 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
670 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
400 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
880 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.09 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
387 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
429 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.06 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.92 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
8.82 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
26.60 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
389 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
377 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
276 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.75 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
453 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
5.11 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
37.36 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
4.64 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
28.90 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
376 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
500 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
397 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.09 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
459 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.44 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
13.40 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).