Files
react-native/packages/rn-tester/NativeComponentExample/ios/RNTMyNativeViewComponentView.mm
T
Nick Gerleman a966291611 Consistent QualifierAlignment (for real this time I swear) (#39217)
Summary:
(reland of D48761722)

Pull Request resolved: https://github.com/facebook/react-native/pull/39217

React Native uses an inconsistent mix of "west const" and "east const". E.g. `const auto &` in 74 files, but `auto const &` in 60. Sometimes they are mixed from one line to the next:  {F1079102436}

Clang format 14 adds a QualifierAlignment option, but fbsource is still on 12, so we cannot use it in our config until the [world is updated]()https://fb.workplace.com/groups/toolchain.fndn/posts/24006558685624673/?comment_id=24009214565359085&reply_comment_id=24009455088668366. This diff just runs a local version of Clang format locally first to fix QualifierAlignment, then reformats with the fbsource version, to fix any other output differences unrelated to that. This will not continually enforce a style, but will make the world more consistent, and hopefully encourage a consistent style until we can set it.

West const seems more popular in `//xplat` so I just picked left alignment somewhat arbitrarily, but we could also maybe take a poll on this.

Changelog: [Internal]

bypass-github-export-checks

Reviewed By: shwanton

Differential Revision: D48852450

fbshipit-source-id: 1789aa0db43948169f482188cb8b5e8f0f0246b8
2023-08-30 19:56:13 -07:00

135 lines
4.6 KiB
Plaintext

/*
* 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 "RNTMyNativeViewComponentView.h"
#import <react/renderer/components/AppSpecs/ComponentDescriptors.h>
#import <react/renderer/components/AppSpecs/EventEmitters.h>
#import <react/renderer/components/AppSpecs/Props.h>
#import <react/renderer/components/AppSpecs/RCTComponentViewHelpers.h>
#import "RCTFabricComponentsPlugins.h"
using namespace facebook::react;
@interface RNTMyNativeViewComponentView () <RCTRNTMyNativeViewViewProtocol>
@end
@implementation RNTMyNativeViewComponentView {
UIView *_view;
}
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<RNTMyNativeViewComponentDescriptor>();
}
// Load is not invoked if it is not defined, therefore, we must ask to update this.
// See the Apple documentation: https://developer.apple.com/documentation/objectivec/nsobject/1418815-load?language=objc
// "[...] but only if the newly loaded class or category implements a method that can respond."
+ (void)load
{
[super load];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const RNTMyNativeViewProps>();
_props = defaultProps;
_view = [[UIView alloc] init];
_view.backgroundColor = [UIColor redColor];
self.contentView = _view;
}
return self;
}
- (UIColor *)UIColorFromHexString:(const std::string)hexString
{
unsigned rgbValue = 0;
NSString *colorString = [NSString stringWithCString:hexString.c_str() encoding:[NSString defaultCStringEncoding]];
NSScanner *scanner = [NSScanner scannerWithString:colorString];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&rgbValue];
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16) / 255.0
green:((rgbValue & 0xFF00) >> 8) / 255.0
blue:(rgbValue & 0xFF) / 255.0
alpha:1.0];
}
- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps
{
const auto &oldViewProps = *std::static_pointer_cast<const RNTMyNativeViewProps>(_props);
const auto &newViewProps = *std::static_pointer_cast<const RNTMyNativeViewProps>(props);
if (oldViewProps.values != newViewProps.values) {
if (_eventEmitter) {
std::vector<int> newVector = {};
std::vector<bool> newBoolVector = {};
std::vector<Float> newFloatVector = {};
std::vector<double> newDoubleVector = {};
std::vector<RNTMyNativeViewEventEmitter::OnIntArrayChangedYesNos> newYesNoVector = {};
std::vector<std::string> newStringVector = {};
std::vector<RNTMyNativeViewEventEmitter::OnIntArrayChangedLatLons> newLatLonVector = {};
std::vector<std::vector<int>> newIntVectorVector = {};
for (auto val : newViewProps.values) {
newVector.push_back(val * 2);
newBoolVector.push_back(val % 2 ? true : false);
newFloatVector.push_back(val * 3.14);
newDoubleVector.push_back(val / 3.14);
newYesNoVector.push_back(
val % 2 ? RNTMyNativeViewEventEmitter::OnIntArrayChangedYesNos::Yep
: RNTMyNativeViewEventEmitter::OnIntArrayChangedYesNos::Nope);
newStringVector.push_back(std::to_string(val));
newLatLonVector.push_back({-1.0 * val, 2.0 * val});
newIntVectorVector.push_back({val, val, val});
}
RNTMyNativeViewEventEmitter::OnIntArrayChanged value = {
newVector,
newBoolVector,
newFloatVector,
newDoubleVector,
newYesNoVector,
newStringVector,
newLatLonVector,
newIntVectorVector};
std::static_pointer_cast<const RNTMyNativeViewEventEmitter>(_eventEmitter)->onIntArrayChanged(value);
}
}
[super updateProps:props oldProps:oldProps];
}
- (void)onChange:(UIView *)sender
{
// No-op
// std::dynamic_pointer_cast<const ViewEventEmitter>(_eventEmitter)
// ->onChange(ViewEventEmitter::OnChange{.value = static_cast<bool>(sender.on)});
}
#pragma mark - Native Commands
- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
{
RCTRNTMyNativeViewHandleCommand(self, commandName, args);
}
- (void)callNativeMethodToChangeBackgroundColor:(NSString *)colorString
{
UIColor *color = [self UIColorFromHexString:std::string([colorString UTF8String])];
_view.backgroundColor = color;
}
@end
Class<RCTComponentViewProtocol> RNTMyNativeViewCls(void)
{
return RNTMyNativeViewComponentView.class;
}