Skip to content

Commit

Permalink
Apply props to Paper component
Browse files Browse the repository at this point in the history
Reviewed By: shergin

Differential Revision: D17710855

fbshipit-source-id: ab6864a22fd8df019b2619c2976876e176e07689
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Oct 3, 2019
1 parent 3f8221d commit e62a4c7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ + (BOOL)isSupported:(NSString *)componentName

#pragma mark - RCTComponentViewProtocol

- (void)prepareForRecycle
{
[super prepareForRecycle];
[_view removeFromSuperview];
_view = NULL;
}

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<LegacyViewManagerInteropComponentDescriptor>();
Expand All @@ -51,13 +58,19 @@ - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask
{
[super finalizeUpdates:updateMask];
assert(_props && _state);
const auto &state = _state->getData();
RCTLegacyViewManagerInteropCoordinator *coordinator = unwrapManagedObject(state.coordinator);

if (!_view) {
const auto &state = _state->getData();
RCTLegacyViewManagerInteropCoordinator *coordinator = unwrapManagedObject(state.coordinator);
UIView *view = [coordinator view];
self.contentView = view;
_view = view;
}

if (updateMask & RNComponentViewUpdateMaskProps) {
const auto &newProps = *std::static_pointer_cast<const LegacyViewManagerInteropViewProps>(_props);
[coordinator setProps:newProps.otherProps forView:_view];
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "LegacyViewManagerInteropComponentDescriptor.h"
#include <React/RCTBridge.h>
#include <React/RCTComponentData.h>
#include <React/RCTUIManager.h>
#include <react/utils/ContextContainer.h>
#include <react/utils/ManagedObjectWrapper.h>
Expand All @@ -18,7 +19,7 @@

static std::string moduleNameFromComponentName(const std::string &componentName)
{
return componentName + "Manager";
return "RCT" + componentName + "Manager";
}

inline NSString *RCTNSStringFromString(const std::string &string)
Expand All @@ -32,9 +33,11 @@
{
auto componentName = *std::static_pointer_cast<std::string const>(flavor);
auto moduleName = moduleNameFromComponentName(componentName);
Class module = NSClassFromString(RCTNSStringFromString(moduleName));
assert(module);
RCTBridge *bridge = (RCTBridge *)unwrapManagedObject(contextContainer->at<std::shared_ptr<void>>("Bridge"));
RCTViewManager *viewManager = [bridge moduleForName:RCTNSStringFromString(moduleName) lazilyLoadIfNecessary:YES];
return wrapManagedObject([[RCTLegacyViewManagerInteropCoordinator alloc] initWithViewManager:viewManager]);
RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:module bridge:bridge];
return wrapManagedObject([[RCTLegacyViewManagerInteropCoordinator alloc] initWithComponentData:componentData]);
}

LegacyViewManagerInteropComponentDescriptor::LegacyViewManagerInteropComponentDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,14 @@
*/

#include "LegacyViewManagerInteropViewProps.h"
#include <folly/json.h>
#include <react/core/propsConversions.h>
#include <iostream>

namespace facebook {
namespace react {

static std::unordered_map<std::string, folly::dynamic> convertToMap(
RawProps const &rawProps) {
std::unordered_map<std::string, folly::dynamic> map{};
auto const dynamic = (folly::dynamic)rawProps;
if (dynamic.isObject()) {
for (auto const &t : dynamic.items()) {
if (t.first.isString()) {
map[t.first.asString()] = t.second;
}
}
}
return map;
}

LegacyViewManagerInteropViewProps::LegacyViewManagerInteropViewProps(
const LegacyViewManagerInteropViewProps &sourceProps,
const RawProps &rawProps)
: ViewProps(sourceProps, rawProps), otherProps(convertToMap(rawProps)) {}
: ViewProps(sourceProps, rawProps), otherProps((folly::dynamic)rawProps) {}

} // namespace react
} // namespace facebook
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LegacyViewManagerInteropViewProps final : public ViewProps {

#pragma mark - Props

const std::unordered_map<std::string, folly::dynamic> otherProps;
folly::dynamic const otherProps;
};

} // namespace react
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <folly/dynamic.h>

NS_ASSUME_NONNULL_BEGIN

@class RCTViewManager;
@class RCTComponentData;

@interface RCTLegacyViewManagerInteropCoordinator : NSObject

- (instancetype)initWithViewManager:(RCTViewManager *)viewManager;
- (instancetype)initWithComponentData:(RCTComponentData *)componentData;

- (UIView *)view;

- (void)setProps:(folly::dynamic const &)props forView:(UIView *)view;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,38 @@
*/

#import "RCTLegacyViewManagerInteropCoordinator.h"
#import <React/RCTViewManager.h>
#include <React/RCTComponentData.h>
#include <folly/json.h>

static NSDictionary<NSString *, id> *something(folly::dynamic const &props)
{
auto json = folly::toJson(props);
NSData *data = [NSData dataWithBytes:json.c_str() length:json.size()];
NSDictionary<NSString *, id> *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
return result;
}

@implementation RCTLegacyViewManagerInteropCoordinator {
RCTViewManager *_viewManager;
RCTComponentData *_componentData;
}

- (instancetype)initWithViewManager:(RCTViewManager *)viewManager;
- (instancetype)initWithComponentData:(RCTComponentData *)componentData;
{
if (self = [super init]) {
_viewManager = viewManager;
_componentData = componentData;
}
return self;
}

- (UIView *)view
{
return _viewManager.view;
return [_componentData createViewWithTag:NULL];
}

- (void)setProps:(folly::dynamic const &)props forView:(UIView *)view
{
NSDictionary<NSString *, id> *convertedProps = something(props);
[_componentData setProps:convertedProps forView:view];
}

@end

0 comments on commit e62a4c7

Please sign in to comment.