From dfb8fd26e64b66ca9731f7f37b6d5082d6df4035 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 10 Apr 2018 16:36:45 -0700 Subject: [PATCH] Fabric: Default implementation of RCTComponentViewProtocol Summary: All methods of RCTComponentViewProtocol are non-optional, but this default implementation removes necessity to implement all of them manually. Reviewed By: mdvacca Differential Revision: D7507522 fbshipit-source-id: e4dff97e8bf64e79d2f1ffca94f0549bd4b5e2fa --- .../Mounting/UIView+ComponentViewProtocol.h | 35 ++++++++++++ .../Mounting/UIView+ComponentViewProtocol.mm | 57 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 React/Fabric/Mounting/UIView+ComponentViewProtocol.h create mode 100644 React/Fabric/Mounting/UIView+ComponentViewProtocol.mm diff --git a/React/Fabric/Mounting/UIView+ComponentViewProtocol.h b/React/Fabric/Mounting/UIView+ComponentViewProtocol.h new file mode 100644 index 000000000..b38b41ba5 --- /dev/null +++ b/React/Fabric/Mounting/UIView+ComponentViewProtocol.h @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +#import + +NS_ASSUME_NONNULL_BEGIN + +/** + * Default implementation of RCTComponentViewProtocol. + */ +@interface UIView (ComponentViewProtocol) + +- (void)mountChildComponentView:(UIView *)childComponentView + index:(NSInteger)index; + +- (void)unmountChildComponentView:(UIView *)childComponentView + index:(NSInteger)index; + +- (void)updateProps:(facebook::react::SharedProps)props + oldProps:(facebook::react::SharedProps)oldProps; + +- (void)updateLayoutMetrics:(facebook::react::LayoutMetrics)layoutMetrics + oldLayoutMetrics:(facebook::react::LayoutMetrics)oldLayoutMetrics; + +- (void)prepareForRecycle; + +@end + +NS_ASSUME_NONNULL_END diff --git a/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm b/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm new file mode 100644 index 000000000..93b5784e3 --- /dev/null +++ b/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "UIView+ComponentViewProtocol.h" + +#import + +@implementation UIView (ComponentViewProtocol) + +- (void)mountChildComponentView:(UIView *)childComponentView + index:(NSInteger)index +{ + [self insertSubview:childComponentView atIndex:index]; +} + +- (void)unmountChildComponentView:(UIView *)childComponentView + index:(NSInteger)index +{ + RCTAssert(childComponentView.superview == self, @"Attempt to unmount improperly mounted component view."); + [childComponentView removeFromSuperview]; +} + +- (void)updateProps:(facebook::react::SharedProps)props + oldProps:(facebook::react::SharedProps)oldProps +{ + // Default implementation does nothing. +} + +- (void)updateLayoutMetrics:(facebook::react::LayoutMetrics)layoutMetrics + oldLayoutMetrics:(facebook::react::LayoutMetrics)oldLayoutMetrics +{ + if (layoutMetrics.frame != oldLayoutMetrics.frame) { + self.frame = { + .origin = { + .x = layoutMetrics.frame.origin.x, + .y = layoutMetrics.frame.origin.y + }, + .size = { + .width = layoutMetrics.frame.size.width, + .height = layoutMetrics.frame.size.height + } + }; + } + + // TODO: Apply another layout metrics here. +} + +- (void)prepareForRecycle +{ + // Default implementation does nothing. +} + +@end