1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#import "ABI49_0_0RCTWrapperViewController.h"
9
10#import <UIKit/UIScrollView.h>
11
12#import "ABI49_0_0RCTAutoInsetsProtocol.h"
13#import "ABI49_0_0RCTUtils.h"
14#import "ABI49_0_0UIView+React.h"
15
16@implementation ABI49_0_0RCTWrapperViewController {
17  UIView *_wrapperView;
18  UIView *_contentView;
19  CGFloat _previousTopInset;
20  CGFloat _previousBottomInset;
21
22  CGFloat _currentTopInset;
23  CGFloat _currentBottomInset;
24}
25
26- (instancetype)initWithContentView:(UIView *)contentView
27{
28  ABI49_0_0RCTAssertParam(contentView);
29
30  if ((self = [super initWithNibName:nil bundle:nil])) {
31    _contentView = contentView;
32    self.automaticallyAdjustsScrollViewInsets = NO;
33  }
34  return self;
35}
36
37ABI49_0_0RCT_NOT_IMPLEMENTED(-(instancetype)initWithNibName : (NSString *)nn bundle : (NSBundle *)nb)
38ABI49_0_0RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder)
39
40- (void)viewWillLayoutSubviews
41{
42  [super viewWillLayoutSubviews];
43
44  _currentTopInset = self.view.safeAreaInsets.top;
45  _currentBottomInset = self.view.safeAreaInsets.bottom;
46}
47
48static BOOL ABI49_0_0RCTFindScrollViewAndRefreshContentInsetInView(UIView *view)
49{
50  if ([view conformsToProtocol:@protocol(ABI49_0_0RCTAutoInsetsProtocol)]) {
51    [(id<ABI49_0_0RCTAutoInsetsProtocol>)view refreshContentInset];
52    return YES;
53  }
54  for (UIView *subview in view.subviews) {
55    if (ABI49_0_0RCTFindScrollViewAndRefreshContentInsetInView(subview)) {
56      return YES;
57    }
58  }
59  return NO;
60}
61
62- (void)viewDidLayoutSubviews
63{
64  [super viewDidLayoutSubviews];
65
66  if (_previousTopInset != _currentTopInset || _previousBottomInset != _currentBottomInset) {
67    ABI49_0_0RCTFindScrollViewAndRefreshContentInsetInView(_contentView);
68    _previousTopInset = _currentTopInset;
69    _previousBottomInset = _currentBottomInset;
70  }
71}
72
73- (void)loadView
74{
75  // Add a wrapper so that the wrapper view managed by the
76  // UINavigationController doesn't end up resetting the frames for
77  //`contentView` which is a ABI49_0_0React-managed view.
78  _wrapperView = [[UIView alloc] initWithFrame:_contentView.bounds];
79  [_wrapperView addSubview:_contentView];
80  self.view = _wrapperView;
81}
82
83@end
84